Question

In the C++ programming language write a program capable of playing 3D Tic-Tac-Toe against the user....

In the C++ programming language write a program capable of playing 3D Tic-Tac-Toe against the user. Your program should use OOP concepts in its design. Use Inheritance to create a derived class from your Lab #9 Tic-Tac-Toe class. You can use ASCII art to generate and display the 3x3x3 playing board. The program should randomly decide who goes first computer or user. Your program should know and inform the user if an illegal move was made (cell already occupied). The program should also keep the score and announce if one of the players wins or if a draw is achieved. While it is desirable for your program to play a strong game, this is not an Artificial Intelligence course so if your program does not play at a world champion level you will not be penalized for it.

The object of a 3D-TTT is to get as many 3-in-a-row as possible. You win just like in traditional TTT, except you can also win by getting 3-in-a-raw down each board. Imagine the boards as placed on top of each other.

Here's my current code:

#include <iostream>
#include <ctime>
#include <string> // for string methods
using namespace std;
class Board
{
private:
char squares[3][3]; //two-dimensional arrays
public:
Board() {}
void PrintBoard();
void BeginGame();
void playerTurn (char num, char Player);
bool CheckWin (char Player, bool gameOver);
bool CheckDraw(bool gameOver);
};
//Organize board for the game
void Board::BeginGame()
{
int n = 1;
int i = 0;
int j = 0;
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 3; j++ )
{
squares[i][j] = '0' + n; //Casting n value to a character
n++;
}
}
} //End SetGameBoard
//printing board
void Board::PrintBoard()
{
int i = 0;
int j = 0;
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 3; j++ )
{
if ( j < 2 )
{
cout << squares[i][j] << " | ";
}
else
{
cout << squares[i][j] << endl;
}
}
}
} //End
void Board:: playerTurn (char num, char Player)
{
int i = 0;
int j = 0;
bool WrongMove = true; //If the value is true then the player has made a wrong move
for( i = 0; i < 3; i++ )
{
for( j = 0; j < 3; j++ )
{
if(squares[i][j] == num)
{
//Assigns the space with the X or O,
squares[i][j] = Player;
WrongMove = false;
}
}
}
if(WrongMove == true) { cout << "You can only mark the open sets!\n"; }
} //End Player Move
//checking for the winner. If not, cotinue or a draw
bool Board:: CheckWin (char Player, bool GameOver)
{
for(int i = 0; i < 3; i++) //To check rows
{
if(squares[i][0] == squares[i][1] && squares[i][1] ==
squares[i][2]) GameOver = true;
}
for(int i = 0; i < 3; i++) //To check columns
{
if(squares[0][i] == squares[1][i] && squares[1][i] ==
squares[2][i]) GameOver = true;
}
if(squares[0][0] == squares[1][1] && squares[1][1] == squares[2][2]) //To check the Diagonals
{
GameOver = true;
}
if(squares[0][2] == squares[1][1] && squares[1][1] == squares[2][0])
{
GameOver = true;
}
if(GameOver == true)
{
cout << "Player " << Player << " wins!\n\n";
cout << "-----------------------" << endl;
cout << "| CONGRATULATIONS " << Player << " |\n";
cout << "-----------------------" << endl << endl;
}
return GameOver;
}
bool Board:: CheckDraw(bool GameOver)
{
int n = 1;
int i = 0;
int j = 0;
int counter = 0;
for( i = 0; i < 3; i++ )
{
for( j = 0; j < 3; j++ )
{
//Check to see if the board if full
if(squares[i][j] == '0' + n)
{
counter++;
}
n++;
}
}
if( counter < 1 )
{
cout << "Game Draw\n\n";
GameOver = true;
}
return GameOver;
}
// Main method starts here
int main()
{
bool finish = false, GameOver = false,isValid;
char Player = 'O', num;
int number;


if(number == 0)
Player = 'X';
else
Player = 'O';
cout << "" << endl;
cout << "-=Tic-Tac-Toe=-\n";
cout << "-------------"<< endl;
Board TicTac;
TicTac.BeginGame();
TicTac.PrintBoard(); //Initialize and output board
do
{
if( Player == 'X' )
{
Player = 'O';
}
else
{
Player = 'X';
}
TicTac.PrintBoard();
cout << "\nPlayer \"" << Player << "\", it's your turn: ";
cin >> num;
cout << "\n";
TicTac.playerTurn (num, Player);
GameOver = TicTac.CheckWin (Player, GameOver);
GameOver = TicTac.CheckDraw(GameOver);
if(GameOver == true)
{
cout << "Thank you for playing!";
finish = true;
}
} while(!finish);
return 0;
}

USE DEV C++ APP ON LAPTOP

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Use Dev C++ :-

Here is your code :-

#include <iostream>
#include<time.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctime>
#include <string> // for string methods
using namespace std;

class Board
{
private:
char squares[3][3]; //two-dimensional arrays
public:
Board() {}
void PrintBoard();
void BeginGame();
void playerTurn (char num, char Player);
bool CheckWin (char Player, bool gameOver);
bool CheckDraw(bool gameOver);

};
//Organize board for the game
void Board::BeginGame()
{
int n = 1;
int i = 0;
int j = 0;
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 3; j++ )
{
squares[i][j] = '0' + n; //Casting n value to a character
n++;
}
}
} //End SetGameBoard


//printing board
void Board::PrintBoard()
{
int i = 0;
int j = 0;
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 3; j++ )
{
if ( j < 2 )
{
cout << squares[i][j] << " | ";
}
else
{
cout << squares[i][j] << endl;
}
}
}
} //End

void Board:: playerTurn (char num, char Player)
{
int i = 0;
int j = 0;
bool WrongMove = true; //If the value is true then the player has made a wrong move
for( i = 0; i < 3; i++ )
{
for( j = 0; j < 3; j++ )
{
if(squares[i][j] == num)
{
//Assigns the space with the X or O,
squares[i][j] = Player;
WrongMove = false;
}
}
}
if(WrongMove == true) { cout << "You can only mark the open sets!\n"; }
} //End Player Move

//checking for the winner. If not, cotinue or a draw
bool Board:: CheckWin (char Player, bool GameOver)
{
for(int i = 0; i < 3; i++) //To check rows
{
if(squares[i][0] == squares[i][1] && squares[i][1] ==
squares[i][2]) GameOver = true;
}
for(int i = 0; i < 3; i++) //To check columns
{
if(squares[0][i] == squares[1][i] && squares[1][i] ==
squares[2][i]) GameOver = true;
}

if(squares[0][0] == squares[1][1] && squares[1][1] == squares[2][2]) //To check the Diagonals
{
GameOver = true;
}
if(squares[0][2] == squares[1][1] && squares[1][1] == squares[2][0])
{
GameOver = true;
}
if(GameOver == true)
{
cout << "Player " << Player << " wins!\n\n";
cout << "-----------------------" << endl;
cout << "| CONGRATULATIONS " << Player << " |\n";
cout << "-----------------------" << endl << endl;
}
return GameOver;
}

bool Board:: CheckDraw(bool GameOver)
{
int n = 1;
int i = 0;
int j = 0;
int counter = 0;
for( i = 0; i < 3; i++ )
{
for( j = 0; j < 3; j++ )
{
//Check to see if the board if full
if(squares[i][j] == '0' + n)
{
counter++;
}
n++;
}
}
if( counter < 1 )
{
cout << "Game Draw\n\n";
GameOver = true;
}
return GameOver;
}

// Main method starts here
int main()
{
bool finish = false, GameOver = false,isValid;

char Player = 'O', num;
int number;
srand( time(NULL) );//seed random from time
number = rand()%2; //Randomly choose a player to start game
if(number == 0)
Player = 'X';
else
Player = 'O';
cout << "" << endl;

cout << "-=Tic-Tac-Toe=-\n";
cout << "-------------"<< endl;
Board TicTac;
TicTac.BeginGame();
TicTac.PrintBoard(); //Initialize and output board
do
{
if( Player == 'X' )
{
Player = 'O';
}
else
{
Player = 'X';
}
TicTac.PrintBoard();
cout << "\nPlayer \"" << Player << "\", it's your turn: ";
cin >> num;
cout << "\n";
TicTac.playerTurn (num, Player);

GameOver = TicTac.CheckWin (Player, GameOver);
GameOver = TicTac.CheckDraw(GameOver);
if(GameOver == true)
{
cout << "Thank you for playing!";
finish = true;
}
} while(!finish);
return 0;
}

Output :-

Add a comment
Know the answer?
Add Answer to:
In the C++ programming language write a program capable of playing 3D Tic-Tac-Toe against the user....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

    Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...

  • In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and...

    In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and crosses on the 3 x 3 game board. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row in the game board wins the game. Super Tic Tac Toe game also has a 3 x 3 game board with super grid. Each super grid itself is a traditional Tic Tac Toe board. Winning a game of Tic...

  • Q1) Write a C function that allows the user to initialize a Tic-Tac-Toe board. The board...

    Q1) Write a C function that allows the user to initialize a Tic-Tac-Toe board. The board is a 2D array of size 3x3. The function will set an id for each cell of the board starting from 1 and stop at 9. The function prototype is ​void​​ ​​InitializeBoard​​(​​int​​ m, ​​int​​ n , ​​char​​ board[][n]) void​​ ​​InitializeBoard​​(​​int​​ m, ​​int​​ n , ​​char​​ board[][n])​​{ ​​int​​ c =​​1​​; ​ for​​(​​int​​ i =​​0​​; i<m; i++){ ​​ for​​(​​int​​ j=​​0​​; j< n; j++){ board[i][j] = c+​​'0'​​;...

  • JAVA TIC TAC TOE - please help me correct my code Write a program that will...

    JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below |    1       |   2        |   3 |    4       |   5        |   6                 |    7      |   8        |   9 The program will assign X to Player 1, and O to Player    The program will ask Player 1, to...

  • Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you...

    Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you must copy and paste to cloud9. Do not change the provided complete functions, or the function stub headers / return values. Currently, if the variables provided in main are commented out, the program will compile. Complete the specifications for each function. As you develop the program, implement one function at a time, and test that function. The provided comments provide hints as to what...

  • I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<i...

    I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<iostream> using namespace std; const int ROWS=3; const int COLS=3; void fillBoard(char [][3]); void showBoard(char [][3]); void getChoice(char [][3],bool); bool gameOver(char [][3]); int main() { char board[ROWS][COLS]; bool playerToggle=false; fillBoard(board); showBoard(board); while(!gameOver(board)) { getChoice(board,playerToggle); showBoard(board); playerToggle=!playerToggle; } return 1; } void fillBoard(char board[][3]) { for(int i=0;i<ROWS;i++) for(int j=0;j<COLS;j++) board[i][j]='*'; } void showBoard(char board[][3]) { cout<<" 1 2 3"<<endl; for(int i=0;i<ROWS;i++) { cout<<(i+1)<<"...

  • This is an advanced version of the game tic tac toe, where the player has to...

    This is an advanced version of the game tic tac toe, where the player has to get five in a row to win. The board is a 30 x 20. Add three more functions that will check for a win vertically, diagonally, and backwards diagonally. Add an AI that will play against the player, with random moves. Bound check each move to make sure it is in the array. Use this starter code to complete the assignment in C++. Write...

  • Write a program to Simulate a game of tic tac toe in c#

    Write a program to Simulate a game of tic tac toe. A game of tic tac toe has two players. A Player class is required to store /represent information about each player. The UML diagram is given below.Player-name: string-symbol :charPlayer (name:string,symbol:char)getName():stringgetSymbol():chargetInfo():string The tic tac toe board will be represented by a two dimensional array of size 3 by 3 characters. At the start of the game each cell is empty (must be set to the underscore character ‘_’). Program flow:1)    ...

  • Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use...

    Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user to enter...

  • Implement a tic-tac-toe game. Currently, if the variables provided in main are commented out, the program...

    Implement a tic-tac-toe game. Currently, if the variables provided in main are commented out, the program will compile. Complete the specifications for each function. As you develop the program, implement one function at a time, and test that function. The provided comments provide hints as to what each function should do and when each should be called. Add variables where you see fit. Implementation Strategies: The template has variables and two global constants for you to utilize. The template has...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT