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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
|
////////////////////////////////////////////////////////////////////////////////
// All code below is exclusively owned by author of Chess4Net - Pavel Perminov
// (packpaul@mail.ru, packpaul1@gmail.com).
// Any changes, modifications, borrowing and adaptation are a subject for
// explicit permition from the owner.
unit PosBaseChessBoardLayerUnit;
interface
uses
Classes,
//
ChessBoardUnit, PosBaseUnit;
type
TGameResult = (grWin, grWinTime, grDraw, grLost, grLostTime);
// Layer extended with Position DB
TPosBaseChessBoardLayer = class(TChessBoardLayerBase)
private
m_bTrainingMode: boolean;
m_lstMovePrior: TList;
m_bUseUserBase: boolean;
m_PosBase, m_ExtPosBase: TPosBase;
m_strPosBaseName, m_strExtPosBaseName: string;
procedure FSetTrainingMode(bValue: boolean);
procedure FSetUseUserBase(bValue: boolean);
procedure FClearMovePriorList;
procedure FReadFromBase;
procedure FWriteGameToBase;
protected
procedure RDraw; override;
procedure ROnAfterMoveDone; override;
procedure ROnAfterSetPosition; override;
procedure ROnAfterModeSet(const OldValue, NewValue: TMode); override;
procedure ROnResetMoveList; override;
public
constructor Create(const strPosBaseName: string = '');
destructor Destroy; override;
procedure SetExternalBase(const strExtPosBaseName: string);
procedure WriteGameToBase(AGameResult: TGameResult);
procedure UnsetExternalBase;
property TrainingMode: boolean read m_bTrainingMode write FSetTrainingMode;
property UseUserBase: boolean read m_bUseUserBase write FSetUseUserBase;
end;
implementation
uses
Graphics, SysUtils,
//
ChessRulesEngine, ChessBoardHeaderUnit;
type
TPrior = (mpNo, mpHigh, mpMid, mpLow);
PMovePrior = ^TMovePrior;
TMovePrior = record
move: TMoveAbs;
prior: TPrior;
end;
TPosBaseOperator = class(TThread)
private
m_Operation: (opRead, opWrite);
m_Layer: TPosBaseChessBoardLayer;
constructor FCreateRead(ALayer: TPosBaseChessBoardLayer;
vbFreeOnTerminate: boolean = TRUE);
constructor FCreateWrite(ALayer: TPosBaseChessBoardLayer);
protected
procedure Execute; override;
public
class function CreateRead(ALayer: TPosBaseChessBoardLayer;
vbFreeOnTerminate: boolean = TRUE): TPosBaseOperator;
class function CreateWrite(ALayer: TPosBaseChessBoardLayer): TPosBaseOperator;
procedure WaitFor;
end;
var
gameResult: TGameResult; // Not threadsafe
gameID: word; // It's used for writing unique positions (not threadsafe)
const
NUM_PRIORITIES = 3; // Maximal number of priorities
{$IFDEF RESTRICT_TRAINING_DB}
MAX_PLY_TO_BASE = 60;
{$ELSE}
MAX_PLY_TO_BASE = -1; // The whole game is saved to the DB
{$ENDIF}
////////////////////////////////////////////////////////////////////////////////
// TPosBaseChessBoardLayer
constructor TPosBaseChessBoardLayer.Create(const strPosBaseName: string = '');
begin
inherited Create;
m_bUseUserBase := TRUE;
m_strPosBaseName := strPosBaseName;
m_lstMovePrior := TList.Create;
end;
destructor TPosBaseChessBoardLayer.Destroy;
begin
FClearMovePriorList;
m_lstMovePrior.Free;
TrainingMode := FALSE;
inherited;
end;
procedure TPosBaseChessBoardLayer.RDraw;
const
ARROW_END_LENGTH = 10; // â ïèêñåëÿõ
ARROW_END_ANGLE = 15 * (Pi / 180); // óãîë êîíöîâ ñòðåëêè
ARROW_INDENT = 7;
HIGH_ARROW_COLOR = clRed;
HIGH_ARROW_WIDTH = 2;
MID_ARROW_COLOR = clTeal;
MID_ARROW_WIDTH = 2;
LOW_ARROW_COLOR = clSkyBlue;
LOW_ARROW_WIDTH = 1;
var
i, x0, y0, x, y: integer;
xa, ya, ca, sa: double;
move: TMoveAbs;
begin
if (not (Assigned(ChessBoard) and Assigned(Canvas))) then
exit;
if (not (m_bTrainingMode and (ChessBoard.Mode in [mGame, mAnalyse]) and
(ChessBoard.PlayerColor = ChessBoard.PositionColor))) then
exit;
Canvas.Pen.Style := psSolid;
for i := 0 to m_lstMovePrior.Count - 1 do
begin
case PMovePrior(m_lstMovePrior[i]).prior of
mpNo: continue;
mpHigh:
begin
Canvas.Pen.Color := HIGH_ARROW_COLOR;
Canvas.Pen.Width := HIGH_ARROW_WIDTH;
end;
mpMid:
begin
Canvas.Pen.Color := MID_ARROW_COLOR;
Canvas.Pen.Width := MID_ARROW_WIDTH;
end;
mpLow:
begin
Canvas.Pen.Color := LOW_ARROW_COLOR;
Canvas.Pen.Width := LOW_ARROW_WIDTH;
end;
end;
move := PMovePrior(m_lstMovePrior[i]).move;
if (not ChessBoard.Flipped) then
begin
x0 := CHB_X + SquareSize * (move.i0 - 1) + (SquareSize div 2);
y0 := CHB_Y + SquareSize * (8 - move.j0) + (SquareSize div 2);
x := CHB_X + SquareSize * (move.i - 1) + (SquareSize div 2);
y := CHB_Y + SquareSize * (8 - move.j) + (SquareSize div 2);
end
else
begin
x0 := CHB_X + SquareSize * (8 - move.i0) + (SquareSize div 2);
y0 := CHB_Y + SquareSize * (move.j0 - 1) + (SquareSize div 2);
x := CHB_X + SquareSize * (8 - move.i) + (SquareSize div 2);
y := CHB_Y + SquareSize * (move.j - 1) + (SquareSize div 2);
end;
// Draw an arrow
ca := (x - x0) / sqrt(sqr(x - x0) + sqr(y - y0));
sa := (y - y0) / sqrt(sqr(x - x0) + sqr(y - y0));
x0 := x0 + Round(ARROW_INDENT * ca);
y0 := y0 + Round(ARROW_INDENT * sa);
x := x - Round(ARROW_INDENT * ca);
y := y - Round(ARROW_INDENT * sa);
Canvas.MoveTo(x0, y0);
Canvas.LineTo(x, y);
xa := x + (-ARROW_END_LENGTH * cos(ARROW_END_ANGLE)) * ca -
(ARROW_END_LENGTH * sin(ARROW_END_ANGLE)) * sa;
ya := y + (-ARROW_END_LENGTH * cos(ARROW_END_ANGLE)) * sa +
(ARROW_END_LENGTH * sin(ARROW_END_ANGLE)) * ca;
Canvas.LineTo(Round(xa), Round(ya));
xa := x + (-ARROW_END_LENGTH * cos(ARROW_END_ANGLE)) * ca -
(-ARROW_END_LENGTH * sin(ARROW_END_ANGLE)) * sa;
ya := y + (-ARROW_END_LENGTH * cos(ARROW_END_ANGLE)) * sa +
(-ARROW_END_LENGTH * sin(ARROW_END_ANGLE)) * ca;
Canvas.MoveTo(x, y);
Canvas.LineTo(Round(xa), Round(ya));
end;
end;
procedure Reestimate(lstMoveEsts: TList; viRec: integer);
var
est: SmallInt;
id: word;
begin
id := LongWord(lstMoveEsts[viRec]) shr 16;
if id = gameID then
exit; // ïîçèöèÿ äóáëèðóåòñÿ â ðàìêàõ îäíîé ïàðòèè
est := SmallInt(lstMoveEsts[viRec]);
case gameResult of
grWin: inc(est, 2);
grWinTime: inc(est);
grDraw: ;
grLost: dec(est, 2);
grLostTime: dec(est);
end;
lstMoveEsts[viRec] := Pointer((gameID shl 16) or Word(est));
end;
procedure TPosBaseChessBoardLayer.FSetTrainingMode(bValue: boolean);
begin
if (m_bTrainingMode = bValue) then
exit;
m_bTrainingMode := bValue;
try
if (m_bTrainingMode) then
begin
if (m_strPosBaseName <> '') then
m_PosBase := TPosBase.Create(m_strPosBaseName, Reestimate);
if (m_strExtPosBaseName <> '') then
m_ExtPosBase := TPosBase.Create(m_strExtPosBaseName);
with TPosBaseOperator.CreateRead(self, FALSE) do
try
WaitFor;
finally
Free;
end;
end
else
begin
FreeAndNil(m_PosBase);
FreeAndNil(m_ExtPosBase);
end;
RDoUpdate;
except
on Exception do
begin
FreeAndNil(m_PosBase);
FreeAndNil(m_ExtPosBase);
m_bTrainingMode := FALSE;
end;
end;
end;
procedure TPosBaseChessBoardLayer.FSetUseUserBase(bValue: boolean);
begin
if (m_bUseUserBase = bValue) then
exit;
m_bUseUserBase := bValue;
TPosBaseOperator.CreateRead(self, FALSE);
end;
procedure TPosBaseChessBoardLayer.ROnAfterMoveDone;
begin
if (m_bTrainingMode) then
begin
if (Assigned(ChessBoard) and
(ChessBoard.PlayerColor = ChessBoard.PositionColor)) then
TPosBaseOperator.CreateRead(self) // Read from PosBase and update
end;
end;
procedure TPosBaseChessBoardLayer.ROnAfterSetPosition;
begin
if (m_bTrainingMode) then
begin
with TPosBaseOperator.CreateRead(self, FALSE) do // Read from DB and update
try
WaitFor;
finally
Free;
end;
end;
end;
procedure TPosBaseChessBoardLayer.SetExternalBase(const strExtPosBaseName: string);
begin
if (m_bTrainingMode) then
begin
if (m_strExtPosBaseName = strExtPosBaseName) then
exit;
FreeAndNil(m_ExtPosBase);
m_ExtPosBase := TPosBase.Create(strExtPosBaseName);
TPosBaseOperator.CreateRead(self, FALSE);
end;
m_strExtPosBaseName := strExtPosBaseName;
end;
procedure TPosBaseChessBoardLayer.WriteGameToBase(AGameResult: TGameResult);
begin
if (m_bTrainingMode) then
begin
gameResult := AGameResult;
TPosBaseOperator.CreateWrite(self);
end;
end;
procedure TPosBaseChessBoardLayer.UnsetExternalBase;
begin
FreeAndNil(m_ExtPosBase);
end;
procedure TPosBaseChessBoardLayer.ROnAfterModeSet(const OldValue, NewValue: TMode);
begin
if (OldValue = mEdit) then
ROnAfterSetPosition; // Read from PosBase
end;
procedure TPosBaseChessBoardLayer.ROnResetMoveList;
begin
if (ChessBoard.Mode = mEdit) then
FClearMovePriorList;
end;
procedure TPosBaseChessBoardLayer.FClearMovePriorList;
var
i: integer;
begin
for i := 0 to m_lstMovePrior.Count - 1 do
Dispose(m_lstMovePrior[i]);
m_lstMovePrior.Clear;
end;
function EstComape(item1, item2: pointer): integer;
begin
Result := SmallInt(PMoveEst(item2).estimate and $FFFF) - SmallInt(PMoveEst(item1).estimate and $FFFF);
end;
procedure TPosBaseChessBoardLayer.FReadFromBase;
procedure ClasterMoves(var rlstMove: TList);
var
i, j, num_clast, i_min, j_min, curr_assoc: integer;
modus_min: double;
clastWeights: array of record
grav: double;
assoc: integer;
end;
mp: PMovePrior;
p: TPrior;
begin
if rlstMove.Count = 0 then
exit;
rlstMove.Sort(EstComape);
SetLength(clastWeights, rlstMove.Count);
num_clast := rlstMove.Count;
for i := 0 to num_clast - 1 do
begin
clastWeights[i].assoc := i + 1;
clastWeights[i].grav := SmallInt(PMoveEst(rlstMove[i]).estimate and $FFFF);
end;
repeat
i_min := 0;
j_min := 0;
modus_min := $7FFF; // $7FFF - ìàêñ. çíà÷åíèå äëÿ îöåíêè
curr_assoc := 0; // òåêóùèé ïðîñìàòðèâàåìûé êëàñòåð
for i := 0 to length(clastWeights) - 2 do
begin
if curr_assoc = clastWeights[i].assoc then
continue;
curr_assoc := clastWeights[i].assoc;
for j := i + 1 to length(clastWeights) - 1 do
if (clastWeights[j].assoc <> clastWeights[j-1].assoc) and
(curr_assoc <> clastWeights[j].assoc) and
(abs(clastWeights[i].grav - clastWeights[j].grav) <= modus_min) then
begin
i_min := i;
j_min := j;
modus_min := abs(clastWeights[i].grav - clastWeights[j].grav);
end;
end;
if (num_clast > Ord(High(TPrior))) or (modus_min = 0.0) then
begin
for i := High(clastWeights) downto j_min do
if clastWeights[i].assoc = clastWeights[j_min].assoc then
clastWeights[i].assoc := clastWeights[i_min].assoc;
clastWeights[i_min].grav := (clastWeights[i_min].grav + clastWeights[j_min].grav) / 2;
end;
dec(num_clast);
until (num_clast <= Ord(High(TPrior))) and ((modus_min <> 0.0) or (num_clast < 1));
p := mpHigh;
for i := 0 to rlstMove.Count - 1 do
begin
new(mp);
if (i > 0) and (clastWeights[i].assoc > clastWeights[i-1].assoc) then
p := Succ(p);
mp.move := PMoveEst(rlstMove[i]).move;
mp.prior := p;
Dispose(rlstMove[i]);
rlstMove[i] := mp;
end;
SetLength(clastWeights, 0);
end;
var
lstUsrMove, lstExtMove: TList;
procedure MergeMoves;
function NEqualMoves(i,j: integer): boolean;
begin
with PMovePrior(lstExtMove[i])^, PMovePrior(m_lstMovePrior[j]).move do
Result := (i0 = move.i0) and (j0 = move.j0) and (j = move.j) and (i = move.i) and
(prom_fig = move.prom_fig);
end;
var
i, j, n: integer;
const
PRIOR_CALC: array[TPrior, TPrior] of TPrior =
((mpNo, mpNo, mpNo, mpNo), // UsrPrior = mpNo - ?, ò.ê. åù¸ íèãäå íå èñï.
(mpHigh, mpHigh, mpHigh, mpMid), // UsrPrior = mpHigh
(mpMid, mpMid, mpMid, mpMid), // UsrPrior = mpMid
(mpLow, mpMid, mpLow, mpLow)); // UsrPrior = mpLow
begin
for i := 0 to lstUsrMove.Count - 1 do
m_lstMovePrior.Add(lstUsrMove[i]);
// Merging of lists
n := m_lstMovePrior.Count;
for i := 0 to lstExtMove.Count - 1 do
begin
j := n - 1;
while (j >= 0) do
begin
if NEqualMoves(i,j) then
begin
PMovePrior(m_lstMovePrior[j]).prior :=
PRIOR_CALC[PMovePrior(m_lstMovePrior[j]).prior,
PMovePrior(lstExtMove[j]).prior];
Dispose(lstExtMove[i]);
break;
end;
dec(j);
end;
if (j < 0) then
m_lstMovePrior.Add(lstExtMove[i]);
end; // for
end;
begin // .FReadFromBase
FClearMovePriorList;
if (not Assigned(Position)) then
exit;
lstExtMove := nil;
lstUsrMove := TList.Create;
try
lstExtMove := TList.Create;
if (m_bUseUserBase or (not Assigned(m_ExtPosBase))) then
begin
if (Assigned(m_PosBase)) then
m_PosBase.Find(Position^, lstUsrMove);
end;
if (Assigned(m_ExtPosBase)) then
m_ExtPosBase.Find(Position^, lstExtMove);
// TODO: Handle wrong DB
ClasterMoves(lstUsrMove);
ClasterMoves(lstExtMove);
MergeMoves;
finally
lstExtMove.Free;
lstUsrMove.Free;
end;
end;
procedure TPosBaseChessBoardLayer.FWriteGameToBase;
var
ply: integer;
begin
if (not (Assigned(m_PosBase) and Assigned(PositionsList))) then
exit;
gameID := Random($FFFF) + 1;
if (ChessBoard.PlayerColor = RGetColorStarts) then
ply := 0
else
ply := 1;
while ((ply < PositionsList.Count) and ((MAX_PLY_TO_BASE < 0) or (ply <= MAX_PLY_TO_BASE))) do
begin
m_PosBase.Add(PPosMove(PositionsList[ply])^);
inc(ply, 2);
end;
end;
////////////////////////////////////////////////////////////////////////////////
// TPosBaseOperator
constructor TPosBaseOperator.FCreateRead(ALayer: TPosBaseChessBoardLayer;
vbFreeOnTerminate: boolean = TRUE);
begin
m_Operation := opRead;
m_Layer := ALayer;
inherited Create(TRUE);
Priority := tpNormal;
FreeOnTerminate := vbFreeOnTerminate;
Resume;
end;
constructor TPosBaseOperator.FCreateWrite(ALayer: TPosBaseChessBoardLayer);
begin
m_Layer := ALayer;
m_Operation := opWrite;
inherited Create(TRUE);
Priority := tpNormal;
FreeOnTerminate := TRUE;
Resume;
end;
class function TPosBaseOperator.CreateRead(ALayer: TPosBaseChessBoardLayer;
vbFreeOnTerminate: boolean = TRUE): TPosBaseOperator;
begin
Result := nil;
if (Assigned(ALayer.ChessBoard) and (ALayer.ChessBoard.Mode <> mEdit)) then
Result := TPosBaseOperator.FCreateRead(ALayer, vbFreeOnTerminate);
end;
class function TPosBaseOperator.CreateWrite(ALayer: TPosBaseChessBoardLayer): TPosBaseOperator;
begin
Result := nil;
if (Assigned(ALayer.ChessBoard) and (ALayer.ChessBoard.Mode <> mEdit)) then
Result := TPosBaseOperator.FCreateWrite(ALayer);
end;
procedure TPosBaseOperator.Execute;
begin
case m_Operation of
opRead:
begin
m_Layer.FReadFromBase;
Synchronize(m_Layer.RDoUpdate);
end;
opWrite:
m_Layer.FWriteGameToBase;
end;
end;
procedure TPosBaseOperator.WaitFor;
begin
if (not Assigned(self)) then
exit;
inherited WaitFor;
end;
initialization
Randomize;
end.
|