-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimalshogi.cpp
executable file
·309 lines (266 loc) · 7.2 KB
/
animalshogi.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include <iostream>
#include <cassert>
#include <map>
#include <set>
#include <vector>
#include <string>
#define BUFFER_SIZE (1000 * 1000)
typedef char koma_v;
union pos {
struct {
koma_v x;
koma_v y;
};
short value;
};
struct pos_comparer {
bool operator()(const pos& x, const pos& y) const {
return x.value < y.value;
}
};
typedef std::set<pos, pos_comparer> pos_set;
typedef std::map<pos, int, pos_comparer> pos_map;
class hiyoko {
public:
static const unsigned char suji() { return 0x40; /* 0b010_0_0_000 */ }
static const unsigned char reverse() { return 0xfa; /* 0b111_1_1_010 */ }
static const char display() { return 'H'; }
};
class elephant {
public:
static const unsigned char suji() { return 0xa5; /* 0b101_0_0_101 */ }
static const unsigned char reverse() { return 0; }
static const char display() { return 'E'; }
};
class giraffe {
public:
static const unsigned char suji() { return 0x5a; /* 0b010_1_1_010 */ }
static const unsigned char reverse() { return 0; }
static const char display() { return 'G'; }
};
class lion {
public:
static const unsigned char suji() { return 0xff; /* 0b111_1_1_111 */ }
static const unsigned char reverse() { return 0; }
static const char display() { return 'L'; }
};
template <class movetype>
class koma {
koma_v v_;
public:
koma() : v_(0) {}
void set(int t, int x, int y) {
v_ = (t << 5) + (x << 2) + y;
}
void move(pos new_pos) {
v_ = (v_ & 0xf0) + (new_pos.x << 2) + new_pos.y;
}
koma_v value() { return v_; }
bool const reversible() { return movetype::reverse()!=0; }
bool const ownturn(int turn) {
if (turn==0) return (v_ & 32)==0;
return (v_ & 32)!=0;
}
pos const position() {
pos x = { (v_ >> 2) & 3, v_ & 3 };
return x;
}
void kikisuji(pos_set& result) {
koma_v x = (v_ >> 2) & 3;
koma_v y = v_ & 3;
int yy;
bool upward = true, downward = true;
if (v_ & 32) { // kouban
yy = -1;
if (y==3) upward = false;
if (y==0) downward = false;
} else { // senban
yy = +1;
if (y==0) upward = false;
if (y==3) downward = false;
}
unsigned char suji = (v_ & 16)?movetype::reverse():movetype::suji();
if ((suji & 0x80)!=0 && x > 0 && upward) {
pos p = { x - 1, y - yy };
result.insert( p );
}
if ((suji & 0x40)!=0 && upward) {
pos p = { x, y - yy };
result.insert( p );
}
if ((suji & 0x20)!=0 && x < 2 && upward) {
pos p = { x + 1, y - yy };
result.insert( p );
}
if ((suji & 0x10)!=0 && x > 0 ) {
pos p = { x - 1, y };
result.insert( p );
}
if ((suji & 0x08)!=0 && x < 2 ) {
pos p = { x + 1, y };
result.insert( p );
}
if ((suji & 0x04)!=0 && x > 0 && downward) {
pos p = { x - 1, y + yy };
result.insert( p );
}
if ((suji & 0x02)!=0 && downward) {
pos p = { x , y + yy };
result.insert( p );
}
if ((suji & 0x01)!=0 && x < 2 && downward) {
pos p = { x + 1, y + yy };
result.insert( p );
}
}
void put_stage_char(char *buf, std::string &m1, std::string &m2) {
char c = (v_ & 16)?'C':movetype::display();
if (v_ & 32) c |= 0x20;
int x = v_ & 0x0f;
if (x<12) {
buf[x] = c;
} else {
if (v_ & 16) {
m2.push_back(c);
} else {
m1.push_back(c);
}
}
}
};
union stage {
struct {
koma<hiyoko> k1;
koma<elephant> k2;
koma<giraffe> k3;
koma<lion> k4;
koma<hiyoko> k5;
koma<elephant> k6;
koma<giraffe> k7;
koma<lion> k8;
};
koma_v data[8];
long long value;
void initialize() {
k1.set(0, 1, 2);
k2.set(0, 0, 3);
k3.set(0, 2, 3);
k4.set(0, 1, 3);
k5.set(1, 1, 1);
k6.set(1, 2, 0);
k7.set(1, 0, 0);
k8.set(1, 1, 0);
}
template <class func_class>
void each( func_class& c ){
c(*this, k1, 0);
c(*this, k2, 1);
c(*this, k3, 2);
c(*this, k4, 3);
c(*this, k5, 4);
c(*this, k6, 5);
c(*this, k7, 6);
c(*this, k8, 7);
}
};
struct stage_comparer {
bool operator()(const stage& x, const stage& y) const {
return x.value < y.value;
}
};
typedef std::set<stage, stage_comparer> stage_set;
typedef std::vector<stage> stage_vector;
class stage_condition {
int turn_;
pos_set rival_suji;
pos_map map_;
public:
stage_condition(int turn) : turn_(turn) {}
pos_set& kikisuji() { return rival_suji; }
pos_map& map() { return map_; }
template <class movetype>
void operator()(const stage& now_stage, koma<movetype>& x, int index) {
map_[x.position()] = index;
if (!x.ownturn(turn_)) x.kikisuji(rival_suji);
}
};
class walk_step {
int turn_;
stage_vector::iterator& it_;
stage_condition &cond_;
public:
walk_step(stage_vector::iterator& it, stage_condition &cond, int turn) : turn_(turn), cond_(cond), it_(it) {}
template <class movetype>
void operator()(const stage& now_stage, koma<movetype>& x, int index) {
if (x.ownturn(turn_)) {
pos_set suji;
x.kikisuji(suji);
pos_set::const_iterator it;
for(it = suji.begin();it != suji.end();++it) {
koma<movetype> new_x = x;
new_x.move(*it);
pos p = new_x.position();
//std::cout << (int)p.x << ", " << (int)p.y << " : " << cond_.map().count(p) << std::endl;
if (cond_.map().count(p) == 0) { // || (now_stage.data[cond_.map()[p]] != index) {
stage new_stage = now_stage;
new_stage.data[index] = new_x.value();
*it_++ = new_stage;
}
}
}
}
};
void next_step( stage_vector::iterator& prev, stage_vector::iterator& post, int turn) {
stage_vector::iterator prev_end = post;
while (prev != prev_end) {
stage x = *prev;
stage_condition cond(turn);
x.each(cond);
walk_step walk(post, cond, turn);
x.each(walk);
++prev;
}
}
class put_stage_char {
char buf[12];
std::string m1, m2;
public:
put_stage_char() {
std::memset(buf, 0x20, sizeof(buf));
}
std::string& sente() { return m1; }
std::string& go_te() { return m2; }
std::string display() {
std::string s(buf, buf+4);
s.push_back(0x0a);
s.append(buf+4, buf+8);
s.push_back(0x0a);
s.append(buf+8, buf+12);
return s;
}
template <class movetype>
void operator()(const stage& now_stage, koma<movetype>& x, int index) {
x.put_stage_char(buf, m1, m2);
}
};
void print_stage(stage& x) {
put_stage_char mochigoma;
x.each(mochigoma);
std::cout << mochigoma.display() << std::endl;
std::cout << "[" << mochigoma.go_te() << "] [" << mochigoma.sente() << "]" << std::endl;
}
int main(int argc, char *argv[]) {
assert(sizeof(stage)==8);
stage initial;
initial.initialize();
stage_vector buffer;
buffer.resize(BUFFER_SIZE);
print_stage(initial);
stage_vector::iterator prev = buffer.begin();
stage_vector::iterator post = buffer.begin();
*post++ = initial;
next_step(prev, post, 0);
for(stage_vector::iterator it=prev;it!=post;++it) {
print_stage(*it);
}
}