C++ Please fix some problems with my Tic Tac Toe Program: a. It should declare the winner. Like "Congratulations! Player X has won the game!" b. A box is already chosen by a player, but if you repeat...



C++



Please fix some problems with my Tic Tac Toe Program:


a. It should declare the winner. Like "Congratulations!
Player X
has won the game!"


b.  A box is already chosen by a player, but if you repeat the location, it changes instead of displaying that it's already occupied.


c. Supply the algorithm needed for a Draw/Tie.



CODE:


#include
#include


//global var declaration
char brd[3][3]={{'*','*','*'},
{'*','*','*'},
{'*','*','*'}};
dispbrd(){
//local var declaration
int r,c;
for(r=0;r<>
for(c=0;c<>
printf("\t%c",brd[r][c]);
printf("\n");
}
}


char switchplyr(char plyr){
if(plyr=='X')
plyr = 'O';
else
plyr = 'X';
return plyr;


}


main(){
//local var declaration
char plyr;
int row,col,win=0;


printf("\n\n\n");
printf(" TIC TAC TOE");
printf("\n\n\n");


dispbrd();


printf("\nWho plays first? ");
fflush(stdin);
scanf("%c",&plyr);
plyr = toupper(plyr);




do{
printf("\nPlayer %c, please select your location: ",plyr);
fflush(stdin);
scanf("%d,%d",&row,&col);
brd[row][col]=plyr;
dispbrd();
//test for winning pattern
if(brd[0][0]==plyr&&brd[0][1]==plyr&&brd[0][2]==plyr) win=1; //pattern 1
if(brd[1][0]==plyr&&brd[1][1]==plyr&&brd[1][2]==plyr) win=1; //pattern 2
if(brd[2][0]==plyr&&brd[2][1]==plyr&&brd[2][2]==plyr) win=1; //pattern 3
if(brd[0][0]==plyr&&brd[1][0]==plyr&&brd[2][0]==plyr) win=1; //pattern 4
if(brd[0][1]==plyr&&brd[1][1]==plyr&&brd[2][1]==plyr) win=1; //pattern 5
if(brd[0][2]==plyr&&brd[1][2]==plyr&&brd[2][2]==plyr) win=1; //pattern 6
if(brd[0][0]==plyr&&brd[1][1]==plyr&&brd[2][2]==plyr) win=1; //pattern 7
if(brd[0][2]==plyr&&brd[1][1]==plyr&&brd[2][0]==plyr) win=1; //pattern 8


plyr = switchplyr(plyr);
printf("player: %c",plyr);
}while(win!=1);
printf("\nCongratulations, you win!!");








}


Finclude <stdio.h><br>#include <ctype.h><br>1<br>2<br>3<br>4<br>//global var declaration<br>char brd[3][3]={{'*','*','*'},<br>{'*','*<br>{'*',<br>5<br>7<br>8E dispbrd(){<br>9<br>//local var declaration<br>10<br>int r,cj<br>11 E<br>for (r=0;r<3;r++){<br>for (c=0;c<3;c++)<br>printf(

Extracted text: Finclude #include 1 2 3 4 //global var declaration char brd[3][3]={{'*','*','*'}, {'*','* {'*', 5 7 8E dispbrd(){ 9 //local var declaration 10 int r,cj 11 E for (r=0;r<3;r++){ for="" (c=""><3;c++) printf("\t%c",="" brd[r][c]);="" printf("\n");="" 12="" 13="" 14="" 15="" 16="" 17="" 18="" e="" char="" switchplyr(char="" plyr){="" if(plyr="='x')" plyr='0' ;="" 19="" 20="" 21="" else="" plyr='X' ;="" return="" plyr;="" 22="" 23="" 24="" 25="" 26="" 27="" e="" main(){="" 28="" local="" var="" declaration="" char="" plyr;="" int="" row,="" col,="" win="0;" 29="" 30="" 31="" printf("\n\n\n");="" printf("="" printf("\n\n\n");="" 32="" 33="" tic="" tаc="" тое");="" 34="" 35="" 36="" dispbrd();="" activate="" windows="">
printf(? fflush(stdin); scanf ("%c",&plyr); plyr = toupper (plyr); 38 "); 39 40 41 42 43 do{ printf("\nPlayer %c, please select your location: fflush(stdin); scanf("%d,%d",&row,&col); brd[row][col]=plyr; dispbrd(); //test for winning pattern if(brd[@][@]==plyr&&brd[@][1]==plyr&&brd[@][2]==plyr) win=1; if(brd[1][e]==plyr&&brd[1][1]==plyr&&brd[1][2]==plyr) win=1; if(brd[2][0]==plyr&&brd[2][1]==plyr&&brd[2][2]==plyr) win=1; if (brd[e][@]==plyr&&brd[1][@]==plyr&&brd[2][ej==plyr) win=1; if (brd[e][1]==plyr&&brd[1][1]==plyr&&brd[2][1]==plyr) win=1; if(brd[e][2]==plyr&&brd[1][2]==plyr&&brd[2][2]==plyr) win=1; if(brd[@][@]==plyr&&brd[1][1]==plyr&&brd[2][2]==plyr) win=1; if(brd[@][2]==plyr&&brd[1][1]==plyr&&brd[2][@]==plyr) win=1; 44 45 ",plyr); 46 47 48 49 50 //pattern 1 //pattern 2 //pattern 3 //pattern 4 //pattern 5 //pattern 6 //pattern 7 //pattern 8 51 52 53 54 55 56 57 58 59 plyr = switchplyr (plyr); printf("player: %c",plyr); }while(win!=1); printf("\nCongratulations, you win!!"); 60 61 62 63 64 65 66 67 68 } Activate Windows "/>
Extracted text: printf(" printf("\n\n\n"); 33 TIC TAC TOE"); 34 35 36 dispbrd(); 37 printf("\nWho plays first? fflush(stdin); scanf ("%c",&plyr); plyr = toupper (plyr); 38 "); 39 40 41 42 43 do{ printf("\nPlayer %c, please select your location: fflush(stdin); scanf("%d,%d",&row,&col); brd[row][col]=plyr; dispbrd(); //test for winning pattern if(brd[@][@]==plyr&&brd[@][1]==plyr&&brd[@][2]==plyr) win=1; if(brd[1][e]==plyr&&brd[1][1]==plyr&&brd[1][2]==plyr) win=1; if(brd[2][0]==plyr&&brd[2][1]==plyr&&brd[2][2]==plyr) win=1; if (brd[e][@]==plyr&&brd[1][@]==plyr&&brd[2][ej==plyr) win=1; if (brd[e][1]==plyr&&brd[1][1]==plyr&&brd[2][1]==plyr) win=1; if(brd[e][2]==plyr&&brd[1][2]==plyr&&brd[2][2]==plyr) win=1; if(brd[@][@]==plyr&&brd[1][1]==plyr&&brd[2][2]==plyr) win=1; if(brd[@][2]==plyr&&brd[1][1]==plyr&&brd[2][@]==plyr) win=1; 44 45 ",plyr); 46 47 48 49 50 //pattern 1 //pattern 2 //pattern 3 //pattern 4 //pattern 5 //pattern 6 //pattern 7 //pattern 8 51 52 53 54 55 56 57 58 59 plyr = switchplyr (plyr); printf("player: %c",plyr); }while(win!=1); printf("\nCongratulations, you win!!"); 60 61 62 63 64 65 66 67 68 } Activate Windows

Jun 10, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here