-
Notifications
You must be signed in to change notification settings - Fork 0
/
reversi2.c~
214 lines (197 loc) · 3.98 KB
/
reversi2.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
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
struct xy
{
int x;
int y;
};
struct xy directions[8] = {
{ -1, -1 }, { 0, -1 }, { 1, -1 },
{ -1, 0 }, { 1, 0 },
{ -1, 1 }, { 0, 1 }, { 1, 1 }
};
int board[8][8];
void init_board()
{
int x, y;
for (y = 0; y < 8; y++)
for (x = 0; x < 8; x++)
board[x][y] = 0;
board[4][3] = board[3][4] = 1; // black disks
board[3][3] = board[4][4] = -1; // white disks
}
void print_board()
{
int x, y;
puts("\n abcdefgh");
for (y = 0; y < 8; y++) {
printf("%d", y + 1);
for (x = 0; x < 8; x++) {
const int d = board[x][y];
char c = '.';
if (d == 1) c = 'O'; // black disk
if (d == -1) c = '#'; // white disk
putchar(c);
}
putchar('\n');
}
putchar('\n');
}
int can_flip(const int side,
struct xy sq, const struct xy dir) // square, direction
{
assert(board[sq.x][sq.y] == 0);
int n = 0;
do {
sq.x += dir.x;
sq.y += dir.y;
n++;
if (sq.x < 0 || sq.x >= 8 || sq.y < 0 || sq.y >= 8) return 0;
} while (board[sq.x][sq.y] == -side);
if (board[sq.x][sq.y] == 0) return 0;
if (n <= 1) return 0;
return 1;
}
int is_legal_move(int side, struct xy sq)
{
assert(sq.x >= 0 && sq.x < 8 && sq.y >= 0 && sq.y < 8);
int i;
if (board[sq.x][sq.y] != 0) return 0;
for (i = 0; i < 8; i++) {
if (can_flip(side, sq, directions[i])) return 1;
}
return 0;
}
int place_disk(const int side, const struct xy sq)
{
assert(is_legal_move(side, sq));
int n = 0, i;
for (i = 0; i < 8; i++) {
const struct xy dir = directions[i];
if (!can_flip(side, sq, dir)) continue;
int x = sq.x + dir.x;
int y = sq.y + dir.y;
while (board[x][y] == -side) {
board[x][y] = side;
n++;
x += dir.x;
y += dir.y;
}
}
board[sq.x][sq.y] = side;
assert(n > 0);
return n;
}
int generate_random_move(const int side, struct xy *move)
{
struct xy legal_moves[60];
int x, y, nmoves = 0, nempty = 0;
for (x = 0; x < 8; x++) {
for (y = 0; y < 8; y++) {
if (board[x][y] != 0) continue;
nempty++;
struct xy sq;
sq.x = x; sq.y = y;
if (!is_legal_move(side, sq)) continue;
assert(nmoves < 60);
legal_moves[nmoves++] = sq;
}
}
if (nempty == 0) return -1;
if (nmoves == 0) return 0;
*move = legal_moves[rand() % nmoves];
return 1;
}
int pass(){
int x,y,flag;
struct xy move;
flag = 0;
for(x = 0; x < 8; x ++){
for(y = 0; y < 8; y++){
move.x = x;
move.y = y;
if (is_legal_move(1,move)){
flag = 1;
return 0;
break;
}
}
}
if(flag == 0){
return 1;
}
}
void win_lose(){
int x,y;
int n1,n2;
n1 = 0;
n2 = 0;
int result = 0;
for(x =0; x < 8; x++){
for(y = 0; y < 8; y ++){
if(board[x][y] == 1){
n1++;
}
else if(board[x][y] == -1){
n2++;
}
}
}
result = n1 - n2;
if(result > 0){
printf("O has won the game\n");
}
else if(result == 0){
printf("Drew\n");
}
else{
printf("# has won this game\n");
}
}
int main(int argc, char **argv)
{
const int human_side = (argc >= 2) ? atoi(argv[1]) : 0;
init_board();
print_board();
int state1,state2;
int turn;
for (turn = 1;; turn *= -1) {
struct xy move;
if (turn == human_side) {
if(!pass()){
state1 = 0;
do {
char buf[1000];
printf("Where? ");
scanf("%s", buf);
move.x = buf[0] - 'a';
move.y = buf[1] - '1';
} while (!is_legal_move(turn, move));
}
else{
printf("pass\n");
state1 = 1;
}
} else {
const int r = generate_random_move(turn, &move);
if (r == -1) break; // no empty square
if (r == 0) {
state2 = 1;
continue;
} // pass (no legal move)
else{
state2 = 0;
}
printf("turn = %d, move = %c%c\n", turn, 'a' + move.x, '1' + move.y);
}
if(is_legal_move(turn,move))
place_disk(turn, move);
print_board();
if(state1 == 1 && state2 == 1){
break;
}
}
win_lose();
}