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

Okay I posted this question earlier, but after following the advice of one of th

ID: 3776344 • Letter: O

Question

Okay I posted this question earlier, but after following the advice of one of the responses, I'm still having trouble with the following program. I was having problems getting the tic tac toe board to tell me that there is a draw after the 9th turn has been played, but now it randomly desides to end the program after a random number of turns. I'm not sure if my draw problem is fixed since I can't finish a game. I've posted a screen shot below my coding of what the executible is doing when I play.

/*Board.hpp*/

#ifndef BOARD_HPP

#define BOARD_HPP

// enumerated variable data type GameState to return a value for the gameState function

enum GameState { X_WON, O_WON, DRAW, UNFINISHED };

class Board

{

private:

public:

char turn; //which player's turn it is

int totalTurns; //total number of turns taken in the game

char board[3][3]; //3x3 game board

Board(); //Default Constructor

bool makeMove(int, int, char); //makeMove Function

int gameState(); //gameState Function

void print(); //print Function

};

#endif

__________________________________________________________________________________________________

/*Board.cpp*/

#include "Board.hpp"

#include <iostream>

using namespace std;

// Default Constructor

int totalTurns = 0;

Board::Board()

{

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

{

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

{

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

}

}

totalTurns = 0;

}

bool Board::makeMove(int x, int y, char turn)

{

//Player x's turn

if (turn == 'x')

{

//Checks if the position player x chose is available

if (board[x][y] == '.') // || board[x][y] != 'o')

{

//Assigns x to that position if it is available

board[x][y] = 'x';

return true;

}

}

//Player o's turn

else if (turn == 'o')

{

//Checks if the position player o chose is available

if (board[x][y] == '.') // || board[x][y] != 'x')

{

//Assigns o to that position if it is available

board[x][y] = 'o';

return true;

}

}

// Returns false if the position is already occupied

else

{

return false;

}

return 0;

}

//board position

int Board::gameState()

{

//Checks on the state of the game

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

{

//Checks if there is winner

if ((board[i][0] == board[i][1] && board[i][1] == board[i][2])

|| (board[0][i] == board[1][i] && board[1][i] == board[2][i])

|| (board[0][0] == board[1][1] && board[1][1] == board[2][2])

|| (board[0][2] == board[1][1] && board[1][1] ==board[2][0]))

{

//Checks if x is the winner

if ((board[i][0] == 'x' && board[i][1] == 'x' && board[i][2] == 'x')

|| (board[0][i] == 'x' && board[1][i] == 'x' && board[2][i] == 'x')

|| (board[0][0] == 'x' && board[1][1] == 'x' && board[2][2] == 'x')

|| (board[0][2] == 'x' && board[1][1] == 'x' && board[2][0] == 'x'))

{

//x is the winner

return X_WON;

}

//Checks if o is the winner

else if ((board[i][0] == 'o' && board[i][1] == 'o' && board[i][2] == 'o')

|| (board[0][i] == 'o' && board[1][i] == 'o' && board[2][i] == 'o')

|| (board[0][0] == 'o' && board[1][1] == 'o' && board[2][2] == 'o')

|| (board[0][2] == 'o' && board[1][1] == 'o' && board[2][0] == 'o'))

{

//o is the winner

return O_WON; }

}

//9 moves have been made, with no available spaces/moves left

if (totalTurns == 9)

{

// The game is a draw

return DRAW;

}

// The game is unfinished and to be continued

else

return UNFINISHED;

}

}

}

//prints game board

void Board::print()

{

cout << endl << " " << "0" << " " << "1" << " " << "2" << endl;

cout << "0" << " " << board[0][0] << " " << board[0][1] << " " << board[0][2] << endl;

cout << "1" << " " << board[1][0] << " " << board[1][1] << " " << board[1][2] << endl; cout << "2" << " " << board[2][0] << " " << board[2][1] << " " << board[2][2] << endl;

cout << "Turn #: " << totalTurns << endl;

}

__________________________________________________________________________________________________________

/*TicTacToe.hpp*/

#include "Board.hpp"

#include <iostream>

using namespace std;

#ifndef TICTACTOE_HPP

#define TICTACTOE_HPP

class TicTacToe

{

private:

char p; //which player's turn it is

public:

//gameBoard object of the Board class

Board gameBoard;

//which player's turn it is

char turn;

// total number of turns taken in the game

int totalTurns;

// character variable to be passed to the TicTacToe constructor

TicTacToe(char);

//play Function

void play();

};

#endif

__________________________________________________________________________________________________________________

/*TicTacTow.cpp*/

#include "Board.hpp"

#include "TicTacToe.hpp"

#include <iostream>

using namespace std;

TicTacToe::TicTacToe(char inputFirstTurn)

{

turn = inputFirstTurn;

}

void TicTacToe::play()

{

int x, y, gameState;

char inputMove;

while (gameBoard.gameState() == UNFINISHED)

{

//Print game board for player to determine next move

gameBoard.print();

cout << "It is player " << turn << "'s turn." << endl;

cout << "Please enter the coordinates of your move." << endl;

cin >> x >> y;

inputMove = gameBoard.makeMove(x, y, turn);

if (inputMove == true)

{

gameBoard.totalTurns++;

if (turn == 'x')

{

turn = 'o';

}

else if (turn == 'o')

{

turn = 'x';

}

}

if (inputMove == false)

{

//Error message

cout << "That space is taken." << endl;

}

}

  

gameState = gameBoard.gameState();

// Player x is the winner

if (gameState == X_WON)

{

gameBoard.print();

cout << "***** x wins!!! *****" << endl;

}

//Player o is the winner

else if (gameState == O_WON)

{

gameBoard.print();

cout << "***** o wins!!! *****" << endl;

}

//The game is a draw

else if (gameState == DRAW)

{

gameBoard.print();

cout << "***** The game is a draw! *****" << endl;

}

}

//main function

int main()

{

char inputFirstTurn;

// Declare gameBoard object of the Board class

Board gameBoard;

//Print the game board

gameBoard.print();

//Get thefirst player (x or o) and their move

cout << "Please enter which player will go first (x or o)." << endl;

cin >> inputFirstTurn;

//Send the first player's turn to the game object of //TicTacToe

TicTacToe game(inputFirstTurn);

//Play function carries out rest of game

game.play();

return 0;

}

0 1 2 0 x o Turn 2 It is player x's turn. Please enter the coordinates of your move. 0 2 0 1 2 0 x o x Turn 3 It is player o's turn Please enter the coordinates of your move. 1 1 0 1 2 0 x to x Turn 4 It is player x's turn. Please enter the coordinates of your move. 1 0 0 1 2 1 x o Turn 5 It is player o's turn. Please enter the coordinates of your move. 1 2 0 1 2 1 x o o Turn 6 It is player x's turn. Please enter the coordinates of your move. 2 1 [flip n/week9 109% /tic

Explanation / Answer

Answer:

Answer:

#include <iostream>

using namespace std;

char input[10] = {'o','1','2','3','4','5','6','7','8','9'};

int checkwinner();
void displayboard();

int main()
{
int player = 1,i,option;

char enable;
do
{
displayboard();
player=(player%2)?1:2;

cout << "Player " << player << ", enter a number: ";
cin >> option;

enable=(player == 1) ? 'X' : 'O';

if (option == 1 && input[1] == '1')

input[1] = enable;
else if (option == 2 && input[2] == '2')

input[2] = enable;
else if (option == 3 && input[3] == '3')

input[3] = enable;
else if (option == 4 && input[4] == '4')

input[4] = enable;
else if (option == 5 && input[5] == '5')

input[5] = enable;
else if (option == 6 && input[6] == '6')

input[6] = enable;
else if (option == 7 && input[7] == '7')

input[7] = enable;
else if (option == 8 && input[8] == '8')

input[8] = enable;
else if (option == 9 && input[9] == '9')

input[9] = enable;
else
{
cout<<"Invalid move ";

player--;
cin.ignore();
cin.get();
}
i=checkwinner();

player++;
}while(i==-1);
displayboard();
if(i==1)

cout<<"==>Player "<<--player<<" win ";
else
cout<<"==>Game draw";

cin.ignore();
cin.get();
return 0;
}


int checkwinner()
{
if (input[1] == input[2] && input[2] == input[3])

return 1;
else if (input[4] == input[5] && input[5] == input[6])

return 1;
else if (input[7] == input[8] && input[8] == input[9])

return 1;
else if (input[1] == input[4] && input[4] == input[7])

return 1;
else if (input[2] == input[5] && input[5] == input[8])

return 1;
else if (input[3] == input[6] && input[6] == input[9])

return 1;
else if (input[1] == input[5] && input[5] == input[9])

return 1;
else if (input[3] == input[5] && input[5] == input[7])

return 1;
else if (input[1] != '1' && input[2] != '2' && input[3] != '3'
&& input[4] != '4' && input[5] != '5' && input[6] != '6'
&& input[7] != '7' && input[8] != '8' && input[9] != '9')

return 0;
else
return -1;
}

void displayboard()
{
system("cls");
cout << " Tic Tac Toe ";

cout << "Player 1 (X) - Player 2 (O)" << endl << endl;
cout << endl;

cout << " | | " << endl;
cout << " " << input[1] << " | " << input[2] << " | " << input[3] << endl;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << input[4] << " | " << input[5] << " | " << input[6] << endl;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << input[7] << " | " << input[8] << " | " << input[9] << endl;

cout << " | | " << endl << endl;
}

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