@@ -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 }
0 commit comments