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
MODS ONLY answer this, I have something for you. I'm reporting this to make it easier
aliya0001 [1]

Answer:

whyyyyy

have  a good day :)

Explanation:

7 0
3 years ago
A 1024-bit message is sent that contains 992 data bits and 32 CRC bits. CRC is computed using the IEEE 802 standardized, 32-degr
Studentka2010 [4]

Answer:

a) Yes

b) Yes

c) Yes

d) No

e) Yes

f) No

Explanation:

a) All single-bit errors  are caught by Cyclic Redundancy Check (CRC) and it produces 100 % of error detection.

b) All double-bit errors for any reasonably long message  are caught by Cyclic Redundancy Check (CRC) during the transmission of 1024 bit. It also produces 100 % of error detection.

c) 5 isolated bit errors  are not caught by Cyclic Redundancy Check (CRC) during the transmission of 1024 bit since CRC may not be able to catch all even numbers of isolated bit errors so it is not even.

It produces nearly 100 % of error detection.

d) All even numbers of isolated bit errors may not be caught by Cyclic Redundancy Check (CRC) during the transmission of 1024 bit. It also produces 100 % of error detection.

e) All burst errors with burst lengths less than or equal to 32 are caught by Cyclic Redundancy Check (CRC) during the transmission of 1024 bit. It also produces 100 % of error detection.

f) A burst error with burst length greater than 32 may not be caught by Cyclic Redundancy Check (CRC) during the transmission of 1024 bit.

Cyclic Redundancy Check (CRC) does not detect the length of error burst which is greater than or equal to r bits.

4 0
3 years ago
Jenny needs to record the names of 30 students, write down the subjects they studied, and note their grades in each subject afte
Marina86 [1]
The correct answer for this question is this one: "She can rename the 2 worksheet by right clicking the sheet tab. After right clicking the mouse, select Rename Sheet and type the preferred name of the sheet."  Hope this helps answer your question and have a nice day ahead.
5 0
3 years ago
Read 2 more answers
Which one of these is NOT a type of printer
rusak2 [61]

Answer:

electric

Explanation:

eletric is not a print ouo

4 0
3 years ago
Read 2 more answers
Write a program that calculates the future value of an investment at a given interest rate for a specified number of years. The
baherus [9]
<h2>Question:</h2>

Write a program that calculates the future value of an investment at a given interest rate for a specified number of years. The formula for the calculation is as follows:

futureValue = investmentAmount * (1 + monthlyInterestRate) ^ years * 12

Note: Allow users to enter the values of the investmentAmount, monthlyInterestRate and years from standard input.

Assume the number of years is always a whole number.

<h2>Answer:</h2>

===================================================

//import the Scanner class

import java.util.Scanner;

//Class header definition

public class FutureValue{

   

    //Main method definition

    public static void main(String []args){

       

       //Create an object of the Scanner class

       Scanner input = new Scanner(System.in);

       

       //Prompt the user to enter the investment amount

       System.out.println("Enter the investment amount");

       

       //Store the input in a double variable called investmentAmount

       double investmentAmount = input.nextDouble();

       

       //Next, prompt the user to enter the interest rate

       System.out.println("Enter the interest rate in percentage");

       

       //Store the input in a double variable called interestRate

       double interestRate = input.nextDouble();

       

       //Convert the interest rate to decimal by multiplying by 0.01

       interestRate *= 0.01;

       

       //Convert the interest rate to monthly interest rate by dividing the

       //result above by 12

       double monthlyInterestRate = interestRate / 12;

       

       //Prompt the user to enter the number of years

       System.out.println("Enter the number of years");

       

       //Store the input in an integer variable called years

       int years = input.nextInt();

       

       //Using the given formula, find the future value

      double futureValue =  investmentAmount * Math.pow(1 + monthlyInterestRate, years * 12);

       

       //Print out the future value

      System.out.println("Future value : " + futureValue);

       

    } //End of main method

     

}  // End of class definition

===================================================

<h2>Sample Output</h2>

===================================================

<em>>> Enter the investment amount </em>

1000

>> <em>Enter the interest rate in percentage </em>

10

>> <em>Enter the number of years </em>

5

Future value : 1645.3089347785854

===================================================

<h2>Explanation:</h2>

The code above has been written using Java. The code contains comments explaining every line of the code. Please go through the comments. The actual lines of code have been written in bold-face to distinguish them from comments. A sample output has also been provided as a result of a run of the code.

8 0
3 years ago
Other questions:
  • Does group policy management matter without a server environment?
    7·1 answer
  • Does anyone watch The Office?
    14·1 answer
  • Suppose the length of each packet is L bits. Also, assume the path from a server to a client includes N links each of rate R (i.
    8·1 answer
  • A characteristic of a 3D model that a 2D model does not have is:
    8·1 answer
  • Set methods are also commonly called _____ methods, and get methods are also commonly called _____ methods.
    6·1 answer
  • An ____ is an object that produces each element of a container, such as a linked list, one element at a time.
    13·2 answers
  • Fill up the blank:- The picture that graphically represents the items you use in Windows is called a/an .......
    5·1 answer
  • How do I add decimals in python?
    6·1 answer
  • In this lab, you complete a C++ program that uses an array to store data for the village of Marengo.
    8·1 answer
  • Class Main {
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!