C language implementation games (III)
# Include <stdio. h> # include <stdlib. h> void init (char arr [3] [3], int I, int j) {for (I = 0; I <3; I ++) {for (j = 0; j <3; j ++) {arr [I] [j] = '';}}} int prit (char arr [3] [3], int I, int j) {for (I = 0; I <3; I ++) {printf ("% c | % c", arr [I] [0], arr [I] [1], arr [I] [2]); if (I! = 2) printf ("\ n --- | --- \ n");} return 0;} void play (char arr [3] [3], int x, int y) {int I = 0; int j = 0; arr [x] [y] = 'X'; for (I = 0; I <3; I ++) {printf ("% c | % c", arr [I] [0], arr [I] [1], arr [I] [2]); if (I! = 2) printf ("\ n --- | --- \ n") ;}} void computer (char arr [3] [3]) {int I = 0; int j = 0; int flag = 0; for (I = 0; I <3; I ++) {if (flag = 1) break; for (j = 0; j <3; j ++) {if (arr [I] [j]! = 'X') & (arr [I] [j]! = 'O') {arr [I] [j] = 'O'; flag = 1; break ;}}for (I = 0; I <3; I ++) {printf ("% c | % c", arr [I] [0], arr [I] [1], arr [I] [2]); if (I! = 2) printf ("\ n --- | --- \ n") ;}} int check (char arr [3] [3], int I, int j) {int k = 0; // judge the diagonal line X int n = 0; // judge the diagonal line O int a = 0; // judge each row int B = 0; // for (I = 0; I <3; I ++) {if (arr [I] [I] = 'X') k ++; else if (arr [I] [I] = 'O') n ++;} if (k = 3) {return 1 ;}if (n = 3) {return 0 ;}for (I = 0; I <3; I ++) {if (arr [I] [0] = 'X ') & (arr [I] [1] = 'X') & (arr [I] [2] = 'X ')) | (arr [0] [I] = 'X ')& & (Arr [1] [I] = 'X') & (arr [2] [I] = 'X ')) | (arr [0] [2] = 'X') & (arr [1] [1] = 'X ') & (arr [2] [0] = 'X') {return 1; break ;} if (arr [I] [0] = 'O') & (arr [I] [1] = 'O ') & (arr [I] [2] = 'O') | (arr [0] [I] = 'O ') & (arr [1] [I] = 'O') & (arr [2] [I] = 'O ')) | (arr [0] [2] = 'O') & (arr [1] [1] = 'O ') & (arr [2] [0] = 'O') {return 0; break ;}} void manu () {printf ("***************** * ********************** \ N "); printf ("**************** welcome to the game *************** \ n "); printf ("*************** 1. start the game ***************** \ n "); printf ("*************** 0. exit the game ***************** \ n "); printf ("************************************* * ** \ n ");} void order () {printf ("*********** select the order of playing chess ************ \ n "); printf ("*************** 1. players can start with ****************** \ n "); printf ("*************** 0. local Machine first ****************** \ n ");} int main () {Char arr [3] [3]; int I = 0; int j = 0; int x = 0; int y = 0; int input = 1; int ret = 0; int c = 0; while (input) {manu (); printf ("SELECT>"); scanf ("% d", & input); switch (input) {case 1: {printf ("start game \ n"); order (); printf ("SELECT>"); scanf ("% d", & c ); switch (c) {case 1: {init (& arr [0], I, j); prit (& arr [0], I, j); while (1) {printf ("\ n enter the coordinates of the position to play the game: \ n"); scanf ("% d", & x, & y ); printf ("players playing chess: \ n"); play (& Amp; arr [0], x, y); ret = check (& arr [0], I, j); if (ret = 1) {printf ("\ n Congratulations! You have won! \ N "); break;} lop: printf (" \ n: \ n "); computer (& arr [0]); ret = check (& arr [0], I, j); if (ret = 0) {printf ("\ n sorry you lost! \ N "); break;} case 0: {init (& arr [0], I, j); prit (& arr [0], I, j); goto lop; break;} default: {printf ("Quit GAME \ n"); system ("pause"); exit (0); break ;}}} system ("pause"); return 0 ;}}