In this assignment, you will implement a simple Tic-Tac-Toe game. It will be pos
ID: 3871840 • Letter: I
Question
In this assignment, you will implement a simple Tic-Tac-Toe game. It will be possible to play human-vs-human, human-vs-computer, or computer-vs-computer. Tic-Tac-Toe, also called X's and O's, Noughts and Crosses, and X and 0 is a simple game played on a 3x3 grid, re ferred to as the board. The grid starts empty, and the two players take turns placing the ir respective symbols, X and O, on an empty grid cell, referred to as mak ing a move. The first player to get a straight line of three symbols wins. If all the cells on the board are filled and neither payer has a line of3 symbols, the game is a tie. Lines may be horizontal, vertical, or You will implement a Board class to represent the 3x3 grid. This class will have functions to determine which symbol if any, is in a cell, to place a symbol in a cell, to determine the winner if any so far, and to print the board to standard output. The board should appear as below: lxIExplanation / Answer
#include <stdio.h>
#include <iostream>
using namespace std;
enum GameStates
{
GAME_RUNNING,
GAME_TIE,
X_WON,
O_WON
};
struct GameMove
{
int x;
int y;
};
class TieTacToe
{
// Access specifier
public:
static const int SIDE = 3; // Length of the board
static const char MOVE0 = 'O';
static const char MOVEX = 'X';
static const char BLANK = ' ';
char board[SIDE][SIDE];
int moves[SIDE*SIDE];
void showBoard();
void showInstructions();
void initialise();
void move(GameMove m, char c);
GameStates rowCrossed();
GameStates columnCrossed();
GameStates diagonalCrossed();
char current_winner;
bool gameOver();
};
/////////////////
// A function to show the current board status
void TieTacToe::showBoard()
{
printf(" ");
printf(" %c | %c | %c ", board[0][0],
board[0][1], board[0][2]);
printf(" ---------- ");
printf(" %c | %c | %c ", board[1][0],
board[1][1], board[1][2]);
printf(" ---------- ");
printf(" %c | %c | %c ", board[2][0],
board[2][1], board[2][2]);
return;
}
// A function to show the instructions
void TieTacToe::showInstructions()
{
printf(" Tic-Tac-Toe ");
printf("Choose a cell numbered from 1 to 9 as below"
" and play ");
printf(" 1 | 2 | 3 ");
printf(" -------------- ");
printf(" 4 | 5 | 6 ");
printf(" -------------- ");
printf(" 7 | 8 | 9 ");
printf("- - - - - - - - - - ");
return;
}
// A function to initialise the game
void TieTacToe::initialise()
{
for (int i = 0; i < SIDE; ++i)
{
for (int j = 0; j < SIDE; ++j)
{
board[i][j] = BLANK;
}
}
current_winner = BLANK;
return;
}
void TieTacToe::move(GameMove m, char c){
if(board[m.x][m.y] == BLANK){
board[m.x][m.y] = c;
showBoard();
}
else{
//cout << "Not empty" << endl;
}
}
// A function that returns true if any of the row
// is crossed with the same player's move
GameStates TieTacToe::rowCrossed()
{
for (int i=0; i<SIDE; i++)
{
if (board[i][0] == board[i][1] &&
board[i][1] == board[i][2] &&
board[i][0] != BLANK)
{
current_winner = board[i][0];
if(board[i][0] == MOVEX) return GameStates::X_WON;
if(board[i][0] == MOVE0) return GameStates::O_WON;
}
}
return GameStates::GAME_RUNNING;
}
// A function that returns true if any of the column
// is crossed with the same player's move
GameStates TieTacToe::columnCrossed()
{
for (int i=0; i<SIDE; i++)
{
if (board[0][i] == board[1][i] &&
board[1][i] == board[2][i] &&
board[0][i] != ' ')
{
current_winner = board[0][i];
if(board[0][i] == MOVEX) return GameStates::X_WON;
if(board[0][i] == MOVE0) return GameStates::O_WON;
}
}
return GameStates::GAME_RUNNING;
}
// A function that returns true if any of the diagonal
// is crossed with the same player's move
GameStates TieTacToe::diagonalCrossed()
{
if (board[0][0] == board[1][1] &&
board[1][1] == board[2][2] &&
board[0][0] != BLANK)
{
current_winner = board[0][0];
if(board[0][0] == MOVEX) return GameStates::X_WON;
if(board[0][0] == MOVE0) return GameStates::O_WON;
}
if (board[0][2] == board[1][1] &&
board[1][1] == board[2][0] &&
board[0][2] != ' ')
{
current_winner = board[0][0];
if(board[1][1] == MOVEX) return GameStates::X_WON;
if(board[1][1] == MOVE0) return GameStates::O_WON;
}
return GameStates::GAME_RUNNING;
}
// A function that returns true if the game is over
// else it returns a false
bool TieTacToe::gameOver()
{
return (
(rowCrossed() != GameStates::GAME_RUNNING)
|| (columnCrossed() != GameStates::GAME_RUNNING)
|| (diagonalCrossed() !=GameStates::GAME_RUNNING)
);
}
int main() {
/* initialize random seed: */
srand (time(NULL));
// Declare an object of class geeks
TieTacToe obj1 ;
obj1.initialise();
GameMove m1;
m1.x = 1;
m1.y = 1;
obj1.move(m1, TieTacToe::MOVEX);
m1.x = 1;
m1.y = 0;
obj1.move(m1, TieTacToe::MOVE0);
for (int i = 0; i < 100; ++i)
{
if (!obj1.gameOver())
{
m1.x = rand() % 3 ;
m1.y = rand() % 3 ;
if(i %2 ){
obj1.move(m1, TieTacToe::MOVE0);
}else{
obj1.move(m1, TieTacToe::MOVEX);
}
}else{
cout << "game over ";
cout << "winner is : " << obj1.current_winner << endl;
break;
}
}
// accessing data member
//obj1.geekname = "Abhi";
// accessing member function
//obj1.printname();
return 0;
}
| |
----------
| X |
----------
| |
| |
----------
O | X |
----------
| |
| |
----------
O | X |
----------
| | X
| | O
----------
O | X |
----------
| | X
| | O
----------
O | X |
----------
| O | X
| | O
----------
O | X | X
----------
| O | X
| | O
----------
O | X | X
----------
O | O | X
| X | O
----------
O | X | X
----------
O | O | X
O | X | O
----------
O | X | X
----------
O | O | X
game over