Question

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.
  • The name of the computer player will be Socrates.
  • The human player must enter a name.
  • The player can quit the game at any time by entering the letter z or Z.
  • When the game is over, display the results for each player

I already have both the computer vs human and player vs player coded as well as determining who the winner is and the user inputting their name.

I just need help displaying the results as well as keeping track of the score and having the winner always being X on the next round.

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

Hi,

Thanks for A2A . Actually i already tested the below code in C , in turbo C ++ and codeblock. You can take help from it andimplement it in C++ . Let me know if not , i ll give you its C++ version .

#include<stdio.h>
#include<conio.h>

char a[9]={'1','2','3','4','5','6','7','8','9'};//globally declaring so that all functions have access to i

void check (char,char);
void gameShow()
{
printf("\n\t\t\t\t TIC-TAC-TOE \n");
}
void input_symbol()
{
printf("\n Player 1 symbol : x ");
printf("\n Player 2 symbol : 0 ");
}

void start()
{

char pa;
printf(" \n Who wants to start first - player 1 or 2 ?-");
scanf("%d",&pa);
}
void play()
{
char p,s;

flu:
printf("\n Enter the position and symbol of the player :");
fflush(stdin);
scanf("%c",&p);
fflush(stdin);
scanf("%c",&s);
/*if((s != '0') || (s != 'x'))
{
printf("%d",i);
printf("Wrong value entered, enter correct symbol again\n");
goto flu;
}
*/
check(p,s);
}

void check (char P, char S)
{
int i ;
for(i=0;i<=8;i++)
{
if (a[i]==P)
{
a[i]=S;

}
}
}

void show()
{
printf("\n\n\t\t\t\t-----|-----|-----\n");
printf("\t\t\t\t %c | %c | %c \n",a[0],a[1],a[2]);
printf("\t\t\t\t-----|-----|-----\n");
printf("\t\t\t\t %c | %c | %c \n",a[3],a[4],a[5]);
printf("\t\t\t\t-----|-----|-----\n");
printf("\t\t\t\t %c | %c | %c \n",a[6],a[7],a[8]);
printf("\t\t\t\t-----|-----|-----\n");

}

int winner ()
{
if(( a[0]=='x' && a[1]=='x' && a[2]=='x') || ( a[3]=='x' && a[4]=='x' && a[5]=='x' ) || ( a[6]=='x' && a[7]=='x' && a[8]=='x') || ( a[0]=='x' && a[3]=='x' && a[6]=='x' ) || ( a[1]=='x' && a[4]=='x' && a[7]=='x') || ( a[2]=='x' && a[5]=='x' && a[8]=='x')|| ( a[0]=='x' && a[4]=='x' && a[8]=='x') || ( a[2]=='x' && a[4]=='x' && a[6]=='x'))
{
return 100;

}
else if(( a[0]=='0' && a[1]=='0' && a[2]=='0') || ( a[3]=='0' && a[4]=='0' && a[5]=='0' ) || ( a[6]=='0' && a[7]=='0' && a[8]=='0') || ( a[0]=='0' && a[3]=='0' && a[6]=='0' ) || ( a[1]=='0' && a[4]=='0' && a[7]=='0') || ( a[2]=='0' && a[5]=='0' && a[8]=='0')|| ( a[0]=='0' && a[4]=='0' && a[8]=='0') || ( a[2]=='0' && a[4]=='0' && a[6]=='0'))
{
return 200;

}
}
int main()
{

//char a, b ;

int k,i;
clrscr();
gameShow();
show();
input_symbol();
start();
play();

label:
clrscr();
show();
play();
k=winner();
clrscr();
show();
for(i=0;i<=8;i++)
{ if((a[i] == 'x') || (a[i]=='0'))
{
int count=0;
count ++;

if(count==8)
{
printf( " Match is DRAW" );
getch();
}
}
printf( "Match continues....\n");
}
if(k==100)
{
printf("++++++Player 1 is winner++++++ ");
}
else if(k==200)
{
printf("++++++Player 2 is winner++++++ ");
}
else
goto label;
//printf("Enter two character values:");
//scanf("%c",&a);
//fflush(stdin);// this will flush the buffer before we enter the values , we were not able to get the second value becz the scanf doesnt take input directly from keyboard , if buffer is full , it will take values already present in buffer.
//scanf("%c",&b);
//printf("the values are : a=%c b=%c ", a, b);
getch();
}

This progarm works well , untilthe game is draw . Need to implement that part .Hope it helps .

Thanks

Add a comment
Know the answer?
Add Answer to:
I need to create a Tic Tac Toe program in C++. These are the requirements Write...
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
  • 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

  • Please make this into one class. JAVA Please: Tic Tac Toe class - Write a fifth...

    Please make this into one class. JAVA Please: Tic Tac Toe class - Write a fifth game program that can play Tic Tac Toe. This game displays the lines of the Tic Tac Toe game and prompts the player to choose a move. The move is recorded on the screen and the computer picks his move from those that are left. The player then picks his next move. The program should allow only legal moves. The program should indicate when...

  • (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...

  • (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 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...

  • 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...

  • (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...

  • 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)    ...

  • Assignment C++: Rock-Scissor-Paper & Tic-Tac-Toe i need you to write two different program for RSP and...

    Assignment C++: Rock-Scissor-Paper & Tic-Tac-Toe i need you to write two different program for RSP and TTT: R-S-P Requirement: - write one program that mimics the Rock-Scissor-Paper game. This program will ask user to enter an input (out of R-S-P), and the computer will randomly pick one and print out the result (user wins / computer wins). - User's input along with computer's random pick will be encoded in the following format: -- user's rock: 10 -- user's scissor: 20...

  • You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people....

    You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people. One player is X and the other player is O. Players take turns placing their X or O. If a player gets three of his/her marks on the board in a row, column, or diagonal, he/she wins. When the board fills up with neither player winning, the game ends in a draw 1) Player 1 VS Player 2: Two players are playing the game...

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