-
Notifications
You must be signed in to change notification settings - Fork 3
/
judgement.c
219 lines (179 loc) · 3.78 KB
/
judgement.c
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/********************************************************
* @author Airead Fan <[email protected]> *
* @date 2011 9月 19 21:27:17 CST *
********************************************************
* after studying C 63 days *
* after studying APUE 28 days *
********************************************************/
#include <stdio.h>
#include <SDL/SDL.h>
#include "xoox.h"
#include "judgement.h"
#define TEST 1
static int leader;
#if TEST
void print_array(int num[][CHESSBOARD_COLUMN], char *name)
{
int i, j;
printf("%s:\n", name);
for(i = 0; i < CHESSBOARD_ROW; i++){
for(j = 0; j < CHESSBOARD_COLUMN; j++){
printf("%4d", num[i][j]);
}
printf("\n");
}
printf("\n");
}
#endif
int mouse_to_piece_map(int mouse_map[][CHESSBOARD_COLUMN], int pieces_map[][CHESSBOARD_COLUMN])
{
int i, j;
int win = 0;
int play_stat;
#if TEST
print_array(pieces_map, "mouse to piece map: receive pieces_map");
#endif
for(i = 0; i < CHESSBOARD_ROW; i++){
for(j = 0; j < CHESSBOARD_COLUMN; j++){
if(mouse_map[i][j] & MOUSECLICK ){
if(pieces_map[i][j] == 0){
win = leader++ % 2 + 1;
pieces_map[i][j] = win;
}
mouse_map[i][j] = 0; /* set mouse map empty */
}
}
}
play_stat = judge_win(win, pieces_map);
#if TEST
printf("leader = %d\n", leader - 1);
printf("mouse to picec map: win = %d\n", win);
printf("play_stat = %d\n", play_stat);
#endif
return play_stat;
}
int judge_win(int win, int pieces[][CHESSBOARD_COLUMN]) /*l 为玩家编号,同时也是玩家棋子编号*/
{
int i ,j, flag = 0, k = 0;
#if TEST
printf("judge_win: win = %d\n", win);
print_array(pieces, "judge_win: receive pieces[][]");
#endif
/********************行判断********************/
for(i = 0; i < CHESSBOARD_ROW; i++)
{
flag = 0;
for(j = 0; j < CHESSBOARD_COLUMN; j++)
{
if(pieces[i][j] == win)
{
flag++;
if(flag == W) return win; //返回和玩家编号相同,以此定
}
if(pieces[i][j] != win)
{
flag = 0;
}
}
}
/********************列判断********************/
for(j = 0; j < CHESSBOARD_ROW; j++)
{
flag = 0;
for(i = 0; i < CHESSBOARD_ROW; i++)
{
if(pieces[i][j] == win)
{
flag++;
#if TEST
printf("flag = %d\n", flag);
#endif
if(flag == W) return win;
}
if(pieces[i][j] != win)
{
flag = 0;
}
}
}
/********************右斜判断********************/
for(k = 0; k < CHESSBOARD_ROW ; k++)
{
flag = 0;
for(i = k, j = 0; j < CHESSBOARD_COLUMN ; i--, j++)
{
if(pieces[i][j] == win)
{
flag++;
if(flag == W) return win;
}
if(pieces[i][j] != win)
{
flag = 0;
}
}
}
for(k = 2 * CHESSBOARD_ROW - 2; k >= CHESSBOARD_ROW; k--)
{
flag = 0;
for(i = CHESSBOARD_ROW - 1, j = k - CHESSBOARD_ROW + 1; j < CHESSBOARD_ROW; i--, j++)
{
if(pieces[i][j] == win)
{
flag++;
if(flag == W) return win;
}
if(pieces[i][j] != win)
{
flag = 0;
}
}
}
/********************左斜判断********************/
for(k = CHESSBOARD_ROW - 1; k >= 0; k--)
{
flag = 0;
for(i = 0, j = k; j < CHESSBOARD_ROW; j++, i++)
{
if(pieces[i][j] == win)
{
flag++;
if(flag == W) return win;
}
if(pieces[i][j] != win)
{
flag = 0;
}
}
}
for(k = 1; k < CHESSBOARD_ROW; k++)
{
flag = 0;
for(i = k, j = 0; i < CHESSBOARD_ROW; j++, i++)
{
if(pieces[i][j] == win)
{
flag++;
if(flag == W) return win;
}
if(pieces[i][j] != win)
{
flag = 0;
}
}
}
/****************判断和棋****************************************/
for(i = 0 ; i < CHESSBOARD_ROW; i++)
{
for(j = 0; j < CHESSBOARD_ROW; j++)
{
if(pieces[i][j] == 0)
return 0; /*返回0代表继续*/
}
}
return 3; /*返回为3代表和棋*/
}
int get_leader()
{
return leader % 2 + 1;
}