-
Notifications
You must be signed in to change notification settings - Fork 0
/
setupboard.d
179 lines (158 loc) · 5.49 KB
/
setupboard.d
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
import tango.math.random.Random;
import position;
class SetupGenerator
{
const static ulong[] rabbit_setups = [0x00FF, // Standard
0x81E7, // 99of9
0xA5A5]; // Fritz
const static ulong[] cat_setups = [0x2400,
0x2400,
0x42];
const static ulong[] dog_setups = [0x8100,
0x18,
0x18];
enum RabbitSetup { ANY = -1,
STANDARD,
NINETY_NINE,
FRITZ };
RabbitSetup rabbit_style = RabbitSetup.ANY;
bool random_minor = true;
bool random_all = false;
int[] setup_weights = [19, 80, 1];
private ulong gold_to_silver(ulong bitboard)
{
const static ulong ROW_1_MASK = 0xFF;
const static ulong ROW_2_MASK = 0xFF00;
const static int ROW_1_TO_8 = 56;
const static int ROW_2_TO_7 = 40;
return ((bitboard & ROW_2_MASK) << ROW_2_TO_7)
| ((bitboard & ROW_1_MASK) << ROW_1_TO_8);
}
private ulong adjust_side(Side s, ulong bitboard)
{
if (s == Side.BLACK)
return gold_to_silver(bitboard);
return bitboard;
}
private void randomize_minor(Side s, inout Position pos)
{
int offset = (s == Side.WHITE) ? 0 : 6;
ulong squares = pos.bitBoards[Piece.WCAT + offset] | pos.bitBoards[Piece.WDOG + offset];
pos.place_piece(cast(Piece)(Piece.WCAT + offset), 0, true);
pos.place_piece(cast(Piece)(Piece.WDOG + offset), 0, true);
int[] pieces = [Piece.WDOG + offset, Piece.WDOG + offset,
Piece.WCAT + offset, Piece.WCAT + offset];
while (squares)
{
ulong sbit = squares & -squares;
squares ^= sbit;
int pix = rand.uniformR!(int)(pieces.length);
int piece = pieces[pix];
pieces[pix] = pieces[length-1];
pieces.length = pieces.length - 1;
pos.place_piece(cast(Piece)piece, sbit);
}
}
private void randomize_all(Side s, inout Position pos)
{
int offset = (s == Side.WHITE) ? 0 : 6;
ulong squares = 0xFFFF;
if (s == Side.BLACK)
squares <<= 48;
for (Piece p = cast(Piece)(Piece.WRABBIT+offset); p <= Piece.WELEPHANT+offset; p++)
{
pos.place_piece(p, 0, true);
}
Piece[] pieces;
pieces.length = 16;
for (int i = 0; i < 8; i++)
{
pieces[i] = cast(Piece)(Piece.WRABBIT+offset);
}
Piece p = cast(Piece)(Piece.WCAT + offset);
for (int i = 8; i < 14; i++)
{
pieces[i] = p;
if (i % 2 == 1)
p++;
}
pieces[14] = cast(Piece)(Piece.WCAMEL + offset);
pieces[15] = cast(Piece)(Piece.WELEPHANT + offset);
while (squares)
{
ulong sbit = squares & -squares;
squares ^= sbit;
int pix = rand.uniformR!(int)(pieces.length);
Piece piece = pieces[pix];
pieces[pix] = pieces[length-1];
pieces.length = pieces.length - 1;
pos.place_piece(piece, sbit);
}
}
void setup_board(Side s, inout Position pos)
{
int offset = (s == Side.WHITE) ? 0 : 6;
RabbitSetup rsetup = rabbit_style;
if (rsetup == RabbitSetup.ANY)
{
int total_weight = 0;
for (int i=0; i <= RabbitSetup.max; i++)
total_weight += setup_weights[i];
int choice_weight = rand.uniformR!(int)(total_weight);
int cur_weight = 0;
for (int i=0; i <= RabbitSetup.max; i++)
{
cur_weight += setup_weights[i];
rsetup = cast(RabbitSetup)i;
if (cur_weight > choice_weight)
break;
}
}
pos.place_piece(cast(Piece)(Piece.WRABBIT+offset), adjust_side(s, rabbit_setups[rsetup]), true);
pos.place_piece(cast(Piece)(Piece.WCAT+offset), adjust_side(s, cat_setups[rsetup]), true);
pos.place_piece(cast(Piece)(Piece.WDOG+offset), adjust_side(s, dog_setups[rsetup]), true);
pos.place_piece(cast(Piece)(Piece.WHORSE+offset), adjust_side(s, 0x4200), true);
pos.place_piece(cast(Piece)(Piece.WCAMEL+offset), adjust_side(s, 0x1000), true);
pos.place_piece(cast(Piece)(Piece.WELEPHANT+offset), adjust_side(s, 0x0800), true);
if (s == Side.BLACK)
{
if (pos.bitBoards[Piece.WELEPHANT] & 0xE8F0)
{
pos.place_piece(Piece.BELEPHANT, 0, true);
pos.place_piece(Piece.BCAMEL, 0, true);
pos.place_piece(Piece.BELEPHANT, 0x10000000000000, true);
pos.place_piece(Piece.BCAMEL, 0x8000000000000, true);
}
}
if (random_minor)
randomize_minor(s, pos);
if (random_all)
randomize_all(s, pos);
}
private ulong random_bit(ulong bits)
{
int num = popcount(bits);
int bix = rand.uniformR!(int)(num);
ulong b;
for (int i=0; i <= bix; i++)
{
b = bits & -bits;
bits ^= b;
}
return b;
}
void setup_handicap(Piece[] pieces, inout Position pos)
{
ulong squares = 0xFFFF;
if (pieces[0] > Piece.WELEPHANT)
{
squares <<= 48;
}
for (int i=0; i < pieces.length; i++)
{
ulong sq = random_bit(squares);
squares ^= sq;
pos.place_piece(pieces[i], sq);
}
}
}