Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// Please use c++ Game of Tic Tac Toe Use a 2-D char array with 3 rows and 3 col

ID: 3874902 • Letter: #

Question

// Please use c++ Game of Tic Tac Toe Use a 2-D char array with 3 rows and 3 columns as the game board. Each element in the array should be initialized with an asterix (*). The program should contain a loop that 1) Displays the contents of the board array. 2) Allows player 1 to select a location on the board for an X, the program should ask the user for the row and col number. Validate the row and column entered. 3) Allows player 2 to select a location on the board for an O. The program should ask the user for the row and col number. Validate the row and column entered. 4) Determine whether a player has won, or a tie has occurred. If the player has won, the program should declare the player the winner and end. If a tie has occurred the program should say so and end. Player 1 wins if there are 3 X's on the board and they are in a row, either in a col, row, or diagonally. A tie occurs when all locations on the board are full, but there is no winner.

Explanation / Answer


Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you

ticTacToe.h
===========

#ifndef ticTacToe_h
#define ticTacToe_h

class ticTacToe
{
private:
char grid[3][3];
static const char INITIAL = '*';
int moves ;
char winner;
void checkWinner(char player);
public:
ticTacToe(char firstPlayer);
bool makeMove(char player, int r, int c);
char getWinner(); //returns 'x' , 'o' , 't' or ' '
void print();
bool gameOver();
};

#endif /* ticTacToe_h */


ticTacToe.cpp
=============


#include "ticTacToe.h"
#include <iostream>
using namespace std;
ticTacToe::ticTacToe(char firstPlayer)
{
for(int i = 0 ;i < 3; i++)
for(int j = 0; j < 3; j++)
grid[i][j] =INITIAL;
  
moves = 0;

}
bool ticTacToe::makeMove(char player, int r, int c)
{
if(grid[r][c] == INITIAL)
{
grid[r][c] = tolower(player);
moves++;
checkWinner(player);
return true;
}
else
return false;

}
//return x or o or t (for tie)
char ticTacToe::getWinner()
{
return winner;
}
void ticTacToe::print()
{
cout << endl;
//print the column numbers in 1st line
cout << " 0 1 2" << endl;
for(int i = 0; i < 3; i++)
{
//print the row number
cout << i << " ";
for(int j = 0; j < 3; j++)
cout << grid[i][j] << " ";
  
cout << endl;
}
  
cout << endl;

}
bool ticTacToe::gameOver()
{
return moves == 9 || winner == 'x' || winner == 'o';
}
void ticTacToe::checkWinner(char player)
{
//top horizontal line
if(grid [0][0] == player && grid[0][0] == grid[0][1] && grid[0][1] == grid[0][2])
winner = grid[0][0];
//middle horizontal line
else if(grid [1][0] == player && grid[1][0] == grid[1][1] && grid[1][1] == grid[1][2])
winner = grid[1][0];
//bottom horizontal line
else if(grid [2][0] == player && grid[2][0] == grid[2][1] && grid[2][1] == grid[2][2])
winner = grid[2][0];
//left vertical line
else if(grid [0][0] == player && grid[0][0] == grid[1][0] && grid[1][0] == grid[2][0])
winner = grid[0][0];
//middle vertical line
else if(grid [0][1] == player && grid[0][1] == grid[1][1] && grid[1][1] == grid[2][1])
winner = grid[0][1];
//right verticle line
else if(grid [0][2] == player && grid[0][2] == grid[1][2] && grid[1][2] == grid[2][2])
winner = grid[0][2];
//diagonal starting at left top
else if(grid [0][0] == player && grid[0][0] == grid[1][1] && grid[1][1] == grid[2][2])
winner = grid[0][0];
//diagonal starting at right top
else if(grid [0][2] == player && grid[0][2] == grid[1][1] && grid[1][1] == grid[2][0])
winner = grid[0][2];
else
{
if(moves == 9)
winner = 't'; //tie
else
winner = ' ';
}
  

}


main.cpp
========

#include "ticTacToe.h"
#include <iostream>
using namespace std;

int main()
{
char player = 'x';
  
ticTacToe tic(player);
  
int row, col;
while(!tic.gameOver())
{
tic.print();
cout << "player " << player << ": please enter your move (enter row col) > ";
cin >> row >> col;
if(!tic.makeMove(player, row, col))
{
cout << "that position is already taken.";
continue;
}
if(player == 'x')
player = 'o';
else
player = 'x';
}
tic.print();
if(tic.getWinner() == 'x')
cout << "x Won!" << endl;
else if(tic.getWinner() == 'o')
cout << "o Won!" << endl;
else
cout << "It's a draw!" << endl;
  

return 0;
}


output
======
0 1 2
0 * * *
1 * * *
2 * * *

player x: please enter your move (enter row col) > 0 0

0 1 2
0 x * *
1 * * *
2 * * *

player o: please enter your move (enter row col) > 1 1

0 1 2
0 x * *
1 * o *
2 * * *

player x: please enter your move (enter row col) > 2 2

0 1 2
0 x * *
1 * o *
2 * * x

player o: please enter your move (enter row col) > 1 2

0 1 2
0 x * *
1 * o o
2 * * x

player x: please enter your move (enter row col) > 2 1

0 1 2
0 x * *
1 * o o
2 * x x

player o: please enter your move (enter row col) > 1 0

0 1 2
0 x * *
1 o o o
2 * x x

o Won!