Chess
Chess is a strategy game that takes place on a square board called
"chessboard", made up of 64 squares, on which each player has 16 pieces.
The purpose of this didactic GUI-based project is to improve my programming skills.
The structure of the project is quite simple. The main classes are the "Chess" class and the "DrawBoard"
class.
The "DrawBoard" class creates the cells of the chessboard, loads the assets of each piece (only once) and
fills the cells with the corresponding color (based on the color scheme that is in use at that moment).
@staticmethod
def laod_assets():
pieces = [ ... ]
for piece in pieces:
const.IMAGES[piece] = pygame.transform.scale(
pygame.image.load(
"./img/assets/{}.png".format(piece)
),
(const.SQ_SIZE, const.SQ_SIZE)
)
The main loop of the game is located in the main() function of the main.py file, which also contains the
controls for the keyboard and mouse events.
In addition to the movement of a piece, done by clicking first on the cell containing the piece to be moved
and then on an empty square (if it's a legal move), it is possible to undo the movement just made by using
the "z" key. And in addition to this, with the "r" key it is possible to reset game (and immediately starting
a new one).
At the moment there are only two color schemes for the chessboard (it's possible to change the color
scheme of the chessboard by using the "1" and "2" keys).
for event in pygame.event.get():
# quit the game
if event.type == pygame.QUIT:
...
# select a cell (could be an empty cell)
elif event.type == pygame.MOUSEBUTTONDOWN:
...
elif event.type == pygame.KEYDOWN:
# undo a move
if event.key == pygame.K_z and not game_over:
...
# set the default colorscheme
if event.key == pygame.K_1:
...
# set the alternative color scheme
if event.key == pygame.K_2:
...
# reset the game
if event.key == pygame.K_r:
...
The main logic of the game is managed in the Chess class. The chessboard is represented as a list of lists.
The elements of each list are strings that represent a single piece. Each piece is given a unique alias
which is associated, in an HashMap, with the respective movement function.
Thanks to these two structures, the methods that compute the possible moves of each piece are all inside
the Chess class.
class Chess(object):
def __init__(self):
# in the actual code, the strings are just aliases for
# each piece and are located in the constants.py file
self.board = np.array(
[
["♜", "♞", "♝", "♛", "♚", "♝", "♞", "♜"],
["♟", "♟", "♟", "♟", "♟", "♟", "♟", "♟"],
[" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "],
["♙", "♙", "♙", "♙", "♙", "♙", "♙", "♙"],
["♖", "♘", "♗", "♕", "♔", "♗", "♘", "♖"]
]
)
self.moves_map = {
"P": self.__pawn_moves,
"R": self.__rook_moves,
"N": self.__knight_moves,
"B": self.__bishop_moves,
"Q": self.__queen_moves,
"K": self.__king_moves
}
Each move, however, is an instance of the "MoveManager" class. This class facilitates the management of
special moves (en-passant, castling and pawn promotion) and associates each move with a unique ID.
Even though the project has been completed, I have decided to add some more features. After testing
the features, my goal is to implement a neural network (so it would also become possible to play alone,
since now the game does not have an AI or an online game-mode).
The source code is accessible from this link.
Project timeline
January 2021
Core of the project. Generate all valid moves, manage the special moves and make the game work without a GUI (by printing the chessboard in the terminal after each move).
February 2021
Design and implement the GUI. A good library for this task is pygame. Make it possible to reset the game, to undo a move, and to change the color scheme of the chessboard.
March 2021
Every component works and the project has been completed.
Possible improvements
Implement the Forsyth–Edwards Notation, add a loader for a custom color scheme, create a setup.py script, and test the project under Linux and MacOS.