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:
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.
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
I need to create a Tic Tac Toe program in C++. These are the requirements Write...
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 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. 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...
How to write a (c++ program) tic tac toe code where its the computer against a human. and the computer always wins. a basic c++ code program using gaming theory arrays <stdio.h> computer plays tic tac toe against a user. computer always when or the game is a draw.
(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 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 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 (arrays and multidimensional arrays) C++ Write an interactive program that plays tic-tac-toe. Represent the board as a 3 X 3 character array. Initialize the array to blanks and ask each player in turn to input a position. The first player's position is marked on the board with a O and the second player's position is marked with an X. Continue the process until a player wins or the game is a draw. To win, a player must have 3...
(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...
Objective: To write a program to allow a game of Tic Tac Toe to be played, and to determine when the game is over Complete the TicTacToe program below. It will allow for a full game of Tic Tac Toe between two players, and it will tell you who won or if the game is over with no winner. Details: The TicTacToe board is stored in a 3x3 multidimensional list of characters containing spaces at the start of the game...