-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCBook.cs
More file actions
381 lines (352 loc) · 12 KB
/
CBook.cs
File metadata and controls
381 lines (352 loc) · 12 KB
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
using NSChess;
using System;
using System.Collections.Generic;
using System.IO;
namespace BookReaderAbk
{
class CHeader
{
public uint header;
public int bytesInHeader;
public int bytesPerMove;
public byte commentLength;
public byte[] comment = new byte[120];
public byte authorLength;
public byte[] author = new byte[80];
public int depth;
public int moves;
public int minGames;
public int minWin;
public int proWinWhite;
public int proWinBlack;
public int probabilityPriority;
public int probabilityGames;
public int probabilityWin;
public int halfMoves;
public byte[] filter = new byte[24946];
public void Clear()
{
header = 0x0341424b;
bytesInHeader = 25200;
bytesPerMove = 28;
commentLength = 0;
Array.Clear(comment, 0, comment.Length);
authorLength = 0;
Array.Clear(author, 0, author.Length);
depth = 25;
moves = 0;
minGames = 1;
proWinWhite = 0;
proWinBlack = 0;
probabilityPriority = 15;
probabilityGames = 15;
probabilityWin = 15;
halfMoves = 25;
for (int n = 0; n < filter.Length; n++)
filter[n] = 0x78;
}
public void Fill()
{
Clear();
moves = Program.book.recList.Count;
}
public void LoadFromBinaryReader(BinaryReader br)
{
header = br.ReadUInt32();
bytesInHeader = br.ReadInt32();
bytesPerMove = br.ReadInt32();
commentLength = br.ReadByte();
comment = br.ReadBytes(comment.Length);
authorLength = br.ReadByte();
author = br.ReadBytes(author.Length);
depth = br.ReadInt32();
moves = br.ReadInt32();
minGames = br.ReadInt32();
minWin = br.ReadInt32();
proWinWhite = br.ReadInt32();
proWinBlack = br.ReadInt32();
probabilityPriority = br.ReadInt32();
probabilityGames = br.ReadInt32();
probabilityWin = br.ReadInt32();
halfMoves = br.ReadInt32();
filter = br.ReadBytes(filter.Length);
}
public void SaveToBinaryWriter(BinaryWriter bw)
{
bw.Write(header);
bw.Write(bytesInHeader);
bw.Write(bytesPerMove);
bw.Write(commentLength);
bw.Write(comment);
bw.Write(authorLength);
bw.Write(author);
bw.Write(depth);
bw.Write(moves);
bw.Write(minGames);
bw.Write(minWin);
bw.Write(proWinWhite);
bw.Write(proWinBlack);
bw.Write(probabilityPriority);
bw.Write(probabilityGames);
bw.Write(probabilityWin);
bw.Write(halfMoves);
bw.Write(filter);
}
public string Author()
{
string result = String.Empty;
for (int n = 0; n < authorLength; n++)
result += (char)author[n];
return result;
}
public string Comment()
{
string result = String.Empty;
for (int n = 0; n < commentLength; n++)
result += (char)comment[n];
return result;
}
}
internal class CBook
{
public const string name = "BookReaderAbk";
public const string version = "2022-11-04";
public const string defExt = ".abk";
public string path = string.Empty;
public static Random rnd = new Random();
public CHeader header = new CHeader();
public CRecList recList = new CRecList();
readonly CChess chess = new CChess();
public void Clear()
{
header.Clear();
recList.Clear();
}
public CRec GetRec(int position)
{
foreach (CRec rec in recList)
if (rec.position == position)
return rec;
return null;
}
public CRec GetRec(CRec rec, string umo)
{
if (rec.GetUci() == umo)
return rec;
rec = GetRec(rec.nextSibling * 28);
if (rec == null)
return rec;
return GetRec(rec, umo);
}
public CRecList GetSiblings(CRec rec)
{
CRecList list = new CRecList();
while (rec != null)
{
list.Add(rec);
rec = GetRec(rec.nextSibling * 28);
}
return list;
}
public void ShowHeader()
{
Console.WriteLine($"bytes per move {header.bytesPerMove}");
Console.WriteLine($"depth {header.depth}");
Console.WriteLine($"moves {header.moves}");
Console.WriteLine($"minimum number of games {header.minGames}");
Console.WriteLine($"minimum number of wins {header.minWin}");
Console.WriteLine($"procent win white {header.proWinWhite}");
Console.WriteLine($"procent win black {header.proWinBlack}");
Console.WriteLine($"probability priority {header.probabilityPriority}");
Console.WriteLine($"probability games {header.probabilityGames}");
Console.WriteLine($"probability win {header.probabilityWin}");
Console.WriteLine($"use book to half move {header.halfMoves}");
Console.WriteLine($"author {header.Author()}");
Console.WriteLine($"comment {header.Comment()}");
}
CRecList GetMoves(string moves)
{
CRecList list = new CRecList();
if (recList.Count == 0)
return list;
CRec rec = recList[0];
string[] am = moves.Trim().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string m in am)
{
rec = GetRec(rec, m);
if (rec == null)
return list;
rec = GetRec(rec.nextMove * 28);
if (rec == null)
return list;
}
return GetSiblings(rec);
}
string GetMove(CRecList rl)
{
string move = String.Empty;
int w = 0;
foreach (CRec r in rl)
{
int v = r.GetValue();
w += v;
if (CChess.rnd.Next(w) < v)
move = r.GetUci();
}
return move;
}
public string GetMove(string moves)
{
CRecList list = GetMoves(moves);
return GetMove(list);
}
public bool LoadFromFile(string p)
{
path = p;
Clear();
if (!File.Exists(path))
return false;
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
using (BinaryReader reader = new BinaryReader(fs))
{
header.LoadFromBinaryReader(reader);
while (reader.BaseStream.Position != reader.BaseStream.Length)
{
CRec rec = new CRec
{
position = reader.BaseStream.Position
};
rec.LoadFromBinaryReader(reader);
recList.Add(rec);
}
}
return true;
}
public bool SaveToFile(string p)
{
string ext = Path.GetExtension(p).ToLower();
if (ext == defExt)
return SaveToAbk(p);
if (ext == ".uci")
return SaveToUci(p);
if (ext == ".pgn")
return SaveToPgn(p);
return false;
}
public bool SaveToAbk(string path)
{
using (FileStream fs = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None))
using (BinaryWriter writer = new BinaryWriter(fs))
{
header.moves = recList.Count;
header.SaveToBinaryWriter(writer);
foreach (CRec rec in recList)
rec.SaveToBinaryWriter(writer);
}
return true;
}
public bool SaveToUci(string p)
{
List<string> sl = GetGames();
FileStream fs = File.Open(p, FileMode.Create, FileAccess.Write, FileShare.None);
using (StreamWriter sw = new StreamWriter(fs))
{
foreach (String uci in sl)
sw.WriteLine(uci);
}
return true;
}
#region file pgn
public bool SaveToPgn(string p)
{
List<string> sl = GetGames();
int line = 0;
FileStream fs = File.Open(p, FileMode.Create, FileAccess.Write, FileShare.None);
using (StreamWriter sw = new StreamWriter(fs))
{
foreach (String uci in sl)
{
string[] arrMoves = uci.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
chess.SetFen();
string pgn = String.Empty;
foreach (string umo in arrMoves)
{
string san = chess.UmoToSan(umo);
if (san == String.Empty)
break;
if (chess.WhiteTurn)
pgn += $" {chess.MoveNumber}. {san}";
else
pgn += $" {san}";
int emo = chess.UmoToEmo(umo);
chess.MakeMove(emo);
}
sw.WriteLine();
sw.WriteLine("[White \"White\"]");
sw.WriteLine("[Black \"Black\"]");
sw.WriteLine();
sw.WriteLine(pgn.Trim());
double pro = ++line * 100.0 / sl.Count;
Console.Write($"\rprogress {pro:N2}%");
}
}
Console.WriteLine();
return true;
}
#endregion file pgn
void GetGames(double piece, double pro, string moves, ref List<string> list)
{
CRecList rl = GetMoves(moves);
if (rl.Count == 0)
{
list.Add(moves);
Console.Write($"\rextracted {list.Count} ({pro:N4}%)");
}
int i = 0;
double cp = piece / rl.Count;
foreach (CRec rec in rl)
{
GetGames(cp, pro + i * cp, $"{moves} {rec.GetUci()}".Trim(), ref list);
i++;
}
}
List<string> GetGames()
{
List<string> sl = new List<string>();
GetGames(100, 0, "", ref sl);
Console.WriteLine();
Console.WriteLine("finish");
Console.Beep();
sl.Sort();
return sl;
}
public void InfoMoves(string moves = "")
{
chess.SetFen();
if (!chess.MakeMoves(moves))
Console.WriteLine("wrong moves");
else
{
CRecList rl = GetMoves(moves);
if (rl.Count == 0)
Console.WriteLine("no moves found");
else
{
rl.SortGames();
string mask = "{0,2} {1,-5} {2,6} {3,6} {4,6}";
Console.WriteLine();
Console.WriteLine(mask, "id", "move", "games", "win", " loose");
int i = 1;
foreach (CRec r in rl)
{
Console.WriteLine(String.Format(mask, i++, r.GetUci(), r.games, r.win, r.loose));
}
}
}
}
public void ShowInfo()
{
InfoMoves();
}
}
}