Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm writing a 3D tic tac toe program for a player against a computer and I need the game to continuously go until the board

I'm writing a 3D tic tac toe program for a player against a computer and I need the game to continuously go until the board is full instead of it stopping when there is a 3 in a row. The objective is to get as many 3 in a row as possible

Heres my code:

#include

#include

#include

#include

using namespace std;

bool Continue();

class board { //board class

public:

board() : Value(0) {};

~board() {};

int ReadValue() const { return Value; };

void setValue(int NewValue);

private:

int Value;

};

void board::setValue(int NewValue) {

if (ReadValue())

return;

else

Value = NewValue;

return;

}

class Cube {

public:

Cube() {};

~Cube() {};

void displayCube() const;

int GameOutcome() const;

bool playboard(int iNum, int PlayerNum);

private:

board Grid[3][3][3];

bool cubeFull() const;

int GameWon() const;

char cInboard(int Row, int Col, int Board) const;

};

class Player {

public:

Player();

Player(int AgainstComputer);

~Player() {};

int GetPlayerNumber() const { return PlayerNumber; };

int Chooseboard() const;

private:

bool playerIsComputer;

int PlayerNumber;

};

Player::Player() {

playerIsComputer = true;

srand(time(0));

PlayerNumber = rand() % 2 + 1;

}

Player::Player(int AgainstComputer) {

playerIsComputer = false;

PlayerNumber = AgainstComputer % 2 + 1;

}

void Cube::displayCube() const { //creating the game board

int i, j;

cout << endl;

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

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

cout << " " << cInboard(i, j, 0) << "\t";

if (j < 2)

cout << "|";

}

if (i < 2) {

cout << endl;

cout << "------------------------" << endl;

}

}

cout << endl;

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

cout << "\t\t\t";

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

cout << " " << cInboard(i, j, 1) << "\t";

if (j < 2)

cout << "|";

}

if (i < 2) {

cout << endl << "\t\t\t";

cout << "------------------------" << endl;

}

}

cout << endl;

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

cout << "\t\t\t\t\t\t";

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

cout << " " << cInboard(i, j, 2) << "\t";

if (j < 2)

cout << "|";

}

if (i < 2) {

cout << endl << "\t\t\t\t\t\t";

cout <<"------------------------" << endl;

}

}

cout << endl << endl;

}

int Cube::GameOutcome() const {

if (GameWon() == -1)

return 1;

else if (GameWon() == 1)

return 2;

else if (cubeFull())

return 3;

else

return 0;

}

bool Cube::playboard(int Num, int PlayerNum) {

int Board = Num / 9;

int Row = (Num % 9) / 3;

int Col = Num % 3;

if (Grid[Row][Col][Board].ReadValue())

return false;

else {

Grid[Row][Col][Board].setValue(pow((-1), PlayerNum));

displayCube();

return true;

}

}

bool Cube::cubeFull() const {

int i, j, k, ctr = 0;

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

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

for (k = 0; k < 3; k++)

if (Grid[i][j][k].ReadValue())

ctr++;

if (ctr == 27)

return true;

else

return false;

}

int Cube::GameWon() const {

int i, j, k, ctr;

for (k = 0; k < 3; k++) {

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

ctr = 0;

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

ctr += Grid[i][j][k].ReadValue();

if (abs(ctr) == 3)

return abs(ctr) / ctr;

}

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

ctr = 0;

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

ctr += Grid[i][j][k].ReadValue();

if (abs(ctr) == 3)

return abs(ctr) / ctr;

}

ctr = 0;

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

j = i;

ctr += Grid[i][j][k].ReadValue();

}

if (abs(ctr) == 3)

return abs(ctr) / ctr;

ctr = 0;

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

j = 2 - i;

ctr += Grid[i][j][k].ReadValue();

}

if (abs(ctr) == 3)

return abs(ctr) / ctr;

}

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

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

ctr = 0;

for (k = 0; k < 3; k++)

ctr += Grid[i][j][k].ReadValue();

if (abs(ctr) == 3)

return abs(ctr) / ctr;

}

ctr = 0;

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

k = j;

ctr += Grid[i][j][k].ReadValue();

}

if (abs(ctr) == 3)

return abs(ctr) / ctr;

ctr = 0;

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

k = 2 - j;

ctr += Grid[i][j][k].ReadValue();

}

if (abs(ctr) == 3)

return abs(ctr) / ctr;

}

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

ctr = 0;

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

k = i;

ctr += Grid[i][j][k].ReadValue();

}

if (abs(ctr) == 3)

return abs(ctr) / ctr;

ctr = 0;

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

k = 2 - i;

ctr += Grid[i][j][k].ReadValue();

}

if (abs(ctr) == 3)

return abs(ctr) / ctr;

}

ctr = 0;

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

j = i;

k = i;

ctr += Grid[i][j][k].ReadValue();

}

if (abs(ctr) == 3)

return abs(ctr) / ctr;

ctr = 0;

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

j = 2 - i;

k = i;

ctr += Grid[i][j][k].ReadValue();

}

if (abs(ctr) == 3)

return abs(ctr) / ctr;

ctr = 0;

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

j = i;

k = 2 - i;

ctr += Grid[i][j][k].ReadValue();

}

if (abs(ctr) == 3)

return abs(ctr) / ctr;

ctr = 0;

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

j = 2 - i;

k = 2 - i;

ctr += Grid[i][j][k].ReadValue();

}

if (abs(ctr) == 3)

return abs(ctr) / ctr;

return 0;

}

char Cube::cInboard(int Row, int Col, int Board) const {

switch (Grid[Row][Col][Board].ReadValue()) {

case -1:

return 'X';

case 1:

return 'O';

default:

return ' ';

}

}

int Player::Chooseboard() const { //user chooses which spot they want to play

int boardNum;

if (playerIsComputer)

boardNum = rand() % 27;

else {

int Row = 0, Col = 0, Board = 0;

cout << "Which board will you play?" << endl;

while (Board < 1 || Board > 3) {

cout << "Board (1-3): ";

cin >> Board;

}//board slection

while (Row < 1 || Row > 3) {

cout << "Row (1-3): ";

cin >> Row;

}//row selection

while (Col < 1 || Col > 3) {

cout << "Column (1-3): ";

cin >> Col;

}//column selection

boardNum = (Board - 1) * 9 + (Row - 1) * 3 + (Col - 1);

}

return boardNum;

}

bool sayYes();

int main()

{

bool playAgain = true;

do {

Player Computer; //creating a computer player

Player User(Computer.GetPlayerNumber());

Cube GameCube;

cout << endl << "3D Tic-Tac-Toe." << endl;

if (Computer.GetPlayerNumber() == 1) {

GameCube.playboard(Computer.Chooseboard(), Computer.GetPlayerNumber());

}

else {

GameCube.displayCube();

}

do {

while (!GameCube.playboard(User.Chooseboard(), User.GetPlayerNumber()))

cout << "That board is already occupied." << endl << endl;

if (GameCube.GameOutcome() == 0) {

while (!GameCube.playboard(Computer.Chooseboard(), Computer.GetPlayerNumber())) {}

}

} while (GameCube.GameOutcome() == 0);

int FinalOutcome = GameCube.GameOutcome();

if (FinalOutcome == 3)

cout << "Draw!";

else {

cout << "Player " << FinalOutcome << " won!" << endl;

}

cout << endl << endl;

cout << "Would you like to play again? (Y/N) ";

playAgain = Continue();

} while (playAgain);

cout << "Goodbye!" << endl << endl;

return 0;

}

bool Continue() {

char cInputString[15] = {'\0'};

while (*cInputString != 'y' && *cInputString != 'Y' && *cInputString != 'n' && *cInputString != 'N')

cin >> cInputString;

if (*cInputString == 'y' || *cInputString == 'Y')

return true;

else

return false;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions