Question: 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.
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.
I need some guidance on how to tackle this. I don't know how to use inheritance in this context.
Here is my current code:
#include #include #include using namespace std; class Board { private: char squares[3][3]; // public: Board () {} void PrintBoard(); void BeginGame(); bool playerTurn (char num, char Player); bool CheckWin (char Player, bool gameOver); bool CheckDraw(bool gameOver); }; void Board::PrintBoard() //function to print the 3x3 board { 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; } } } } void Board::BeginGame() { int n = 1, i = 0, j = 0; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { squares[i][j] = '0' + n; n++; } } } bool Board::playerTurn(char num, char Player) { int i = 0,j = 0; for (i = 0; i < 3; i++) { for ( j = 0; j < 3; j++) { if (squares[i][j] == num) { squares[i][j] = Player; return true; //not an invalid move } } } return false; //an invalid move } bool Board::CheckWin(char Player, bool gameOver) { for (int i = 0; i < 3; i++) { if (squares[i][0] == squares[i][1] && squares [i][1] == squares[i][2]) gameOver = true; } for (int i = 0; i < 3; i++) { 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]) { gameOver = true; } if (squares[0][2] == squares[1][1] && squares[1][1] == squares[2][0]) { gameOver = true; } if (gameOver == true) { cout<< "Player " << Player << " wins "; } 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++) { if (squares[i][j] == '0' + n) { counter++; } n++; } } if (counter < 1) { cout << "Game Draw "; gameOver = true; } return gameOver; } int main() { bool gameOver = false, finish = false, isValid; char Player, 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; Board TicTac; TicTac.BeginGame(); TicTac.PrintBoard(); //Initialize and output board do { isValid = false; //isvalid = false if player chooses used sqaure if(Player == 'X') { Player = 'O'; cout << " Player " << Player << ", it's your turn:" << endl; cin >> num; isValid = TicTac.playerTurn(num, Player); if(!isValid) while (!isValid) { cout << "Incorrect Selection. PLease select another: " << endl; cin >> num; isValid = TicTac.playerTurn(num, Player); } } else { Player = 'X'; cout << " Player " << Player << "'s turn" << endl; while(!isValid) { number = rand()%9+1; //WARNING: COMPUTER IS EXTREMELY STUPID num = '0' + number; isValid = TicTac.playerTurn(num, Player); } } TicTac.PrintBoard(); gameOver = TicTac.CheckWin(Player, gameOver); gameOver = TicTac.CheckDraw(gameOver); if(gameOver == true) { cout << "Thank you for playing!"; finish = true; } }while(!finish); return 0; }