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. Heres my last code:
#include
// display function void Board::display() { int i = 0; int r = 0; for ( i = 0; i < 3; i++) { for (r = 0; r < 3; r++) { if (r < 2) { cout << game[i][r] << " | "; } else { cout << game [i][r] << endl; } } } }
//Board function void Board::Gameboard() { int n = 1; int i = 0; int r = 0; for (i = 0; i < 3; i++) { for (r = 0; r < 3; r++) { game[i][r] = '0' + n; n++; } } }
bool Board::WinCheck(char Player, bool gameOver) { for ( int i = 0; i < 3; i++) { if (game[i][0] == game [i][1] && game [i][1] == game [i][2]) gameOver = true; } for ( int i = 0; i < 3; i++) { if (game[0][i] == game [1][i] && game [1][i] == game [2][i]) gameOver = true; } if (game [0][0] == game [1][1] && game [1][1] == game [2][2]) { gameOver = true; } if (game [0][2] == game[1][1] && game [1][1] == game [2][0]) { gameOver = true; } if (gameOver == true) { cout<< "Player " << Player << " wins! "; } return gameOver; }
bool Board::playerTurn(char num, char Player) { int i = 0; int r = 0; for (i = 0; i < 3; i++) { for ( r = 0; r < 3; r++) { if (game[i][r] == num) { game[i][r] = Player; return true; } } } return false; }
bool Board::DrawCheck(bool gameOver) { int n = 1; int i = 0; int r = 0; int counter = 0; for (i = 0; i < 3; i++) { for (r = 0; r < 3; r++) { if (game [i][r] == '0' +n) { counter++; } n++; } } if (counter < 1) { cout << " Draw "; gameOver = true; } return gameOver; } int main() { bool finish = false; bool gameOver = false; bool isValid; int number; char Player, num; srand(time(NULL)); number = rand()%2; if(number == 0) Player = 'X'; else Player = 'O'; cout << "Tic Tac Toe" << endl; Board myGame; myGame.Gameboard(); myGame.display(); do { isValid = false; if(Player == 'X') { Player = 'O'; cout << " Player " << Player << ", your turn:" << endl; cin >> num; isValid = myGame.playerTurn(num, Player); if(!isValid) while (!isValid) { cout << "Invalid selection choose again:" << endl; cin >> num; isValid = myGame.playerTurn(num, Player); } } else { Player = 'X'; cout << " Player " << Player << "'s turn" << endl; while (!isValid) { number = rand()%9+1; num = '0' + number; isValid = myGame.playerTurn(num, Player); } } myGame.display(); gameOver = myGame.WinCheck(Player, gameOver); gameOver = myGame.DrawCheck(gameOver); if(gameOver == true) { cout << "Do you want to Play again? Y/N "; cin >> num; if (num == 'Y' || num == 'y') { cout << endl; myGame.Gameboard(); gameOver = false; } else { cout << "Bye " << endl; finish = true; } } }while(!finish); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started