c++ myprogramminglab chapter 7 projects 70421 7.10: Tic Tac Toe (sort of) Write
ID: 657480 • Letter: C
Question
c++
myprogramminglab chapter 7 projects 70421
7.10: Tic Tac Toe (sort of)
Write a program that will allow two users to play tic-tac-toe. The program should ask for moves alternately from player X and player O. The program displays the game positions as follows:
1 2 3
4 5 6
7 8 9
The players enter their moves by entering the position number (a single digit, 1-9) they wish to mark. After each move, the program re-displays the changed board. A sample board configuration is as follows: X X O
4 5 6
O 8 9
Input Notes: The inputs are 9 single digits, each on a line by itself.
Output Notes (Prompts and Labels): Each input is prompted for by "Enter move:". After each input the board is re-displayed (see above). At the end, after the nineth and last move, the program displays: "Game over!".
Note: this is not to be confused with a real tic-tac-toe program !
Explanation / Answer
#include <iostream>
using namespace std;
void printBoard( char[3][3] );
bool isGameOver( char[3][3] );
bool getTurn( bool &XTurn, char input, char board[3][3] );
int main( ) {
char board[3][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}
};
bool XTurn = true;
cout << "Tic-Tac-Toe game" << endl;
do {
cout << " It's now " << (XTurn ? "X" : "0") << "'s turn. ";
printBoard(board);
do {
char input;
cout << "Enter an empty square number : ";
cinput >> input;
if (getTurn( XTurn, input, board )) {
break;
}
} while (true);
} while (!isGameOver( board ));
return 0;
}
void printBoard( char board[3][3] ) {
for (int r = 0; r < 3; r++)
for (int c = 0; c < 3; c++)
cout << board[r][c] << (c == 2 ? " " : " ");
return;
}
bool isGameOver( char board[3][3] ) {
for (int r = 0; r < 3; r++)
if ((board[r][0] == board[r][1]) && (board[r][1] == board[r][2])) {
cout << board[r][0] << " wins. ";
return true;
}
for (int c = 0; c < 3; c++)
if ((board[0][c] == board[1][c]) && (board[1][c] == board[2][c])) {
cout << board[0][c] << " wins. ";
return true;
}
if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2])) {
cout << board[1][1] << " wins. ";
return true;
}
if ((board[2][0] == board[1][1]) && (board[1][1] == board[2][0])) {
cout << board[1][1] << " wins. ";
return true;
}
int unfilled = 0;
for (int r = 0; r < 3; r++)
for (int c = 0; c < 3; c++)
if (board[r][c] >= '1' && board[r][c] <= '9')
unfilled++; // count unfilled squares
if (unfilled == 0) {
cout << "No more unfilled squares. Draw game. ";
return true;
}
return false;
}
bool getTurn( bool &XTurn, char input, char board[3][3] ) {
if (input < '1' || input > '9') {
cout << "Please enter a digit from 1 to 9." << endl;
return false;
}
int intInput = input - '1';
int r = (intInput / 3), c = intInput % 3;
//cout << input << intInput << r << c << board[r][c] << endl;
if (board[r][c] == 'X' || board[r][c] == 'O') {
cout << "Square " << input << " is already filled." << endl;
return false;
}
board[r][c] = (XTurn ? 'X' : 'O');
XTurn = !XTurn;
return true;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.