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

Write a class called Board that represents a tic-tac-toe board. It should have a

ID: 3868506 • Letter: W

Question

Write a class called Board that represents a tic-tac-toe board. It should have a 3x3 array as a data member, which will store the locations of the players' moves. It should have a default constructor that initializes the 3x3 array to being empty. It should have a method called makeMove that takes the x and y coordinates of the move (see the example below) and which player's turn it is as parameters. If that location is unoccupied, makeMove should record the move and return true. If that location is already occupied, makeMove should just return false. There should be a method called gameState that takes no parameters and returns one of the four following values: X_WON, O_WON, DRAW, or UNFINISHED - use an enum for this, not strings (the enum definition should go in Board.hpp, before the Board class definition). There should also be a method called print, which just prints out the current board to the screen. Write a class called TicTacToe that allows two people to play a game. This class will have a field for a Board object and a field to keep track of which player's turn it is. It should have a constructor that takes a char parameter that specifies whether 'x' or 'o' should have the first move. It should have a method called play that starts the game. The play method should keep looping, asking the correct player for their move and sending it to the board (with makeMove) until someone has won or it's a draw (as indicated by gameState), and then declare what the outcome was. Write a main method (in TicTacToe.cpp) that asks the user which player should go first, creates a new TicTacToe object and starts the game. For this assignment only, you will not comment out your main method. Input validation: If someone tries to take an occupied square, tell them that square is already occupied and ask for a different move. Here's an example portion of a game (already in progress): 0 1 2 0 x . . 1 . . . 2 . . . Player O: please enter your move. 1 2 0 1 2 0 x . . 1 . . o 2 . . . Player X: please enter your move. 1 2 That square is already taken. 0 1 2 0 x . . 1 . . o 2 . . . Player X: please enter your move. The files must be named: Board.hpp, Board.cpp, TicTacToe.hpp and TicTacToe.cpp

Explanation / Answer

board.h

    #include <iostream>
    #include <string>
    using namespace std;
    class Board
    {
     private:
     string playerName;
   
     public:
     void getName(string n);
     int getIndex();
    };

board.cpp

    #include <iostream>
    #include <string>
     using namespace std;
     #include "board.h"
     #include "tictactoe.h"
   
     void Board::getName(string n)
     {
      playerName = n;
      cout << playerName;
     }
      int Board::getIndex()
      {
       srand((unsigned)time(0));
       int random_integer = rand();
       if (random_integer%2)
        return 2;
       else
        return 1;
      }

tictactoe.h

    #include <iostream>
    #include <string>
    using namespace std;
    class Tictactoe
    {
     private:
     int row, col;
     char square[3][3];
   
     public:
     Tictactoe();
     void displayBoard(ostream & out);
     bool makeMove(int row, int col, char player);
     int gameState();
int main()
{
Tictactoe game;
return 0;
}
    };

tictactoe.cpp

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

    Tictactoe::Tictactoe()
    {
        row = 0;
       col = 0;
       char player = '';
       for (int i = 0; i < 3; i++)
       {
           for (int j = 0; j < 3; j++)
           {
               square[i][j] = player;
           }
       }  
    }
   
    void Tictactoe::displayBoard(ostream & out)
     {
     out << endl << endl
       << " " << square[1][1] << " | " << square[1][2] << " | " << square[1][3] << endl
       << " -----------" << endl
       << " " << square[2][1] << " | " << square[2][2] << " | " << square[2][3] << endl
       << " -----------" << endl
       << " " << square[3][1] << " | " << square[3][2] << " | " << square[3][3] << endl << endl;
     }
    bool Tictactoe::makeMove(int row, int col, char player)
    {
      row = row - 1;
      col = col - 1;
      if (row != 0 && row != 1 && row != 2)
      {
       return false;
      }
      else if (col != 0 && col != 1 && col != 2)
      {
       return false;
      }
      else if ((square[row][col] = 'X') || (square[row][col] = 'O'))
      {
       return false;
      }
      else
      {
       square[row][col] = player;
       return true;
      }
    }
    int Tictactoe::gameState()
     {
      if (square[0][0] == square[0][1] && square[0][0] == square[0][2])
       return true;
      else if (square[1][0] == square[1][1] && square[1][0] == square[1][2])
       return true;
      else if (square[2][0] == square[2][1] && square[2][0] == square[2][2])
       return true;
      else if (square[0][0] == square[1][0] && square[0][0] == square[2][0])
       return true;
      else if (square[0][1] == square[1][1] && square[0][1] == square[2][1])
       return true;
      else if (square[0][2] == square[1][2] && square[0][2] == square[1][2])
       return true;
      else if (square[0][0] == square[1][1] && square[0][0] == square[2][2])
       return true;
      else if (square[0][2] == square[1][1] && square[0][2] == square[2][0])
       return true;
      else
       return false;
     }

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