Skip to content

Commit

Permalink
Fix %eval sometimes off by one centipawn
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Dec 26, 2022
1 parent b04275a commit cc079b6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
14 changes: 7 additions & 7 deletions chess/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -1538,16 +1538,16 @@ def _go(self, limit: Limit, *, root_moves: Optional[Iterable[chess.Move]] = None
builder.append("ponder")
if limit.white_clock is not None:
builder.append("wtime")
builder.append(str(max(1, int(limit.white_clock * 1000))))
builder.append(str(max(1, round(limit.white_clock * 1000))))
if limit.black_clock is not None:
builder.append("btime")
builder.append(str(max(1, int(limit.black_clock * 1000))))
builder.append(str(max(1, round(limit.black_clock * 1000))))
if limit.white_inc is not None:
builder.append("winc")
builder.append(str(int(limit.white_inc * 1000)))
builder.append(str(round(limit.white_inc * 1000)))
if limit.black_inc is not None:
builder.append("binc")
builder.append(str(int(limit.black_inc * 1000)))
builder.append(str(round(limit.black_inc * 1000)))
if limit.remaining_moves is not None and int(limit.remaining_moves) > 0:
builder.append("movestogo")
builder.append(str(int(limit.remaining_moves)))
Expand All @@ -1562,7 +1562,7 @@ def _go(self, limit: Limit, *, root_moves: Optional[Iterable[chess.Move]] = None
builder.append(str(max(1, int(limit.mate))))
if limit.time is not None:
builder.append("movetime")
builder.append(str(max(1, int(limit.time * 1000))))
builder.append(str(max(1, round(limit.time * 1000))))
if infinite:
builder.append("infinite")
if root_moves is not None:
Expand Down Expand Up @@ -2160,9 +2160,9 @@ def start(self, engine: XBoardProtocol) -> None:
if limit.depth is not None:
engine.send_line(f"sd {max(1, int(limit.depth))}")
if limit.white_clock is not None:
engine.send_line("{} {}".format("time" if board.turn else "otim", max(1, int(limit.white_clock * 100))))
engine.send_line("{} {}".format("time" if board.turn else "otim", max(1, round(limit.white_clock * 100))))
if limit.black_clock is not None:
engine.send_line("{} {}".format("otim" if board.turn else "time", max(1, int(limit.black_clock * 100))))
engine.send_line("{} {}".format("otim" if board.turn else "time", max(1, round(limit.black_clock * 100))))

if draw_offered and engine.features.get("draw", 1):
engine.send_line("draw")
Expand Down
2 changes: 1 addition & 1 deletion chess/pgn.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ def eval(self) -> Optional[chess.engine.PovScore]:
# who has been mated.
return chess.engine.PovScore(score, turn)
else:
score = chess.engine.Cp(int(float(match.group("cp")) * 100))
score = chess.engine.Cp(round(float(match.group("cp")) * 100))

return chess.engine.PovScore(score if turn else -score, turn)

Expand Down
6 changes: 6 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2806,6 +2806,12 @@ def test_annotations(self):
game.set_arrows([])
self.assertEqual(game.comment, "foo [%bar] baz")

def test_eval(self):
game = chess.pgn.Game()
for cp in range(199, 220):
game.set_eval(chess.engine.PovScore(chess.engine.Cp(cp), chess.WHITE))
self.assertEqual(game.eval().white().cp, cp)

def test_float_emt(self):
game = chess.pgn.Game()
game.comment = "[%emt 0:00:01.234]"
Expand Down

0 comments on commit cc079b6

Please sign in to comment.