Download presentation
Presentation is loading. Please wait.
Published byNelson Barrett Modified over 9 years ago
1
259201 Computer Programming for Engineers
2
Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between players
3
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
4
Drawing a 3x3 grid Input/Output? A 2 dimensional array stores O,X in the main Output? 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
5
Drawing a 3x3 grid Q1: 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 – 2 Write 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
6
q1.c #include 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(" "); if (j!=2) printf("|"); } printf("\n"); if (i!=2) printf("---+---+---\n"); }
7
Receiving the inputs The inputs are the 2-D coordinate as follows Receiving the row #, then column # 2,02,1 1,01,1 0,00,1 2,2 1,2 0,2
8
Receiving the inputs: Cont. Q2: Add codes to Q1 to accept the location for the ‘O’ player as follows: Enter O horizontal axis: 1 Enter O vertical axis: 0 If the location is occupied, re-prompt for another location. O O X X O
9
q2.c #include 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(" "); if (j!=2) printf("|"); } printf("\n"); if (i!=2) printf("---+---+---\n"); }
10
Checking for a winner O O X X O O XX O X A winner is found, when a player place their marks three in a row, which could be horizontal, vertical and diagonal
11
Checking for a winner (Cont.) Q3: Write the following program Declare a 3x3 array Initialize all members to zero Checking 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
12
q3.c // unused codes can be marked as comments #include 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; else return 0; }
13
Players taking turns Create a variable storing the status of current player 0 for player ‘o’ 1 for player ‘x’ Toggling between players can be done by using the logic: !0 -> 1 !1 -> 0
14
Players taking turns (Cont.) Q4: Modify codes for Q2 and Q3 such that: Create empty spaces on a 3x3 grid Starting with player ‘o’ and taking turns Input the location for ‘0’ and ‘x’ where it is not occupied Check for a winner for row#0
15
q4.c #include 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"); }
16
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;
17
Lab Sheet The previous program does not consider the draw case. Modify the code so that it can deal with the draw case.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.