Write a program that allows two players to play a game of tic-tac-toe. Use a two
ID: 3684450 • Letter: W
Question
Write a program that allows two players to play a game of tic-tac-toe. Use a twodimensional
char array with three rows and three columns as the game board. Each
element of the array should be initialized with an asterisk (*). 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.
Constraints:
DO NOT USE GLOBAL VARIABLES (GLOBAL CONSTANTS ARE OK)
Program must use functions and CANNOT be written as one main program.
Use a function to display a set of instructions describing how the user must enter a "move" (this has to called only once at the beginning of the program)
Use a function to display the contents of the board.
The program must clear the display screen after the user enters a new move, then display the contents of the board with the new move shown (see sample output below).
Use a function for the player's turn.
Validate each users move (i.e. if the user enters a location that already contains an X or an O or the numbers entered are not a valid position on the board an error will be displayed). The program must then ask the user to re-input their move.
Use a function to determine whether the player has won. The function should receive as arguments, the board and the player ( 'X' or 'O'). You also check for a Tie or End of Game in this function or create a separate function to do this.
The program should ask the player to enter the row and column number.
The board and the elements contained within the board must line up correctly.
The program MUST use a 2D Array.
Determine whether a player has won or there is a tie. If there is a winner, the program should display that a player has won the game and which player has won. If there is a tie the program should display that a tie has occurred an exit.
Explanation / Answer
#include <iostream.h>
#include <iomanip.h>
using namespace std;
const int SIZE = 3;
char a[3][3] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};
char b = '0';
void draw();
bool getinputPlayer1();
bool getinputPlayer2();
bool checkwinPlayer1();
bool checkwinPlayer2();
int main()
{
draw();
cout << "Player one make a move to play";
getinputPlayer1();
draw();
cout << "Player two make a move to play";
getinputPlayer2();
draw();
for (;;)
{
cout << "Player one make a move to play" ;
getinputPlayer1();
if(checkwinPlayer1() == 1)
{
draw();
cout << "Player one, you win!" ;
break;
}
draw();
cout << "Player two make a move to play";
getinputPlayer2();
if(checkwinPlayer2() == 1)
{
draw();
cout << "Player two, you win!";
break;
}
else
{
draw();
cout << "It's a tie" ;
break;
}
}
return 0;
}
void draw()
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
cout << setw(5) << a[j];
}
cout << endl;
}
}
bool getinputPlayer1()
{
cout << " Enter the move to play game ";
cin >> b;
switch(b)
{
case '1' :
a[0][0] = 'X';
break;
case '2' :
a[0][1] = 'X';
break;
case '3' :
a[0][2] = 'X';
break;
case '4' :
a[1][0] = 'X';
break;
case '5' :
a[1][1] = 'X';
break;
case '6' :
a[1][2] = 'X';
break;
case '7' :
a[2][0] = 'X';
break;
case '8' :
a[2][1] = 'X';
break;
case '9' :
a[2][2] = 'X';
break;
default:
cout << "Hey!! Play it according to rules !" ;
}
return 0;
}
bool getinputPlayer2()
{
cout << "Enter the move to play game" ;
cin >> b;
switch(b)
{
case '1' :
a[0][0] = 'O';
break;
case '2' :
a[0][1] = 'O';
break;
case '3' :
a[0][2] = 'O';
break;
case '4' :
a[1][0] = 'O';
break;
case '5' :
a[1][1] = 'O';
break;
case '6' :
a[1][2] = 'O';
break;
case '7' :
a[2][0] = 'O';
break;
case '8' :
a[2][1] = 'O';
break;
case '9' :
a[2][2] = 'O';
break;
default:
cout << "Hey!! Play it according to rules!" ;
}
return 0;
}
bool checkwinPlayer1()
{
if(a[0][0] == 'X' && a[0][1] == 'X' && a[0][2] == 'X')
return 1;
if(a[1][0] == 'X' && a[1][1] == 'X' && a[1][2] == 'X')
return 1;
if(a[2][0] == 'X' && a[2][1] == 'X' && a[2][2] == 'X')
return 1;
if(a[0][0] == 'X' && a[1][0] == 'X' && a[2][0] == 'X')
return 1;
if(a[0][1] == 'X' && a[1][1] == 'X' && a[2][1] == 'X')
return 1;
if(a[0][2] == 'X' && a[1][2] == 'X' && a[2][2] == 'X')
return 1;
if(a[0][0] == 'X' && a[1][1] == 'X' && a[2][2] == 'X')
return 1;
if(a[2][0] == 'X' && a[1][1] == 'X' && a[0][2] == 'X')
return 1;
}
bool checkwinPlayer2()
{
if(a[0][0] == 'O' && a[0][1] == 'O' && a[0][2] == 'O')
return 1;
if(a[1][0] == 'O' && a[1][1] == 'O' && a[1][2] == 'O')
return 1;
if(a[2][0] == 'O' && a[2][1] == 'O' && a[2][2] == 'O')
return 1;
if(a[0][0] == 'O' && a[1][0] == 'O' && a[2][0] == 'O')
return 1;
if(a[0][1] == 'O' && a[1][1] == 'O' && a[2][1] == 'O')
return 1;
if(a[0][2] == 'O' && a[1][2] == 'O' && a[2][2] == 'O')
return 1;
if(a[0][0] == 'O' && a[1][1] == 'O' && a[2][2] == 'O')
return 1;
if(a[2][0] == 'O' && a[1][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.