Question

(C++) Please create a tic tac toe game. Must write a Player class to store each...

(C++) Please create a tic tac toe game. Must write a Player class to store each players’ information (name and score) and to update their information (increase their score if appropriate) and to retrieve their information (name and score).The players must be called by name during the game. The turns alternate starting with player 1 in the first move of round 1. Whoever plays last in a round will play second in the next round (assuming there is one).The rows must be numbered 1 to 3, and also the columns. The user will always enter the position of choice by entering two numbers with spaces between them.You can assume the user always enters a valid pair of numbers (within the 1-3 range) and will always select an open position (but it would be a good exercise to write code to test against this).

1. Write the Player class first. Make sure you can create instances of the class, call its member functions, and that those member functions work correctly.

2. Using a std::vector variable, create a representation of an empty game board with only the numbers 1-3, pipes, and dashes, but no x’s or o’s.

Each row will be one of the std::strings stored in the vector.

3. Write a loop that will print the empty board.

4. Write code that will prompt a user for a desired coordinate and update the variable to have an ’x’ at that position. Check it works by printing the board.Sample output:

This is a Tic Tac Toe game!

Please enter the name of player 1: [USER ENTERS A NAME]

Thanks. Please enter the name of player 2: [USER ENTERS A NAME]

How many rounds would you like to play? [USER ENTERS A NUMBER]

Let the game begin!

//This will be repeated while the number of rounds played has not exceeded the number of rounds chosen

//This will be repeated each round until one of the players has 3 in a row, 3 down, 3 in a diagonal, or the board is full.

//Board is displayed in a table format with pipes (|) and dashes (-), with 1 2 3 indicating row and column, and with player 1 represented by x’s and player 2 by o’s. See display*** It is [THE CORRECT PLAYER’S TURN].

Where would you like to play? Enter your row position and column position: row col: [USER ENTERS ROW NUMBER SPACE A COLUMN NUMBER]

//After the selection, the user’s token should be placed at the square and the board should be displayed again

//The board should be displayed after the round is over and a message (however you wish to convey the message) should say who won the round or state there was a draw if all 9 squares were filled but no one won; it should then give a summary of the current number of points each player has. The winning player should have their score increased by 1 and if there is a draw then neither player is awarded a point.

//When all the rounds have been played, there should be a final recap of the number of points and who the winner was (or state there was a draw if there was a tie in the scores)*

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

using namespace std;

char square[10] = {'o','1','2','3','4','5','6','7','8','9'};

int check_win();

void board();

int main()

{

int user = 1,count,choice;

char marks;

do

{

board();

user=(user%2)?1:2;

cout << "user " << user<< ", enter a number: ";

cin >> choice;

marks=(user == 1) ? 'X' : 'O';

if (choice == 1 && square[1] == '1')

square[1] = marks;

else if (choice == 2 && square[2] == '2')

square[2] = marks;

else if (choice == 3 && square[3] == '3')

square[3] = marks;

else if (choice == 4 && square[4] == '4')

square[4] = marks;

else if (choice == 5 && square[5] == '5')

square[5] = marks;

else if (choice == 6 && square[6] == '6')

square[6] = marks;

else if (choice == 7 && square[7] == '7')

square[7] = marks;

else if (choice == 8 && square[8] == '8')

square[8] = marks;

else if (choice == 9 && square[9] == '9')

square[9] = marks;

else

{

cout<<"Invalid move ";

user--;

cin.ignore();

cin.get();

}

count=checkwin();

user++;

}while(count==-1);

board();

if(count==1)

cout<<"==>\auser "<<--user<<" win ";

else

cout<<"==>\aGame draw";

cin.ignore();

cin.get();

return 0;

}

int check_win()

{

if (square[1] == square[2] && square[2] == square[3])

return 1;

else if (square[4] == square[5] && square[5] == square[6])

return 1;

else if (square[7] == square[8] && square[8] == square[9])

return 1;

else if (square[1] == square[4] && square[4] == square[7])

return 1;

else if (square[2] == square[5] && square[5] == square[8])

return 1;

else if (square[3] == square[6] && square[6] == square[9])

return 1;

else if (square[1] == square[5] && square[5] == square[9])

return 1;

else if (square[3] == square[5] && square[5] == square[7])

return 1;

else if (square[1] != '1' && square[2] != '2' && square[3] != '3'

&& square[4] != '4' && square[5] != '5' && square[6] != '6'

&& square[7] != '7' && square[8] != '8' && square[9] != '9')

return 0;

else

return -1;

}

void board()

{

system("cls");

cout << "\n\n\tTic Tac Toe\n\n";

cout << "user1 (X) - user 2 (O)" << endl << endl;

cout << endl;

cout << " | | " << endl;

cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;

cout << "_____|_____|_____" << endl;

cout << " | | " << endl;

cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;

cout << " | | " << endl << endl;

}

Hope your query is solved. Kindly comment below for any futher doubts. happy learning :)

Add a comment
Know the answer?
Add Answer to:
(C++) Please create a tic tac toe game. Must write a Player class to store each...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program to Simulate a game of tic tac toe in c#

    Write a program to Simulate a game of tic tac toe. A game of tic tac toe has two players. A Player class is required to store /represent information about each player. The UML diagram is given below.Player-name: string-symbol :charPlayer (name:string,symbol:char)getName():stringgetSymbol():chargetInfo():string The tic tac toe board will be represented by a two dimensional array of size 3 by 3 characters. At the start of the game each cell is empty (must be set to the underscore character ‘_’). Program flow:1)    ...

  • 18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use di...

    18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Write . Displays the contents of the board array. . Allows player 1 to select a location on the board for an X. The program should ask the...

  • (Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe....

    (Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 two-dimensional array. Use an enumeration to represent the value in each cell of the array. The enumeration’s constants should be named X, O and EMPTY (for a position that does not contain an X or an O). The constructor should initialize the board elements to EMPTY. Allow two human players. Wherever the first player moves, place an X...

  • I need screenshots for this solution done in Flowgorithm. Thank you. Tic-Tac-Toe Game Design a program...

    I need screenshots for this solution done in Flowgorithm. Thank you. Tic-Tac-Toe Game Design a program that allows two players to play a game of tic-tac-toe. Use a two- dimensional String array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: a. Displays the contents of the board array. b. Allows player 1 to select a location...

  • (Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an...

    (Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an available cell in a grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has achieved a win. Create...

  • Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use...

    Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user to enter...

  • (Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play the...

    (Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play the game of Tic-Tac-Toe. The class contains a private 3-by-3 rectangular array of integers. The constructor should initialize the empty board to all 0s. Allow two human players. Wherever the first player moves, place a 1 in the specified square, and place a 2 wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game has...

  • I need to create a Tic Tac Toe program in C++. These are the requirements Write...

    I need to create a Tic Tac Toe program in C++. These are the requirements Write a program that allows the computer to play TicTacToe against a human player or allow two human players to play one another. Implement the following conditions: The player that wins the current game goes first in the next round, and their symbol is X. The other player will be O. Keep track of the number of games played, wins, and draws for each player....

  • Using C Programming: (use printf, scanf) 18. Tic-Tac-Toc Game Write a program that allows two players...

    Using C Programming: (use printf, scanf) 18. Tic-Tac-Toc Game Write a program that allows two players to play a game of tic-tac-toc. Use a two- dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that Displays the contents of the board array Allows player 1 to select a location on the board for an X. The program should...

  • 1. Use Turtle Graphics to create a tic tac toe game in Python. Write a Python program that allows for one player vs computer to play tic tac toe game, without using turtle.turtle

    1. Use Turtle Graphics to create a tic tac toe game in Python. Write a Python program that allows for one player vs computer to play tic tac toe game, without using turtle.turtle

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT