Skip to content

Commit

Permalink
Add alternative move as variation
Browse files Browse the repository at this point in the history
* If engine and game moves are the same, just add score/depth comment to the game move.
* If engine and game moves are not the same, add engine move as variation along with score/depth comment and also add score/depth comment to the game move so user can see the difference in score.
* Refactor function evaluate()

Example output:

[Event "New in Chess Classic Prelims 2021"]
[Site "http://www.chessbomb.com"]
[Date "Sat Apr 24 2021"]
[Round "01"]
[White "Radjabov, Teimour"]
[Black "Jones, Gawain C B"]
[Result "1-0"]
[WhiteFideId "13400924"]
[BlackFideId "409561"]
[WhiteElo "2765"]
[BlackElo "2670"]
[TimeControl "900+10"]
[WhiteClock "0:02:28"]
[BlackClock "0:00:11"]
[Annotator "Stockfish @500 ms/pos"]

1. Nf3 Nf6 2. g3 g6 3. Bg2 Bg7 4. c4 c5 5. Nc3 O-O 6. O-O Nc6 7. d3 d5 8. cxd5 {0.0/18} Nxd5 {0.0/20} 9. Nxd5 {0.0/22} Qxd5 {0.0/22} 10. Be3 {0.0/23} Qd6 {-1.0/18} (10... Bxb2 {0.0/23}) 11. Rc1 {0.0/19} Nd4 {-0.0/19} 12. Nxd4 {1.0/21} cxd4 {-0.0/20} 13. Bd2 {0.0/17} Be6 {-1.0/19} (13... Bg4 {-1.0/17}) 14. Qa4 {1.0/19} a5 {-1.0/19} 15. Qb5 {1.0/18} (15. Rc2 {1.0/19}) Bxa2 $2 {-2.0/20} (15... Bd7 {-1.0/19}) 16. Bxa5 {2.0/19} Qe6 {-1.0/17} (16... Be6 {-2.0/17}) 17. Bf3 {1.0/18} (17. Bc7 {2.0/18}) Be5 $2 {-2.0/19} (17... Qb3 {-1.0/19}) 18. Bc7 {2.0/20} Bxc7 {-2.0/21} (18... Bd6 {-2.0/21}) 19. Rxc7 {2.0/21} Qd6 {-3.0/19} (19... Rab8 {-2.0/20}) 20. Rd7 {3.0/20} Qf6 {-3.0/18} 21. Rxb7 {2.0/20} (21. Qb4 {3.0/19}) Be6 {-2.0/21} 22. h4 {2.0/20} Rac8 {-3.0/18} (22... Rfc8 {-2.0/21}) 23. Qb4 {2.0/18} (23. Ra1 {3.0/19}) Rfe8 {-2.0/19} 24. Ra1 {2.0/20} h5 {-3.0/19} 25. Qd2 {2.0/18} (25. Kg2 {3.0/19}) Bg4 {-2.0/18} (25... Ra8 {-2.0/19}) 26. Bxg4 {2.0/18} (26. Kg2 {2.0/18}) hxg4 {-2.0/19} 27. Rb5 {2.0/18} Rc7 {-2.0/19} (27... Rc6 {-2.0/19}) 28. Rba5 {1.0/19} (28. b4 {2.0/19}) Kg7 {-1.0/19} (28... Rec8 {-2.0/18}) 29. Ra6 {2.0/20} (29. Qg5 {1.0/20}) e6 {-1.0/19} (29... Qf5 {-1.0/21}) 30. Ra8 {1.0/20} (30. b4 {1.0/19}) Rec8 $2 {-2.0/22} (30... Rxa8 {-1.0/22}) 31. Rxc8 {2.0/22} Rxc8 {-2.0/21} 32. b4 {2.0/21} Qe5 {-2.0/19} (32... Rb8 {-2.0/21}) 33. Qb2 {2.0/20} (33. Rb1 {2.0/21}) Rb8 {-2.0/20} 34. Qd2 {2.0/19} (34. Rc1 {2.0/19}) Qd6 {-2.0/19} (34... Rb7 {-2.0/19}) 35. Qg5 {2.0/21} Rxb4 {-3.0/21} (35... Qxb4 {-2.0/20}) 36. h5 {3.0/21} Qb8 {-3.0/20} (36... Rb7 {-3.0/20}) 37. Qxg4 {3.0/21} e5 {-5.0/22} (37... Rb1+ {-3.0/22}) 38. hxg6 {6.0/21} fxg6 {-6.0/20} 39. Qd7+ {6.0/19} Kh6 {-6.0/20} 40. Ra7 {6.0/21} Rb1+ {-6.0/21} 41. Kg2 {6.0/23} Rb6 {-0.0/30} (41... Qxa7 {-6.0/25}) 42. Qh3+ {0.0/39} (42. Qh7+ {0.0/36}) Kg5 {-0.0/42} 43. Rf7 {75.0/28} (43. Qh4+ {0.0/44}) Qa8+ {-0.0/245} (43... Qb7+ {-76.0/33}) 44. f3 {0.0/245} 1-0
  • Loading branch information
fsmosca committed May 2, 2021
1 parent a8a29e6 commit dcd9c29
Showing 1 changed file with 37 additions and 29 deletions.
66 changes: 37 additions & 29 deletions savvy.jl
Expand Up @@ -7,12 +7,8 @@ function evaluate(engine, board, movetime::Int)
score = nothing
depth = 0

if ischeckmate(board)
return "checkmate", score, depth
end

if isstalemate(board)
return "stalemate", score, depth
if ischeckmate(board) || isstalemate(board)
return bm, score, depth
end

setboard(engine, board)
Expand Down Expand Up @@ -65,7 +61,7 @@ function analyze(in_pgnfn::String, out_pgnfn::String, engine_filename::String, m
bd = g.node.board
gm_movesan = movetosan(bd, move)

domove!(mygame, move) # Save the move on our game.
domove!(mygame, move) # Save the move to mygame.

if ply(mygame) < analysisminply
forward!(g) # Push the move on the main board.
Expand All @@ -76,43 +72,55 @@ function analyze(in_pgnfn::String, out_pgnfn::String, engine_filename::String, m
bm, score, depth = evaluate(engine, bd, movetime)
em_movesan = movetosan(bd, bm)
em_score = round(score.value/100)
em_comment = em_movesan * " " * string(em_score) * "/" * string(depth)
em_comment = string(em_score) * "/" * string(depth)

# If engine best move and game move are not the same, evaluate the game move too.
if gm_movesan != em_movesan
undo = domove!(bd, move)

bm, score, depth = evaluate(engine, bd, movetime)
_, score, depth = evaluate(engine, bd, movetime)
if isnothing(score)
gm_comment = bm
gm_score = score
else
gm_score = round(-score.value/100) # cp to p
gm_comment = gm_movesan * " " * string(gm_score) * "/" * string(depth)
gm_comment = string(gm_score) * "/" * string(depth)
end

undomove!(bd, undo)
end

# Add comment.
if gm_movesan == em_movesan
comment = "game: " * em_comment
else
comment = "game: " * gm_comment * ", engine: " * em_comment
end

adddata!(mygame.node, "comment", comment)

# Add NAGs.
# https://en.wikipedia.org/wiki/Numeric_Annotation_Glyphs
if gm_movesan != em_movesan && !isnothing(gm_score)
# Add ??, from playable to losing score.
if gm_score <= -3.0 && em_score >= -1.0
adddata!(mygame, "nag", 4)
# Add ?, from playable to bad score.
elseif gm_score < -1.0 && em_score >= -1.0
adddata!(mygame, "nag", 2)
# Insert engine move as variation to mygame if the game and engine move
# are not the same otherwise just add comment to game move.
if gm_movesan != em_movesan
if !isnothing(gm_score)
# Add comment for the evaluation of game move.
adddata!(mygame.node, "comment", gm_comment)

# Add NAG's
# Ref: https://en.wikipedia.org/wiki/Numeric_Annotation_Glyphs

# Add ?? NAG to game move, when score turns from playable to losing.
if gm_score <= -3.0 && em_score >= -1.0
adddata!(mygame, "nag", 4)
# Else add ?, from playable to bad score.
elseif gm_score < -1.0 && em_score >= -1.0
adddata!(mygame, "nag", 2)
end
end

# Back off 1 ply to insert engine move as variation.
back!(mygame)
addmove!(mygame, em_movesan)

# Add comment for the evaluation of engine move.
adddata!(mygame.node, "comment", em_comment)

# Restore to a node after inserting the variation.
back!(mygame)
forward!(mygame)
else
# Add comment for the evaluation of game move, use the engine evaluation.
adddata!(mygame.node, "comment", em_comment)
end

forward!(g) # Push the move on the main board.
Expand Down

0 comments on commit dcd9c29

Please sign in to comment.