259201 Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving...

17
259201 Computer Programming for Engineers

Transcript of 259201 Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving...

Page 1: 259201 Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.

259201 Computer Programming for Engineers

Page 2: 259201 Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.

OutlineTic-Tac-Toe (O-X Game)

Drawing 3x3 gridReceiving the inputsChecking for a winnerTaking turns between players

Page 3: 259201 Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.

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

Page 4: 259201 Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.

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

Page 5: 259201 Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.

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

Page 6: 259201 Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.

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");}

}

Page 7: 259201 Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.

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

Page 8: 259201 Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.

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

Page 9: 259201 Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.

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");}

}

Page 10: 259201 Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.

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

Page 11: 259201 Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.

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

Page 12: 259201 Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.

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;

}

Page 13: 259201 Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.

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

Page 14: 259201 Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.

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

Page 15: 259201 Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.

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");

}

Page 16: 259201 Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.

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;

Page 17: 259201 Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.

Lab SheetThe previous program does not consider the draw case. Modify the code so that it can deal with the draw case.