Please help me with this program: Write a program that will allow two users to p
ID: 3536749 • Letter: P
Question
Please help me with this program:
Write a program that will allow two users to play tic tac toe. The program should ask for moves alternately from player x and o. The program displays the game position as follows
1 2 3
4 5 6
7 8 9
The players enter their moves by entering the position number they wish to mark. After each move, the program displays the changed board. A sample board configuration is as follows:
X X O
4 5 6
7 X X
#include <iostream>
#include <fstream>
using namespace std;
void ini_game(char a[][3]);
void display_game(char a[][3]);
int main()
{
/*
tic tac toe game
data structure: a 2 dim char array
input from player: char p
a boolean var represent players
1. a player enters a number (char for position)
2. validate input:
-input with 1 - 9
if (p>='1' && p<='9')
-position on board still available
if ( avail_spot(gb, p))
3. place the player id onto game board
4. display the game board
5. check if game has ended
- if winner, announce winner
- if there is any more available spot,
if not, announce a draw
6. switch player, and go back to step 1
*/
char gb[3][3];
char p; //input from player
ini_game(gb);
display_game(gb);
cout << endl;
system("pause");
return 0;
}
bool avail_spot(char a[][3], char c, bool playerx)
{
int n=c - 48;
int i, j;
i = (n-1) /3;
j = (n-1)%3;
if (a[i][j] == c)
{
if (playerx)
{a[i][j] = 'X';}
else
{a[i][j] = 'O';}
return true;
}
else
{
return false;
}
}
void ini_game(char a[][3])
{
char c='1';
for (int i=0; i<3; i++)
{
for (int j=0; j<3; j++)
{
a[i][j] = c;
c++;
}
}
}
void display_game(char a[][3])
{
for (int i=0; i<3; i++)
{
for (int j=0; j<3; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
}
Explanation / Answer
//tic tac toe game #include using std::cout; using std::endl; using std::cin; //#include //using std::setw; int main() { // char square[3] [3]; char sq1('1'); char sq2('2'); char sq3('3'); char sq4('4'); char sq5('5'); char sq6('6'); char sq7('7'); char sq8('8'); char sq9('9'); int player(1);//player bool gameover(true); do { coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.