1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Otrada [13]
3 years ago
14

Programming assignment (100 pts): In the C++ programming language write a program capable of playing Tic-Tac-Toe against the use

r. Your program should use OOP concepts in its design. You can use ASCII art to generate and display the 3x3 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 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.
Computers and Technology
1 answer:
STALIN [3.7K]3 years ago
8 0

Answer:

#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;

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;

}

You might be interested in
Which of the following contributes to your active digital footprint
VLD [36.1K]
Personal information intentionally put on a secure website
3 0
3 years ago
Read 2 more answers
Following a company on likedin is most similar to
san4es73 [151]
Liking a company on Facebook
6 0
3 years ago
If a driving who is under the age of 21 is stopping by a law enforcement officer and shown to here a bal of .02 or greater, he o
Yanka [14]
Six months.........ur welcome
4 0
3 years ago
Does anybody play nba2k21 On ps4
Vera_Pavlovna [14]

Answer:

yeah but I gotta re-download it

3 0
3 years ago
Read 2 more answers
The only way to print a photo is on a special glossy paper using professional printing services in a store
DaniilM [7]

i believe this is true


8 0
3 years ago
Read 2 more answers
Other questions:
  • What term refers to a piece of software that interfaces with the hardware on your computer?
    10·2 answers
  • Word 2013 in order to share a document online you must first
    10·1 answer
  • What type of social engineering attack uses email to direct you to their their website where they claim you need to update/valid
    6·2 answers
  • sqrt is being defined as a variable name, how do I get its math function to... well... function? (Python)
    11·1 answer
  • What is Human Dignity
    12·1 answer
  • When troubleshooting firewalls, you should never attempt to repeat the problem because you could do more damage. T/F
    6·1 answer
  • An alteration threat violates information integrity. <br> a. True <br> b. False
    13·1 answer
  • What 2 important components are found on a motherboard? (2 points each)
    13·1 answer
  • Which operating system (OS) is used to run your laptop?
    15·2 answers
  • Which of the following is classified as a workgroup information system?
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!