Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <stdio.h>
- void printBoard();
- void resetBoard();
- bool checkSpace(int);
- bool checkForWin(int);
- void setSpace(int, char);
- using namespace std;
- char spaces[9];
- bool in_progress = true;
- int turn = 0;
- char playerTurn = 'X';
- int main ()
- {
- // print out title
- cout << endl << "\tT I C \n\tT A C \n\tT O E " << endl << " ~For Terminal~" << endl << endl;
- resetBoard();
- // while game is in session
- while (in_progress) {
- int spaceNum;
- bool noValidInput = true;
- string temp = "";
- printBoard();
- while (noValidInput) { // waits for player to select valid and empty space on board
- cout << "Player (" << playerTurn << "), enter your choice space: [1-9]\n";
- getline(cin, temp);
- cout << endl;
- spaceNum = stoi(temp);
- if (spaceNum > 0 || spaceNum < 10) {
- if (checkSpace(spaceNum)) {
- setSpace(spaceNum, playerTurn);
- noValidInput = false;
- }
- } else {
- cout << "Error: invalid input\n";
- }
- }
- system("clear");
- if (checkForWin(turn)) {
- cout << "Player (" << playerTurn << ") has won!\n";
- printBoard();
- in_progress = false;
- }
- if (playerTurn == 'X') {
- playerTurn = 'O';
- } else if (playerTurn == 'O') {
- playerTurn = 'X';
- } else {
- cout << "Fatal Error; Error Code: \"Dinosaur\"";
- in_progress = false;
- }
- turn++;
- if (turn >= 9 && in_progress) {
- cout << "Tie!\n";
- printBoard();
- in_progress = false;
- }
- }
- return(0);
- }
- bool checkForWin(int turn) {
- // no way to win prior to turn 4
- if (turn < 4) {
- return false;
- }
- // check rows
- if ((spaces[0] != '_') && (spaces[0] == spaces[1] && spaces[0] == spaces[2])) {
- return true;
- } else if ((spaces[3] != '_') && (spaces[3] == spaces[4] && spaces[3] == spaces[5])) {
- return true;
- } else if ((spaces[6] != '_') && (spaces[6] == spaces[7] && spaces[6] == spaces[8])) {
- return true;
- }
- // check columns
- if ((spaces[0] != '_') && (spaces[0] == spaces[3] && spaces[0] == spaces[6])) {
- return true;
- } else if ((spaces[1] != '_') && (spaces[1] == spaces[4] && spaces[1] == spaces[7])) {
- return true;
- } else if ((spaces[2] != '_') && (spaces[2] == spaces[5] && spaces[2] == spaces[8])) {
- return true;
- }
- // check diagonals
- if ((spaces[0] != '_') && (spaces[0] == spaces[4] && spaces[0] == spaces[8])) {
- return true;
- } else if ((spaces[2] != '_') && (spaces[2] == spaces[4] && spaces[2] == spaces[6])) {
- return true;
- }
- return false;
- }
- // checkSpace determines if space is already owned
- bool checkSpace(int spaceValue) {
- if (spaces[spaceValue - 1] == '_') {
- return true;
- } else {
- cout << "Space (" << spaceValue << ") has already been selected.\n";
- return false;
- }
- }
- // setSpace sets the current player's character to spaces array
- void setSpace(int spaceValue, char playerChar) {
- spaces[spaceValue - 1] = playerChar;
- cout << "Space (" << spaceValue << ") has been set to: " << playerChar << endl;
- }
- // resetBoard clears the array and sets turn count to 0
- void resetBoard() {
- turn = 0;
- playerTurn = 'X';
- for (int i = 0; i < 9; i ++) {
- spaces[i] = '_';
- }
- }
- // printBoard displays the spaces array as tic-tac-toe board for players
- void printBoard() {
- cout << endl;
- for (int i = 0; i < 9; i ++) {
- if ((i + 3) % 3 == 0) {
- cout << "\t";
- }
- cout << spaces[i] << " ";
- if ((i + 1) % 3 == 0) {
- cout << endl;
- }
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement