#include <iostream> #include <ctime> #include <string> using namespace std; clas
ID: 3699380 • Letter: #
Question
#include <iostream> #include <ctime> #include <string> using namespace std; class Board { private: char squares[3][3]; public: Board() {} void PB(); void BG(); void UT (char num, char Player); bool CW (char Player, bool gameOver); bool CD(bool gameOver); };
void Board::BG() { 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; n++; } } } void Board::PB() { 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:: UT (char num, char Player) { int i = 0; int j = 0; bool OHCHIT = true; for( i = 0; i < 3; i++ ) { for( j = 0; j < 3; j++ ) { if(squares[i][j] == num) {
squares[i][j] = Player; OHCHIT = false; } } } if(OHCHIT == true) { cout << "Only mark spots that are open! "; } } bool Board:: CW (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! "; cout << "-----------------------" << endl; cout << "| NICE JOB " << Player << " | "; cout << "-----------------------" << endl << endl; } return GameOver; } bool Board:: CD(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 << "Draw "; GameOver = true; } return GameOver; }
int main() { bool finish = false, GameOver = false,isValid; char Player = 'O', num; int number; srand(time(NULL)); number = rand()%2; if(number == 0) Player = 'X'; else Player = 'O'; cout << "" << endl; cout << "-=Tic-Tac-Toe=- "; cout << "-------------"<< endl; Board TicTac; TicTac.BG(); TicTac.PB(); do { if( Player == 'X' ) { Player = 'O'; } else { Player = 'X'; } TicTac.PB(); cout << " Player "" << Player << "", it's your turn: "; cin >> num; cout << " "; TicTac.UT (num, Player); GameOver = TicTac.CW (Player, GameOver); GameOver = TicTac.CD(GameOver); if(GameOver == true) { cout << "Thank you for playing!"; finish = true; } } while(!finish);
} #include <iostream> #include <ctime> #include <string> using namespace std; class Board { private: char squares[3][3]; public: Board() {} void PB(); void BG(); void UT (char num, char Player); bool CW (char Player, bool gameOver); bool CD(bool gameOver); };
void Board::BG() { 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; n++; } } } void Board::PB() { 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:: UT (char num, char Player) { int i = 0; int j = 0; bool OHCHIT = true; for( i = 0; i < 3; i++ ) { for( j = 0; j < 3; j++ ) { if(squares[i][j] == num) {
squares[i][j] = Player; OHCHIT = false; } } } if(OHCHIT == true) { cout << "Only mark spots that are open! "; } } bool Board:: CW (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! "; cout << "-----------------------" << endl; cout << "| NICE JOB " << Player << " | "; cout << "-----------------------" << endl << endl; } return GameOver; } bool Board:: CD(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 << "Draw "; GameOver = true; } return GameOver; }
int main() { bool finish = false, GameOver = false,isValid; char Player = 'O', num; int number; srand(time(NULL)); number = rand()%2; if(number == 0) Player = 'X'; else Player = 'O'; cout << "" << endl; cout << "-=Tic-Tac-Toe=- "; cout << "-------------"<< endl; Board TicTac; TicTac.BG(); TicTac.PB(); do { if( Player == 'X' ) { Player = 'O'; } else { Player = 'X'; } TicTac.PB(); cout << " Player "" << Player << "", it's your turn: "; cin >> num; cout << " "; TicTac.UT (num, Player); GameOver = TicTac.CW (Player, GameOver); GameOver = TicTac.CD(GameOver); if(GameOver == true) { cout << "Thank you for playing!"; finish = true; } } while(!finish);
}
Programming assignment (100 pts): 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 can use ASCII art to generate and display the 3x3x3 playing board. The program should randomly decide who goes first computer or user 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. Lab #9 Tic-Tac-Toe class. You Your program should know and inform the 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 Blank Board Game in Progress X10_1.0 xIxX 010 x 0-1 I-
Explanation / Answer
Code
#include <iostream>
using namespace std;
int wincheck( char[]);
void boards( char[]);
int main() //main method
{
char sqr[10] = {'o','1','2','3','4','5','6','7','8','9'}; //declaring variables
int player = 1,i,choice; //declaring variables
char flag; //declaring variables
do //do while loop
{
boards(sqr); //calling board f tictactoe
if(player%2==1)
player=1;
else
player=2;
if(player==2) //player 2
{
cout << "Players " << player<<endl;
cout<<"Press Enter for CPU"<<endl;
choice=rand()%9;
flag='O';
int turn=1;
int placed=0;
while(placed==0)
{
if (choice == 1 && sqr[1] == '1'){
sqr[1] = flag;
placed=1;
}
else if (choice == 2 && sqr[2] == '2'){
sqr[2] = flag;
placed=1;
}
else if (choice == 3 && sqr[3] == '3'){
sqr[3] = flag;
placed=1;
}
else if (choice == 4 && sqr[4] == '4'){
placed=1;
sqr[4] = flag;
}
else if (choice == 5 && sqr[5] == '5'){
sqr[5] = flag;
placed=1;
}
else if (choice == 6 && sqr[6] == '6'){
sqr[6] = flag;
placed=1;
}
else if (choice == 7 && sqr[7] == '7'){
sqr[7] = flag;
placed=1;
}
else if (choice == 8 && sqr[8] == '8'){
sqr[8] = flag;
placed=1;
}
else if (choice == 9 && sqr[9] == '9'){
sqr[9] = flag;
placed=1;
}
else
{
choice=rand()%9;
}
}
i=wincheck(sqr);
player++;
boards(sqr);
}
// player 1
else if(player==1)
{
cout << "Players " << player << ", enter a number: ";
cin >> choice;
flag='X';
if (choice == 1 && sqr[1] == '1')
sqr[1] = flag;
else if (choice == 2 && sqr[2] == '2')
sqr[2] = flag;
else if (choice == 3 && sqr[3] == '3')
sqr[3] = flag;
else if (choice == 4 && sqr[4] == '4')
sqr[4] = flag;
else if (choice == 5 && sqr[5] == '5')
sqr[5] = flag;
else if (choice == 6 && sqr[6] == '6')
sqr[6] = flag;
else if (choice == 7 && sqr[7] == '7')
sqr[7] = flag;
else if (choice == 8 && sqr[8] == '8')
sqr[8] = flag;
else if (choice == 9 && sqr[9] == '9')
sqr[9] = flag;
else
{
cout<<"Invalid move ";
player--;
}
i=wincheck(sqr);
player++;
}
}while(i==-1);
boards(sqr);
if(i==1)
cout<<"Congratulation! Player "<<--player<<" win ";
else
cout<<" OOps! Game draw";
}
void boards(char sqr[])
{
system("cls");
cout << " Tic Tac Toe ";
cout << "Player 1 (X) - Player 2 (O)" << endl<< endl;
cout << endl;
cout << " | | " << endl;
cout << " " << sqr[1] << " | " << sqr[2]<< " | " << sqr[3] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << sqr[4] << " | " << sqr[5]<< " | " << sqr[6] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << sqr[7] << " | " << sqr[8]<< " | " << sqr[9] << endl;
cout << " | | " << endl << endl;
}
int wincheck(char sqr[])
{
if (sqr[1] == sqr[2] && sqr[2] == sqr[3])
return 1;
else if (sqr[4] == sqr[5] && sqr[5] == sqr[6])
return 1;
else if (sqr[7] == sqr[8] && sqr[8] == sqr[9])
return 1;
else if (sqr[1] == sqr[4] && sqr[4] == sqr[7])
return 1;
else if (sqr[2] == sqr[5] && sqr[5] == sqr[8])
return 1;
else if (sqr[3] == sqr[6] && sqr[6] == sqr[9])
return 1;
else if (sqr[1] == sqr[5] && sqr[5] == sqr[9])
return 1;
else if (sqr[3] == sqr[5] && sqr[5] == sqr[7])
return 1;
else if (sqr[1] != '1' && sqr[2] != '2' && sqr[3] != '3'
&& sqr[4] != '4' && sqr[5] != '5' && sqr[6] != '6'
&& sqr[7] != '7' && sqr[8] != '8' && sqr[9] != '9')
return 0;
else
return -1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.