C Language Realization Small Game _ three sub-chess

Source: Internet
Author: User

Game rules: The player walks one step, the computer walks one step. Of course, the code of how the computer goes is also written by you.

#include  <stdio.h> #include  <stdlib.h>/************************************************** function function: Initialize checkerboard/******************************************************************** /void init_chess (char  (*chess_arr) [3]) {int i,j;printf ("game start: \ n"); for (i = 0 ; i<3;i++) {for (j = 0;j<3;j++) {* (* (chess_arr+i) +j)  =  '   ';}} for (i = 0;i<3;i++) {printf (" %c | %c | %c"  ,chess_arr[i][0],chess_ ARR[I][1],CHESS_ARR[I][2]), if (i!=2) {printf ("\ n---|---|---\ n");} printf ("\ n");} /*******************************************************************************///function: Players enter the coordinates of chess/************* /void input_player_xy (char  (*chess _arr) [3],int x,int y] {int i = 0;if (chess_arr[x][y]== '   ') {chess_arr[x][y] =   ' Y ';} else{printf ("Step to pass \ n");} for (i = 0;i<3;i++) {printf (" %c | %c | %c"  ,chess_arr[i][0],chess_arr[i][1],chess_arr[i][2]); if (i! =2) {printf ("\ n---|---|---\ n");}}     printf ("\ n");} /*******************************************************************************///function: Print the computer chess coordinates/************* /void input_computer_xy (char  (* Chess_arr) [3]) {int i = 0;for (i = 0;i<3;i++) {printf (" %c | %c |  %c " ,chess_arr[i][0],chess_arr[i][1],chess_arr[i][2]); if (i!=2) {printf (" \ n---|---|---\ n ");} printf ("\ n");} /*******************************************************************************///function: Determines the coordinates of the player to play chess/************* /void input_player (char  (*chess_ ARR) [3]) {int x = 0,y = 0;printf ("Please move Your chess:"); scanf ("%d%d", &x,&y); input_player_xy (chess_arr,x,y);} /*****************************function function: Determine the coordinates of computer chess/******************************************* /void input_computer (char  (*chess_arr) [3]) {Int i =0,j  = 0;for (i = 0;i<3;i++) {for (j = 0;j<3;j++) {if (chess_arr[i][j]== '   ') {   chess_arr[i][j] =  ' O ';   goto flag;}} Flag:    input_computer_xy (Chess_arr);} /*******************************************************************************///function Function: The main function determines whether the computer or the player wins/********* /int main () {char chess_arr[3][ 3];int flag = 1;init_chess (Chess_arr); while (flag) {Int i = 0,j = 0;int  diag_flag_player = 0,diag_flag_computer = 0,count = 0;input_player (Chess_ ARR); Input_computer (Chess_arr); for (i = 0;i<3;i++) {if ((chess_arr[i][0]== ' Y ') &&(chess_arr[i][1]== ' y ') && (chess_arr[i][2]== ' y ')) | |       ((chess_arr[0][i]== ' y ') && (chess_arr[1][i]== ' y ') && (chess_arr[2][i ]== ' Y ')   ) {printf ("player win!\n"); flag = 0;break;} if ((chess_arr[i][0]== ' o ') && (chess_arr[i][1]== ' o ') && (chess_arr[i][2]== ' o ')) | |            ((chess_arr[0][i]== ' O ') && (chess_arr[1][i]= = ' O ') && (chess_arr[2][i]== ' o ')        ) {printf ("computer win!\n "); flag = 0;break;}     for (j = 0;j<3;j++) {if (chess_arr[i][j]!= '   ') {count++;}} if (chess_arr[i][i]== ' Y ') {diag_flag_player++;} if (chess_arr[i][2-i]== ' O ') {diag_flag_computer++;}} if (diag_flag_player==3) {flag = 0;printf ("player win!\n");} if (diag_flag_computer==3) {flag = 0;printf ("computer win!\n");} if (count==9) {flag = 0;printf ("player equal computer!\n ");}} System ("pause"); return 0;}

Because it is clear to everyone, it is not placed in multiple files.

Let me explain how the computer is going: When the person finishes walking, the computer just sees the vacant position and goes. Of course this can make the computer more powerful. The code is for you to write.

C Language Realization Small Game _ three sub-chess

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.