Using C language to implement a 3*3 board game

Source: Internet
Author: User

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

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.