#include using namespace std; bool checkWinner(char board[][3]) { if ((board[0][0] == board[0][1]) && (board[0][1] == board[0][2])) return true; // rows else if ((board[1][0] == board[1][1]) && (board[1][1] == board[1][2])) return true; else if ((board[2][0] == board[2][1]) && (board[2][1] == board[2][2])) return true; else if ((board[0][0] == board[1][0]) && (board[1][0] == board[2][0])) return true; // columns else if ((board[0][1] == board[1][1]) && (board[1][1] == board[2][1])) return true; else if ((board[0][2] == board[1][2]) && (board[1][2] == board[2][2])) return true; else if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2])) return true; // diagonals else if ((board[0][2] == board[1][1]) && (board[1][1] == board[2][0])) return true; else return false; } void getMove(char board[][3], char playerToGo) { int position; int row; int column; char entry; cout << endl; cout << "Player " << playerToGo << ", please select your position: "; cin >> position; position = position - 1; row = position / 3; column = position % 3; entry = board[row][column]; while ((entry == 'X') || (entry == 'O')) { cout << "That position has already been played, choose again: "; cin >> position; position = position - 1; row = position / 3; column = position % 3; entry = board[row][column]; } board[row][column] = playerToGo; } // not passing in size, as I know explicitly it's 3 by 3 void displayBoard(char board[][3]) { int indexOne; int indexTwo; for (indexOne = 0; indexOne < 3; indexOne++) { for (indexTwo = 0; indexTwo < 3; indexTwo++) { cout << board[indexOne][indexTwo]; if (indexTwo < 2) cout << " | "; } cout << endl; if (indexOne < 2) { cout << "---------" << endl;; } } } int main(int argc, char* argv[]) { int numberOfTurns; char playerToGo; bool winner; char board[3][3]; board[0][0] = '1'; board[0][1] = '2'; board[0][2] = '3'; board[1][0] = '4'; board[1][1] = '5'; board[1][2] = '6'; board[2][0] = '7'; board[2][1] = '8'; board[2][2] = '9'; cout << "Welcome to TicTacToe!" << endl << endl; numberOfTurns = 0; winner = false; playerToGo = 'X'; while ((winner == false) && (numberOfTurns < 9)) { displayBoard(board); getMove(board, playerToGo); winner = checkWinner(board); if (winner) { cout << playerToGo << " WINS!" << endl; } else { if (playerToGo == 'X') playerToGo = 'O'; else playerToGo = 'X'; } numberOfTurns = numberOfTurns + 1; } if ((winner == false) && (numberOfTurns == 9)) cout << "CAT: It's a tie!" << endl; }