-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBook.cs
415 lines (389 loc) · 16 KB
/
Book.cs
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace SrcChess {
/// <summary>Handle the book opening.</summary>
public class Book {
/// <summary>Entry in the book entries</summary>
private struct BookEntry {
/// <summary>Position of this entry (Start + (End * 256))</summary>
public short Pos;
/// <summary>How many move for this entry at the index</summary>
public short Size;
/// <summary>Index in the table for the entry</summary>
public int Index;
/// <summary>How many child book entries this one has</summary>
public int Weight;
};
/// <summary>Comparer use to sort array of int</summary>
private class CompareIntArray : IComparer<int[]> {
//*********************************************************
//
/// <summary>
/// Comparer of Array of int
/// </summary>
/// <param name="x"> First array</param>
/// <param name="y"> Second array</param>
/// <returns>
/// -1 if x less than y, 1 if x greater than y, 0 if x = y
/// </returns>
//
//*********************************************************
public int Compare(int[] x, int[] y) {
int iRetVal = 0;
int iIndex;
int iMinSize;
iMinSize = x.Length;
if (y.Length < iMinSize) {
iMinSize = y.Length;
}
iIndex = 0;
while (iIndex < iMinSize && iRetVal == 0) {
if (x[iIndex] < y[iIndex]) {
iRetVal--;
} else if (x[iIndex] > y[iIndex]) {
iRetVal++;
} else {
iIndex++;
}
}
if (iRetVal == 0) {
if (x.Length < y.Length) {
iRetVal--;
} else if (x.Length > y.Length) {
iRetVal++;
}
}
return(iRetVal);
}
}
/// <summary>List of book entries</summary>
private BookEntry[] m_bookEntries;
//*********************************************************
//
/// <summary>
/// Class constructor
/// </summary>
//
//*********************************************************
public Book() {
m_bookEntries = new BookEntry[1];
m_bookEntries[0].Size = 0;
m_bookEntries[0].Pos = 0;
m_bookEntries[0].Index = 1;
m_bookEntries[0].Weight = 0;
}
//*********************************************************
//
/// <summary>
/// Compute the number of child for each child moves
/// </summary>
/// <param name="iParent"> Parent move</param>
/// <returns>
/// Nb of child
/// </returns>
//
//*********************************************************
private int ComputeWeight(int iParent) {
int iRetVal;
int iStart;
int iEnd;
iStart = m_bookEntries[iParent].Index;
iRetVal = m_bookEntries[iParent].Size;
iEnd = iStart + iRetVal;
for (int iIndex = iStart; iIndex < iEnd; iIndex++) {
iRetVal += ComputeWeight(iIndex);
}
m_bookEntries[iParent].Weight += iRetVal;
return(iRetVal);
}
//*********************************************************
//
/// <summary>
/// Compute the number of child for each child moves
/// </summary>
//
//*********************************************************
private void ComputeWeight() {
ComputeWeight(0);
}
//*********************************************************
//
/// <summary>
/// Read the book from a binary file
/// </summary>
//
//*********************************************************
public void ReadBookFromFile(string strFileName) {
FileStream fileStream;
BinaryReader reader;
string strSignature;
int iSize;
using(fileStream = File.OpenRead(strFileName)) {
reader = new BinaryReader(fileStream);
strSignature = reader.ReadString();
if (strSignature == "BOOK090") {
iSize = reader.ReadInt32();
m_bookEntries = new BookEntry[iSize];
for (int iIndex = 0; iIndex < iSize; iIndex++) {
m_bookEntries[iIndex].Pos = reader.ReadInt16();
m_bookEntries[iIndex].Size = reader.ReadInt16();
m_bookEntries[iIndex].Index = reader.ReadInt32();
}
}
}
ComputeWeight();
}
//*********************************************************
//
/// <summary>
/// Save the book to a binary file
/// </summary>
//
//*********************************************************
public void SaveBookToFile(string strFileName) {
FileStream fileStream;
BinaryWriter writer;
string strSignature = "BOOK090";
int iSize;
using(fileStream = File.Create(strFileName)) {
writer = new BinaryWriter(fileStream);
writer.Write(strSignature);
iSize = m_bookEntries.Length;
writer.Write(iSize);
for (int iIndex = 0; iIndex < iSize; iIndex++) {
writer.Write(m_bookEntries[iIndex].Pos);
writer.Write(m_bookEntries[iIndex].Size);
writer.Write(m_bookEntries[iIndex].Index);
}
}
}
//*********************************************************
//
/// <summary>
/// Find a move from the book
/// </summary>
/// <param name="arrPreviousMove"> List of previous moves</param>
/// <param name="rnd"> Random to use to pickup a move from a list. Can be null</param>
/// <returns>
/// Move in the form of StartPos + (EndPos * 256) or -1 if none found
/// </returns>
//
//*********************************************************
public short FindMoveInBook(ChessBoard.MovePosS[] arrPreviousMove, Random rnd) {
short nRetVal;
bool bFound = true;
int iMoveCount;
int iMoveIndex;
ChessBoard.MovePosS move;
int[] arrRnd;
int iStartIndex;
int iIndex;
int iSize;
int iBiggestRnd;
short nPos;
iSize = m_bookEntries[0].Size;
iStartIndex = m_bookEntries[0].Index;
iMoveCount = arrPreviousMove.Length;
iMoveIndex = 0;
while (iMoveIndex < iMoveCount && bFound) {
move = arrPreviousMove[iMoveIndex];
nPos = (short)(move.StartPos + (move.EndPos << 8));
bFound = false;
iIndex = 0;
while (iIndex < iSize && !bFound) {
if (m_bookEntries[iStartIndex + iIndex].Pos == nPos) {
bFound = true;
} else {
iIndex++;
}
}
if (bFound) {
iSize = m_bookEntries[iStartIndex + iIndex].Size;
iStartIndex = m_bookEntries[iStartIndex + iIndex].Index;
bFound = (iSize != 0);
iMoveIndex++;
}
}
if (bFound && iSize != 0) {
arrRnd = new int[iSize];
for (iIndex = 0; iIndex < iSize; iIndex++) {
arrRnd[iIndex] = (rnd == null) ? m_bookEntries[iStartIndex + iIndex].Weight + 2 : rnd.Next(m_bookEntries[iStartIndex + iIndex].Weight + 2);
}
iIndex = 0;
iBiggestRnd = -1;
for (int i = 0; i < iSize; i++) {
if (arrRnd[i] > iBiggestRnd) {
iBiggestRnd = arrRnd[i];
iIndex = i;
}
}
nRetVal = m_bookEntries[iStartIndex + iIndex].Pos;
} else {
nRetVal = -1;
}
return(nRetVal);
}
//*********************************************************
//
/// <summary>
/// Compare the begining of two lists
/// </summary>
/// <param name="piFirst"> First list</param>
/// <param name="piSecond"> Second list</param>
/// <param name="iMaxDepth"> Maximum depth to compare</param>
/// <returns>
/// true if begining is equal
/// </returns>
//
//*********************************************************
private bool CompareList(int[] piFirst, int[] piSecond, int iMaxDepth) {
bool bRetVal = true;
int iIndex;
iIndex = 0;
while (iIndex < iMaxDepth && bRetVal) {
if (piFirst[iIndex] != piSecond[iIndex]) {
bRetVal = false;
}
iIndex++;
}
return(bRetVal);
}
//*********************************************************
//
/// <summary>
/// Compare a key with a move list
/// </summary>
/// <param name="piMoveList"> Move list</param>
/// <param name="arrKey"> Key to compare</param>
/// <returns>
/// true if equal
/// </returns>
//
//*********************************************************
private bool CompareKey(int[] piMoveList, List<int> arrKey) {
bool bRetVal;
int iIndex;
bRetVal = true;
iIndex = 0;
while (iIndex < arrKey.Count && bRetVal) {
if (arrKey[iIndex] != piMoveList[iIndex]) {
bRetVal = false;
}
iIndex++;
}
return(bRetVal);
}
//*********************************************************
//
/// <summary>
/// Create entries in the book
/// </summary>
/// <param name="arrMoveList"> Array of move list</param>
/// <param name="arrBookEntry"> Book entry to be filled</param>
/// <param name="arrKey"> Current key</param>
/// <param name="iPosIndex"> Current position in the list</param>
/// <param name="iDepth"> Current depth.</param>
/// <returns>
/// Nb of entries created
/// </returns>
//
//*********************************************************
private int CreateEntries(List<int[]> arrMoveList, List<BookEntry> arrBookEntry, List<int> arrKey, out int iPosIndex, int iDepth) {
int iRetVal = 0;
int iKeySize;
int iOldValue;
List<int> arrValues;
BookEntry entry;
iKeySize = arrKey.Count;
iOldValue = -1;
arrValues = new List<int>(256);
foreach (int[] piMoveList in arrMoveList) {
if (CompareKey(piMoveList, arrKey)) {
if (piMoveList[iKeySize] != iOldValue) {
iOldValue = piMoveList[iKeySize];
arrValues.Add(iOldValue);
}
}
}
iRetVal = arrValues.Count;
iPosIndex = arrBookEntry.Count;
for (int iIndex = 0; iIndex < iRetVal; iIndex++) {
entry.Pos = (short)arrValues[iIndex];
entry.Size = 0;
entry.Index = 0;
entry.Weight = 0;
arrBookEntry.Add(entry);
}
if (iDepth != 0) {
for (int iIndex = 0; iIndex < iRetVal; iIndex++) {
arrKey.Add(arrValues[iIndex]);
entry = arrBookEntry[iPosIndex+iIndex];
entry.Index = iPosIndex;
entry.Size = (short)CreateEntries(arrMoveList, arrBookEntry, arrKey, out entry.Index, iDepth - 1);
arrBookEntry[iPosIndex+iIndex] = entry;
arrKey.RemoveAt(arrKey.Count - 1);
}
}
return(iRetVal);
}
//*********************************************************
//
/// <summary>
/// Create the book entries from a series of move list
/// </summary>
/// <param name="arrMoveList"> Array of move list</param>
/// <param name="iMaxDepth"> Maximum depth of the moves.</param>
/// <returns>
/// Nb of entries created
/// </returns>
//
//*********************************************************
private BookEntry[] CreateBookList(List<int[]> arrMoveList, int iMaxDepth) {
List<BookEntry> arrBookEntry;
List<int> arrKey;
BookEntry entry;
arrKey = new List<int>(iMaxDepth);
arrBookEntry = new List<BookEntry>(arrMoveList.Count * 10);
entry.Pos = -1;
entry.Index = 1;
entry.Size = 0;
entry.Weight = 0;
arrBookEntry.Add(entry);
entry.Size = (short)CreateEntries(arrMoveList, arrBookEntry, arrKey, out entry.Index, iMaxDepth - 1);
arrBookEntry[0] = entry;
return(arrBookEntry.ToArray());
}
//*********************************************************
//
/// <summary>
/// Create the book entries from a series of move list
/// </summary>
/// <param name="arrMoveList"> Array of move list</param>
/// <param name="iMinMoveCount"> Minimum number of moves a move list must have to be consider</param>
/// <param name="iMaxDepth"> Maximum depth of the moves.</param>
/// <returns>
/// Nb of entries created
/// </returns>
//
//*********************************************************
public int CreateBookList(List<int[]> arrMoveList, int iMinMoveCount, int iMaxDepth) {
int[] piLast = null;
List<int[]> arrUniqueMoveList;
arrUniqueMoveList = new List<int[]>(arrMoveList.Count);
arrMoveList.Sort(new CompareIntArray());
foreach (int[] piMoveList in arrMoveList) {
if (piMoveList.Length >= iMinMoveCount) {
if (piLast == null || !CompareList(piMoveList, piLast, iMaxDepth)) {
arrUniqueMoveList.Add(piMoveList);
piLast = piMoveList;
}
}
}
m_bookEntries = CreateBookList(arrUniqueMoveList, iMaxDepth);
ComputeWeight();
return(m_bookEntries.Length);
}
}
}