Write a tic-tac-toe game to be played between two players. Use an array to emula
ID: 3618040 • Letter: W
Question
Write a tic-tac-toe game to be played between two players. Use an array to emulate
the game board. The array can be one dimensional or two-dimensional. Initialize
each element of the array to "_" (underscore). YOur program should start by
displaying the initial board and then let player X and player O take turns.
Associate X with player X's moves and O with O's. Determine as the game
progresses whether there is a winner or the game tied. If one of the player wins the
game identify it as player X or O. All Xs or Os in a row, in a column or in a diagonal
is a winning condition. When no more element is left the game is a tie.
Expected Output:
(i) X wins.
Columns
1 2 3
Row 1: _ _ _
Row 2: _ _ _
Row 3: _ _ _
Player X's turn.
Enter a row and column to place an X.
Row: 1
Column: 1
Columns
1 2 3
Row 1: X _ _
Row 2: _ _ _
Row 3: _ _ _
Player O's turn.
Enter a row and column to place an O.
Row: 2
Column: 2
Columns
1 2 3
Row 1: X _ _
Row 2: _ O _
Row 3: _ _ _
Player X's turn.
Enter a row and column to place an X.
Row: 1
Column: 3
Columns
1 2 3
Row 1: X _ X
Row 2: _ O _
Row 3: _ _ _
Player O's turn.
Enter a row and column to place an O.
Row: 1
Column: 2
Columns
1 2 3
Row 1: X O X
Row 2: _ O _
Row 3: _ _ _
Player X's turn.
Enter a row and column to place an X.
Row: 2
Column: 1
Columns
1 2 3
Row 1: X O X
Row 2: X O _
Row 3: _ _ _
Player O's turn.
Enter a row and column to place an O.
Row: 3
Column: 3
Columns
1 2 3
Row 1: X O X
Row 2: X O _
Row 3: _ _ O
Player X's turn.
Enter a row and column to place an X.
Row: 1
Column: 3
That location is not available. Select another location.
Row: 3
Column: 1
Columns
1 2 3
Row 1: X O X
Row 2: X O _
Row 3: X _ O
Columns
1 2 3
Row 1: X O X
Row 2: X O _
Row 3: X _ O
Player 1 (X) WINS!!!!!
Explanation / Answer
please rate - thanks #include using namespace std; void initializeBoard(char board[][3]); void instructions(); void displayBoard(char board[][3]);/*gets input from user returnscharacter number of location chosen by user*/ void getInput(char board[][3], char marker,int& r,int&c);/* mark character marker of player on chosen character pos ofboard*/ void markBoard(char board[][3],int r,int c, char marker);/*checksto see if someone has won returns true or false*/ bool gameOver(char board[][3]); /*checks to see if someone won onrows of board*/ bool checkHorizontal(char board[][3], char marker);/*checks to seeif someone won on columns of board*/ bool checkVertical(char board[][3], char marker);/*checks to see ifsomeone won on diagonal of board*/ bool checkDiagonal(char board[][3], char marker);/*checks to see ifplayers have tied*/ bool checkTie(char board[][3]);/*prints winner as marker orties*/ void printWinner(char marker);/*checks to see if selected locationis available returns false when location has already been taken oris an invalid number*/ bool validMove(char board[][3], int r,int c); int main() { char board[3][3]; char exit; int r,c; do {instructions(); initializeBoard(board); displayBoard(board); while(true) { //player 1's turn getInput(board,'X',r,c); markBoard(board, r,c, 'X'); displayBoard(board); if (gameOver(board)) //if player 1 wins break out of loopbreak break; //player 2's turn getInput(board,'O',r,c); markBoard(board, r,c, 'O'); displayBoard(board); if (gameOver(board)) //if player 2 wins break out of loop break; } cout > exit; }while((exit != 'N') && (exit != 'n')); return 0; } void initializeBoard(char board[][3]) { char count('0'); for (int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.