Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

help me fix this.AFTER THE GAME IS PLAYED the restart game option does not work like after the winner is declared it is suppose to

help me fix this.AFTER THE GAME IS PLAYED the restart game option does not work like after the winner is declared it is suppose to say press 1 to restart but instead it says should have pressed 1 to play again without giving me the option to press1

#include

#include

#include

#include

char x, o;

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

char u1[50], u2[50];

void board();

void rules();

int checkforwin();

void vsPlayer(FILE *p, int player, int choice, char symbol, int score)

{

read:

p = fopen("score.txt", "a+");

printf(" Enter name of player1: ");

scanf("%s", u1);

fprintf(p, " %s", u1);

printf("Enter name of player2: ");

scanf("%s", u2);

fprintf(p, "\t%s", u2);

fclose(p);

if (!strcmp(u1, u2))

{

printf("Enter names of different players! ");

goto read;

}

else

decision();

system("color fc"); //set color of all text

board();

do

{

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

printf(" hi ");

if (player == 1)

printf("%s Type any digit from 1-9 to fill your response:- ", u1);

else

printf("%s Type any digit from 1-9 to fill your response:- ", u2);

scanf("%d", &choice);

symbol = ((player == 1) ? x : o);

if (choice == 1 && a[0] == '1')

a[0] = symbol;

else if (choice == 2 && a[1] == '2')

a[1] = symbol;

else if (choice == 3 && a[2] == '3')

a[2] = symbol;

else if (choice == 4 && a[3] == '4')

a[3] = symbol;

else if (choice == 5 && a[4] == '5')

a[4] = symbol;

else if (choice == 6 && a[5] == '6')

a[5] = symbol;

else if (choice == 7 && a[6] == '7')

a[6] = symbol;

else if (choice == 8 && a[7] == '8')

a[7] = symbol;

else if (choice == 9 && a[8] == '9')

a[8] = symbol;

else

{

printf("Wrong Selection ");

player--;

}

score = checkforwin();

player++;

board();

} while (score == -1);

p = fopen("score.txt", "a+");

if (score == 1)

{

if (player == 2)

{

printf(" Player1 %s Wins! ", u1);

fprintf(p, "\t%s", u1);

getch();

}

else

{

printf(" Player2 %s Wins! ", u2);

fprintf(p, "\t%s", u2);

getch();

}

fclose(p);

}

else

printf(" Game Draws! ");

fprintf(p, "\t%s", "DRAW");

getch();

}

void vsComputer(FILE *p, int player, int choice, char symbol, int score)

{

read:

p = fopen("score.txt", "a+");

printf(" Enter name of player: ");

scanf("%s", u1);

fprintf(p, " %s", u1);

// printf("Enter name of player2: ");

// scanf("%s", u2);

strcpy(u2, "Computer");

fprintf(p, "\t%s", "Computer");

fclose(p);

// if (!strcmp(u1, u2))

// {

// printf("Enter names of different players! ");

// goto read;

// }

// else

decision();

system("color fc"); //set color of all text

board();

srand(time(0));

do

{

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

if (player == 1)

{

printf("%s Type any digit from 1-9 to fill your response:- ", u1);

scanf("%d", &choice);

}

else

{

printf("Computer is thinking wait for a minute... ");

sleep(1);

choice = (rand() % 9) + 1;

}

symbol = ((player == 1) ? x : o);

if (choice == 1 && a[0] == '1')

a[0] = symbol;

else if (choice == 2 && a[1] == '2')

a[1] = symbol;

else if (choice == 3 && a[2] == '3')

a[2] = symbol;

else if (choice == 4 && a[3] == '4')

a[3] = symbol;

else if (choice == 5 && a[4] == '5')

a[4] = symbol;

else if (choice == 6 && a[5] == '6')

a[5] = symbol;

else if (choice == 7 && a[6] == '7')

a[6] = symbol;

else if (choice == 8 && a[7] == '8')

a[7] = symbol;

else if (choice == 9 && a[8] == '9')

a[8] = symbol;

else

{

printf("Wrong Selection ");

player--;

}

score = checkforwin();

player++;

board();

} while (score == -1);

p = fopen("score.txt", "a+");

if (score == 1)

{

if (player == 2)

{

printf(" Player1 %s Wins! ", u1);

fprintf(p, "\t%s", u1);

getch();

}

else

{

printf(" Computer Wins! ");

fprintf(p, "\t%s", "Computer");

getch();

}

fclose(p);

}

else

printf(" Game Draws! ");

fprintf(p, "\t%s", "DRAW");

getch();

}

int main()

{

FILE *p;

p = fopen("score.txt", "a+"); //scoreboard

fclose(p);

system("color 09"); //change colour to blue

int player = 1;

int choice, score = -1, vs;

char symbol, re;

char start, dec;

int s;

rules();

printf(" Type 1 to start the game:- Type 2 to view leader board:- ");

scanf("%d", &s);

if (s == 1)

{

read:

printf(" Type 1 to play vs Computer:- Type 2 to play vs another player: ");

scanf("%d", &vs);

if (vs == 2)

{

score = -1;

player = 1;

vsPlayer(p, player, choice, symbol, score);

}

else

{

score = -1;

player = 1;

vsComputer(p, player, choice, symbol, score);

}

}

if (s == 2)

{

int cho;

system("cls");

printf(" ");

printf("\tLEADERBOARD ");

char c;

p = fopen("score.txt", "r");

while ((c = getc(p)) != EOF)

{

printf("%c", c);

}

fclose(p);

printf(" Press 1 to start the game:- "); //restart

scanf("%d", &cho);

if (cho == 1)

{

goto read;

}

else

getch();

}

else

{

printf(" Should have typed 1 to play the game! Hope to see you back soon! ");

getch();

}

}

//combinations for winning

int checkforwin()

{

if (a[0] == a[1] && a[1] == a[2])

return 1;

else if (a[3] == a[4] && a[4] == a[5])

return 1;

else if (a[6] == a[7] && a[7] == a[8])

return 1;

else if (a[0] == a[3] && a[3] == a[6])

return 1;

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

return 1;

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

return 1;

else if (a[0] == a[4] && a[4] == a[8])

return 1;

else if (a[2] == a[4] && a[4] == a[6])

return 1;

else if (a[0] != '1' && a[1] != '2' && a[2] != '3' && a[3] != '4' && a[4] != '5' && a[5] != '6' && a[6] != '7' && a[7] != '8' && a[8] != '9')

return 0;

else

return -1;

}

//function to create tic tac toe board

void board()

{

int i;

system("cls");

printf("\tTic-Tac-Toe ");

printf(" ");

printf("%s:- (%c) %s:- (%c) ", u1, x, u2, o);

printf(" %c | %c | %c ", a[0], a[1], a[2]);

printf(" | | ");

printf("----|----|---- ");

printf(" | | ");

printf(" %c | %c | %c ", a[3], a[4], a[5]);

printf(" | | ");

printf("----|----|---- ");

printf(" %c | %c | %c ", a[6], a[7], a[8]);

printf(" | | ");

}

//function to generate rules

void rules()

{

char link;

printf("\tTic-Tac-Toe ");

printf("Welcome to the most played 2D game and a sort of fun using X and O ");

printf("Rules:- ");

printf(" 1:Each player will be entering the number to put respective X or O in the desired position");

printf(" 2:Player who gets a combination of 3 same characters either diagonal or horizontally or vertically will be declared as the winner");

printf(" Enjoy the game! Be a Winner! ");

printf("For more clarifications press Y else type any other character:- ");

scanf("%c", &link);

if (link == 'y' || link == 'Y')

{

system("start http://www.wikihow.com/Play-Tic-Tac-Toe");

}

}

//for players to decide whether they want x or o

int decision()

{

char dec;

deci:

printf(" Player1 %s choose the X or 0:", u1);

dec = getchar();

scanf("%c", &dec);

{

if (dec == 'X' || dec == 'x')

{

x = 'X';

o = '0';

}

else if (dec == '0')

{

x = '0';

o = 'X';

}

else

{

printf("Please enter either X or 0 only ");

goto deci;

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Database And Knowledge Base Systems

Authors: S Krishna

1st Edition

9810206208, 978-9810206208

More Books

Students also viewed these Databases questions