-
Notifications
You must be signed in to change notification settings - Fork 2
/
HashUnit.pas
258 lines (237 loc) · 6.46 KB
/
HashUnit.pas
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
unit HashUnit;
interface
uses Boardunit;
Procedure Zorb(var z1,z2,z3:cardinal);
Procedure AddToHash(Zn1,zn2,zn3:Cardinal;BestMN:Byte;Depth:Shortint);
Procedure AddToHashScore(Zn1,zn2,zn3:Cardinal;Depth:ShortInt;ScoreT:ShortInt;Score:Integer;ply:byte);
Procedure InitHash;
Procedure SetHashSize(size:integer);
Procedure NewHashLevel;
Procedure AddDrawScoreToHash;
Procedure BestMoveFromHash(z1,z2,z3:Cardinal;Var Best1,Best2:Byte;Var DepthBest:ShortInt);
Function ScoreFromHash
(z1,z2,z3:Cardinal;A,B:Integer;PV:Boolean;Var Score:Integer;Depth:Shortint;ply:byte):Boolean;
Type HashZ=Record
z1,z2:Cardinal;
DepthM:Shortint;
NomDost:byte;
BestM1,BestM2:Byte;
DepthS:Shortint;
ScoreT:Shortint;
ScoreS:Integer;
end;
Var Hash:Array of HashZ;
implementation
Var Level:Byte;
Var HashSize:Cardinal;
var zorb1,zorb2,zorb3:array[0..3,0..31] of cardinal;
// ïîëó÷åíèå Õåøà Ïîçèöèè
Procedure Zorb(var z1,z2,z3:cardinal);
Var i,kto:byte;
begin
z1:=0;
z2:=0;
z3:=0;
for i:=0 to 31 do
begin
kto:=pole[poledosk[i]];
if Kto>0 then
Begin
kto:=nomkto[kto];
z1:=z1 xor zorb1[Kto,i];
z2:=z2 xor zorb2[Kto,i];
z3:=z3 xor zorb3[Kto,i];
end;
end;
if ochod then
Begin
z1:=Not z1;
z2:=not z2;
z3:=not z3;
end;
z3:=z3 mod HashSize;
end;
// Ïîëó÷åíèå Ëó÷øåãî Õîäà èç Õåøà
Procedure BestMoveFromHash(z1,z2,z3:Cardinal;Var Best1,Best2:Byte;Var DepthBest:ShortInt);
Begin
Best1:=0;
Best2:=0;
DepthBest:=0;
if Hash[z3].z1=z1 then
if Hash[z3].z2=z2 then
Begin
Hash[z3].NomDost:=Level;
if Hash[z3].BestM1>0 then
Begin
Best1:=Hash[z3].BestM1;
Best2:=Hash[z3].BestM2;
DepthBest:=Hash[z3].DepthM;
end;
end;
end;
// ïîëó÷åíèå îöåíêè èç Õåøà
Function ScoreFromHash
(z1,z2,z3:Cardinal;A,B:Integer;PV:Boolean;Var Score:Integer;Depth:Shortint;ply:byte):Boolean;
Begin
if Hash[z3].z1=z1 then
if Hash[z3].z2=z2 then
Begin
Score:=Hash[z3].ScoreS;
if Score>15000 then Score:=Score-ply;
if Score<-15000 then Score:=Score+ply;
Hash[z3].NomDost:=Level;
if Hash[z3].DepthS>=Depth then
Begin
if Hash[z3].ScoreT=0 then
Begin
ScoreFromHash:=true;
exit;
end;
if (Not PV) and (Hash[z3].ScoreT=1)
And (Score>=B) then
Begin
ScoreFromHash:=true;
exit;
end;
if (Not PV) and (Hash[z3].ScoreT=-1)
And (Score<=A) then
Begin
ScoreFromHash:=true;
exit;
end;
end;
if (Score<=a)and(Score<-15000)and(Hash[z3].ScoreT<=0) then
Begin
ScoreFromHash:=true;
exit;
end;
if (Score>=b)and(Score>15000)and(Hash[z3].ScoreT>=0) then
Begin
ScoreFromHash:=true;
exit;
end;
end;
ScoreFromHash:=False;
end;
// Äîáàâëåíèå õîäà â õåø
procedure AddToHash(Zn1,zn2,zn3:Cardinal;BestMN:Byte;Depth:Shortint);
Var H:^HashZ;
Begin
H:=addr(Hash[zn3]);
if (H.z1<>zn1)or (h.z2<>zn2) then
Begin // çàòðåì ñòàðóþ ïîçèöèþ
if ((H.DepthS<=Depth)and(H.DepthM<=Depth))or
((level<>H.NomDost)and(H.DepthS<100)) then
Begin
h.z1:=zn1;
h.z2:=zn2;
h.DepthM:=depth;
h.NomDost:=Level;
h.BestM1:=BestMN;
h.BestM2:=0;
h.DepthS:=0;
h.ScoreT:=0;
h.ScoreS:=0;
end;
end
Else // Èìåííî ýòà ïîçèöèÿ ñåé÷àñ â õåøå.
Begin
H.NomDost:=Level;
if (BestMN<>H.BestM1) then
Begin
if Depth>=h.DepthM then
Begin
h.BestM2:=h.BestM1;
h.BestM1:=BestMN;
End
Else h.BestM2:=BestMN;
end;
if h.DepthM<Depth then h.DepthM:=depth;
end;
end;
// Õåø îöåíîê.
procedure AddToHashScore
(Zn1,zn2,zn3:Cardinal;Depth:Shortint;ScoreT:ShortInt;Score:Integer;ply:Byte);
Var H:^HashZ;
Begin
// êîððåêòèðîâêà ìàòîâîé îöåíêè
if Score>=15000 then Score:=Score+ply;
if Score<=-15000 then Score:=Score-ply;
H:=addr(Hash[zn3]);
if (H.z1<>zn1)or (h.z2<>zn2) then
Begin // çàòðåì ñòàðóþ ïîçèöèþ
if ((H.DepthS<=Depth)and(H.DepthM<=Depth))or
((level<>H.NomDost)and(H.DepthS<100)) then
Begin
h.z1:=zn1;
h.z2:=zn2;
h.DepthM:=0;
h.NomDost:=Level;
h.BestM1:=0;
h.BestM2:=0;
h.DepthS:=Depth;
h.ScoreT:=ScoreT;
h.ScoreS:=Score;
end;
end
Else // Èìåííî ýòà ïîçèöèÿ ñåé÷àñ â õåøå.
Begin
H.NomDost:=Level;
if h.DepthS<=Depth then
Begin
h.DepthS:=Depth;
h.ScoreT:=ScoreT;
h.ScoreS:=Score;
end;
end;
end;
// çàïèñàòü íè÷åéíóþ îöåíêó òåêóùåé ïîçèöèè â õåø
// ñäåëàíî äëÿ êîíòðëÿ ïîâòîðåíèé ïîçèöèè
Procedure AddDrawScoreToHash;
var z1,z2,z3:Cardinal;
Begin
zorb(z1,z2,z3);
AddToHashScore(Z1,z2,z3,100,0,0,0);
end;
// óñòàíîâèì ðàçìåð Õåøà
Procedure SetHashSize(size:integer);
Begin
if size>512 then size:=512;
if size<32 then size:=32;
HashSize:=(size-2)*51000;
setLength(Hash,HashSize+1);
end;
// Îáñ÷åò äðóãîé ïîçèöèè
Procedure NewHashLevel;
Begin
if Level>=254 then Level:=1 Else Level:=Level+1;
end;
// Èíèöèàëèçàöèÿ Çîáðèñòà è î÷èñòêà õåøà
Procedure InitHash;
var i,j:Byte;
Var i1:Cardinal;
Begin
setLength(Hash,HashSize+1);
level:=1;
Randomize();
for i:=0 to 3 do
for j:=0 to 31 do
Begin
Zorb1[i,j]:=(cardinal(Random(65534)+1) Shl 16)xor cardinal(Random(65534)+1);
Zorb2[i,j]:=(cardinal(Random(65534)+1) Shl 16)xor cardinal(Random(65534)+1);
Zorb3[i,j]:=(cardinal(Random(65534)+1) Shl 16)xor cardinal(Random(65534)+1);
end;
for i1:=0 to HashSize do
Begin
Hash[i1].z1:=0;
Hash[i1].z2:=0;
Hash[i1].DepthM:=0;
Hash[i1].NomDost:=0;
Hash[i1].BestM1:=0;
Hash[i1].BestM2:=0;
Hash[i1].DepthS:=0;
Hash[i1].ScoreT:=0;
Hash[i1].ScoreS:=0;
end;
end;
end.