I am writing a c++ progam and make a game, that allow will alow user to player a
ID: 3789649 • Letter: I
Question
I am writing a c++ progam and make a game, that allow will alow user to player against computer and watch computer vs computer. When, it comes to 0 my code break, and also at the end my program does not display which player win the game. Please help me with this code and i really needed to done before tommmrow.
I think may be i mess up with isGamerOver funcstion....
Herader file:
#ifndef TICTACTOE_H
#define TICTACTOE_H
namespace Khaliq_10
{
class TicTacToe
{
private:
char playerSymbol, playingField[9];
public:
// CONSTRUCTOR
TicTacToe(char Symbol);
// MODIFICATION MEMBER FUNCTIONS
void playerNextMove(int const fieldLocation, char playerSymbol);
// CONSTANT MEMBER FUNCTIONS
char isSpaceOccupied(int const fieldLocation) const;
int isGameOver() const;
void displayTicBoard() const;
};
}
#endif
//implactions file...
#include
#include
#include
#include "tic-ac-toe_3-10.h"
using namespace std;
using namespace Khaliq_10;
namespace Khaliq_10
{
//COSTRUCTOR
TicTacToe::TicTacToe(char Symbol)
{
playerSymbol = Symbol;
//using for loop to count the player filed locations
for (int count = 0; count < 9; count++)
{
playingField[count] = 'E';
}
}
//MODIFICATION MEMBER FUNCTIONS
void TicTacToe::playerNextMove(int const fieldLocation, char playerSymbol)
{
assert((fieldLocation < 9) && (isSpaceOccupied(fieldLocation) == 'E'));
playingField[fieldLocation] = playerSymbol;
}
//CONSTANT MEMBER FUNCTIONS
char TicTacToe::isSpaceOccupied(int const fieldLocation) const
{
char currentlyFiledSpace;
assert(fieldLocation < 9);
currentlyFiledSpace = playingField[fieldLocation];
return currentlyFiledSpace;
}
void TicTacToe::displayTicBoard() const
{
//displying the tictactoe board
cout << " " << playingField[0] << " | " << playingField[1] << " | " << playingField[2] << endl;
cout << "----------" << endl;
cout << " " << playingField[3] << " | " << playingField[4] << " | " << playingField[5] << endl;
cout << "----------" << endl;
cout << " " << playingField[6] << " | " << playingField[7] << " | " << playingField[8] << endl << endl;
}
int TicTacToe::isGameOver() const
{
//all 3 top are in same row
if ((isSpaceOccupied(0) == isSpaceOccupied(1) == isSpaceOccupied(2)) && (isSpaceOccupied(0) != 'E'))
{
if (isSpaceOccupied(0) == 'X')
return 1;
else
return 2;
}
//all 3 middle row are the same
if ((isSpaceOccupied(3) == isSpaceOccupied(4) == isSpaceOccupied(5)) && (isSpaceOccupied(3) != 'E'))
{
if (isSpaceOccupied(3) == 'X')
return 1;
else
return 2;
}
//all 3 in bottom row are the same
if ((isSpaceOccupied(6) == isSpaceOccupied(7) == isSpaceOccupied(8)) && (isSpaceOccupied(6) != 'E'))
{
if (isSpaceOccupied(6) == 'X')
return 1;
else
return 2;
}
//all in 3 in the left colunmn are the same
if ((isSpaceOccupied(0) == isSpaceOccupied(3) == isSpaceOccupied(6)) && (isSpaceOccupied(0) != 'E'))
{
if (isSpaceOccupied(0) == 'X')
return 1;
else
return 2;
}
//all the 3 middle column are the same
if ((isSpaceOccupied(1) == isSpaceOccupied(4) == isSpaceOccupied(7)) && (isSpaceOccupied(1) != 'E'))
{
if (isSpaceOccupied(1) == 'X')
return 1;
else
return 2;
}
//all the three right column are the same
if ((isSpaceOccupied(2) == isSpaceOccupied(5) == isSpaceOccupied(8)) && (isSpaceOccupied(2) != 'E'))
{
if (isSpaceOccupied(2) == 'X')
return 1;
else
return 2;
}
//all the 3 digonally from top left to bottom right are the same
if ((isSpaceOccupied(0) == isSpaceOccupied(4) == isSpaceOccupied(8)) && (isSpaceOccupied(0) != 'E'))
{
if (isSpaceOccupied(0) == 'X')
return 1;
else
return 2;
}
//all 3 diangonally from bottom left to top right are the same
if ((isSpaceOccupied(2) == isSpaceOccupied(4) == isSpaceOccupied(6)) && (isSpaceOccupied(2) != 'E')) // All 3 diagonally from bottom-left to top-right
{
if (isSpaceOccupied(2) == 'X')
return 1;
else
return 2;
}
//this for loop will check is there any empty space left
for (int i = 0; i < 9; i++)
{
if (isSpaceOccupied(i) == 'E')
return 0;
else
return 3;
}
return 1;
}
}
//main
#include <iostream>
#include <cstdlib>
#include <time.h>
#include "tic-ac-toe_3-10.h"
using namespace Khaliq_10;
using namespace std;
//delcaring functons protypes
int computer_next_Move(TicTacToe board, char opponent_Symoble);
void menu();
int main()
{
//delcaring variables as requried for this applications
char user_symbol;
char computer_symbol;
char computer_symbol_1 = 'X';
char computer_symbol_2 = 'O';
bool has_Finished = false;
bool again = true;
bool PC_VS_Person = false;
int input;
while (true)
{
menu();
//asking user to choose men
cout << " Choose options from menu: ";
cin >> user_symbol;
//uisng if else chain for user input
if (user_symbol == '1')
{
//this if use for to watch game computer vs computer
PC_VS_Person = false;
break;
}
else if (user_symbol == '2')
{
//this else if use for to play game computer vs person.
PC_VS_Person = true;
computer_symbol = 'X';
break;
}
else
//will check the user input if that vaild or unvaild
cout << "Invaild number that you entered, please choose options from menu." << endl;
break;
}
if (PC_VS_Person)
{
while (true)
{
//giving user options, if user like to play as a X or O.
cout << "Press 1 to play as a X." << endl;
cout << "Press 2 to play as the O." << endl;
cin >> user_symbol;
//if user like to play as a X postions
if (user_symbol == '1')
{
user_symbol = 'X';
computer_symbol = 'O';
break;
}
//this else user if user wants to play as
else if (user_symbol == '2')
{
user_symbol = 'O';
computer_symbol = 'X';
break;
}
else
cout << "Invaild, number that you enterd." << endl;
}
TicTacToe playingField(user_symbol);
while (true)
{
int move_for_X;
int move_for_O;
if (user_symbol == 'X')
{
while (true)
{
cout << "enter a number from 0 to 8 for what position you want to take your turn." << endl;
cin >> input;
if (input < 9)
{
playingField.playerNextMove(input, user_symbol);
break;
}
else
cout << "enter a valid position" << endl;
}
{
//displyaing the number has placeed on which space
cout << "An " << user_symbol << " was placed in space number " << input << "." << endl << endl;
playingField.displayTicBoard();
if (playingField.isGameOver() != 0)
break;
}
{
move_for_O = computer_next_Move(playingField, computer_symbol_2);
playingField.playerNextMove(move_for_O, computer_symbol_2);
cout << "An " << computer_symbol_2 << " was placed in space number " << move_for_O << "." << endl << endl;
playingField.displayTicBoard();
if (playingField.isGameOver() != 0)
break;
}
}
else if (user_symbol == 'O')
{
move_for_X = computer_next_Move(playingField, computer_symbol_1);
playingField.playerNextMove(move_for_X, computer_symbol_1);
cout << "An " << computer_symbol_1 << " was placed in space number " << move_for_X << "." << endl << endl;
playingField.displayTicBoard();
if (playingField.isGameOver() != 0)
break;
while (true)
{
cout << "enter a number from 0 to 8 for what position you want to take your turn." << endl;
cin >> input;
if (input < 9)
{
playingField.playerNextMove(input, user_symbol);
break;
}
else
cout << "enter a valid position" << endl;
}
cout << "An " << user_symbol << " was placed in space number " << input << "." << endl << endl;
playingField.displayTicBoard();
if (playingField.isGameOver() != 0)
break;
}
}
}
else
{
TicTacToe computerField(computer_symbol_1);
int gameOutcome;
while (true)
{
int move_for_X;
int move_for_O;
move_for_X = computer_next_Move(computerField, computer_symbol_2);
computerField.playerNextMove(move_for_X, computer_symbol_1);
cout << "An " << computer_symbol_1 << " was placed in space number " << move_for_X << "." << endl << endl;
computerField.displayTicBoard();
gameOutcome = computerField.isGameOver();
if (computerField.isGameOver() != 0)
break;
move_for_O = computer_next_Move(computerField, computer_symbol_1);
computerField.playerNextMove(move_for_O, computer_symbol_2);
cout << "An " << computer_symbol_2 << " was placed in space number " << move_for_O << "." << endl << endl;
computerField.displayTicBoard();
gameOutcome = computerField.isGameOver();
if (computerField.isGameOver() != 0)
break;
}
{
//gameOutcome = computerField.isGameOver();
if (gameOutcome == 1)
cout << "The computer was controlling for X." << endl;
else if (gameOutcome == 2)
cout << "The computer was controlling for O." << endl;
else
cout << "Now this point nethire computer vs computer win." << endl;
}
}
system("pause");
return 0;
}
void menu()
{
cout << "Press 1 to watch Tic-Toa-Toe played against computer vs computer. " << endl;
cout << "Press 2 to play a game of Tic-Tac-Toe against a computer." << endl;
}
int computer_next_Move(TicTacToe board, char opponent_Symoble)
{
//using if condations
if (board.isSpaceOccupied(4) == 'E')
{
return 4;
}
if (board.isSpaceOccupied(0) == 'E' && ((board.isSpaceOccupied(1) == board.isSpaceOccupied(2) == opponent_Symoble) ||
(board.isSpaceOccupied(4) == board.isSpaceOccupied(8) == opponent_Symoble) ||
(board.isSpaceOccupied(3) == board.isSpaceOccupied(6) == opponent_Symoble)))
{
return 0;
}if (board.isSpaceOccupied(1) == 'E' && ((board.isSpaceOccupied(0) == board.isSpaceOccupied(2) == opponent_Symoble) ||
(board.isSpaceOccupied(4) == board.isSpaceOccupied(7) == opponent_Symoble)))
{
return 1;
}
if (board.isSpaceOccupied(2) == 'E' && ((board.isSpaceOccupied(0) == board.isSpaceOccupied(1) == opponent_Symoble) ||
(board.isSpaceOccupied(4) == board.isSpaceOccupied(6) == opponent_Symoble) ||
(board.isSpaceOccupied(5) == board.isSpaceOccupied(8) == opponent_Symoble)))
{
return 2;
}
if (board.isSpaceOccupied(3) == 'E' && ((board.isSpaceOccupied(0) == board.isSpaceOccupied(6) == opponent_Symoble) ||
(board.isSpaceOccupied(4) == board.isSpaceOccupied(5) == opponent_Symoble)))
{
return 3;
}
//The middle of the board will never be occupied after the 1st turn
if (board.isSpaceOccupied(5) == 'E' && ((board.isSpaceOccupied(2) == board.isSpaceOccupied(8) == opponent_Symoble) ||
(board.isSpaceOccupied(3) == board.isSpaceOccupied(4) == opponent_Symoble)))
{
return 5;
}
if (board.isSpaceOccupied(6) == 'E' && ((board.isSpaceOccupied(0) == board.isSpaceOccupied(3) == opponent_Symoble) ||
(board.isSpaceOccupied(4) == board.isSpaceOccupied(2) == opponent_Symoble) ||
(board.isSpaceOccupied(7) == board.isSpaceOccupied(8) == opponent_Symoble)))
{
return 6;
}
if (board.isSpaceOccupied(7) == 'E' && ((board.isSpaceOccupied(6) == board.isSpaceOccupied(8) == opponent_Symoble) ||
(board.isSpaceOccupied(1) == board.isSpaceOccupied(4) == opponent_Symoble)))
{
return 7;
}
if (board.isSpaceOccupied(8) == 'E' && ((board.isSpaceOccupied(2) == board.isSpaceOccupied(5) == opponent_Symoble) ||
(board.isSpaceOccupied(0) == board.isSpaceOccupied(4) == opponent_Symoble) ||
(board.isSpaceOccupied(6) == board.isSpaceOccupied(7) == opponent_Symoble)))
{
return 8;
}
else
{
//for the else will set the seconds that coomputer will move
//declaring variables as needed
int random_number;
time_t seconds;
time(&seconds);
srand((unsigned int)seconds);
while (true)
{
random_number = rand() % (9);
if (board.isSpaceOccupied(random_number) == 'E')
return random_number;
}
}
}
Explanation / Answer
// Some modifications are done in isGameOver() method look at the for loop in the last of a method.
// And also I made an playingField array of 10 elements so user can start from 1.
//And just look at the condition checking of is return value of GameOver() method in the main file at each players movement.
//Header File
#ifndef TICTACTOE_H
#define TICTACTOE_H
namespace Khaliq_10
{
class TicTacToe
{
private:
char playerSymbol, playingField[10];
public:
// CONSTRUCTOR
TicTacToe(char Symbol);
// MODIFICATION MEMBER FUNCTIONS
void playerNextMove(int const fieldLocation, char playerSymbol);
// CONSTANT MEMBER FUNCTIONS
char isSpaceOccupied(int const fieldLocation) const;
int isGameOver() const;
void displayTicBoard() const;
};
}
#endif
//Implementation file
#include <bits/stdc++.h>
#include <iostream>
#include "tictactoe.h"
using namespace std;
using namespace Khaliq_10;
namespace Khaliq_10
{
//COSTRUCTOR
TicTacToe::TicTacToe(char Symbol)
{
playerSymbol = Symbol;
//using for loop to count the player filed locations
for (int count = 0; count < 10; count++)
{
playingField[count] = 'E';
}
}
//MODIFICATION MEMBER FUNCTIONS
void TicTacToe::playerNextMove(int const fieldLocation, char playerSymbol)
{
assert((fieldLocation < 10) && (isSpaceOccupied(fieldLocation) == 'E'));
playingField[fieldLocation] = playerSymbol;
}
//CONSTANT MEMBER FUNCTIONS
char TicTacToe::isSpaceOccupied(int const fieldLocation) const
{
char currentlyFiledSpace;
assert(fieldLocation < 10);
currentlyFiledSpace = playingField[fieldLocation];
return currentlyFiledSpace;
}
void TicTacToe::displayTicBoard() const
{
//displying the tictactoe board
cout << " " << playingField[1] << " | " << playingField[2] << " | " << playingField[3] << endl;
cout << "----------" << endl;
cout << " " << playingField[4] << " | " << playingField[5] << " | " << playingField[6] << endl;
cout << "----------" << endl;
cout << " " << playingField[7] << " | " << playingField[8] << " | " << playingField[9] << endl << endl;
}
int TicTacToe::isGameOver() const
{
//all 3 top are in same row
if ((isSpaceOccupied(1) == isSpaceOccupied(2) == isSpaceOccupied(3)) && (isSpaceOccupied(1) != 'E'))
{
if (isSpaceOccupied(1) == 'X')
return 1;
else
return 2;
}
//all 3 middle row are the same
if ((isSpaceOccupied(4) == isSpaceOccupied(5) == isSpaceOccupied(6)) && (isSpaceOccupied(4) != 'E'))
{
if (isSpaceOccupied(4) == 'X')
return 1;
else
return 2;
}
//all 3 in bottom row are the same
if ((isSpaceOccupied(7) == isSpaceOccupied(8) == isSpaceOccupied(9)) && (isSpaceOccupied(7) != 'E'))
{
if (isSpaceOccupied(7) == 'X')
return 1;
else
return 2;
}
//all in 3 in the left colunmn are the same
if ((isSpaceOccupied(1) == isSpaceOccupied(4) == isSpaceOccupied(7)) && (isSpaceOccupied(1) != 'E'))
{
if (isSpaceOccupied(1) == 'X')
return 1;
else
return 2;
}
//all the 3 middle column are the same
if ((isSpaceOccupied(2) == isSpaceOccupied(5) == isSpaceOccupied(8)) && (isSpaceOccupied(2) != 'E'))
{
if (isSpaceOccupied(2) == 'X')
return 1;
else
return 2;
}
//all the three right column are the same
if ((isSpaceOccupied(3) == isSpaceOccupied(6) == isSpaceOccupied(9)) && (isSpaceOccupied(3) != 'E'))
{
if (isSpaceOccupied(3) == 'X')
return 1;
else
return 2;
}
//all the 3 digonally from top left to bottom right are the same
if ((isSpaceOccupied(1) == isSpaceOccupied(5) == isSpaceOccupied(9)) && (isSpaceOccupied(1) != 'E'))
{
if (isSpaceOccupied(1) == 'X')
return 1;
else
return 2;
}
//all 3 diangonally from bottom left to top right are the same
if ((isSpaceOccupied(3) == isSpaceOccupied(5) == isSpaceOccupied(7)) && (isSpaceOccupied(3) != 'E')) // All 3 diagonally from bottom-left to top-right
{
if (isSpaceOccupied(3) == 'X')
return 1;
else
return 2;
}
int flag=3;
for(int i=1;i<=9;i++)
{
if(isSpaceOccupied(i)=='E')
flag=4;
}
//this for loop will check is there any empty space left
/* for (int i = 1; i <= 9; i++)
{
if (isSpaceOccupied(i) == 'E')
return 0;
else
return 3;
}
*/
return flag; //for continueing the game
}
}
//Main file
//main
#include <iostream>
#include <cstdlib>
#include <time.h>
#include "tictactoe.h"
using namespace Khaliq_10;
using namespace std;
//delcaring functons protypes
int computer_next_Move(TicTacToe board, char opponent_Symoble);
void menu();
int main()
{
//delcaring variables as requried for this applications
char user_symbol;
char computer_symbol;
char computer_symbol_1 = 'X';
char computer_symbol_2 = 'O';
bool has_Finished = false;
bool again = true;
bool PC_VS_Person = false;
int input;
while (true)
{
menu();
//asking user to choose men
cout << " Choose options from menu: ";
cin >> user_symbol;
//uisng if else chain for user input
if (user_symbol == '1')
{
//this if use for to watch game computer vs computer
PC_VS_Person = false;
break;
}
else if (user_symbol == '2')
{
//this else if use for to play game computer vs person.
PC_VS_Person = true;
computer_symbol = 'X';
break;
}
else
//will check the user input if that vaild or unvaild
cout << "Invaild number that you entered, please choose options from menu." << endl;
break;
}
if (PC_VS_Person)
{
int ff=0;
while (true)
{
//giving user options, if user like to play as a X or O.
cout << "Press 1 to play as a X." << endl;
cout << "Press 2 to play as the O." << endl;
cin >> user_symbol;
//if user like to play as a X postions
if (user_symbol == '1')
{
user_symbol = 'X';
computer_symbol = 'O';
break;
}
//this else user if user wants to play as
else if (user_symbol == '2')
{
user_symbol = 'O';
computer_symbol = 'X';
break;
}
else
cout << "Invaild, number that you enterd." << endl;
}
TicTacToe playingField(user_symbol);
while (true)
{
int move_for_X;
int move_for_O;
if (user_symbol == 'X')
{
while (true)
{
cout << "enter a number from 0 to 8 for what position you want to take your turn." << endl;
cin >> input;
if (input <= 9)
{
playingField.playerNextMove(input, user_symbol);
break;
}
else
cout << "enter a valid position" << endl;
}
{
//displyaing the number has placeed on which space
cout << "An " << user_symbol << " was placed in space number " << input << "." << endl << endl;
playingField.displayTicBoard();
int res=playingField.isGameOver();
if(res!=4)
{
if(res==1)
cout<<"Player 1 is winner ";
else if(res==2)
cout<<"Player 2 is winner ";
else if(res==3)
cout<<"Match draw ";
break;
}// if (playingField.isGameOver() != 0)
// break;
}
{
move_for_O = computer_next_Move(playingField, computer_symbol_2);
playingField.playerNextMove(move_for_O, computer_symbol_2);
cout << "An " << computer_symbol_2 << " was placed in space number " << move_for_O << "." << endl << endl;
playingField.displayTicBoard();
// if (playingField.isGameOver() != 0)
// break;
int res=playingField.isGameOver();
if(res!=4)
{
if(res==1)
cout<<"Player 1 is winner ";
else if(res==2)
cout<<"Player 2 is winner ";
else if(res==3)
cout<<"Match draw ";
break;
}
}
}
else if (user_symbol == 'O')
{
move_for_X = computer_next_Move(playingField, computer_symbol_1);
playingField.playerNextMove(move_for_X, computer_symbol_1);
cout << "An " << computer_symbol_1 << " was placed in space number " << move_for_X << "." << endl << endl;
playingField.displayTicBoard();
// if (playingField.isGameOver() != 0)
// break;
int res=playingField.isGameOver();
if(res!=4)
{
if(res==1)
cout<<"Player 1 is winner ";
else if(res==2)
cout<<"Player 2 is winner ";
else if(res==3)
cout<<"Match draw ";
break;
}
while (true)
{
cout << "enter a number from 1 to 9 for what position you want to take your turn." << endl;
cin >> input;
if (input <= 9)
{
playingField.playerNextMove(input, user_symbol);
break;
}
else
cout << "enter a valid position" << endl;
}
cout << "An " << user_symbol << " was placed in space number " << input << "." << endl << endl;
playingField.displayTicBoard();
// if (playingField.isGameOver() != 0)
// break;
res=playingField.isGameOver();
if(res!=4)
{
if(res==1)
cout<<"Player 1 is winner ";
else if(res==2)
cout<<"Player 2 is winner ";
else if(res==3)
cout<<"Match draw ";
break;
} }
}
}
else
{
TicTacToe computerField(computer_symbol_1);
int gameOutcome;
while (true)
{
int move_for_X;
int move_for_O;
move_for_X = computer_next_Move(computerField, computer_symbol_2);
computerField.playerNextMove(move_for_X, computer_symbol_1);
cout << "An " << computer_symbol_1 << " was placed in space number " << move_for_X << "." << endl << endl;
computerField.displayTicBoard();
gameOutcome = computerField.isGameOver();
//if (computerField.isGameOver() != 0)
// break;
int res=computerField.isGameOver();
if(res!=4)
{
if(res==1)
cout<<"Player 1 is winner ";
else if(res==2)
cout<<"Player 2 is winner ";
else if(res==3)
cout<<"Match draw ";
break;
}
move_for_O = computer_next_Move(computerField, computer_symbol_1);
computerField.playerNextMove(move_for_O, computer_symbol_2);
cout << "An " << computer_symbol_2 << " was placed in space number " << move_for_O << "." << endl << endl;
computerField.displayTicBoard();
gameOutcome = computerField.isGameOver();
res=computerField.isGameOver();
if(res!=4)
{
if(res==1)
cout<<"Player 1 is winner ";
else if(res==2)
cout<<"Player 2 is winner ";
else if(res==3)
cout<<"Match draw ";
break;
}
// if (computerField.isGameOver() != 0)
// break;
}
{
//gameOutcome = computerField.isGameOver();
if (gameOutcome == 1)
cout << "The computer was controlling for X." << endl;
else if (gameOutcome == 2)
cout << "The computer was controlling for O." << endl;
else
cout << "Now this point nethire computer vs computer win." << endl;
}
}
system("pause");
return 0;
}
void menu()
{
cout << "Press 1 to watch Tic-Toa-Toe played against computer vs computer. " << endl;
cout << "Press 2 to play a game of Tic-Tac-Toe against a computer." << endl;
}
int computer_next_Move(TicTacToe board, char opponent_Symoble)
{
//using if condations
if (board.isSpaceOccupied(4) == 'E')
{
return 4;
}
if (board.isSpaceOccupied(0) == 'E' && ((board.isSpaceOccupied(1) == board.isSpaceOccupied(2) == opponent_Symoble) ||
(board.isSpaceOccupied(4) == board.isSpaceOccupied(8) == opponent_Symoble) ||
(board.isSpaceOccupied(3) == board.isSpaceOccupied(6) == opponent_Symoble)))
{
return 0;
}if (board.isSpaceOccupied(1) == 'E' && ((board.isSpaceOccupied(0) == board.isSpaceOccupied(2) == opponent_Symoble) ||
(board.isSpaceOccupied(4) == board.isSpaceOccupied(7) == opponent_Symoble)))
{
return 1;
}
if (board.isSpaceOccupied(2) == 'E' && ((board.isSpaceOccupied(0) == board.isSpaceOccupied(1) == opponent_Symoble) ||
(board.isSpaceOccupied(4) == board.isSpaceOccupied(6) == opponent_Symoble) ||
(board.isSpaceOccupied(5) == board.isSpaceOccupied(8) == opponent_Symoble)))
{
return 2;
}
if (board.isSpaceOccupied(3) == 'E' && ((board.isSpaceOccupied(0) == board.isSpaceOccupied(6) == opponent_Symoble) ||
(board.isSpaceOccupied(4) == board.isSpaceOccupied(5) == opponent_Symoble)))
{
return 3;
}
//The middle of the board will never be occupied after the 1st turn
if (board.isSpaceOccupied(5) == 'E' && ((board.isSpaceOccupied(2) == board.isSpaceOccupied(8) == opponent_Symoble) ||
(board.isSpaceOccupied(3) == board.isSpaceOccupied(4) == opponent_Symoble)))
{
return 5;
}
if (board.isSpaceOccupied(6) == 'E' && ((board.isSpaceOccupied(0) == board.isSpaceOccupied(3) == opponent_Symoble) ||
(board.isSpaceOccupied(4) == board.isSpaceOccupied(2) == opponent_Symoble) ||
(board.isSpaceOccupied(7) == board.isSpaceOccupied(8) == opponent_Symoble)))
{
return 6;
}
if (board.isSpaceOccupied(7) == 'E' && ((board.isSpaceOccupied(6) == board.isSpaceOccupied(8) == opponent_Symoble) ||
(board.isSpaceOccupied(1) == board.isSpaceOccupied(4) == opponent_Symoble)))
{
return 7;
}
if (board.isSpaceOccupied(8) == 'E' && ((board.isSpaceOccupied(2) == board.isSpaceOccupied(5) == opponent_Symoble) ||
(board.isSpaceOccupied(0) == board.isSpaceOccupied(4) == opponent_Symoble) ||
(board.isSpaceOccupied(6) == board.isSpaceOccupied(7) == opponent_Symoble)))
{
return 8;
}
else
{
//for the else will set the seconds that coomputer will move
//declaring variables as needed
int random_number;
time_t seconds;
time(&seconds);
srand((unsigned int)seconds);
while (true)
{
random_number = rand() % (9);
if (board.isSpaceOccupied(random_number) == 'E')
return random_number;
}
}
}
//Output :
G580:~/codes/schegg$ ./a.out
Press 1 to watch Tic-Toa-Toe played against computer vs computer.
Press 2 to play a game of Tic-Tac-Toe against a computer.
Choose options from menu: 2
Press 1 to play as a X.
Press 2 to play as the O.
2
An X was placed in space number 4.
E | E | E
----------
X | E | E
----------
E | E | E
enter a number from 1 to 9 for what position you want to take your turn.
2
An O was placed in space number 2.
E | O | E
----------
X | E | E
----------
E | E | E
An X was placed in space number 1.
X | O | E
----------
X | E | E
----------
E | E | E
enter a number from 1 to 9 for what position you want to take your turn.
5
An O was placed in space number 5.
X | O | E
----------
X | O | E
----------
E | E | E
An X was placed in space number 3.
X | O | X
----------
X | O | E
----------
E | E | E
enter a number from 1 to 9 for what position you want to take your turn.
8
An O was placed in space number 8.
X | O | X
----------
X | O | E
----------
E | O | E
An X was placed in space number 0.
X | O | X
----------
X | O | E
----------
E | O | E
enter a number from 1 to 9 for what position you want to take your turn.
9
An O was placed in space number 9.
X | O | X
----------
X | O | E
----------
E | O | O
An X was placed in space number 7.
X | O | X
----------
X | O | E
----------
X | O | O
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.