259201 Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving...
-
Upload
nelson-barrett -
Category
Documents
-
view
217 -
download
0
Transcript of 259201 Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving...
259201 Computer Programming for Engineers
OutlineTic-Tac-Toe (O-X Game)
Drawing 3x3 gridReceiving the inputsChecking for a winnerTaking turns between players
Tic-Tac-Toe, O-X: Flow of the game
O
O
X
X
• Starting with a clean 3x3 grid• Taking turns• Marking the spaces• Checking for winners• Drawing a new grid• Until there is a winner
Drawing a 3x3 gridInput/Output?
A 2 dimensional array stores O,X in the mainOutput?
How to draw a grid?# of status of the var?
1 space for var
1 space for “|”
1 space for “_”
1 space for “+”
An empty space
Drawing a 3x3 gridQ1: Write the following program
Declare a 3x3 (2-D) array, Initialize members in the array to zero (0)Randomly generate members in the array to 0
– 2Write a function that draws the grid and if
the value is 1, draw O the value is 2, draw X the value is 0, draw a space
Call the above function
q1.c#include<stdio.h>void print_tbl(int [3][3]);
void main(void) {int table[3][3]={0,0,0,0,0,0,0,0,0};int i, j;
for (i=0; i<3; i++) for(j=0; j<3; j++) table[i][j] = rand()%3;
print_tbl(table);
}
void print_tbl(int tbl[3][3]) {int i, j;
for (i=0; i<3; i++) { for (j=0; j<3; j++) {
printf(" ");if (tbl[i][j]==1)
printf("o");else if (tbl[i][j]==2)
printf("x");else
printf(" ");printf(" ");if (j!=2)
printf("|"); } printf("\n"); if (i!=2)
printf("---+---+---\n");}
}
Receiving the inputsThe inputs are the 2-D coordinate as follows
Receiving the row #, then column #2,0 2,1
1,0 1,1
0,0 0,1
2,2
1,2
0,2
Receiving the inputs: Cont.Q2: Add codes to Q1 to accept the location
for the ‘O’ player as follows:Enter O horizontal axis: 1Enter O vertical axis: 0
If the location is occupied, re-prompt for another location.
O
O
X
XO
q2.c#include<stdio.h>void print_tbl(int [3][3]);
void main(void) {int table[3][3]={0,0,0,0,0,0,0,0,0};int i, j;int h, v;
for (i=0; i<3; i++) for(j=0; j<3; j++)
table[i][j] = rand()%3; print_tbl(table);
do { printf("Enter o horizontal axis: "); scanf("%d", &h); printf("Enter o vertical axis: "); scanf("%d", &v);}while(table[h][v]==1||table[h][v]==2);
table[h][v]=1;print_tbl(table);
}
void print_tbl(int tbl[3][3]) {int i, j;
for (i=0; i<3; i++) { for (j=0; j<3; j++) {
printf(" ");if (tbl[i][j]==1)
printf("o");else if (tbl[i][j]==2)
printf("x");else
printf(" ");printf(" ");if (j!=2)
printf("|"); } printf("\n"); if (i!=2)
printf("---+---+---\n");}
}
Checking for a winner
O
O
X
X
O
O
X X
O
X
A winner is found, when a player place their marks three in a row, which could be horizontal, vertical and diagonal
Checking for a winner (Cont.)Q3: Write the following program
Declare a 3x3 arrayInitialize all members to zeroChecking for winner on row#0, and
Return 0, if no winner Return 1, if ‘0’ won Return 2, if ‘x’ won
Test the program with random numbers
q3.c // unused codes can be marked as comments#include<stdio.h>void print_tbl(int [3][3]);int check_winning(int [3][3]);
void main(void) {int table[3][3]={0,0,0,0,0,0,0,0,0};int winner = 0;
table[0][0]=1;table[0][1]=1;table[0][2]=1;
print_tbl(table);
while (winner==0) {winner = check_winning(table);
}if (winner==1)
printf("O win\n");else if (winner==2)
printf("X win\n");}
int check_winning(int tbl[3][3]) {if (tbl[0][0]==1 &&tbl[0][1]==1
&&tbl[0][2]==1)return 1;
else if (tbl[0][0]==2 &&tbl[0][1]==2&&tbl[0][2]==2)return 2;
elsereturn 0;
}
Players taking turnsCreate a variable storing the status of
current player0 for player ‘o’1 for player ‘x’Toggling between players can be done by using
the logic:!0 -> 1!1 -> 0
Players taking turns (Cont.)Q4: Modify codes for Q2 and Q3 such that:
Create empty spaces on a 3x3 gridStarting with player ‘o’ and taking turnsInput the location for ‘0’ and ‘x’ where it is not
occupiedCheck for a winner for row#0
q4.c#include<stdio.h>
void print_tbl(int [3][3]);
int check_winning(int [3][3]);
void main(void) {
int table[3][3]={0,0,0,0,0,0,0,0,0};
int h, v, winner = 0, player=0;
print_tbl(table);
while (winner==0) {
if (player==0) {
do {
printf("Enter o horizontal axis: ");
scanf("%d", &h);
printf("Enter o vertical axis: ");
scanf("%d", &v);
} while(table[h][v]==1||table[h][v]==2);
table[h][v]=1;
} else {
do {
printf("Enter x horizontal axis: ");
scanf("%d", &h);
printf("Enter x vertical axis: ");
scanf("%d", &v);
} while ( (table[h][v]==1||table[h][v]==2);
table[h][v]=2;
}
winner = check_winning(table); player=!player; printf("\n"); print_tbl(table);}if (winner==1) printf("O win!\n");else if (winner==2) printf("X win!\n");
}
Note that the code on the left can be rewritten as the one of the right
if (player==1) { do { printf("Enter o row: "); scanf("%d", &h); printf("Enter o column: "); scanf("%d", &v); } while(table[h][v]==1||table[h][v]==2); table[h][v]=1;} else { do { printf("Enter x row: "); scanf("%d", &h); printf("Enter x column: "); scanf("%d", &v); } while ( (table[h][v]==1||table[h][v]==2); table[h][v]=2;}
char p;...if (player==1) p = ‘o’;else p = ‘x’; do { printf("Enter %c row: “, p); scanf("%d", &h); printf("Enter %c column: “, p); scanf("%d", &v);} while(table[h][v]==1||table[h][v]==2);table[h][v]=player;
Lab SheetThe previous program does not consider the draw case. Modify the code so that it can deal with the draw case.