Tic-Tac-Toe Game Write a modular program that allows two players to play a game
ID: 3765377 • Letter: T
Question
Tic-Tac-Toe Game
Write a modular program that allows two players to play a game of tic-tac-toe. Use a twodimensional
char array with 3 rows and 3 columns as the game board. Each element of
the array should be initialized with an asterisk (*). The program should display the initial
board configuration and then start a loop that does the following:
• Allow player 1 to select a location on the board for an X by entering a row and column
number. Then redisplay the board with an X replacing the * in the chosen location.
• If there is no winner yet and the board is not yet full, allow player 2 to select a location
on the board for an O by entering a row and column number. Then redisplay the
board with an O replacing the * in the chosen location.
The loop should continue until a player has won or a tie has occurred, then display a message
indicating who won, or reporting that a tie occurred.
• Player 1 wins when there are three Xs in a row, a column, or a diagonal on the game
board.
• Player 2 wins when there are three Os in a row, a column, or a diagonal on the game
board.
• A tie occurs when all of the locations on the board are full, but there is no winner.
Input Validation:
Only allow legal moves to be entered. The row must be 1, 2, or 3.
The column must be 1, 2, or 3. The (row, column) position entered must currently be
empty (i.e., still have an asterisk in it).
Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
const int SIZE = 3;
char t[3][3] = {
{'*','*','*'},
{'*','*','*'},
{'*','*','*'}
};
char ch = '0';
void draw();
bool getinputP1();
bool getinputP2();
bool checkwinP1();
bool checkwinP2();
int main()
{
draw();
cout << "Player one make your move" << endl;
getinputP1();
draw();
cout << "Player two make your move" << endl;
getinputP2();
draw();
for (;;)
{
cout << "Player one make your move" << endl;
getinputP1();
if(checkwinP1() == 1)
{
draw();
cout << "Player one, you win!" << endl;
break;
}
draw();
cout << "Player two make your move" << endl;
getinputP2();
if(checkwinP2() == 1)
{
draw();
cout << "Player two, you win!" << endl;
break;
}
else
{
draw();
cout << "It's a tie" << endl;
break;
}
}
return 0;
}
void draw()
{
for (int n = 0; n < SIZE; n++)
{
for (int m = 0; m < SIZE; m++)
{
cout << setw(5) << t[n][m];
}
cout << endl;
}
}
bool getinputP1()
{
cout << "Enter your move" << endl;
cin >> ch;
switch(ch)
{
case '1' : t[0][0] = 'X'; break;
case '2' : t[0][1] = 'X'; break;
case '3' : t[0][2] = 'X'; break;
case '4' : t[1][0] = 'X'; break;
case '5' : t[1][1] = 'X'; break;
case '6' : t[1][2] = 'X'; break;
case '7' : t[2][0] = 'X'; break;
case '8' : t[2][1] = 'X'; break;
case '9' : t[2][2] = 'X'; break;
default: cout << you have to play the rule << endl;
}
return 0;
}
bool getinputP2()
{
cout << "Enter your move" << endl;
cin >> ch;
switch(ch)
{
case '1' : t[0][0] = 'O'; break;
case '2' : t[0][1] = 'O'; break;
case '3' : t[0][2] = 'O'; break;
case '4' : t[1][0] = 'O'; break;
case '5' : t[1][1] = 'O'; break;
case '6' : t[1][2] = 'O'; break;
case '7' : t[2][0] = 'O'; break;
case '8' : t[2][1] = 'O'; break;
case '9' : t[2][2] = 'O'; break;
default: cout << "you have to play the rule!" << endl;
}
return 0;
}
bool checkwinP1()
{
if(a[0][0] == 'X' && t[0][1] == 'X' && a[0][2] == 'X') return 1;
if(a[1][0] == 'X' && t[1][1] == 'X' && a[1][2] == 'X') return 1;
if(a[2][0] == 'X' && t[2][1] == 'X' && a[2][2] == 'X') return 1;
if(a[0][0] == 'X' && t[1][0] == 'X' && a[2][0] == 'X') return 1;
if(a[0][1] == 'X' && t[1][1] == 'X' && a[2][1] == 'X') return 1;
if(a[0][2] == 'X' && t[1][2] == 'X' && a[2][2] == 'X') return 1;
if(a[0][0] == 'X' && t[1][1] == 'X' && a[2][2] == 'X') return 1;
if(a[2][0] == 'X' && t[1][1] == 'X' && a[0][2] == 'X') return 1;
}
bool checkwinP2()
{
if(a[0][0] == 'O' && t[0][1] == 'O' && a[0][2] == 'O') return 1;
if(a[1][0] == 'O' && t[1][1] == 'O' && a[1][2] == 'O') return 1;
if(a[2][0] == 'O' && t[2][1] == 'O' && a[2][2] == 'O') return 1;
if(a[0][0] == 'O' && t[1][0] == 'O' && a[2][0] == 'O') return 1;
if(a[0][1] == 'O' && t[1][1] == 'O' && a[2][1] == 'O') return 1;
if(a[0][2] == 'O' && t[1][2] == 'O' && a[2][2] == 'O') return 1;
if(a[0][0] == 'O' && t[1][1] == 'O' && a[2][2] == 'O') return 1;
if(a[2][0] == 'O' && t1][1] == 'O' && a[0][2] == 'O') return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.