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
How to write conclusion of hard disk​
sergeinik [125]

Answer:

el disco duro es un componte de almacenamiento de una pc, su uso es de guardar archivos también como el sistema operativo de ejemplo :linux, window ,mac etc

Explanation:

7 0
2 years ago
LIST THE 7 BEST COMPUTER SCIENCE MOVIES.
Serggg [28]

The Pirates of Silicon Valley.

Tron

Hackers

Antitrust

The Social Network

The Internship

The Imitation Game

5 0
3 years ago
A cookie is.... a. an illegal use of information about a customer b. a feature of a Web site designed to attract children c. a f
Elodia [21]

Answer:

C. A file that a Web site stores on a visitor's computer

Explanation:

Have a nice day! :)

3 0
2 years ago
What's the difference between an exe file and an msi file? check all that apply.
motikmotik

An EXE file is an executable that may have an MSI file as one its help; An executable or EXE file can "wrap" an MSI file, which is used by the Windows Installer to guide the building process of an application.

MSI files are utilized by the Windows Installer to control how your application is installed.

<h3>What is the difference between an EXE file and MSI file?</h3>

The main difference between the two attachments is their purpose. EXE is used mainly to indicate that the file is an executable one.

In comparison, MSI indicates that the file is a Windows installer. While an MSI is operated only with installers, this is not the case with EXE.

To learn more about EXE file, refer

brainly.com/question/28146265

#SPJ4

7 0
1 year ago
What is full form of RAM??? ​
stepladder [879]

Answer:

Random Access Memory, it's used to store working data and machine codes

Explanation:

Hope it helped !
Adriel

7 0
2 years ago
Read 2 more answers
Other questions:
  • 50+ POINTS AND BRAIN IF CORRECT Chris is working with other employees on a worksheet. The other employees have made comments, bu
    8·1 answer
  • Explain how Deep Packet Inspection works (DPI). How is this technology beneficial to Perimeter Security? Lastly, describe a scen
    6·1 answer
  • Enter the number 2568 into the box below
    14·1 answer
  • Please help!! Even if you help a little I will be very thankful!
    7·1 answer
  • A server provides the necessary IP configuration to your network host so the host can communicate on the network. If this servic
    7·1 answer
  • Seth is considering advertising his business using paid search results.What do you think makes paid search advertising so effect
    11·2 answers
  • I have $80 and I want a smartphone that you can call for free what should I get
    14·2 answers
  • knowledge contributes vitally to the development of __________ memory, or recollections of personally experienced events that oc
    12·1 answer
  • Which statement about intellectual property is true? (CSI-7.6) Group of answer choices It is okay to use code you find on the in
    14·1 answer
  • If you anticipate running a particular query often, you can improve overall performance by saving the query in a special file ca
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!