In C++, specify design, and implement a class that can be one player in a game o
ID: 3790887 • Letter: I
Question
In C++, specify design, and implement a class that can be one player in a game of tic-tac-toe. The constructor should specify whether the object is to be the first player (X's) or the second player (O's). THere should be a member function to ask the object to make its next move, and a member function that tells the object what the opponent's next move is. Also include other useful member functions, such as a function to ask whether a given spot of the tic-tac-toe board is occupied, and if so, whether the occupation is with an X or an O. Also, include a member function to determine wien the game is over, and whether it was a draw, an X win, or an O win.
Use the class in two programs: a program that plays tic-tac-toe against the program's user, and a program that has two tic-tac-toe objects play against each other.
Here is the skeleton code for the tic-tac-toe main() program for computer vs. computer:
Here is the skeleton code for the tic-tac-toe main() for computer vs. player:
Here is the skeleton code for the header file:
The skeleton code doesn't have to be followed, but was given as a guideline if we chose to follow it. A random function can be used to play for the computer, and it doesn't have to make a move that is logical.
Explanation / Answer
#include<iostream>
using namespace std;
//TicTacToe class definition
class TicTacToe
{
//Default constructor to initialize each board position to zero
public:
//To store TicTacToe board value
int board[9];
//To store player status
int player;
TicTacToe()
{
//Loops through 9 places and initializes to zero
for(int r = 0; r < 9; r++)
{
board[r] = 0;
}//End of for loop for row
}//End of constructor
//Shows the board status
void showBoard()
{
//Loops through 9 places in board
for(int r = 1; r <= 9; r++)
{
//Displays position value
cout<<board[r-1]<<" ";
//After 3 display a new line printed
if(r % 3 == 0)
cout<<endl;
}//End of for loop for row
}//End of method showBoard
//Players move status
void move(int playerNo, int position)
{
//Checks whether the player move is legal or not
if(legalPlayer(playerNo))
{
/* Checks the position is legal or not. If the position is less then zero or more than 9 or board position is not zero i.e., marked by any other player it is illegal*/
if(position < 0 || position >= 9 || board[position] != 0)
cout<<" Error: Illegal Position"<<endl;
//If player is legal and position is legal
else
{
//Board position is assigned with player number
board[position] = playerNo;
//If currently it is player 1 move set it for player 2
if(playerNo == 1)
player = 2;
//If currently it is player 2 move set it for player 1
else
player = 1;
}//End of else
}//End of if condition
//For illegal player number
else
cout<<" Illegal Player Number"<<endl;
}//End of method move
//Returns true if player is legal otherwise false
bool legalPlayer(int playerNo)
{
//Checks if the current player is the existing player returns false
if(player != playerNo)
return false;
//If the current player is not the existing player returns true
else
return true;
}//End of method legalPlayer
//Displays the game result
void gameResult(int res)
{
showBoard();
//If the result is 1 player 1 won
if(res == 1)
cout<<" Player 1 won";
//If the result is 2 player 2 won
else if(res == 2)
cout<<" Player 2 won";
//If the result is 0 it is tie
else if(res == 0)
cout<<" Game tie";
}//End of method gameResult
//Returns the board status
bool boardStatus()
{
//Initially status is true
bool status = true;
//Loops through 9 places
for(int i = 0; i < 9; i++)
//If current board value is zero then move is available in the board. Return false. */
if(board[i] == 0)
status = false;
/* If all the places in the board value is not zero board is full return true. */
return status;
}//End of method boardStatus
//Returns the winner status
bool ownStatus()
{
/* If either row or column or diagonal is filled with 1 player 1 won*/
if(rowStasus(1) || columnStatus(1) || diagonalStatus(1))
{
//Calls the gameResult method to display Player 1 won.
gameResult(1);
return true;
}//End of if for player 1
/* If either row or column or diagonal is filled with 2 player 2 won*/
else if(rowStasus(2) || columnStatus(2) || diagonalStatus(2))
{
//Calls the gameResult method to display Player 2 won.
gameResult(2);
return true;
}//End of if for player 2
//Calls the method tie() to check whether it is tie or not.
else if(tie())
{
//Calls the gameResult method to display it is tie.
gameResult(0);
return true;
}//End of else for tie
//Otherwise moves are available
else
return false;
}//End of method ownStatus
//Displays the tie status
bool tie()
{
//Checks for the board status whether moves are available or not.
if(boardStatus())
{
//Checks if neither player 1 or player 2 won returns true for tie
if(!(rowStasus(1) || columnStatus(1) || diagonalStatus(1) || rowStasus(2) || columnStatus(2) || diagonalStatus(2)))
return true;
}//End of if
//Otherwise returns false
return false;
}//End of method tie
//Returns row status for player 1 or 2
bool rowStasus(int playerNo)
{
//Creates an array for each row sum
int rSum[3];
int value;
//If it is player 1 row sum will be 3
if(playerNo == 1)
value = 3;
//If it is player 2 row sum will be 6
else
value = 6;
//Loops through 9 places in board
for(int r = 0, t = 0; r < 9; r+=3, t++)
{
//Initializes the current row sum position to zero.
rSum[t] = 0;
//Loops for 3 times for each row
for(int c = 0; c < 3; c++)
{
//Checks for board value is 1 and player 1
if(board[r+c] == 1 && playerNo == 1)
//Adds the row sum with the board value
rSum[t] += board[r+c];
//Checks for board value is 2 and player 2
else if(board[r+c] == 2 && playerNo == 2)
//Adds the row sum with the board value
rSum[t] += board[r+c];
}//End of for loop for variable c
}//End of for loop for variable r
/* Checks if any of the row sum is equal to the value for the player. Value 3 for player 1 value 6 for player 2. */
if(rSum[0] == value || rSum[1] == value || rSum[2] == value)
return true;
//Otherwise returns false
else
return false;
}//End of method rowStatus
//Returns column status for player 1 or 2
bool columnStatus(int playerNo)
{
//Creates an array for each column sum
int cSum [3];
int value;
//If it is player 1 column sum will be 3
if(playerNo == 1)
value = 3;
//If it is player 2 column sum will be 6
else
value = 6;
//Loops through 9 places in board
for(int r = 0; r < 3; r++)
{
//Initializes the current column sum position to zero.
cSum[r] = 0;
//Loops through 9 places in board
for(int c = r; c < 9; c+=3)
{
//Checks for board value is 1 and player 1
if(board[c] == 1 && playerNo == 1)
//Adds the column sum with the board value
cSum[r] += board[c];
//Checks for board value is 2 and player 2
else if(board[c] == 2 && playerNo == 2)
//Adds the column sum with the board value
cSum[r] += board[c];
}//End of for loop for variable c
}//End of for loop for variable r
/* Checks if any of the column sum is equal to the value for the player. Value 3 for player 1 value 6 for player 2. */
if(cSum[0] == value || cSum[1] == value || cSum[2] == value)
return true;
else
return false;
}//End of method columnStatus
//Returns diagonal status for player 1 or 2
bool diagonalStatus(int playerNo)
{
//Initially left and right diagonal value is zero.
int lDig = 0;
int rDig = 0;
int value;
//If it is player 1 diagonal sum will be 3
if(playerNo == 1)
value = 3;
//If it is player 2 diagonal sum will be 6
else
value = 6;
//Loops through 9 places in the board for left diagonal.
for(int c = 0; c < 9; c+=4)
//Checks for board value is 1 and player 1
if(board[c] == 1 && playerNo == 1)
//Adds the left diagonal sum with the board value
lDig += board[c];
//Checks for board value is 2 and player 2
else if(board[c] == 2 && playerNo == 2)
//Adds the left diagonal sum with the board value
lDig += board[c];
//Loops through 9 places in the board for right diagonal.
for(int c = 2; c < 9; c+=2)
//Checks for board value is 1 and player 1
if(board[c] == 1 && playerNo == 1)
//Adds the right diagonal sum with the board value
rDig += board[c];
//Checks for board value is 2 and player 2
else if(board[c] == 2 && playerNo == 2)
//Adds the right diagonal sum with the board value
rDig += board[c];
/* Checks if any of the diagonal sum is equal to the value for the player.
* Value 3 for player 1 value 6 for player 2. */
if(lDig == value || rDig == value)
return true;
else
return false;
}//End of method diagonalStatus
};//End of class
//Main method
int main()
{
//Variable for player and position
int currentPlayer, position;
//Creates an object of TicTacToe class
TicTacToe tt;
//Variable to keep track who is the first player to start game.
int c = 0;
//Loops till either a player wins or it is tie
while(!tt.ownStatus())
{
//Displays the board
tt.showBoard();
//Accepts the player number
cout<<" Enter the player number: 1 for Player 1 2 for Player 2 ";
cin>>currentPlayer;
//Accepts the position
cout<<" Enter the Position: 0 - 8: ";
cin>>position;
//Checks for the first player to start game
if(c == 0 && (currentPlayer == 1 || currentPlayer == 2))
{
tt.player = currentPlayer;
c++;
}//End of if condition
//Calls the move function
tt.move(currentPlayer, position);
}//End of while loop
}//End of main method
Output:
0 0 0
0 0 0
0 0 0
Enter the player number:
1 for Player 1
2 for Player 2 1
Enter the Position: 0 - 8: 3
0 0 0
1 0 0
0 0 0
Enter the player number:
1 for Player 1
2 for Player 2 1
Enter the Position: 0 - 8: 6
Illegal Player Number
0 0 0
1 0 0
0 0 0
Enter the player number:
1 for Player 1
2 for Player 2 2
Enter the Position: 0 - 8: 6
0 0 0
1 0 0
2 0 0
Enter the player number:
1 for Player 1
2 for Player 2 1
Enter the Position: 0 - 8: 4
0 0 0
1 1 0
2 0 0
Enter the player number:
1 for Player 1
2 for Player 2 2
Enter the Position: 0 - 8: 7
0 0 0
1 1 0
2 2 0
Enter the player number:
1 for Player 1
2 for Player 2 1
Enter the Position: 0 - 8: 5
0 0 0
1 1 1
2 2 0
Player 1 won