Using C language to implement a 3*3 board game
Play chess in a 3*3 matrix. If one of the three pawns is in a straight line, the game wins. Note: When playing chess, the coordinates of the matrix corresponding to the chess piece are entered. The Code is as follows:
# Include <stdio. h> # include <stdlib. h> # include <time. h> char a [3] [3] = {0}; int size = 9; void qipan () // print the chessboard {int I = 0, j = 0; printf ("| \ n"); for (I = 0; I <3; I ++) {printf ("_ % c _ | _ % c _ \ n", a [I] [0], a [I] [1], a [I] [2]); if (I <2) printf ("| \ n") ;}} void wjxq () // players play chess {int m = 0, n = 0; printf ("the player pawns are @:"); // enter the coordinates scanf ("% d", & m, & n) of the flag ); if (m <1 | m> 3 | n <1 | n> 3 | a [M-1] [n-1]! = '') {Printf (" your output coordinate position is invalid! "); Wjxq ();} else {a [M-1] [n-1] = '@'; size = size-1 ;}} void dn () // computer chess {int m = 0, n = 0; printf ("computer chess piece #: \ n"); srand (time (NULL); m = rand () % 3; n = rand () % 3; while (a [m] [n]! = '') {M = rand () % 3; n = rand () % 3;} a [m] [n] = '#'; size = size-1;} int winner () // determine the winner {if (a [0] [0] = a [0] [1] & a [0] [1] = a [0] [2] & a [0] [2] = '@') | (a [1] [0] = a [1] [1] & a [1] [1] = a [1] [2] & [1] [2] = '@') | (a [2] [0] = a [2] [1] & a [2] [1] = a [2] [2] & [2] [2] = '@') | (a [0] [0] = a [1] [0] & a [1] [0] = a [2] [0] & [2] [0] = '@') | (a [0] [1] = a [1] [1] & a [1] [1] = a [2] [1] & [2] [1] = '@') | (a [0] [2] = a [1] [2] & a [1] [2] = a [2] [2] & [2] [2] = '@') | (a [0] [0] = a [1] [1] & a [1] [1] = a [2] [2] & [2] [2] = '@') | (a [0] [2] = a [1] [1] & a [1] [1] = a [2] [0] & [2] [0] = '@')) return 1; // The Player wins else if (a [0] [0] = a [0] [1] & a [0] [1] = a [0] [2] & a [0] [2] = '@') | (a [1] [0] = a [1] [1] & a [1] [1] = a [1] [2] & [1] [2] = '@') | (a [2] [0] = a [2] [1] & a [2] [1] = a [2] [2] & [2] [2] = '@') | (a [0] [0] = a [1] [0] & a [1] [0] = a [2] [0] & [2] [0] = '@') | (a [0] [1] = a [1] [1] & a [1] [1] = A [2] [1] & a [2] [1] = '@') | (a [0] [2] = a [1] [2] & a [1] [2] = a [2] [2] & [2] [2] = '@') | (a [0] [0] = a [1] [1] & a [1] [1] = a [2] [2] & [2] [2] = '@') | (a [0] [2] = a [1] [1] & a [1] [1] = a [2] [0] & [2] [0] = '@')) return 0; // The player loses, that is, the computer wins else if (size = 0) return-1; // draw elsereturn 2;} int main () {int I = 0, j = 0; // empty array initialization for (I = 0; I <3; I ++) {for (j = 0; j <3; j ++) {a [I] [j] = '';}} while (1) {qipan (); if (winner () =-1 | winner () = 0 | winner () = 1) {Break;} wjxq (); qipan (); if (winner () =-1 | winner () = 0 | winner () = 1) {break;} dn () ;}if (winner () = 1) {printf ("Players win! ");} Else if (winner () = 0) {printf (" players lost! ");} Else {printf (" Draw ");} system (" pause "); return 0 ;}