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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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 <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
Transcribed Image Text: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
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
Transcribed Image Text: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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 4 images

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY