Main Page
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
include
osl
ptypeTraits.h
Go to the documentation of this file.
1
/* ptypeTraits.h
2
*/
3
#ifndef OSL_PTYPETRAITS_H
4
#define OSL_PTYPETRAITS_H
5
6
#include "
osl/misc/mask.h
"
7
#include "
osl/misc/loki.h
"
8
#include "
osl/ptype.h
"
9
#include "
osl/directionTraits.h
"
10
#include "
osl/square.h
"
11
12
namespace
osl
13
{
14
template
<Ptype T>
15
struct
PtypeTraits
;
16
17
template
<>
18
struct
PtypeTraits
<
PTYPE_EMPTY
>
19
{
20
static
const
bool
isBasic
=
false
;
21
static
const
bool
canPromote
=
false
;
23
static
const
bool
betterToPromote=
false
;
24
static
const
char
*
name
() {
return
"PTYPE_EMPTY"
;}
25
static
const
char
*
csaName
() {
return
".."
;}
26
static
const
int
moveMask=0;
27
};
28
29
template
<>
30
struct
PtypeTraits
<
PTYPE_EDGE
>
31
{
32
static
const
bool
isBasic
=
false
;
33
static
const
bool
canPromote
=
false
;
34
static
const
bool
betterToPromote=
false
;
35
static
const
char
*
name
() {
return
"PTYPE_EDGE"
;}
36
static
const
char
*
csaName
() {
return
"XX"
;}
37
static
const
int
moveMask=0;
38
};
39
40
template
<>
41
struct
PtypeTraits
<
GOLD
>
42
{
43
static
const
bool
isBasic
=
true
;
44
static
const
bool
canPromote
=
false
;
45
static
const
bool
betterToPromote=
false
;
46
static
const
Ptype
moveType=
GOLD
;
47
static
const
char
*
name
() {
return
"GOLD"
;}
48
static
const
char
*
csaName
() {
return
"KI"
;}
49
static
const
int
indexMin=26;
50
static
const
int
indexLimit=30;
51
static
const
int
dropBlackFromY=1;
52
static
const
int
dropBlackToY=9;
53
static
const
Ptype
basicType=
GOLD
;
54
static
const
int
moveMask=
55
DirectionTraits<UL>::mask
|
DirectionTraits<U>::mask
56
|
DirectionTraits<UR>::mask
|
DirectionTraits<L>::mask
57
|
DirectionTraits<R>::mask
|
DirectionTraits<D>::mask
;
58
};
59
60
template
<>
61
struct
PtypeTraits
<
PAWN
>
62
{
63
static
const
bool
isBasic
=
true
;
64
static
const
bool
canPromote
=
true
;
65
static
const
bool
betterToPromote=
true
;
66
static
const
Ptype
moveType=
PAWN
;
67
static
const
Ptype
basicType=
PAWN
;
68
static
const
char
*
name
() {
return
"PAWN"
;}
69
static
const
char
*
csaName
() {
return
"FU"
;}
70
static
const
int
indexMin=0;
71
static
const
int
indexLimit=18;
72
static
const
int
dropBlackFromY=2;
73
static
const
int
dropBlackToY=9;
74
static
const
int
mayPromoteToY=4;
75
static
const
int
moveMask=
DirectionTraits<U>::mask
;
76
};
77
78
template
<>
79
struct
PtypeTraits
<
PPAWN
>
80
{
81
static
const
bool
isBasic
=
false
;
82
static
const
bool
canPromote
=
false
;
83
// 疑問 falseの方がよいのでは?
84
static
const
bool
betterToPromote=
true
;
85
static
const
Ptype
moveType=
GOLD
;
86
static
const
char
*
name
() {
return
"PPAWN"
;}
87
static
const
char
*
csaName
() {
return
"TO"
;}
88
static
const
int
moveMask=
PtypeTraits<GOLD>::moveMask
;
89
static
const
Ptype
basicType=
PAWN
;
90
static
const
int
indexMin=
PtypeTraits<basicType>::indexMin
;
91
};
92
93
template
<>
94
struct
PtypeTraits
<
LANCE
>
95
{
96
static
const
bool
isBasic
=
true
;
97
static
const
bool
canPromote
=
true
;
98
static
const
bool
betterToPromote=
false
;
99
static
const
Ptype
moveType=
LANCE
;
100
static
const
Ptype
basicType=
LANCE
;
101
static
const
char
*
name
() {
return
"LANCE"
;}
102
static
const
char
*
csaName
() {
return
"KY"
;}
103
static
const
int
indexMin=32;
104
static
const
int
indexLimit=36;
105
static
const
int
dropBlackFromY=2;
106
static
const
int
dropBlackToY=9;
107
static
const
int
mayPromoteToY=9;
108
static
const
int
moveMask=
DirectionTraits<LONG_U>::mask
;
109
};
110
111
template
<>
112
struct
PtypeTraits
<
PLANCE
>
113
{
114
static
const
bool
isBasic
=
false
;
115
static
const
bool
canPromote
=
false
;
116
static
const
bool
betterToPromote=
false
;
117
static
const
Ptype
moveType=
GOLD
;
118
static
const
char
*
name
() {
return
"PLANCE"
;}
119
static
const
char
*
csaName
() {
return
"NY"
;}
120
static
const
int
moveMask=
PtypeTraits<GOLD>::moveMask
;
121
static
const
Ptype
basicType=
LANCE
;
122
static
const
int
indexMin=
PtypeTraits<basicType>::indexMin
;
123
};
124
125
template
<>
126
struct
PtypeTraits
<
KNIGHT
>
127
{
128
static
const
bool
isBasic
=
true
;
129
static
const
bool
canPromote
=
true
;
130
static
const
bool
betterToPromote=
false
;
131
static
const
Ptype
moveType=
KNIGHT
;
132
static
const
Ptype
basicType=
KNIGHT
;
133
static
const
char
*
name
() {
return
"KNIGHT"
;}
134
static
const
char
*
csaName
() {
return
"KE"
;}
135
static
const
int
indexMin=18;
136
static
const
int
indexLimit=22;
137
static
const
int
dropBlackFromY=3;
138
static
const
int
dropBlackToY=9;
139
static
const
int
mayPromoteToY=5;
140
static
const
int
moveMask=
DirectionTraits<UUL>::mask
|
DirectionTraits<UUR>::mask
;
141
};
142
143
template
<>
144
struct
PtypeTraits
<
PKNIGHT
>
145
{
146
static
const
bool
isBasic
=
false
;
147
static
const
bool
canPromote
=
false
;
148
static
const
bool
betterToPromote=
false
;
149
static
const
Ptype
moveType=
GOLD
;
150
static
const
char
*
name
() {
return
"PKNIGHT"
;}
151
static
const
char
*
csaName
() {
return
"NK"
;}
152
static
const
int
moveMask=
PtypeTraits<GOLD>::moveMask
;
153
static
const
Ptype
basicType=
KNIGHT
;
154
static
const
int
indexMin=
PtypeTraits<basicType>::indexMin
;
155
};
156
157
template
<>
158
struct
PtypeTraits
<
SILVER
>
159
{
160
static
const
bool
isBasic
=
true
;
161
static
const
bool
canPromote
=
true
;
162
static
const
bool
betterToPromote=
false
;
163
static
const
Ptype
moveType=
SILVER
;
164
static
const
Ptype
basicType=
SILVER
;
165
static
const
char
*
name
() {
return
"SILVER"
;}
166
static
const
char
*
csaName
() {
return
"GI"
;}
167
static
const
int
indexMin=22;
168
static
const
int
indexLimit=26;
169
static
const
int
dropBlackFromY=1;
170
static
const
int
dropBlackToY=9;
171
static
const
int
mayPromoteToY=4;
172
static
const
int
moveMask=
173
DirectionTraits<UL>::mask
|
DirectionTraits<U>::mask
174
|
DirectionTraits<UR>::mask
|
DirectionTraits<DL>::mask
175
|
DirectionTraits<DR>::mask
;
176
};
177
178
template
<>
179
struct
PtypeTraits
<
PSILVER
>
180
{
181
static
const
bool
isBasic
=
false
;
182
static
const
bool
canPromote
=
false
;
183
static
const
bool
betterToPromote=
false
;
184
static
const
Ptype
moveType=
GOLD
;
185
static
const
char
*
name
() {
return
"PSILVER"
;}
186
static
const
char
*
csaName
() {
return
"NG"
;}
187
static
const
int
moveMask=
PtypeTraits<GOLD>::moveMask
;
188
static
const
Ptype
basicType=
SILVER
;
189
static
const
int
indexMin=
PtypeTraits<basicType>::indexMin
;
190
};
191
192
template
<>
193
struct
PtypeTraits
<
BISHOP
>
194
{
195
static
const
bool
isBasic
=
true
;
196
static
const
bool
canPromote
=
true
;
197
static
const
bool
betterToPromote=
true
;
198
static
const
Ptype
moveType=
BISHOP
;
199
static
const
Ptype
basicType=
BISHOP
;
200
static
const
char
*
name
() {
return
"BISHOP"
;}
201
static
const
char
*
csaName
() {
return
"KA"
;}
202
static
const
int
indexMin=36;
203
static
const
int
indexLimit=38;
204
static
const
int
dropBlackFromY=1;
205
static
const
int
dropBlackToY=9;
206
static
const
int
mayPromoteToY=9;
207
static
const
int
moveMask=
208
DirectionTraits<LONG_UL>::mask
|
DirectionTraits<LONG_UR>::mask
209
|
DirectionTraits<LONG_DL>::mask
|
DirectionTraits<LONG_DR>::mask
;
210
};
211
212
template
<>
213
struct
PtypeTraits
<
PBISHOP
>
214
{
215
static
const
bool
isBasic
=
false
;
216
static
const
bool
canPromote
=
false
;
217
// 疑問 falseの方がよいのでは?
218
static
const
bool
betterToPromote=
true
;
219
static
const
Ptype
moveType=
PBISHOP
;
220
static
const
char
*
name
() {
return
"PBISHOP"
;}
221
static
const
char
*
csaName
() {
return
"UM"
;}
222
static
const
int
moveMask=
223
DirectionTraits<LONG_UL>::mask
|
DirectionTraits<LONG_UR>::mask
224
|
DirectionTraits<LONG_DL>::mask
|
DirectionTraits<LONG_DR>::mask
225
|
DirectionTraits<U>::mask
|
DirectionTraits<L>::mask
226
|
DirectionTraits<R>::mask
|
DirectionTraits<D>::mask
;
227
static
const
Ptype
basicType=
BISHOP
;
228
static
const
int
indexMin=
PtypeTraits<basicType>::indexMin
;
229
};
230
231
template
<>
232
struct
PtypeTraits
<
ROOK
>
233
{
234
static
const
bool
isBasic
=
true
;
235
static
const
bool
canPromote
=
true
;
236
static
const
bool
betterToPromote=
true
;
237
static
const
Ptype
moveType=
ROOK
;
238
static
const
Ptype
basicType=
ROOK
;
239
static
const
char
*
name
() {
return
"ROOK"
;}
240
static
const
char
*
csaName
() {
return
"HI"
;}
241
static
const
int
indexMin=38;
242
static
const
int
indexLimit=40;
243
static
const
int
dropBlackFromY=1;
244
static
const
int
dropBlackToY=9;
245
static
const
int
mayPromoteToY=9;
246
static
const
int
moveMask=
247
DirectionTraits<LONG_U>::mask
|
DirectionTraits<LONG_L>::mask
248
|
DirectionTraits<LONG_R>::mask
|
DirectionTraits<LONG_D>::mask
;
249
};
250
251
template
<>
252
struct
PtypeTraits
<
PROOK
>
253
{
254
static
const
bool
isBasic
=
false
;
255
static
const
bool
canPromote
=
false
;
256
// 疑問 falseの方がよいのでは?
257
static
const
bool
betterToPromote=
true
;
258
static
const
Ptype
moveType=
PROOK
;
259
static
const
char
*
name
() {
return
"PROOK"
;}
260
static
const
char
*
csaName
() {
return
"RY"
;}
261
static
const
int
moveMask=
262
DirectionTraits<LONG_U>::mask
|
DirectionTraits<LONG_L>::mask
263
|
DirectionTraits<LONG_R>::mask
|
DirectionTraits<LONG_D>::mask
264
|
DirectionTraits<UL>::mask
|
DirectionTraits<UR>::mask
265
|
DirectionTraits<DL>::mask
|
DirectionTraits<DR>::mask
;
266
static
const
Ptype
basicType=
ROOK
;
267
static
const
int
indexMin=
PtypeTraits<basicType>::indexMin
;
268
};
269
270
271
template
<>
272
struct
PtypeTraits
<
KING
>
273
{
274
static
const
bool
isBasic
=
true
;
275
static
const
bool
canPromote
=
false
;
276
static
const
bool
betterToPromote=
false
;
277
static
const
Ptype
moveType=
KING
;
278
static
const
Ptype
basicType=
KING
;
279
static
const
char
*
name
() {
return
"KING"
;}
280
static
const
char
*
csaName
() {
return
"OU"
;}
281
static
const
int
indexMin=30;
282
static
const
int
indexLimit=32;
283
static
const
int
dropBlackFromY=1;
284
static
const
int
dropBlackToY=9;
285
static
const
int
moveMask=
286
DirectionTraits<U>::mask
|
DirectionTraits<L>::mask
287
|
DirectionTraits<R>::mask
|
DirectionTraits<D>::mask
288
|
DirectionTraits<UL>::mask
|
DirectionTraits<UR>::mask
289
|
DirectionTraits<DL>::mask
|
DirectionTraits<DR>::mask
;
290
};
291
292
template
<Ptype T,
bool
IsBasic>
293
struct
PtypeFunsSub
;
294
295
template
<Ptype T>
296
struct
PtypeFunsSub
<T,true>
297
{
298
#if OSL_WORDSIZE == 64
299
static
const
mask_int_t indexMask=
static_cast<
mask_int_t
>
((-1LL<<(PtypeTraits<T>::indexMin))^(-1LL<<(PtypeTraits<T>::indexLimit)));
300
#elif OSL_WORDSIZE == 32
301
static
const
mask_int_t indexMask=
static_cast<
mask_int_t
>
((-1<<(
PtypeTraits<T>::indexMin
&31))^((-1<<(((
PtypeTraits<T>::indexLimit
-1)&31)))<<1));
302
#endif
303
static
const
Ptype
promotePtype=
static_cast<
Ptype
>
(
static_cast<
int
>
(T)-8);
304
static
const
Ptype
basicType = T;
305
};
306
307
template
<Ptype T>
308
struct
PtypeFunsSub
<T,false>
309
{
310
static
const
mask_int_t indexMask=
static_cast<
mask_int_t
>
(0);
311
// static const Ptype promotePtype=PTYPE_EMPTY;
312
static
const
Ptype
promotePtype=T;
313
static
const
Ptype
basicType =
PtypeTraits<T>::basicType
;
314
};
315
316
template
<Ptype T>
317
struct
PtypeFuns
318
{
319
#if OSL_WORDSIZE == 64
320
static
const
unsigned
int
indexNum=0;
321
#elif OSL_WORDSIZE == 32
322
static
const
unsigned
int
indexNum=(
PtypeTraits<T>::indexMin
>> 5);
323
#endif
324
static
const
bool
hasLongMove
=(
PtypeTraits<T>::indexMin
>=32);
325
static
const
mask_int_t
indexMask
=
PtypeFunsSub<T,PtypeTraits<T>::isBasic
>
::indexMask
;
326
static
const
Ptype
promotePtype
=
PtypeFunsSub<T,PtypeTraits<T>::canPromote
>
::promotePtype
;
327
static
const
Ptype
basicType
=
PtypeFunsSub<T,PtypeTraits<T>::isBasic
>
::basicType
;
328
};
329
334
enum
MoveConstraint
{
336
CannotMove
,
338
OnlyPromoted
,
340
OnlyBasic
,
342
NoConstraint
,
343
};
344
345
template
<Ptype T,Direction D>
346
struct
PtypeDirectionTraits
347
{
348
static
const
bool
hasMove
=(
PtypeTraits<T>::moveMask
&
DirectionTraits<D>::mask
)!=0;
349
static
const
bool
canMove
=
350
(
PtypeTraits<T>::moveMask
&
DirectionTraits<D>::mask
)!=0 ||
351
(
PtypeTraits<T>::moveMask
&
352
DirectionTraits
<
DirectionTraits<D>::longDir
>::mask)!=0;
353
static
const
MoveConstraint
moveConstraint
=
354
(
PtypeDirectionTraits<T,D>::canMove
355
? (
PtypeDirectionTraits<PtypeFuns<T>::promotePtype
,
D
>
::canMove
356
?
NoConstraint
:
OnlyBasic
)
357
: (
PtypeDirectionTraits
<
PtypeFuns<T>::promotePtype
,
D
>::
canMove
358
?
OnlyPromoted
:
CannotMove
));
359
};
360
361
362
template
<Player T>
363
struct
KingTraits
364
{
365
static
const
int
index
=
PtypeTraits<KING>::indexMin
+
PlayerTraits<T>::index
;
366
};
367
368
template
<Ptype T,Player P>
369
struct
PtypePlayerTraits
370
{
371
static
bool
canDropTo
(
Square
/*pos*/
,Int2Type<false>)
372
{
373
assert(0);
374
return
false
;
375
}
376
static
bool
canDropTo
(
Square
pos,Int2Type<true>)
377
{
378
if
(
PtypeTraits<T>::dropBlackFromY
== 1)
379
return
true
;
380
381
if
(P==
BLACK
)
382
return
pos.
y
() >=
PtypeTraits<T>::dropBlackFromY
;
383
else
384
return
pos.
y
() <=
Square::reverseY
(
PtypeTraits<T>::dropBlackFromY
);
385
}
386
static
bool
canDropTo
(
Square
pos)
387
{
388
return
canDropTo
(pos,Int2Type<
PtypeTraits<T>::isBasic
>());
389
}
394
static
bool
mayPromote
(
Square
pos,Int2Type<true>)
395
{
396
if
(
PtypeTraits<T>::mayPromoteToY
== 9)
397
return
true
;
398
399
if
(P==
BLACK
)
400
return
pos.
y
() <=
PtypeTraits<T>::mayPromoteToY
;
401
else
402
return
pos.
y
() >=
Square::reverseY
(
PtypeTraits<T>::mayPromoteToY
);
403
}
404
static
bool
mayPromote
(
Square
pos)
405
{
406
return
mayPromote
(pos,Int2Type<
PtypeTraits<T>::isBasic
&&
407
PtypeTraits<T>::canPromote
>());
408
}
412
static
bool
mustPromote
(
Square
pos)
413
{
414
if
(P==
BLACK
){
415
if
(T==
PAWN
|| T==
LANCE
)
return
pos.
yEq
<2>();
416
else
if
(T==
KNIGHT
)
return
pos.
yLe
<4>();
417
else
return
false
;
418
}
419
else
{
420
if
(T==
PAWN
|| T==
LANCE
)
return
pos.
yEq
<8>();
421
else
if
(T==
KNIGHT
)
return
pos.
yGe
<6>();
422
else
return
false
;
423
}
424
}
428
static
bool
canPromote
(
Square
pos)
429
{
430
if
(P==
BLACK
){
431
if
(T==
PAWN
|| T==
LANCE
)
return
pos.
yLe
<4>();
432
else
if
(T==
KNIGHT
)
return
pos.
yLe
<5>();
433
else
return
pos.
yLe
<3>();
434
}
435
else
{
436
if
(T==
PAWN
|| T==
LANCE
)
return
pos.
yGe
<6>();
437
else
if
(T==
KNIGHT
)
return
pos.
yGe
<5>();
438
else
return
pos.
yGe
<7>();
439
}
440
}
445
static
bool
checkPromote
(
Square
pos)
446
{
447
if
(P==
BLACK
){
448
if
(T==
SILVER
)
return
pos.
yEq
<4>();
449
else
if
(T==
LANCE
|| T==
ROOK
|| T==
BISHOP
)
450
return
true
;
451
else
return
false
;
452
}
453
else
{
454
if
(T==
SILVER
)
return
pos.
yEq
<6>();
455
else
if
(T==
LANCE
|| T==
ROOK
|| T==
BISHOP
)
456
return
true
;
457
else
return
false
;
458
}
459
}
463
static
bool
noPromote
(
Square
pos)
464
{
465
if
(P==
BLACK
){
466
if
(T==
PAWN
|| T==
SILVER
)
return
pos.
yGe
<5>();
467
else
if
(T==
KNIGHT
)
return
pos.
yGe
<6>();
468
else
if
(T==
LANCE
|| T==
ROOK
|| T==
BISHOP
)
return
false
;
469
else
return
true
;
470
}
471
else
{
472
if
(T==
PAWN
|| T==
SILVER
)
return
pos.
yLe
<5>();
473
else
if
(T==
KNIGHT
)
return
pos.
yLe
<4>();
474
else
if
(T==
LANCE
|| T==
ROOK
|| T==
BISHOP
)
return
false
;
475
else
return
true
;
476
}
477
}
478
};
479
480
481
}
// namespace osl
482
483
#endif
/* OSL_PTYPETRAITS_H */
484
// ;;; Local Variables:
485
// ;;; mode:c++
486
// ;;; c-basic-offset:2
487
// ;;; End:
Generated on Sun Jul 21 2013 13:37:26 by
1.8.4