// TicTacToe demo program #include #include #include using namespace std; string displayBoard(string board[9]); // returns updated tic tac toe board void displaySampleBoard(); // displays tic tac toe board with position numbers int main() { string board[9]; // tic tac toe, top row 0 thru 2, middle row 3 thru 5, bottom row 6 thru 8 int position = 0; // player's position bool gameOver = false; // loop flag variable bool validMove = false; // determines when valid move is inputted // initializing board with blank spaces for (int i = 0; i < 9; i++) { board[i] = " "; } while (!gameOver) { // ***************** x makes move validMove = false; while (!validMove) { displaySampleBoard(); cout << "Enter your position: "; cin >> position; if (board[position] == " " && position >= 0 && position <= 8) { validMove = true; board[position] = "x"; } else { cout << "That position is already occupied." << endl; cout << displayBoard(board) << endl; } } cout << displayBoard(board) << endl; // ***************** y makes move } system("pause"); return 0; }// end of main string displayBoard(string board[9]) { string str = ""; for (int i = 0; i < 9; i++) { if (i == 0) { str = str + "|" + board[i]; } else if (i == 2 || i == 5) { str = str + board[i] + "|\n|"; } else if (i == 8) { str = str + board[i] + "|\n"; } else { str = str + board[i]; } } return str; }// end of displayBoard void displaySampleBoard() { cout << "|012|" << endl; cout << "|345|" << endl; cout << "|678|" << endl; }// end of displayBoard