-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchess.fun.c
More file actions
124 lines (102 loc) · 2.4 KB
/
chess.fun.c
File metadata and controls
124 lines (102 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<memory.h>
#include"chessboard.h"
#include<stdlib.h>
#include<time.h>
#define ROWS 3
#define COLS 3
void menu()
{
printf("---------------------\n");
printf("---------------------\n");
printf("-------1 play -------\n");
printf("-------0 exit -------\n");
printf("---------------------\n");
printf("---------------------\n");
}
void init_board(char board[ROWS][COLS],int x,int y)
{
memset(board,' ',ROWS*COLS*sizeof(char));
}
void game()
{
/*int a,b;
scanf("%d",&a,&b);*/
char board[ROWS][COLS];
init_board(board,ROWS,COLS);
do
{
display_board(board,ROWS,COLS);
player_play(board,ROWS,COLS);
check_win(board,ROWS,COLS);
computer_play(board,ROWS,COLS);
check_win(board,ROWS,COLS);
}while(1);
}
void display_board(char board[ROWS][COLS],int x,int y)
{
int i = 0;
for(i=0;i<ROWS;i++)
{
printf(" %c | %c | %c \n",board[i][0],board[i][1],board[i][2]);
if(2!=i)
printf("--------------\n");
}
}
void player_play(char board[ROWS][COLS],int x,int y/*int* a,int* b*/)
{
int a = 0,b = 0,flag = 1;
do{
printf("please enter :");
scanf("%d %d",&a,&b);
if((a<x+1 && b<x+1)&&(board[a-1][b-1] == ' '))
{ board[a-1][b-1] = 'X';
flag = 0;}
//printf("错误输入\n");
}while(flag);
}
void computer_play(char board[ROWS][COLS],int x,int y)
{
int i = 0,j = 0;
do{
i = rand()%3;
j = rand()%3;
//i = rand()%3;
//j = rand()%3;
}while(board[i][j] != ' ');
board[i][j] = '0';
}
void check_win(char board[ROWS][COLS],int x,int y)
{
int i;
if(((board[0][0]==board[1][1])&&(board[1][1]==board[2][2]))||\
((board[0][2]==board[1][1])&&(board[1][1]==board[2][0])))
{
if(board[1][1] == 'X')
printf("player win\n");
if(board[1][1] == '0')
printf("computer win\n");
}
for(i=0;i<3;i++)
{
if((board[i][0]==board[i][1])&&(board[i][1]==board[i][2])&&(board[0][i] != ' '))
//((board[0][i]==board[1][i])&&(board[1][i]==board[2][i]))
{
if(board[i][0] == 'X')//&&(board[0][i] == 'X'))
printf("player win\n");
if(board[i][0] == '0')//&&(board[i][0] == '0'))
printf("computer win\n");
}
}
for(i=0;i<3;i++)
{
if((board[0][i]==board[1][i])&&(board[1][i]==board[2][i])&&(board[0][i] != ' '))
{
if(board[0][i] == 'X');
printf("player win\n");
if(board[0][i] == '0');
printf("computer win\n");
}
}
}