#include <iostream> #include <ctime> #include <string> using namespace std; clas
ID: 3701055 • 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 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 computer or user. 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 x1.010 1.0 1.o
Explanation / Answer
Here is Code which work and computer always wins, or game draws. It is using artificial inteligence. Code is rewritten from scratch.
#include<iostream>
using namespace std;
// tic tac toe with computer, atificial intelligency
void display_board(int *b) {
int k=0;
cout<<"==============="<<endl;
for(int p=0;p<3;p++){
for(int j=0;j<3;j++){
cout<<"| ";
if(b[k] == 0){
cout<<k+1<<" |";
}
else{
if(b[k]==-1){
cout<<"X |";
}
else{
cout<<"O |";
}
}
k++;
}
cout<<" ==============="<<endl;
}
}
int win(const int *mark) {
//determines if a player has won, returns 0 otherwise.
unsigned wins[8][3] = {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};
int p;
for(p = 0; p < 8; ++p) {
if(mark[wins[p][0]] != 0 &&
mark[wins[p][0]] == mark[wins[p][1]] && mark[wins[p][1]] == mark[wins[p][2]])
return mark[wins[p][2]];
}
return 0;
}
int minimax(int *mark, int player) {
int winner = win(mark);
if(winner != 0)
return winner*player;
int move = -1;
int point = -2;
for(int p = 0; p < 9; p++) {
if(mark[p] == 0) {
mark[p] = player;
int thisScore = -minimax(mark, player*-1);
if(thisScore > point) {
point = thisScore;
move = p;
}
mark[p] = 0;//Reset mark after try
}
}
if(move == -1)
return 0;
return point;
}
int computerMove(int *mark) {
int move = -1;
int point = -2;
for(int p = 0; p < 9; ++p) {
if(mark[p] == 0) {
mark[p] = 1;
int tempScore = -minimax(mark, -1);
mark[p] = 0;
if(tempScore > point) {
point = tempScore;
move = p;
}
}
}
//returns a point based on minimax tree at a given node.
return move;
}
int main(){
cout <<"~~~~~~~~~~~~~Tic Tac Toe~~~~~~~~~~~~~ ";
cout <<" mark: ";
cout << "---------------" << endl;
cout << "| 1 || 2 || 3 |" << endl;
cout << "--------------" << endl;
cout << "| 4 || 5 || 6 |" << endl;
cout << "---------------" << endl;
cout << "| 7 || 8 || 9 |" << endl;
cout << "---------------" << endl ;
cout<<"Only legal moves are the numbers you see on the mark ";
int mark[9]={0};
int moves=0,k;
//Player = -1 ; Computer = 1
while(moves < 9){
int mv;
cout<<"HUMAN MOVE: ";
cin>>mv;
if(mark[mv-1]==0){
mark[mv-1]=-1;
moves++;
cout<<" Game after your move: ";
display_board(mark);
if(win(mark)==0){
k=computerMove(mark);
mark[k]=1;
cout<<" Game after computer's move: ";
display_board(mark);
moves++;
if (win(mark) != 0){
break;
}
}
else
break;
}
else{
cout<<"Not a valid move, Try again !! ";
}
}
switch(win(mark)) {
case 0:
cout<<"Its a draw. You played good but not able to win yet ";
break;
case 1:
cout<<"You lose. ";
break;
case -1:
cout<<"Haha dont worry what is written here not gonna output never because you can never win ";
break;
}
return 0;
}
OUTPUT OF THE CODE:
dps@machine:~/Desktop$ ./2tic_tac
~~~~~~~~~~~~~Tic Tac Toe~~~~~~~~~~~~~
mark:
---------------
| 1 || 2 || 3 |
--------------
| 4 || 5 || 6 |
---------------
| 7 || 8 || 9 |
---------------
Only legal moves are the numbers you see on the mark
HUMAN MOVE: 1
Game after your move:
===============
| X || 2 || 3 |
===============
| 4 || 5 || 6 |
===============
| 7 || 8 || 9 |
===============
Game after computer's move:
===============
| X || 2 || 3 |
===============
| 4 || O || 6 |
===============
| 7 || 8 || 9 |
===============
HUMAN MOVE: 9
Game after your move:
===============
| X || 2 || 3 |
===============
| 4 || O || 6 |
===============
| 7 || 8 || X |
===============
Game after computer's move:
===============
| X || O || 3 |
===============
| 4 || O || 6 |
===============
| 7 || 8 || X |
===============
HUMAN MOVE: 8
Game after your move:
===============
| X || O || 3 |
===============
| 4 || O || 6 |
===============
| 7 || X || X |
===============
Game after computer's move:
===============
| X || O || 3 |
===============
| 4 || O || 6 |
===============
| O || X || X |
===============
HUMAN MOVE: 3
Game after your move:
===============
| X || O || X |
===============
| 4 || O || 6 |
===============
| O || X || X |
===============
Game after computer's move:
===============
| X || O || X |
===============
| 4 || O || O |
===============
| O || X || X |
===============
HUMAN MOVE: 4
Game after your move:
===============
| X || O || X |
===============
| X || O || O |
===============
| O || X || X |
===============
Game after computer's move:
===============
| X || O || X |
===============
| X || O || O |
===============
| O || X || X |
===============
Its a draw. You played good but not able to win yet
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.