This is the problem I am working with and need help coding thanks PC 17 pg 461 1
ID: 3621022 • Letter: T
Question
This is the problem I am working with and need help coding thanksPC 17 pg 461
17. Tic-Tac-Toe Game
Write a program that allows two players to play a game of tic-tac-toe. Use a twovdimensional
char array with three rows and three columns as the game board. Each
element of the array should be initialized with an asterisk (':0). The program should run a
loop that
• Displays the contents of the board array
• Allows player 1 to select a location on the board for an X. The program should
ask the user to enter the row and column number.
• Allows player 2 to select a location on the board for an O. The program should
ask the user to enter the row and column number.
• Determines whether a player has won, or a tie has occurred. If a player has won,
the program should declare that player the winner and end. If a tie has occurred,
the program should say so and end.
Player 1 wins when there are three Xs in a row on the game board. The Xs can appear
in a row, in a column, or diagonally across the board. A tie occurs when all of the
locations on the board are full, but there is no winner.
Explanation / Answer
please rate - thanks
coordinates are 1-3 and entered separated by a comma.
top left hand corner you would enter as 1,1
#include <iostream>
using namespace std;
char matrix[3][3]; /* the tic tac toe matrix */
char check(void);
void init_matrix(void);
void get_player_move(void);
void get_computer_move(void);
void disp_matrix(void);
int main(void)
{char done;
printf("This is the game of Tic Tac Toe. ");
printf("You will be playing against the computer. ");
done = '*';
init_matrix();
do {
disp_matrix();
get_player_move();
done = check(); /* see if winner */
if(done!= '*') break; /* winner!*/
get_computer_move();
done = check(); /* see if winner */
} while(done== '*');
if(done=='X') cout<<"You won! ";
else cout<<"I won!!!! ";
disp_matrix(); /* show final positions */
system("pause");
return 0;
}
/* Initialize the matrix. */
void init_matrix(void)
{ int i, j;
for(i=0; i<3; i++)
for(j=0; j<3; j++) matrix[i][j] = '*';
}
/* Get a player's move. */
void get_player_move(void)
{ int x, y;
char comma;
cout<<"Enter X,Y coordinates for your move: ";
cin>>x>>comma>>y;
x--;
y--;
if(matrix[x][y]!= '*'){
cout<<"Invalid move, try again. ";
get_player_move();
}
else matrix[x][y] = 'X';
}
/* Get a move from the computer. */
void get_computer_move(void)
{
int i, j;
for(i=0; i<3; i++){
for(j=0; j<3; j++)
if(matrix[i][j]=='*') break;
if(matrix[i][j]=='*') break;
}
if(i*j==9) {
cout<<"draw ";
exit(0);
}
else
matrix[i][j] = 'O';
}
/* Display the matrix on the screen. */
void disp_matrix(void)
{ int t;
for(t=0; t<3; t++) {
cout<<" "<<matrix[t][0]<<" | "<< matrix[t][1]<<" | "<< matrix [t][2];
if(t!=2) cout<<" ---|---|--- ";
}
cout<<" ";
}
/* See if there is a winner. */
char check(void)
{
int i;
for(i=0; i<3; i++) /* check rows */
if(matrix[i][0]==matrix[i][1] &&
matrix[i][0]==matrix[i][2]) return matrix[i][0];
for(i=0; i<3; i++) /* check columns */
if(matrix[0][i]==matrix[1][i] &&
matrix[0][i]==matrix[2][i]) return matrix[0][i];
/* test diagonals */
if(matrix[0][0]==matrix[1][1] &&
matrix[1][1]==matrix[2][2])
return matrix[0][0];
if(matrix[0][2]==matrix[1][1] &&
matrix[1][1]==matrix[2][0])
return matrix[0][2];
return ' ';
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.