Users/jamyc/Downloads/Assignment3.pdf Programming Exercise: Create a tic-tac-toe
ID: 3727921 • Letter: U
Question
Users/jamyc/Downloads/Assignment3.pdf Programming Exercise: Create a tic-tac-toe game by using arrays. Consider the following requirements during your implementation: 1. You have the choice to play: user-to-user or user-to-computer (computer must generate random numbers) 2. The program should ask the user ifhe wants to go first (be X) or second (be 0) 3 The program should have the ability to make "intelligent moves acting either as the X-player or the 0-player. It should 4. The program should have the ability to make "intelligent moves acting either as the X player or the 0-player. It should make no illegal or impossible moves, nor remove any previous move until the game is over The program should have referee capability which can determine whether a move is a good move and also whether a game has been won by X-player or 0-player. It should stop the player from making an impossible move (e g one not on the game board) and an illegal move (e g. one that attempts to place X or O over an already occupied location) 5. 6. The program should re-display the current board after each move, and should have the following appearance including the matrix indices. (Take this as an example of a game in progress.) (0,0) (0,2) (2.0) (2.2) 7. The user should be able to enter his move by answering at a prompt Your mote "with the location of his next move. rc (that is, row#, column#). For instance, in the game above, the user being X could have responded with 2, 2 -in which case he would have been notified that he won the game, Tic-tac-toe!" But, for example, if the user had entered. 1 0 he would have been told that it is illegal to attempt to move on to an already occupied location.Explanation / Answer
#include <iostream.h>
using namespace std;
class TicTacToe
{
public:
TicTacToe();
int PlayerMove(int i);
void NextPlayer();
void WinCheck();
void DrawBoard();
private:
int board[3][3];
int turn; // player1 == 1, player2 == 2
int gameOver;
void PlayGame();
};
TicTacToe::TicTacToe()
{
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
board[i][j] = 0;// 0 means empty
turn = 1; // player1
gameOver = 0;
DrawBoard();
PlayGame();
}
/*****************************************************************************
* i is the board position that the player selects.
* Calculate the x and y coordinates.
* Ensure board position is valid. Check if game has been won. Switch Players. Update board.
* Return: 0 if move was made, otherwise return the value stored in the board position.
*****************************************************************************/
int TicTacToe::PlayerMove(int i)
{
int x = (i - 1)/3;
int y = ((i + 2) % 3);
int returnVal = board[x][y];
if (returnVal == 0)
{
board[x][y] = turn;
WinCheck();
if (!gameOver)
NextPlayer();
}
else
cout << "Invalid move, try again. ";
DrawBoard();
return returnVal;
}
void TicTacToe::NextPlayer()
{
if (turn == 1)
turn = 2;
else
turn = 1;
}
/*****************************************************************************
* If the game has been won, set gameOver equal to turn.
* Turn always contains a value that is boolean true: 1 or 2.
*****************************************************************************/
void TicTacToe::WinCheck()
{
if ((board[0][0] == turn) && (board[1][0] == turn) && (board[2][0] == turn))
gameOver = turn;
else
if ((board[0][1] == turn) && (board[1][1] == turn) && (board[2][1] == turn))
gameOver = turn;
else
if ((board[0][2] == turn) && (board[1][2] == turn) && (board[2][2] == turn))
gameOver = turn;
else
if ((board[0][0] == turn) && (board[0][1] == turn) && (board[0][2] == turn))
gameOver = turn;
else
if ((board[1][0] == turn) && (board[1][1] == turn) && (board[1][2] == turn))
gameOver = turn;
else
if ((board[2][0] == turn) && (board[2][1] == turn) && (board[2][2] == turn))
gameOver = turn;
else
if ((board[0][0] == turn) && (board[1][1] == turn) && (board[2][2] == turn))
gameOver = turn;
else
if ((board[0][2] == turn) && (board[1][1] == turn) && (board[2][0] == turn))
gameOver = turn;
}
/*****************************************************************************
* If the game has been won, set gameOver equal to turn.
* Turn always contains a value that is boolean true: 1 or 2.
*****************************************************************************/
void TicTacToe::PlayGame()
{
int i;
while (gameOver!=turn)
{
//DrawBoard();
cout << "Player[" << turn << "] Please enter move: ";
cin >> i;
PlayerMove(i);
}
cout << "Player[" << turn << "] Wins!" << endl;
}
/*****************************************************************************
* Display the game board using ASCII characters.
*****************************************************************************/
void TicTacToe::DrawBoard()
{
int temp[9];
int k = 0;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
{
if (board[i][j] == 0)
temp[k] = k+49;
else
{
if (board[i][j] == 1)
temp[k] = 88;
else
temp[k] = 79;
}
k++;
}
cout << "+---+---+---+ ";
cout <<"| " << (char)temp[0] << " | " << (char)temp[1] << " | " << (char)temp[2] << " | ";
cout << "+---+---+---+ ";
cout <<"| " << (char)temp[3] << " | " << (char)temp[4] << " | " << (char)temp[5] << " | ";
cout << "+---+---+---+ ";
cout <<"| " << (char)temp[6] << " | " << (char)temp[7] << " | " << (char)temp[8] << " | ";
cout << "+---+---+---+ ";
}
/*****************************************************************************
* Instantiate a TicTacToe object, which effectively starts your game play.
*****************************************************************************/
int main()
{
TicTacToe Game;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.