Question: I found this code in chegg answer and it has more than 3 files. I only know how to compile 3 files (e.g main.cpp Board.cpp

I found this code in chegg answer and it has more than 3 files. I only know how to compile 3 files (e.g main.cpp Board.cpp Board.h) I never had more than 3. I'm trying to compile this in Visual C++. I need help ASAP. Thanks

main.cpp

-------------------------------------------- #include #include "Player.h" #include"PlayerHuman.h" #include"PlayerRandom.h" using namespace std; void manvsman(); void computervscomputer(); void humanvscomputer(); int main() { cout << "Welcome to Tic-Tac-Toe" << endl << " X go first" << endl << "press 1 for man vs man," << endl << "press 2 for man vs computer" << endl << "press 3 for computer vs computer" << endl; int dd=0; cin >> dd; while (dd != 1 && dd != 2 && dd != 3) {//reject not valid input and let user input again cin >> dd; cout << "not valid enter again" << endl; } if (dd == 1) manvsman(); else if (dd == 2) humanvscomputer(); else if (dd == 3) computervscomputer(); else { cout << "invalid input" << endl; } return 0; }

void computervscomputer() {int q = 1, w = 2;int count = 0; Board *k = new Board; Player *objA = new PlayerRandom(&q); //objA->setposition(&q); Player *objB = new PlayerRandom(&w); //objB->setposition(&w); k->PrintBoard(); while (k->DetermineWinner() != true && count <= 8) { objA->NextMove(*k); k->PrintBoard(); count++; if (k->DetermineWinner() != true && count <= 8) { objB->NextMove(*k); k->PrintBoard(); count++; } } if (k->printwinner(count, k) == 0) cout << "No winner." << endl; else if (k->printwinner(count, k) == 1) cout << "winner is:" << objB->getsymbol() << endl; else if (k->printwinner(count, k) == 2) cout << "winner is:" << objA->getsymbol() << endl; k->PrintBoard(); delete k; delete objA; delete objB; }

void humanvscomputer() { Board *k = new Board; Player *objA ; Player *objB; k->PrintBoard(); int q , w ; int count = 0; cout << "Do you want play X or O ?" << endl << "1 for X, 2 for O" << endl; cin >> q; while (q!=1 && q!=2) { cout << "Invalid input. Please try again: "; cin >> q; } if (q == 1) { w = 2; objA = new PlayerHuman(&q); objB = new PlayerRandom(&w); } else if(q==2) { w = 1; objA = new PlayerRandom(&q); objB = new PlayerHuman(&w); } while (k->DetermineWinner() != true && count <= 8) { objA->NextMove(*k); k->PrintBoard(); count++; if (k->DetermineWinner() != true && count <= 8) { objB->NextMove(*k); k->PrintBoard(); count++; } } if (k->printwinner(count, k) == 0) cout << "No winner." << endl; else if (k->printwinner(count, k) == 1) cout << "winner is:" << objB->getsymbol() << endl; else if (k->printwinner(count, k) == 2) cout << "winner is:" << objA->getsymbol() << endl; k->PrintBoard(); delete k; delete objA; delete objB; }

void manvsman() { Board *k = new Board; k->PrintBoard(); int q = 1, w = 2; int count = 0; Player *objA = new PlayerHuman(&q); //objA->setposition(&q); Player *objB = new PlayerHuman(&w); //objB->setposition(&w); while (k->DetermineWinner() != true && count <= 8) { objA->NextMove(*k); k->PrintBoard(); count++; if (k->DetermineWinner() != true && count <= 8) { objB->NextMove(*k); k->PrintBoard(); count++; } } if (k->printwinner(count, k) == 0) cout << "No winner." << endl; else if (k->printwinner(count, k) == 1) cout << "winner is:" << objB->getsymbol() << endl; else if (k->printwinner(count, k) == 2) cout << "winner is:" << objA->getsymbol() << endl; k->PrintBoard(); delete k; delete objA; delete objB; } ------------------------------------------------------------------------------------------- Board.cpp ---------------------------------------------- #include #include "Board.h"

using namespace std;

Board::Board() { Pposition = new char[n]; for (int i = 0; i

} Board & Board::operator = (const Board & obj) // assignment operator { if (this != &obj) { delete Pposition; Pposition = new char[n]; for (int i = 0; i < n; i++) Pposition[i] = obj.Pposition[i]; } return *this; } void Board::PrintBoard() {//print the board cout << "+---+---+---+" << endl; cout << "|" << Pposition[0] << " |" << Pposition[1] << " |" << Pposition[2] << " |" << endl; cout << "+---+---+---+" << endl; cout << "|" << Pposition[3] << " |" << Pposition[4] << " |" << Pposition[5] << " |" << endl; cout << "+---+---+---+" << endl; cout << "|" << Pposition[6] << " |" << Pposition[7] << " |" << Pposition[8] << " |" << endl; cout << "+---+---+---+" << endl;

} char Board::getposition(int p)const { return Pposition[p];

} bool Board::DetermineWinner() {//return true if we have winner function printwinner determine we have winner or not if (Pposition[0] == Pposition[1] && Pposition[0] == Pposition[2] && Pposition[0] != ' ') return true; else if (Pposition[3] == Pposition[4] && Pposition[3] == Pposition[5] && Pposition[3] != ' ') return true; else if (Pposition[6] == Pposition[7] && Pposition[6] == Pposition[8] && Pposition[6] != ' ') return true; else if (Pposition[0] == Pposition[4] && Pposition[0] == Pposition[8] && Pposition[0] != ' ') return true; else if (Pposition[2] == Pposition[4] && Pposition[2] == Pposition[6] && Pposition[2] != ' ') return true; else if (Pposition[0] == Pposition[3] && Pposition[0] == Pposition[6] && Pposition[0] != ' ') return true; else if (Pposition[1] == Pposition[4] && Pposition[1] == Pposition[7] && Pposition[1] != ' ') return true; else if (Pposition[2] == Pposition[5] && Pposition[2] == Pposition[8] && Pposition[2] != ' ') return true;

return false; }

int Board::printwinner(int count, Board *s)//determine we have winner or not { if (s->DetermineWinner() != true) { /*cout << "No winner." << endl;*/ return 0; } else if (s->DetermineWinner() == true) { if (count % 2 == 0) return 1;/* cout << "winner is:" << objB->getsymbol() << endl;*/ else if (count % 2 != 0) return 2; /*cout << "winner is:" << objA->getsymbol() << endl;*/ } }

void Board::SetPosition(int position,char player) const{//set symbol to the board

if (Pposition[position] == ' ') { Pposition[position] = player; } else if (Pposition[position] != ' ') { cout << "error" << endl; } } --------------------------------------------------------------------------------------- Board.h --------------------------------------- #pragma once using namespace std; const int n = 9; class Board{ private: char *Pposition; public: Board(); ~Board(); Board(const Board &obj); Board& operator = (const Board & s); void PrintBoard(); bool DetermineWinner(); int printwinner(int count, Board * s); char getposition(int p)const; void SetPosition( int position,char player)const; }; ------------------------------------------------------------------------------------ Player.cpp --------------------------------------- #include #include "Player.h"

using namespace std; Player::Player() { symbol = new char; position = new int; } Player::~Player() { delete symbol ; delete position ; cout << "Destroying Player.h"; } Player::Player(char *p,int *q) { symbol = p; position = q; } Player::Player(const Player &obj) { symbol = obj.symbol; position = obj.position; }

Player & Player::operator=(Player const & s) { if (this != &s) { delete symbol; delete position; symbol = new char; position = new int; symbol = s.symbol; position = s.position; } return *this;// TODO: insert return statement here } char Player::getsymbol()const { return *symbol; } int Player::getposition()const { return *position; } void Player::setsymbol(char a) { *symbol = a; } void Player::setposition(int a) { *position = a; } --------------------------------------------------------------------------------------------- Player.h --------------------------------------- #pragma once #include "Board.h" using namespace std;

class Player { private: char *symbol; int *position; public: Player(); virtual ~Player(); Player(char *p,int *q); Player(const Player &obj); Player& operator = (Player const& s) ; virtual void NextMove(const Board &obj) = 0; char getsymbol()const; int getposition()const; void setsymbol(char a); void setposition(int a); };

---------------------------------------------------------------------------------------------------- PlayerHuman.cpp --------------------------------------- #include #include"PlayerHuman.h" PlayerHuman::PlayerHuman() { playerNumber = NULL; //playerNumber = new int;//it is for other version of this program }

PlayerHuman::~PlayerHuman() {if(playerNumber!=NULL) playerNumber=NULL; cout << "Destroying PlayerHuman"; } PlayerHuman::PlayerHuman(int *p) { playerNumber = p; } PlayerHuman::PlayerHuman(const PlayerHuman &obj) { playerNumber = obj.playerNumber; } PlayerHuman & PlayerHuman::operator=(const PlayerHuman & s) { if (this != &s) { delete playerNumber; playerNumber = new int; playerNumber = s.playerNumber; } return *this; // TODO: insert return statement here } void PlayerHuman::NextMove(const Board & obj) { int move; cout << "Next move(Ex. 0,1,2,3,4,5,6,7,8): "; cin >> move; while (move<0 || move>8) {//ASCII table 0=48 8=56 static_cast(move) <48 && static_cast(move)>56 cout << "Invalid position. Please try again: "; cin >> move; } while (obj.getposition(move) != ' ') { cout << "That space has already been taken. Input again: "; cin >> move; } //Player::setposition(move); if (*playerNumber == 1) {/*Player::getposition()*/

Player::setsymbol('X'); } else if (*playerNumber== 2) {/*Player::getposition()*/

Player::setsymbol('O'); } obj.SetPosition(move, Player::getsymbol()); } ---------------------------------------------------------------------------------------- PlayerHuman.h -------------------------------------------- #pragma once #include"Player.h" class PlayerHuman :public Player { private: int *playerNumber; public: PlayerHuman(); ~PlayerHuman(); PlayerHuman(int *p); PlayerHuman(const PlayerHuman &obj); PlayerHuman& operator = (const PlayerHuman & s); virtual void NextMove(const Board &obj); }; -------------------------------------------------------------------------------------------- PlayerRandom.cpp ------------------------------------------ #include "PlayerRandom.h" #include /* srand, rand */ #include #include PlayerRandom::PlayerRandom() { playerNumber = NULL; //playerNumber = new int;//it is for other version of this program }

PlayerRandom::~PlayerRandom() { if (playerNumber != NULL) playerNumber = NULL; cout << "Destroying PlayerRandom"; }

PlayerRandom::PlayerRandom(int *p) { playerNumber = p; }

PlayerRandom::PlayerRandom(const PlayerRandom & obj) { playerNumber = obj.playerNumber; }

PlayerRandom & PlayerRandom::operator=(const PlayerRandom & s) { if (this != &s) { delete playerNumber; playerNumber = new int; playerNumber = s.playerNumber; } return *this; }

void PlayerRandom::NextMove(const Board & obj) { int move; srand(time(0)); move = rand() % 9; while (move<0 || move>9) { //reject invalid input move = rand() % 9; } while (obj.getposition(move) != ' ') { move = rand() % 9; } //Player::setposition(playerNumber); if (*playerNumber == 1) { /*Player::getposition()*/ Player::setsymbol('X');//set symbol to Player class } else if (*playerNumber == 2) {/*Player::getposition()*/ Player::setsymbol('O'); } obj.SetPosition( move, Player::getsymbol());//set position and symbol } ---------------------------------------------------------------------------------------- PlayerRandom.h ------------------------------------------ #pragma once #include"Player.h" class PlayerRandom :public Player { private: int *playerNumber; public: PlayerRandom(); ~PlayerRandom(); PlayerRandom(int *p); PlayerRandom(const PlayerRandom &obj); PlayerRandom &operator =(const PlayerRandom & s); virtual void NextMove(const Board &obj); };

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!