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
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
CODE:
#include <stdio.h>
#include <ctype.h>
//global var declaration
char brd[3][3]={{'*','*','*'},
{'*','*','*'},
{'*','*','*'}};
dispbrd(){
//local var declaration
int r,c;
for(r=0;r<3;r++){
for(c=0;c<3;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<X/O>? ");
fflush(stdin);
scanf("%c",&plyr);
plyr = toupper(plyr);
do{
printf("\nPlayer %c, please select your location<row,col>: ",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>
#include <ctype.h>
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=0;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
37](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F045224b3-7c4a-4d96-a741-7db2b796c9c4%2F1249a418-944c-4356-bcfc-0d28e66bed7a%2Feof12jm_processed.png&w=3840&q=75)
![printf("
printf("\n\n\n");
33
TIC TAC TOE");
34
35
36
dispbrd();
37
printf("\nWho plays first<X/o>?
fflush(stdin);
scanf ("%c",&plyr);
plyr = toupper (plyr);
38
");
39
40
41
42
43
do{
printf("\nPlayer %c, please select your location<row,col>:
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](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F045224b3-7c4a-4d96-a741-7db2b796c9c4%2F1249a418-944c-4356-bcfc-0d28e66bed7a%2Ftlakb6_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 4 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)