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
What is the empty space inside the character 'O' called?
mariarad [96]

Answer:

In typography, a counter is the area of a letter that is entirely or partially enclosed by a letter form or a symbol (the counter-space/the hole of). The stroke that creates such a space is known as a "bowl".

7 0
2 years ago
The superclass Calculator contains: a protected double instance variable, accumulator, that contains the current value of the ca
yawa3891 [41]

Answer:

The following program are written in JAVA Programming Language.

//inherit the Calculator class

public class CalculatorWithMemory extends Calculator {  

   private double memory = 0.0;    //initialize double type variable

   public void save() {   //define function

       memory = accumulator;    

   }

   public void recall() {   //define function

       accumulator = memory;

   }

   public void clearMemory() {   //define function

       memory = 0;     //initialize the value

   }

   public double getMemory() {  

       return memory;    // return the value in "memory" variable

   }

}

Explanation:

Here, we inherit the property of the "Calculator" class.

Then, we define the private double type variable "memory" and assign value 0.0.

Then we set the function "save()" inside it we assign the value of "memory" in "accumulator" and close the function.

Then, we define the function "recall()" inside it we assign the value of "accumulator" in "memory" and close the function.

Then we set the function "clearMemory()" inside it we assign "memory" to 0.

After all, we set the double type function "getMemory()" inside we return the value of the "memory".

7 0
3 years ago
Online databases allow you access to material you may not be able to find when using popular search engines like Google. Select
Anna007 [38]

Answer:

True

Explanation:

6 0
3 years ago
If a user's input string matches a known text message abbreviation, output the unabbreviated form, else output: Unknown. Support
Molodets [167]

Answer:

Here is the JAVA program. I  have added a few more abbreviations apart from LOL and IDK.

import java.util.Scanner; // to take input from user

public class MessageAbbreviation {

public static void main(String[] args) { //start of main() function body

   Scanner input = new Scanner(System.in); // create object of Scanner

       String abbreviation= ""; //stores the text message abbreviation

/* In the following lines the abbreviation string type variables contain the un-abbreviated forms */

           String LOL = "laughing out loud";

           String IDK = "i don't know";

           String BFF = "best friends forever";

           String IMHO = "in my humble opinion";

           String TMI = "too much information";

           String TYT = "take your time";

           String IDC = "I don't care";

           String FYI = "for your information";

           String BTW = "by the way";

           String OMG = "oh my God";

//prompts the user to enter an abbreviation for example LOL

           System.out.println("Input an abbreviation:" + " ");

           if(input.hasNext()) { // reads the abbreviation form the user

           abbreviation= input.next(); }

// scans and reads the abbreviation from user in the abbreviation variable

/*switch statement checks the input abbreviation with the cases and prints the un abbreviated form in output accordingly. */

    switch(abbreviation) {

       case "LOL" : System.out.println(LOL);

                    break;

       case "IDK" : System.out.println(IDK);

                    break;

       case "BFF" : System.out.println(BFF);

                    break;

       case "IMHO": System.out.println(IMHO);

                     break;

       case "TMI": System.out.println(TMI);

                     break;

       case "TYT": System.out.println(TYT);

                     break;

       case "IDC": System.out.println(IDC);

                     break;

       case "FYI": System.out.println(FYI);

                     break;

       case "BTW": System.out.println(BTW);

                     break;

       case "OMG": System.out.println(OMG);

                     break;

                     

       default    : System.out.println("Unknown"); } } }

Explanation:

The program first prompts the user to enter an abbreviation. Lets say the user enters LOL. Now the switch statement has some cases which are the conditions that are checked against the input abbreviation. For example if the user enters LOL then the following statement is executed:

case "LOL" : System.out.println(LOL);

This means if the case is LOL then the message ( un abbreviated form) stored in the LOL variable is displayed on output screen So the output is laughing out loud.

Anything else entered other than the given abbreviations will display the message: Unknown

The output is attached as screen shot.

5 0
3 years ago
Read 2 more answers
Fix the regular expression used in the rearrange_name function so that it can match middle names, middle initials, as well as do
marysya [2.9K]

Answer:

name = re.search(r"^([\w \.-]), ([\w \.-])$", list_name)

Explanation:

Regular expression is used to simplify the mode in which items in a data structure are for. It uses wildcards as shortcuts.

The python module for regular expression is 're'. The import statement is used to get the module and the search() method of the module is used to get the one matching item while the findall() method is used to get a list of all the matching items in a data structure.

7 0
2 years ago
Other questions:
  • What weight pencil is recommended for darkening lines and for lettering? *
    7·2 answers
  • Which access control principle specifies that no unnecessary access to data exists by regulating members so they can perform onl
    11·1 answer
  • Jean has created a database to track all of this month’s family meals. To find an entry for lasagna, what would should Jean do?
    6·1 answer
  • It is important to name your files in a variety of formats. true or false
    15·2 answers
  • 5. Question<br> The control flag that isn't really in use by modern networks is the<br> flag.
    15·1 answer
  • Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A
    13·1 answer
  • When a collection of honeypots connects several honeypot systems on a subnet, it may be called a(n) honeynet
    9·1 answer
  • System. Construct an ER diagram for keeping records for exam section of a college.​
    10·1 answer
  • You are going to create an Arduino sketch where you have two push buttons, one piezo, one
    10·1 answer
  • 2. Which pattern microphone should Jennifer take to the press conference? Jennifer is a journalist with one of the leading newsp
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!