header File
#ifndef __game_h__#define __game_h__#include <stdio.h>#include <stdlib.h>#include <time.h>#include <windows.h>extern void Get_board(char a[][3],int LINE,int LIST);extern void Player_operation(char a[][3],int LINE,int LIST);extern void Computer_operation(char a[][3],int LINE,int LIST);extern void Init(char a[][3],int LINE,int LIST);extern int Discriminant_board(char a[][3],int LINE,int LIST); extern int Winning_or_losing_discriminant(char a[][3],int LINE,int LIST);#endif
Game Test Module
#include "game.h" void menu () {printf ("######################\n"); printf ("# # # #1. PALY 2.exit####\n"); printf ("####### ###############\n ");} void game () {int line = 3; int list = 3; Char board[][3] ={"a"}; Init (board,line,list); while (1) {Get_board (board,line,list); if (1==winning_or_losing_discriminant (board,line,list)) {printf ("player wins \ n"); Sleep (+); Init (board,line,list); Get_board (board,line,list);} if (2==winning_or_losing_discriminant (board,line,list)) {printf ("Computer wins \ n"); Sleep (+); Init (board,line,list); Get_board (board,line,list);} if (Discriminant_board (board,line,list)) {if (Winning_or_losing_discriminant (board,line,list) ==0) {printf ("Draw \ n"); Sleep (+); Init (board,line,list); Get_board (board,line,list);}} Player_operation (board,line,list); if (1==winning_or_losing_discriminant (board,line,list)) {printf ("player wins \ n"); Sleep (+); Init (board,line,list); Get_board (board,line,list);} if (2==winning_or_losing_discriminant (bOard,line,list) {printf ("Computer wins \ n"); Sleep (+); Init (board,line,list); Get_board (board,line,list);} if (Discriminant_board (board,line,list)) {if (Winning_or_losing_discriminant (board,line,list) ==0) {printf ("Draw \ n"); Sleep (+); Init (board,line,list); Get_board (board,line,list);}} Computer_operation (board,line,list); }}int Main () {int input = 0; Srand ((unsigned int) time (NULL)); menu (); do{printf ("Please input:>\n"); scanf_s ("% D ", &input); switch (input) {case 1:game (); Break Case 2:printf ("Exit program \ n"); Goto end; default:printf ("Input error \ n"); }}while (1); End:system ("pause"); return 0;}
Game Implementation Function Module
```
void Init (char a[][3],int line,int LIST)
{int i = 0;
int j = 0;
for (i=0; i<line; i++)
for (j=0; j<list; j + +)
A[I][J] = ";
}//初始化棋盘 void Get_board(char a[][3],int LINE,int LIST) {int i = 0; int j = 0; for(i=0; i<LINE; i++) { for(j=0; j<LIST; j++) { printf("_%c_",a[i][j]); if(j<LIST-1) printf("|"); } printf("\n"); }}//打印棋盘 void Player_operation(char a[][3],int LINE,int LIST) { int x = 1; int y = 1; printf("玩家请走:>\n");
RP:SCANF ("%d%d", &x,&y);
if ((x<4&&x>0) && (y<4&&y>0) &&a[x-1][y-1]== ")
A[x-1][y-1] = ' x ';
else if (a[x-1][y-1]!= ')
{printf ("Input error, currently occupied, please re-enter: >\n");
Goto RP;}
Else
printf ("err\n");
}//player operation Void Computer_operation (char a[][3],int line,int LIST) {int x = 0; int y = 0; printf ("Computer operation: >\n"); while (1) {x = rand ()%3; y = rand ()%3; if ((a[x][y]!= ' x ') && (a[x][y]!= ' 0 ')) {a[x][y] = ' 0 '; Break }}}//computer operation int Discriminant_board (char a[][3],int line,int LIST) {int i = 0; int j = 0; int num = 0; For (i=0, i<line; i++) {for (j=0; j<list; J + +) {if (a[i][j]== ') num++; }} if (num) return 0; if (!num) return 1; }//discriminant function One int winning_or_losing_discriminant (char a[][3],int line,int LIST) {int i = 0; int j = 0; for (i=0; i<line; i++) {if ((a[i][0]== ' x ') && (a[i][1]== ' x ') && (a[i][2]== ' x ')) | | ((a[0][i]== ' x ') && (a[1][i]== ' x ') && (a[2][i]== ' x ')) | | ((a[0][0]== ' x ') && (a[2][2]== ' x ') && (a[1][1]== ' x ')) | | ((a[0][2]== ' x ') && (a[1][1]== ' x ') && (a[2][0]== ' x '))) {return 1;} if ((a[i][0]== ' 0 ') && (a[i][1]== ' 0 ') && (a[i][2]== ' 0 ')) | | ((a[0][i]== ' 0 ') && (a[1][i]== ' 0 ') && (a[2][i]== ' 0 ')) | | ((a[0][0]== ' 0 ') && (a[2][2]== ' 0 ') && (a[1][1]== ' 0 ')) | | (a[0][2]== ' 0 ') && (a[1][1]== ' 0 ') && (a[2][0]== ' 0 '))) {return 2;} else {return 0;} }}//discriminant Function II
The three sub-chess games implemented by the
C language