Skip to content

Commit 6b65277

Browse files
committed
27 Perft and more
1 parent a7d6f69 commit 6b65277

File tree

6 files changed

+132
-17
lines changed

6 files changed

+132
-17
lines changed

.vscode/launch.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "debug",
12+
"remotePath": "",
13+
"port": 2345,
14+
"host": "127.0.0.1",
15+
"program": "${workspaceFolder}",
16+
"env": {},
17+
"args": [],
18+
"showLog": true
19+
}
20+
]
21+
}

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
"GoBit.exe~": true,
1414
"london.txt": true,
1515
"engine-interface.txt": true,
16-
"README.md": true
16+
"README.md": true,
17+
"perft_tests.txt": true,
18+
"temp.goo": true
1719
},
1820
"cSpell.words": [
1921
"Pval",

engine.go

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ func root(toEngine chan bool, frEngine chan string) {
158158
childPV.clear()
159159
b.move(mv)
160160
tell("info depth ", strconv.Itoa(depth), " currmove ", mv.String(), " currmovenumber ", strconv.Itoa(ix+1))
161+
//fmt.Printf("alpha=%v beta=%v\n",alpha,beta)
161162
score := -search(-beta, -alpha, depth-1, 1, &childPV, b)
162163

163164
b.unmove(mv)
@@ -207,8 +208,6 @@ func root(toEngine chan bool, frEngine chan string) {
207208
}
208209
}
209210

210-
//TODO search: Null Move
211-
212211
//TODO search: Late Move Reduction
213212
//TODO search: Internal Iterative Depening
214213
//TODO search: Delta Pruning
@@ -220,6 +219,16 @@ func search(alpha, beta, depth, ply int, pv *pvList, b *boardStruct) int {
220219
//return signEval(b.stm, evaluate(b))
221220
return qs(beta, b)
222221
}
222+
223+
// Are we in mate search?
224+
if mateSc := addMatePly(mateEval-1, ply); mateSc < beta {
225+
beta = mateSc
226+
227+
if mateSc <= alpha {
228+
return mateSc
229+
}
230+
}
231+
223232
pv.clear()
224233

225234
pvNode := depth > 0 && beta != alpha+1
@@ -284,6 +293,7 @@ func search(alpha, beta, depth, ply int, pv *pvList, b *boardStruct) int {
284293
bm := noMove
285294

286295
var genInfo = genInfoStruct{sv: 0, ply: ply, transMove: transMove}
296+
cntMoves:=0
287297
next = nextNormal
288298
for mv, msg := next(&genInfo, b); mv != noMove; mv, msg = next(&genInfo, b) {
289299
_ = msg
@@ -302,9 +312,9 @@ func search(alpha, beta, depth, ply int, pv *pvList, b *boardStruct) int {
302312
} else {
303313
score = -search(-beta, -alpha, depth-1, ply+1, &childPV, b)
304314
}
305-
315+
cntMoves++
306316
b.unmove(mv)
307-
317+
308318
if score > bs {
309319
bs = score
310320
bm = mv
@@ -350,6 +360,19 @@ func search(alpha, beta, depth, ply int, pv *pvList, b *boardStruct) int {
350360
return alpha
351361
}
352362
}
363+
364+
if cntMoves==0{ // whe didn't find any legal moves - either mate or stalemate
365+
sc:=0 // we could have a contempt value here instead
366+
if inCheck{ // must be a mate
367+
sc= -mateEval+ply+1
368+
}
369+
370+
if useTT {
371+
trans.store(b.fullKey(), noMove, transDepth, ply, sc, scoreTypeBetween)
372+
}
373+
return sc
374+
}
375+
353376
if bm.cmp(transMove) {
354377
trans.cBest++
355378
}
@@ -891,7 +914,7 @@ func nextNormal(genInfo *genInfoStruct, b *boardStruct) (move, string) {
891914

892915
return mv, "bad capt"
893916
default: // shouldn't happen
894-
panic("neve come here! nextNormal sv=" + strconv.Itoa(genInfo.sv))
917+
panic("never come here! nextNormal sv=" + strconv.Itoa(genInfo.sv))
895918
}
896919
}
897920

@@ -915,13 +938,13 @@ func startPerft(depth int, bd *boardStruct) uint64 {
915938
continue
916939
}
917940
dbg := false
918-
/*
941+
/*
919942
/////////////////////////////////////////////////////////////
920943
if mv.fr() == D4 && mv.to() == F4 {
921944
dbg = true
922945
}
923946
/////////////////////////////////////////////////////////////
924-
*/
947+
*/
925948
count := perft(dbg, depth-1, 1, bd)
926949
totCount += count
927950
fmt.Printf("%2d: %v \t%v \t%v\n", ix+1, mv.String(), count, msg)
@@ -952,7 +975,7 @@ func perft(dbg bool, depth, ply int, bd *boardStruct) uint64 {
952975
}
953976
_ = msg
954977
deb := false
955-
/*
978+
/*
956979
////////////////////////////////////////////////////////////////
957980
if dbg && mv.fr() == F5 && mv.to() == F4 {
958981
deb = true
@@ -961,10 +984,10 @@ func perft(dbg bool, depth, ply int, bd *boardStruct) uint64 {
961984
deb = true
962985
}
963986
////////////////////////////////////////////////////////////////
964-
*/
987+
*/
965988
cnt := perft(deb, depth-1, ply+1, bd)
966989
count += cnt
967-
/*
990+
/*
968991
/////////////////////////////////////////////
969992
if dbg && !deb {
970993
fmt.Println(ix+1, ":(e4) ", mv.String(), msg, "\t", cnt)
@@ -974,7 +997,7 @@ func perft(dbg bool, depth, ply int, bd *boardStruct) uint64 {
974997
}
975998
}
976999
////////////////////////////////////////////
977-
*/
1000+
*/
9781001
bd.unmove(mv)
9791002
ix++
9801003
}

perft_tests.txt

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
Position 1 - start position
2+
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
3+
1 20
4+
2 400
5+
3 8,902
6+
4 197,281
7+
5 4,865,609
8+
6 119,060,324
9+
7 3,195,901,860
10+
8 84,998,978,956
11+
9 2,439,530,234,167
12+
10 69,352,859,712,417
13+
11 2,097,651,003,696,806
14+
12 62,854,969,236,701,747
15+
13 1,981,066,775,000,396,239
16+
17+
Position 2
18+
also know as "Kiwipete" by Peter McKenzie [3]
19+
r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -
20+
1 48
21+
2 2039
22+
3 97862
23+
4 4085603
24+
5 193690690
25+
26+
Position 3
27+
8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - -
28+
1 14
29+
2 191
30+
3 2812
31+
4 43238
32+
5 674624
33+
6 11030083
34+
7 178633661
35+
36+
37+
Position 4
38+
r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1
39+
Or mirrored (with the same perft results):
40+
r2q1rk1/pP1p2pp/Q4n2/bbp1p3/Np6/1B3NBn/pPPP1PPP/R3K2R b KQ - 0 1
41+
1 6
42+
2 264
43+
3 9467
44+
4 422333
45+
5 15833292
46+
6 706045033
47+
48+
Position 5
49+
This position was discussed on Talkchess and caught bugs in engines
50+
rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8
51+
1 44
52+
2 1,486
53+
3 62,379
54+
4 2,103,487
55+
5 89,941,194
56+
57+
Position 6
58+
An alternative Perft given by Steven Edwards
59+
r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 10
60+
1 46
61+
2 2,079
62+
3 89,890
63+
4 3,894,594
64+
5 164,075,551
65+
6 6,923,051,137
66+
7 287,188,994,746
67+
8 11,923,589,843,526
68+
9 490,154,852,788,714

trans.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,9 @@ func removeMatePly(sc, ply int) int {
393393
// addMatePly adjusts mate value with ply if mate score
394394
func addMatePly(sc, ply int) int {
395395
if sc < minEval+maxPly {
396-
return mateEval - ply
397-
} else if sc > maxEval-maxPly {
398396
return -mateEval + ply
397+
} else if sc > maxEval-maxPly {
398+
return mateEval - ply
399399
}
400400
return sc
401401
}

uci.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ func uci(input chan string) {
5757
handleQuit()
5858
quit = true
5959
continue
60-
///// My own commands ///////////////////
60+
61+
///// My own commands ///////////////////
6162
case "perft":
6263
if len(words) > 1 {
6364
depth, err := strconv.Atoi(words[1])
@@ -347,11 +348,11 @@ func handleMoveVal() {
347348
bestMv = mv
348349
bestMsg = msg
349350
}
350-
fmt.Printf("%v: %v history %v, see %v, pcSqTab %v (%v)\n", ix+1, mv, history.get(mv.fr(), mv.to(), board.stm), see(mv.fr(), mv.to(), &board), pcSqScore(mv.pc(), mv.to()), msg)
351+
fmt.Printf("%v: %v history %v,\tsee %3v,\tdpcSqTab %v\t(%v)\n", ix+1, mv, history.get(mv.fr(), mv.to(), board.stm), see(mv.fr(), mv.to(), &board), pcSqScore(mv.pc(), mv.to())-pcSqScore(mv.pc(), mv.fr()), msg)
351352
ix++
352353
}
353354

354-
fmt.Printf("best History (%v): %v %v best hist+see (%v): %v %v \n", bestHmsg, bestHmv, bestHsc, bestMsg, bestMv, bestSc)
355+
fmt.Printf("best History (%v): %v %v \tbest hist+see (%v): %v %v \n", bestHmsg, bestHmv, bestHsc, bestMsg, bestMv, bestSc)
355356
}
356357

357358
// not implemented uci commands

0 commit comments

Comments
 (0)