-
Notifications
You must be signed in to change notification settings - Fork 5
/
UVA-220.cpp
151 lines (142 loc) · 2.5 KB
/
UVA-220.cpp
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
#include<cstdio>
#define MAXN 10
char arr[MAXN][MAXN];
char player;
char x,y;
void changeBW() {
if(player == 'B')
player = 'W';
else
player = 'B';
}
void showBoard() {
int i,j;
for(i=1; i<9; ++i) {
for(j=1; j<9; ++j)
putchar(arr[i][j]);
putchar('\n');
}
}
int xstep[8] = {1,0,-1,0,1,-1,-1,1};
int ystep[8] = {0,1,0,-1,1,-1,1,-1};
bool move() {
int i,j,k;
int xt,yt;
bool flagMove = false ,flag;
for(i=0; i<8; ++i) {
xt = x + xstep[i];
yt = y + ystep[i];
if(xt < 1 || xt > 8 || yt < 1 || yt > 8)
continue;
if(arr[xt][yt] == '-' || arr[xt][yt] == player)
continue;
flag = false;
while(1) {
xt = xt + xstep[i];
yt = yt + ystep[i];
if(xt < 1 || xt > 8 || yt < 1 || yt > 8 || arr[xt][yt] == '-')
break;
if(arr[xt][yt] == player) {
flag = true;
break;
}
}
if(flag == false)
continue;
flagMove = true;
arr[x][y] = player;
for( ; !(xt==x&&yt==y); xt = xt - xstep[i],yt = yt - ystep[i])
arr[xt][yt] = player;
}
return flagMove;
}
void showList() {
int i,j,k,xt,yt;
bool flagMove = false, flag;
for(i=1;i<9;++i)
for(j=1;j<9;++j) {
if(arr[i][j] != '-')
continue;
for(k=0;k<8;++k) {
xt = i + xstep[k];
yt = j + ystep[k];
if(xt < 1 || xt > 8 || yt < 1 || yt > 8)
continue;
if(arr[xt][yt] == '-' || arr[xt][yt] == player)
continue;
flag = false;
while(1) {
xt = xt + xstep[k];
yt = yt + ystep[k];
if(xt < 1 || xt > 8 || yt < 1 || yt > 8 || arr[xt][yt] == '-')
break;
if(arr[xt][yt] == player) {
flag = true;
break;
}
}
if(flag == false)
continue;
if(flagMove == true)
putchar(' ');
flagMove = true;
printf("(%d,%d)",i,j);
break;
}
}
if(flagMove == false)
puts("No legal move.");
else
puts("");
}
void stat() {
int b=0,w=0,i,j;
for(i=1;i<9;++i)
for(j=1;j<9;++j) {
if(arr[i][j] == 'B')
++b;
if(arr[i][j] == 'W')
++w;
}
printf("Black - %2d White - %2d\n", b,w);
}
void choose() {
char c;
while(1) {
scanf(" %c", &c);
switch(c) {
case 'L':
showList();
break;
case 'M':
scanf(" %c%c",&x,&y);
x -= '0';
y -= '0';
if(move() == false) {
changeBW();
move();
}
changeBW();
stat();
break;
case 'Q':
showBoard();
return;
}
}
}
int main() {
int n,i,j,k;
char c;
scanf("%d", &n);
for(k=0; k<n; ++k) {
if(k != 0)
puts("");
for(i=1; i<9; ++i)
for(j=1; j<9; ++j)
scanf(" %c", &arr[i][j]);
scanf(" %c", &player);
choose();
}
return 0;
}