A simple three-game (implemented in C)
The question is: in a 3*3 matrix, the chess party has three consecutive sub-members and the winning code is as follows:
# Include <stdio. h> # include <stdlib. h> # include <time. h> char arr [3] [3] = {0}; // All functions that directly define external variables can be used directly because the board size is fixed and int size = 9; // fixed the size of the board. Each child will have the following capacity-1 void chessboard () // print the Board {int I = 0, j = 0; printf ("| \ n"); for (I = 0; I <3; I ++) {printf ("_ % c _ | _ % c _ \ n", arr [I] [0], arr [I] [1], arr [I] [2]); // match the position of the board. if (I <2) printf ("| \ n") ;}} void player () // players play chess {int m = 0, n = 0; printf ("player (@): "); // Enter the corresponding coordinate scanf_s (" % d ", & m, & n ); // The coordinates entered by the player must be within the range and cannot be the place to be placed. if (m <1 | m> 3 | n <1 | n> 3 | arr [m-1] [n-1]! = '') {Printf (" this place can not play! \ N "); // if it does not meet the requirements, continue to call this function player ();} else {arr [m-1] [n-1] = '@'; // Add the pawns to the corresponding content. The size of the checkerboard is-1 size -- ;}} void computer () // play chess on the computer {printf ("computer (#): \ n "); int m = 0, n = 0; srand (time (NULL); // The coordinates of the computer are randomly generated using time as the seed m = rand () % 3; // obtain the remainder of 3 and generate a random number of 0-2 to assign the array subscript n = rand () % 3; while (arr [m] [n]! = '') // If the generated coordinates do not meet the requirements, the cycle is generated (low efficiency) {m = rand () % 3; n = rand () % 3 ;} arr [m] [n] = '#'; // post-operation capacity-1 size --;} int winner () // judge the winner {// although the code is too large, but the efficiency is high. I wanted to use a function. Considering the low efficiency, I gave up if (arr [0] [0] = arr [0] [1] & arr [0] [1] = arr [0] [2] & arr [0] [2] = '@') | (arr [1] [0] = arr [1] [1] & arr [1] [1] = arr [1] [2] & arr [1] [2] = '@') | (arr [2] [0] = arr [2] [1] & arr [2] [1] = arr [2] [2] & arr [2] [2] = '@') | (arr [0] [0] = = Arr [1] [0] & arr [1] [0] = arr [2] [0] & arr [2] [0] = '@') | (arr [0] [1] = arr [1] [1] & arr [1] [1] = arr [2] [1] & arr [2] [1] = '@') | (arr [0] [2] = arr [1] [2] & arr [1] [2] = arr [2] [2] & arr [2] [2] = '@') | (arr [0] [0] = arr [1] [1] & arr [1] [1] = arr [2] [2] & arr [2] [2] = '@') | (arr [0] [2] = arr [1] [1] & arr [1] [1] = arr [2] [0] & arr [2] [0] = '@')) return 1; // The Player wins else if (arr [0] [0] = arr [0] [1] & arr [0] [1] = arr [0] [2] & arr [0] [2] = '#') | (arr [1] [0] = arr [1] [1] & arr [1] [1] = arr [1] [2] & arr [1] [2] = '#') | (arr [2] [0] = arr [2] [1] & arr [2] [1] = arr [2] [2] & arr [2] [2] = '#') | (arr [0] [0] = arr [1] [0] & arr [1] [0] = arr [2] [0] & arr [2] [0] = '#') | (arr [0] [1] = arr [1] [1] & arr [1] [1] = arr [2] [1] & arr [2] [1] = '#') | (arr [0] [2] = arr [1] [2] & arr [1] [2] = arr [2] [2] & Arr [2] [2] = '#') | (arr [0] [0] = arr [1] [1] & arr [1] [1] = arr [2] [2] & arr [2] [2] = '#') | (arr [0] [2] = arr [1] [1] & arr [1] [1] = arr [2] [0] & arr [2] [0] = '#')) return 0; // The computer wins else if (size = 0) return-1; // The draw else return 2; // only to eliminate the compilation warning and has no practical significance} int main () {int I = 0, j = 0; // initialize the array as a space for (I = 0; I <3; I ++) {for (j = 0; j <3; j ++) {arr [I] [j] = '';}} while (1) // round robin until one party wins or even Board {chessboard (); // loop print the board if (winner () =-1 | winner () = 0 | winner () = 1) {break;} player (); // print the Board after each subtask, and then judge that no one wins the chessboard (); if (winner () =-1 | winner () = 0 | winner () = 1) {break;} computer () ;}if (winner () = 1) {printf ("player win! \ N ");} else if (winner () = 0) {printf (" computer win! \ N ");} else {printf (" no one win! \ N ");} system (" pause "); return 0 ;}
Of course there are still many improvements to this program. This is just a simple prototype.