Skip to content

Commit

Permalink
Added Board.evaluate_score
Browse files Browse the repository at this point in the history
  • Loading branch information
hrushikeshrv committed Jan 27, 2023
1 parent e68434f commit 33336b8
Show file tree
Hide file tree
Showing 25 changed files with 110 additions and 99 deletions.
83 changes: 48 additions & 35 deletions chessengine/bitboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class Board:
``black_bishops``, ``white_queens``, ``black_kings``.
:param side: The side that the _board_ will play. Should be one of "white" or "black"
:var score: The score/evaluation of the current board positions. A higher/more positive score favors
white, a lower/more negative score favors black
"""

def __init__(self, side: str):
Expand Down Expand Up @@ -100,6 +102,7 @@ def __init__(self, side: str):

self.all_pieces = self.all_black | self.all_white

self.score = 0
self.piece_count = {
("white", "kings"): 1,
("white", "queens"): 1,
Expand Down Expand Up @@ -223,33 +226,6 @@ def __hash__(self):
hash_str += str(self.board[(side, piece)]) + " "
return hash(hash_str)

@property
def score(self):
"""
The "score" of the board. A higher/more positive score favors white,
a lower/more negative score favors black.
"""
s = 0
piece_values = {
"pawns": 100,
"rooks": 500,
"knights": 320,
"bishops": 330,
"queens": 900,
"kings": 20000,
}
for i in range(64):
pos = 2**i
side, piece, _ = self.identify_piece_at(pos)
if side is None:
continue
elif side == "white":
s += piece_values[piece] + piece_square_table[(side, piece)][i]
else:
s -= piece_values[piece] + piece_square_table[(side, piece)][i]

return s

@property
def board_pieces(self):
"""
Expand Down Expand Up @@ -301,6 +277,35 @@ def copy(self):
Create and return a copy of the board.
"""
return copy(self)

def evaluate_score(self) -> int:
"""
Evaluate the current score/evaluation of the board state. Use this method to
reset the board score to the correct value if the game starts from an intermediate
stage.
:return: The score/evaluation of the current board state.
"""
s = 0
piece_values = {
"pawns": 100,
"rooks": 500,
"knights": 320,
"bishops": 330,
"queens": 900,
"kings": 20000,
}
for i in range(64):
pos = 2 ** i
side, piece, _ = self.identify_piece_at(pos)
if side is None:
continue
elif side == "white":
s += piece_values[piece] + piece_square_table[(side, piece)][i]
else:
s -= piece_values[piece] + piece_square_table[(side, piece)][i]

return s

def get_side_bitboard(self, side: str) -> int:
"""
Expand Down Expand Up @@ -408,7 +413,7 @@ def identify_piece_at(self, position: int) -> tuple:
if board & position > 0:
return side, piece, board

def move(self, start: int, end: int, track: bool = True) -> None:
def move(self, start: int, end: int, score: int = None, track: bool = True) -> None:
"""
Moves the piece at start to end. Doesn't check anything, just makes
the move (unless the start or end positions are invalid). Also checks if move
Expand All @@ -426,6 +431,7 @@ def move(self, start: int, end: int, track: bool = True) -> None:
:param start: The start position of the move. See :ref:`position_representation`
:param end: The end position of the move. See :ref:`position_representation`
:param score: The new score/evaluation of the board after the move is made
:param track: If ``True``, the move made will be stored in self.moves
:raises PositionError: If an invalid position was passed.
Expand Down Expand Up @@ -484,7 +490,7 @@ def move(self, start: int, end: int, track: bool = True) -> None:

# Track moves made so we can undo
if track:
start_state = (start, end, end_side, end_piece, end_board, castle_type)
start_state = (start, end, end_side, end_piece, end_board, castle_type, self.score)
self.moves.append(start_state)

# Check en passant moves
Expand Down Expand Up @@ -561,6 +567,12 @@ def move(self, start: int, end: int, track: bool = True) -> None:
self.move(2**56, 2**59, False) # Don't track this move
self.black_king_side_castle = False
self.black_queen_side_castle = False

# Update the board evaluation
if track:
if score is None:
score = self.evaluate_score()
self.score = score

def move_raw(self, start: int, end: int, track: bool = True) -> None:
"""
Expand Down Expand Up @@ -704,24 +716,25 @@ def undo_move(self) -> None:
"""
if not self.moves:
raise RuntimeError("No moves have been made yet to undo.")
end, start, side, piece, board, castle_type = self.moves.pop()

end, start, side, piece, board, castle_type, prev_score = self.moves.pop()
self.score = prev_score

if castle_type is not None:
# TODO - if user castles when both self.white_kingside and self.white_queenside are
# True, undoing this move only lets the user castle to the same side they castled
# before undoing. Same for black.
if castle_type == "white_kingside":
self.move(start=start, end=end, track=False) # Move king
self.move(2**5, 2**7) # Move rook
self.move(2**5, 2**7, track=False) # Move rook
elif castle_type == "white_queenside":
self.move(start=start, end=end, track=False) # Move king
self.move(2**3, 2**0) # Move rook
self.move(2**3, 2**0, track=False) # Move rook
elif castle_type == "black_kingside":
self.move(start=start, end=end, track=False) # Move king
self.move(2**61, 2**63) # Move rook
self.move(2**61, 2**63, track=False) # Move rook
elif castle_type == "black_queenside":
self.move(start=start, end=end, track=False) # Move king
self.move(2**59, 2**56) # Move rook
self.move(2**59, 2**56, track=False) # Move rook
else:
self.move(start=start, end=end, track=False)
if side is not None:
Expand Down
2 changes: 1 addition & 1 deletion chessengine/moves.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""


from chessengine.utils import get_rank, get_file
from chessengine.utils import get_rank, get_file, score_from_move

HIGHEST_SQUARE = 2**63

Expand Down
Binary file modified docs/_build/doctrees/environment.pickle
Binary file not shown.
Binary file modified docs/_build/doctrees/ref/chessengine.bitboard.doctree
Binary file not shown.
Binary file modified docs/_build/doctrees/ref/chessengine.utils.doctree
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/_build/html/.buildinfo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Sphinx build info version 1
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
config: ed7cd21f22bbbe764e31c3aef3b6d02b
config: 3ff6f3f857ba0379aaf1dace8b5e8b60
tags: 645f666f9bcd5a90fca523b33c5a78b7
2 changes: 1 addition & 1 deletion docs/_build/html/_static/documentation_options.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var DOCUMENTATION_OPTIONS = {
URL_ROOT: document.getElementById("documentation_options").getAttribute('data-url_root'),
VERSION: '0.3.0',
VERSION: '0.3.1',
LANGUAGE: 'en',
COLLAPSE_INDEX: false,
BUILDER: 'html',
Expand Down
6 changes: 3 additions & 3 deletions docs/_build/html/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<link rel="index" title="Index" href="genindex.html" /><link rel="search" title="Search" href="search.html" /><link rel="next" title="chessengine.bitboard" href="ref/chessengine.bitboard.html" /><link rel="prev" title="Welcome to chessengine’s documentation!" href="index.html" />

<meta name="generator" content="sphinx-5.1.1, furo 2022.09.15"/>
<title>API Reference - chessengine 0.3.0 documentation</title>
<title>API Reference - chessengine 0.3.1 documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="_static/styles/furo.css?digest=9ec31e2665bf879c1d47d93a8ec4893870ee1e45" />
<link rel="stylesheet" type="text/css" href="_static/styles/furo-extensions.css?digest=30d1aed668e5c3a91c3e3bf6a60b675221979f0e" />
Expand Down Expand Up @@ -123,7 +123,7 @@
</label>
</div>
<div class="header-center">
<a href="index.html"><div class="brand">chessengine 0.3.0 documentation</div></a>
<a href="index.html"><div class="brand">chessengine 0.3.1 documentation</div></a>
</div>
<div class="header-right">
<div class="theme-toggle-container theme-toggle-header">
Expand All @@ -146,7 +146,7 @@
<div class="sidebar-sticky"><a class="sidebar-brand" href="index.html">


<span class="sidebar-brand-text">chessengine 0.3.0 documentation</span>
<span class="sidebar-brand-text">chessengine 0.3.1 documentation</span>

</a><form class="sidebar-search-container" method="get" action="search.html" role="search">
<input class="sidebar-search" placeholder=Search name="q" aria-label="Search">
Expand Down
7 changes: 4 additions & 3 deletions docs/_build/html/chessboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<link rel="index" title="Index" href="genindex.html" /><link rel="search" title="Search" href="search.html" /><link rel="next" title="Usage &amp; Installation" href="usage.html" /><link rel="prev" title="chessengine.pgn.parser" href="ref/chessengine.pgn.parser.html" />

<meta name="generator" content="sphinx-5.1.1, furo 2022.09.15"/>
<title>Internal Board Representation - chessengine 0.3.0 documentation</title>
<title>Internal Board Representation - chessengine 0.3.1 documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="_static/styles/furo.css?digest=9ec31e2665bf879c1d47d93a8ec4893870ee1e45" />
<link rel="stylesheet" type="text/css" href="_static/styles/furo-extensions.css?digest=30d1aed668e5c3a91c3e3bf6a60b675221979f0e" />
Expand Down Expand Up @@ -123,7 +123,7 @@
</label>
</div>
<div class="header-center">
<a href="index.html"><div class="brand">chessengine 0.3.0 documentation</div></a>
<a href="index.html"><div class="brand">chessengine 0.3.1 documentation</div></a>
</div>
<div class="header-right">
<div class="theme-toggle-container theme-toggle-header">
Expand All @@ -146,7 +146,7 @@
<div class="sidebar-sticky"><a class="sidebar-brand" href="index.html">


<span class="sidebar-brand-text">chessengine 0.3.0 documentation</span>
<span class="sidebar-brand-text">chessengine 0.3.1 documentation</span>

</a><form class="sidebar-search-container" method="get" action="search.html" role="search">
<input class="sidebar-search" placeholder=Search name="q" aria-label="Search">
Expand All @@ -157,6 +157,7 @@
<ul class="current">
<li class="toctree-l1 has-children"><a class="reference internal" href="api.html">API Reference</a><input class="toctree-checkbox" id="toctree-checkbox-1" name="toctree-checkbox-1" role="switch" type="checkbox"/><label for="toctree-checkbox-1"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l2"><a class="reference internal" href="ref/chessengine.bitboard.html">chessengine.bitboard</a></li>
<li class="toctree-l2"><a class="reference internal" href="ref/chessengine.exceptions.html">chessengine.exceptions</a></li>
<li class="toctree-l2"><a class="reference internal" href="ref/chessengine.lookup_tables.html">chessengine.lookup_tables</a></li>
<li class="toctree-l2"><a class="reference internal" href="ref/chessengine.moves.html">chessengine.moves</a></li>
<li class="toctree-l2"><a class="reference internal" href="ref/chessengine.utils.html">chessengine.utils</a></li>
Expand Down
7 changes: 4 additions & 3 deletions docs/_build/html/contributing.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<link rel="index" title="Index" href="genindex.html" /><link rel="search" title="Search" href="search.html" /><link rel="prev" title="Playing A Game" href="playing.html" />

<meta name="generator" content="sphinx-5.1.1, furo 2022.09.15"/>
<title>Contributing A Patch - chessengine 0.3.0 documentation</title>
<title>Contributing A Patch - chessengine 0.3.1 documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="_static/styles/furo.css?digest=9ec31e2665bf879c1d47d93a8ec4893870ee1e45" />
<link rel="stylesheet" type="text/css" href="_static/styles/furo-extensions.css?digest=30d1aed668e5c3a91c3e3bf6a60b675221979f0e" />
Expand Down Expand Up @@ -123,7 +123,7 @@
</label>
</div>
<div class="header-center">
<a href="index.html"><div class="brand">chessengine 0.3.0 documentation</div></a>
<a href="index.html"><div class="brand">chessengine 0.3.1 documentation</div></a>
</div>
<div class="header-right">
<div class="theme-toggle-container theme-toggle-header">
Expand All @@ -146,7 +146,7 @@
<div class="sidebar-sticky"><a class="sidebar-brand" href="index.html">


<span class="sidebar-brand-text">chessengine 0.3.0 documentation</span>
<span class="sidebar-brand-text">chessengine 0.3.1 documentation</span>

</a><form class="sidebar-search-container" method="get" action="search.html" role="search">
<input class="sidebar-search" placeholder=Search name="q" aria-label="Search">
Expand All @@ -157,6 +157,7 @@
<ul class="current">
<li class="toctree-l1 has-children"><a class="reference internal" href="api.html">API Reference</a><input class="toctree-checkbox" id="toctree-checkbox-1" name="toctree-checkbox-1" role="switch" type="checkbox"/><label for="toctree-checkbox-1"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
<li class="toctree-l2"><a class="reference internal" href="ref/chessengine.bitboard.html">chessengine.bitboard</a></li>
<li class="toctree-l2"><a class="reference internal" href="ref/chessengine.exceptions.html">chessengine.exceptions</a></li>
<li class="toctree-l2"><a class="reference internal" href="ref/chessengine.lookup_tables.html">chessengine.lookup_tables</a></li>
<li class="toctree-l2"><a class="reference internal" href="ref/chessengine.moves.html">chessengine.moves</a></li>
<li class="toctree-l2"><a class="reference internal" href="ref/chessengine.utils.html">chessengine.utils</a></li>
Expand Down
10 changes: 4 additions & 6 deletions docs/_build/html/genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<meta name="color-scheme" content="light dark"><link rel="index" title="Index" href="#" /><link rel="search" title="Search" href="search.html" />

<meta name="generator" content="sphinx-5.1.1, furo 2022.09.15"/><title>Index - chessengine 0.3.0 documentation</title>
<meta name="generator" content="sphinx-5.1.1, furo 2022.09.15"/><title>Index - chessengine 0.3.1 documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="_static/styles/furo.css?digest=9ec31e2665bf879c1d47d93a8ec4893870ee1e45" />
<link rel="stylesheet" type="text/css" href="_static/styles/furo-extensions.css?digest=30d1aed668e5c3a91c3e3bf6a60b675221979f0e" />
Expand Down Expand Up @@ -121,7 +121,7 @@
</label>
</div>
<div class="header-center">
<a href="index.html"><div class="brand">chessengine 0.3.0 documentation</div></a>
<a href="index.html"><div class="brand">chessengine 0.3.1 documentation</div></a>
</div>
<div class="header-right">
<div class="theme-toggle-container theme-toggle-header">
Expand All @@ -144,7 +144,7 @@
<div class="sidebar-sticky"><a class="sidebar-brand" href="index.html">


<span class="sidebar-brand-text">chessengine 0.3.0 documentation</span>
<span class="sidebar-brand-text">chessengine 0.3.1 documentation</span>

</a><form class="sidebar-search-container" method="get" action="search.html" role="search">
<input class="sidebar-search" placeholder=Search name="q" aria-label="Search">
Expand Down Expand Up @@ -453,12 +453,10 @@ <h2>P</h2>
<h2>S</h2>
<table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="ref/chessengine.bitboard.html#chessengine.bitboard.Board.score">score (chessengine.bitboard.Board property)</a>
<li><a href="ref/chessengine.bitboard.html#chessengine.bitboard.Board.search_forward">search_forward() (chessengine.bitboard.Board method)</a>
</li>
</ul></td>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="ref/chessengine.bitboard.html#chessengine.bitboard.Board.search_forward">search_forward() (chessengine.bitboard.Board method)</a>
</li>
<li><a href="ref/chessengine.bitboard.html#chessengine.bitboard.Board.set_bitboard">set_bitboard() (chessengine.bitboard.Board method)</a>
</li>
</ul></td>
Expand Down
6 changes: 3 additions & 3 deletions docs/_build/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<link rel="index" title="Index" href="genindex.html" /><link rel="search" title="Search" href="search.html" /><link rel="next" title="API Reference" href="api.html" />

<meta name="generator" content="sphinx-5.1.1, furo 2022.09.15"/>
<title>chessengine 0.3.0 documentation</title>
<title>chessengine 0.3.1 documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="_static/styles/furo.css?digest=9ec31e2665bf879c1d47d93a8ec4893870ee1e45" />
<link rel="stylesheet" type="text/css" href="_static/styles/furo-extensions.css?digest=30d1aed668e5c3a91c3e3bf6a60b675221979f0e" />
Expand Down Expand Up @@ -123,7 +123,7 @@
</label>
</div>
<div class="header-center">
<a href="#"><div class="brand">chessengine 0.3.0 documentation</div></a>
<a href="#"><div class="brand">chessengine 0.3.1 documentation</div></a>
</div>
<div class="header-right">
<div class="theme-toggle-container theme-toggle-header">
Expand All @@ -146,7 +146,7 @@
<div class="sidebar-sticky"><a class="sidebar-brand" href="#">


<span class="sidebar-brand-text">chessengine 0.3.0 documentation</span>
<span class="sidebar-brand-text">chessengine 0.3.1 documentation</span>

</a><form class="sidebar-search-container" method="get" action="search.html" role="search">
<input class="sidebar-search" placeholder=Search name="q" aria-label="Search">
Expand Down
Binary file modified docs/_build/html/objects.inv
Binary file not shown.
Loading

0 comments on commit 33336b8

Please sign in to comment.