Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C programming urgent can you modify my code so that it also includes a player vs computer option along with the current player vs player

C programming urgent can you modify my code so that it also includes a player vs computer option along with the current player vs player option pls add it into the new code .i know you have to add a rand function but idk how.pls show the new code in the ans with the modification

#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(); 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; 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: 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); 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(); } 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

Programming The Perl DBI Database Programming With Perl

Authors: Tim Bunce, Alligator Descartes

1st Edition

1565926994, 978-1565926998

More Books

Students also viewed these Databases questions

Question

Define intercultural conflict

Answered: 1 week ago