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

Write a program that allows two players to play a game of tic-tac-toe. You will

ID: 3911039 • Letter: W

Question

Write a program that allows two players to play a game of tic-tac-toe. You will create a game class. Do you need any data elements other than the board? What member functions do you need? Use a two dimensional char array with 3 rows and 3 columns for the game board. Each element of the array should be initialized with an asterisk (*). The program should display the initial board. and then start a loop that does the following: • Allow player 1 to select a location on the board for an X by entering a row and column number. Then display the board with an X replacing the * in the chosen location. • If there is no winner yet and the board is not yet full, allow player 2 to select a location on the board for an O by entering a row and column number. Then display the board with an O replacing the * in the chosen location. The loop should continue until a player has won or a tie has occurred, then display a message indicating who won, or reporting that a tie occurred. • Player 1 wins when there are three Xs in a row, a column, or a diagonal on the game board. • Player 2 wins when there are three Os in a row, a column, or a diagonal on the game board. • A tie occurs when all of the locations on the board are full, but there is no winner. Ask the users if they want to play again or if they want to exit the program. Input Validation: Only allow legal moves to be entered. The row must be 1, 2, or 3. The column must be 1, 2, or 3. The (row, column) position entered must currently be empty (i.e., still have an asterisk in it). Always make a copy of working code before making changes for other parts of the assignment! That means do it now. J You’ll have 3 separate programs to submit. 2. Modify your program to provide a computer opponent for the human user. You can call this AI if you want, but you are just required to provide an automated opponent. It does not need to be a smart opponent. There are not many options for a response. Remember that the board can be rotated and/or flipped. So you only need one response to an initial corner move, or an initial side move, or a starting move in the center. Or it can simply select an open space and mark it. Give the user the choice to play first or second. So your automated opponent must have a starting move. NOTE- Your automated player doesn’t need to be smart. It doesn’t need to win. It is only required to make a legal move. Nothing more. 3. Tic-tac-toe on a 3x3 board is boring. It is common knowledge that you can easily play for a draw, i.e. not losing. Modify your initial class to ask the user for the size of the grid. You did save your code, right? No automated player is required. Prompt the user for a value N that will be the number of rows and columns for the grid. For simplicity in grading please limit N to the range 2 to 5. How do you change your input validation? You will use a dynamic array to hold the board. Remember to free your memory if the users are playing again. Must you make any other changes? 4. Create a makefile to compile each program. Remember to put all files into a zip archive and submit that zip file to TEACH before the deadline. Grading: • programming style and documentation (10%) • reflections present DESIGN and testing (15%) #1 • create, initialize, and correctly display the game board (5%) • create and correctly make player one moves (10%) • create and correctly make player two moves (10%) • correctly determine which player won, or if it was a draw (10%) • correct input validation (5%) #2 • your automated opponent makes legal moves (10%) #3 • correctly implement and use the dynamic array (10%) • the victory conditions work correctly for each possible grid (10%) #4 • Create a makefile (5%)

Explanation / Answer

Please let me know if you have any doubts or you want me to modify the code. And if you find this code useful then don't forget to rate my answer as thumps up. Thank you! :)

//Program1

// tictactoe.cpp

#include <iostream>

#include <iomanip>

using namespace std;

//game class

class Game

{

private:

     //Declare the game board two dimensional char

     //array with 3 rows and 3 columns

     char board[3][3];

public:

     //Constructor that intializes the each element of the

     //game board array with an asterisk (*)

     Game()

     {

          for (int i = 0; i < 3; i++)

          {

              for (int j = 0; j < 3; j++)

              {

                   board[i][j] = '*';

              }

          }

     }

     //intializes the each element of the

     //game board array with an asterisk (*)

     void resetBoard()

     {

          for (int i = 0; i < 3; i++)

          {

              for (int j = 0; j < 3; j++)

              {

                   board[i][j] = '*';

              }

          }

     }

     void displayBoard()

     {

          for (int i = 0; i < 3; i++)

          {

              for (int j = 0; j < 3; j++)

              {

                   cout << setw(5) << board[i][j];

              }

              cout << endl;

          }

     }

     void replace(int x, int y,char player)

     {

          board[x][y] = player;

     }

     bool validLocation(int x, int y)

     {

          if (board[x][y] == '*')

              return 1;

          else

              return 0;

     }

     //Returns true(1), if player 1 won the game

     //Returns false(0), otherwise

     bool checkPlayer1Win()

     {

          if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X') return 1;

          if (board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X') return 1;

          if (board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X') return 1;

          if (board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X') return 1;

          if (board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X') return 1;

          if (board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X') return 1;

          if (board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X') return 1;

          if (board[2][0] == 'X' && board[1][1] == 'X' && board[0][2] == 'X') return 1;

          return 0;

     }

     //Returns true(1), if player 2 won the game

     //Returns false(0), otherwise

     bool checkPlayer2Win()

     {

          if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O') return 1;

          if (board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O') return 1;

          if (board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O') return 1;

          if (board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O') return 1;

          if (board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O') return 1;

          if (board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O') return 1;

          if (board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O') return 1;

          if (board[2][0] == 'O' && board[1][1] == 'O' && board[0][2] == 'O') return 1;

          return 0;

     }

     //Returns true(1), if all of the locations on the

     //board are full. Returns false(0) , otherwise.

     bool boardFull()

     {

          for(int i=0;i<3;i++)

              for (int j = 0; j < 3; j++)

              {

                   if (board[i][j] == '*')

                        return 0;

              }

          return 1;

     }

     //Returns true(1), if all of the locations on the

     //board are full, but there is no winner

     bool isTie()

     {

          if (!checkPlayer1Win() && !checkPlayer2Win() && boardFull())

              return 1;

          else

              return 0;

     }

};

int main()

{

     Game tictactoe;

  

     int player = 0;

     int r, c;

     char nextGame = 'n';

     while (1)

     {

          //Display the initial board

          tictactoe.resetBoard();    

          cout << " Welcome to Tic-Tac-Toe game" << endl;

          tictactoe.displayBoard();

          //Allow the players to select a location on the board

          //until a player won the game or tie.

          while (1)

          {

              //Start with player1

              cout << "Player1:" << endl;

              cout << " Select a location (x,y) on Bard" << endl;

              cout << "Enter row(1-3): ";

              cin >> r;

              cout << "Enter column(1-3): ";

              cin >> c;

              //Prompt for row and column number

              //until the user enters a valid location

              while (!tictactoe.validLocation(r-1, c-1) || (r < 1 || r>3) || (c < 1 || c>3))

              {

                   cout << "Enter valid row and column numbers or proper location " << endl;

                   cout << "Player1:" << endl;

                   cout << " Select a location (x,y) on Bard" << endl;

                   cout << "Enter row: ";

                   cin >> r;

                   cout << "Enter column: ";

                   cin >> c;

              }

              //Replace the * in the chosen location

              //with the corresponding character

              tictactoe.replace(r - 1, c - 1, 'X');

              //Then display the updated baord

              tictactoe.displayBoard();

              //Check for player1 win

              if (tictactoe.checkPlayer1Win() == 1)

              {

                   cout << "Player1 won the game!" << endl;

                   break;

              }

              //Check for tie

              if (tictactoe.isTie())

              {

                   cout << "Game is tie!" << endl;

                   break;

              }

              //Player2's turn

              cout << "Player2:" << endl;

              cout << " Select a location (x,y) on Bard" << endl;

              cout << "Enter row(1-3): ";

              cin >> r;

              cout << "Enter column(1-3): ";

              cin >> c;

              //Prompt for row and column number

              //until the user enters a valid location

              while (!tictactoe.validLocation(r - 1, c - 1) || (r < 1 || r>3) || (c < 1 || c>3))

              {

                   cout << "Enter valid row and column numbers or proper location " << endl;

                   cout << "Player2:" << endl;

                   cout << " Select a location (x,y) on Bard" << endl;

                   cout << "Enter row: ";

                   cin >> r;

                   cout << "Enter column: ";

                   cin >> c;

              }

              //Replace the * in the chosen location

              //with the corresponding character

              tictactoe.replace(r - 1, c - 1, 'O');

              //Then display the updated baord

              tictactoe.displayBoard();

              //Check for player2 win

              if (tictactoe.checkPlayer2Win() == 1)

              {

                   cout << "Player2 won the game!" << endl;

                   break;

              }

              //Check for tie

              if (tictactoe.isTie())

              {

                   cout << "Game is tie!" << endl;

                   break;

              }

          }

          //Ask the users if they want to play again

          //or if they want to exit the program

          cout << " Do you want to play again(y|n):";

          cin >> nextGame;

          //If user want to exit, break the loop

          if (nextGame == 'n' | nextGame == 'N')

              break;

     }

     cout << "Thank you..." << endl;

     return 0;

}
----------------------------------------------------------------------------
//Program2

// tictactoe.cpp

#include <iostream>

#include <iomanip>

#include <time.h>

using namespace std;

//game class

class Game

{

private:

       //Declare the game board two dimensional char

       //array with 3 rows and 3 columns

       char board[3][3];

public:

       //Constructor that intializes the each element of the

       //game board array with an asterisk (*)

       Game()

       {

              for (int i = 0; i < 3; i++)

              {

                     for (int j = 0; j < 3; j++)

                     {

                           board[i][j] = '*';

                     }

              }

       }

       //intializes the each element of the

       //game board array with an asterisk (*)

       void resetBoard()

       {

              for (int i = 0; i < 3; i++)

              {

                     for (int j = 0; j < 3; j++)

                     {

                           board[i][j] = '*';

                     }

              }

       }

       void displayBoard()

       {

              for (int i = 0; i < 3; i++)

              {

                     for (int j = 0; j < 3; j++)

                     {

                           cout << setw(5) << board[i][j];

                     }

                     cout << endl;

              }

       }

       void replace(int x, int y,char player)

       {

              board[x][y] = player;

       }

       bool validLocation(int x, int y)

       {

              if (board[x][y] == '*')

                     return 1;

              else

                     return 0;

       }

       //Returns true(1), if player 1 won the game

       //Returns false(0), otherwise

       bool checkPlayer1Win()

       {

              if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X') return 1;

              if (board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X') return 1;

              if (board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X') return 1;

              if (board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X') return 1;

              if (board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X') return 1;

              if (board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X') return 1;

              if (board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X') return 1;

              if (board[2][0] == 'X' && board[1][1] == 'X' && board[0][2] == 'X') return 1;

              return 0;

       }

       //Returns true(1), if player 2 won the game

       //Returns false(0), otherwise

       bool checkPlayer2Win()

       {

              if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O') return 1;

              if (board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O') return 1;

              if (board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O') return 1;

              if (board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O') return 1;

              if (board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O') return 1;

              if (board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O') return 1;

              if (board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O') return 1;

              if (board[2][0] == 'O' && board[1][1] == 'O' && board[0][2] == 'O') return 1;

              return 0;

       }

       //Returns true(1), if all of the locations on the

       //board are full. Returns false(0) , otherwise.

       bool boardFull()

       {

              for(int i=0;i<3;i++)

                     for (int j = 0; j < 3; j++)

                     {

                           if (board[i][j] == '*')

                                  return 0;

                     }

              return 1;

       }

       //Returns true(1), if all of the locations on the

       //board are full, but there is no winner

       bool isTie()

       {

              if (!checkPlayer1Win() && !checkPlayer2Win() && boardFull())

                     return 1;

              else

                     return 0;

       }

};

int main()

{

srand (time(NULL));

       Game tictactoe;

    

       int player = 0;

       int r, c;

       char nextGame = 'n';

       while (1)

       {

              //Display the initial board

              tictactoe.resetBoard();        

              cout << " Welcome to Tic-Tac-Toe game" << endl;

              tictactoe.displayBoard();

              //Allow the players to select a location on the board

              //until a player won the game or tie.

              while (1)

              {

                     //Start with Human Player

                     cout << "Human Player:" << endl;

                     cout << " Select a location (x,y) on Bard" << endl;

                     cout << "Enter row(1-3): ";

                     cin >> r;

                     cout << "Enter column(1-3): ";

                     cin >> c;

                     //Prompt for row and column number

                     //until the user enters a valid location

                     while (!tictactoe.validLocation(r-1, c-1) || (r < 1 || r>3) || (c < 1 || c>3))

                     {

                           cout << "Enter valid row and column numbers or proper location " << endl;

                           cout << "Human Player:" << endl;

                           cout << " Select a location (x,y) on Bard" << endl;

                           cout << "Enter row: ";

                           cin >> r;

                           cout << "Enter column: ";

                           cin >> c;

                     }

                     //Replace the * in the chosen location

                     //with the corresponding character

                     tictactoe.replace(r - 1, c - 1, 'X');

                     //Then display the updated baord

                     tictactoe.displayBoard();

                     //Check for player1 win

                     if (tictactoe.checkPlayer1Win() == 1)

                     {

                           cout << "Player1 won the game!" << endl;

                           break;

                     }

                     //Check for tie

                     if (tictactoe.isTie())

                     {

                           cout << "Game is tie!" << endl;

                           break;

                     }

                     //Computer's turn

                     cout << "Computer:" << endl;                 

                     r = rand() % 3 + 1;

                     c= rand() % 3 + 1;

                     //Prompt for row and column number

                     //until the user enters a valid location

                     while (!tictactoe.validLocation(r - 1, c - 1) || (r < 1 || r>3) || (c < 1 || c>3))

                     {                       

                           r = rand() % 3 + 1;

                           c = rand() % 3 + 1;

                     }

                     cout << "Computer selects " <<"("<< r << "," << c <<")"<< endl;

                     //Replace the * in the chosen location

                     //with the corresponding character

                     tictactoe.replace(r - 1, c - 1, 'O');

                     //Then display the updated baord

                     tictactoe.displayBoard();

                     //Check for computer win

                     if (tictactoe.checkPlayer2Win() == 1)

                     {

                           cout << "Computer won the game!" << endl;

                           break;

                     }

                     //Check for tie

                     if (tictactoe.isTie())

                     {

                           cout << "Game is tie!" << endl;

                           break;

                     }

              }

              //Ask the users if they want to play again

              //or if they want to exit the program

              cout << " Do you want to play again(y|n):";

              cin >> nextGame;

              //If user want to exit, break the loop

              if (nextGame == 'n' | nextGame == 'N')

                     break;

       }

       cout << "Thank you..." << endl;

       return 0;

}


--------------------------------------------------------------------------------------------------------------
//Program 3

// tictactoe.cpp
#include <iostream>
#include <iomanip>
using namespace std;
//game class
class Game
{
private:
int N; //size of the board
//Declare the game board two dimensional char
//array with 3 rows and 3 columns
char board[5][5];
public:
//Constructor that intializes the each element of the
//game board array with an asterisk (*)
Game(int size)
{
N = size;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
board[i][j] = '*';
}
}
}
//intializes the each element of the
//game board array with an asterisk (*)
void resetBoard()
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
board[i][j] = '*';
}
}
}
void displayBoard()
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
cout << setw(5) << board[i][j];
}
cout << endl;
}
}
void replace(int x, int y, char player)
{
board[x][y] = player;
}
bool validLocation(int x, int y)
{
if (board[x][y] == '*')
return 1;
else
return 0;
}
//Returns true(1), if player 1 won the game
//Returns false(0), otherwise
bool checkPlayer1Win()
{
bool winFlag = true;
//Check for rows
for (int i = 0; i < N; i++)
{
winFlag = true;
for (int j = 0; j < N; j++)
{
if (board[i][j] != 'X')
{
winFlag = false;
break;
}
}
if (winFlag == true)
return winFlag;
}
//Check for columns
for (int i = 0; i < N; i++)
{
winFlag = true;
for (int j = 0; j < N; j++)
{
if (board[j][i] != 'X')
{
winFlag = false;
break;
}
}
if (winFlag == true)
return winFlag;
}
//Check for diagonals
winFlag = true;
for (int j = 0; j < N; j++)
{
if (board[j][j] != 'X')
{
winFlag = false;
break;
}
}
if (winFlag == true)
return winFlag;
int j = N - 1;
for (int i = 0; i < N; i++)
{
winFlag = true;
if (board[i][j] != 'X')
{
winFlag = false;
break;
}
j--;
}
if (winFlag == true)
return winFlag;

return winFlag;
}
//Returns true(1), if player 2 won the game
//Returns false(0), otherwise
bool checkPlayer2Win()
{
bool winFlag = true;
//Check for rows
for (int i = 0; i < N; i++)
{
winFlag = true;
for (int j = 0; j < N; j++)
{
if (board[i][j] != 'O')
{
winFlag = false;
break;
}
}
if (winFlag == true)
return winFlag;
}
//Check for columns
for (int i = 0; i < N; i++)
{
winFlag = true;
for (int j = 0; j < N; j++)
{
if (board[j][i] != 'O')
{
winFlag = false;
break;
}
}
if (winFlag == true)
return winFlag;
}
//Check for diagonals
winFlag = true;
for (int j = 0; j < N; j++)
{
if (board[j][j] != 'O')
{
winFlag = false;
break;
}
}
if (winFlag == true)
return winFlag;

int j = N - 1;
for (int i = 0; i < N; i++)
{
winFlag = true;
if (board[i][j] != 'O')
{
winFlag = false;
break;
}
j--;
}
if (winFlag == true)
return winFlag;

return winFlag;
}
//Returns true(1), if all of the locations on the
//board are full. Returns false(0) , otherwise.
bool boardFull()
{
for (int i = 0; i<N; i++)
for (int j = 0; j < N; j++)
{
if (board[i][j] == '*')
return 0;
}
return 1;
}
//Returns true(1), if all of the locations on the
//board are full, but there is no winner
bool isTie()
{
if (!checkPlayer1Win() && !checkPlayer2Win() && boardFull())
return 1;
else
return 0;
}
};

int main()
{


int player = 0;
int r, c;
char nextGame = 'n';
int N;
//Prompt the user for size of the board
cout << "Enter the board size(2-5): ";
cin >> N;
while (N < 2 || N>5)
{
cout << "Invalid size!" << endl;
cout << "Enter the board size(2-5): ";
cin >> N;
}
//Pass the board size to the class
Game tictactoe(N);
while (1)
{
//Display the initial board
tictactoe.resetBoard();
cout << " Welcome to Tic-Tac-Toe game" << endl;
tictactoe.displayBoard();
//Allow the players to select a location on the board
//until a player won the game or tie.
while (1)
{
//Start with player1
cout << "Player1:" << endl;
cout << " Select a location (x,y) on Bard" << endl;
cout << "Enter row(1-N): ";
cin >> r;
cout << "Enter column(1-N): ";
cin >> c;
//Prompt for row and column number
//until the user enters a valid location
while (!tictactoe.validLocation(r - 1, c - 1) || (r < 1 || r>N) || (c < 1 || c>N))
{
cout << "Enter valid row and column numbers or proper location " << endl;
cout << "Player1:" << endl;
cout << " Select a location (x,y) on Bard" << endl;
cout << "Enter row: ";
cin >> r;
cout << "Enter column: ";
cin >> c;
}
//Replace the * in the chosen location
//with the corresponding character
tictactoe.replace(r - 1, c - 1, 'X');
//Then display the updated baord
tictactoe.displayBoard();
//Check for player1 win
if (tictactoe.checkPlayer1Win() == 1)
{
cout << "Player1 won the game!" << endl;
break;
}
//Check for tie
if (tictactoe.isTie())
{
cout << "Game is tie!" << endl;
break;
}
//Player2's turn
cout << "Player2:" << endl;
cout << " Select a location (x,y) on Bard" << endl;
cout << "Enter row(1-3): ";
cin >> r;
cout << "Enter column(1-3): ";
cin >> c;
//Prompt for row and column number
//until the user enters a valid location
while (!tictactoe.validLocation(r - 1, c - 1) || (r < 1 || r>N) || (c < 1 || c>N))
{
cout << "Enter valid row and column numbers or proper location " << endl;
cout << "Player2:" << endl;
cout << " Select a location (x,y) on Bard" << endl;
cout << "Enter row: ";
cin >> r;
cout << "Enter column: ";
cin >> c;
}
//Replace the * in the chosen location
//with the corresponding character
tictactoe.replace(r - 1, c - 1, 'O');
//Then display the updated baord
tictactoe.displayBoard();
//Check for player2 win
if (tictactoe.checkPlayer2Win() == 1)
{
cout << "Player2 won the game!" << endl;
break;
}
//Check for tie
if (tictactoe.isTie())
{
cout << "Game is tie!" << endl;
break;
}
}
//Ask the users if they want to play again
//or if they want to exit the program
cout << " Do you want to play again(y|n):";
cin >> nextGame;
//If user want to exit, break the loop
if (nextGame == 'n' | nextGame == 'N')
break;
}
cout << "Thank you..." << endl;
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote