Language/Sprache: .English. .Deutsch.
About Me TinkeringWall ClockOscilloscope ConsoleTic-Tac-ToeChess Visualizer Web Designpoppi.rockssoftechenergy.comrs-topclean-hms.ch Illustrations
My Digital Chessboard As a forerunner for my electronical chessboard
Fool's Mate
Fool's Mate

Alongside the Tic-Tac-Toe game, this small project is also a systematic component of a larger project.

By implementing the rules of chess in C++, I was able to validate the game controller component of my electronic chessboard ZAPUS on PC hardware. The graphical representation was implemented using Simple DirectMedia Layer (SDL).

In addition to developing the algorithms for determining the possible moves of individual pieces according to the rules of chess, the main effort involved designing a software architecture that abstracts the underlying chessboard. Since this software was also intended to be used for the electronic chessboard ZAPUS, it was necessary to define an interface through which the graphical SDL chessboard could easily be replaced by an implementation for a hardware chessboard. The architecture required for this was derived from an interaction diagram.

For a chess game, recording the moves made and the positions of the individual pieces is of the utmost importance. If the game logic were to miss even a single move, this could have a significant impact on the subsequent course of the game. It is therefore essential to ensure that calculating possible moves never blocks the detection of new input. The calculation must consequently be interruptible so that recording new input can be prioritized.

Architecture

Class diagram
Class diagram

The game logic Game is instantiated as an attribute by the chessboard Board. When Game is constructed, it is passed a pointer to the chessboard Board, allowing Game to access the Board that created it.

As described above, the chessboard should be easily replaceable. Therefore, the Board facade defines a fixed interface that specifies communication between the game logic and the chessboard through the three methods pick(position), highlight(bitmap), and clearHighlight(). A concrete chessboard implementation is provided by a class derived from Board, which implements the required methods.

The game logic Game creates the initial arrangement of the pieces, which are implemented as subclasses of Piece, and acts as an intermediary between the pieces and the chessboard. The 64-byte array situation assigns either a piece or nullptr to each position on the chessboard, with nullptr indicating that the corresponding square is empty. The game logic therefore coordinates the positioning of the pieces within situation.

A Piece provides a method for calculating its possible moves within a given chess position represented by situation. When a square is selected, the game logic passes a pointer to situation to the selected piece, giving it access to the current state of the chessboard from which it can derive its possible moves. Since calculating possible moves also requires checking whether a move would leave the allied king in check, each piece provides a method that can determine with minimal computational effort whether the king would be in check after a move is executed.

Interaction Profile

Interaction diagram
Sketch of an interaction between the system components

According to the interaction diagram, the game logic, implemented by the Game class, provides the chessboard with only the pick(position) method. The chessboard calls this method whenever a square on the board is pressed. Based on the sequence of square selections, Game determines whether the input represents the selection of a piece or the execution of a move. When a move is executed, the game logic distinguishes between the different types of moves and provides the interaction profile required for each of them.

The chessboard provides the game logic with only two methods. Using highlight(bitmap), the game logic Game can provide a bitmap indicating which squares should be illuminated or highlighted. clearHighlight() resets all highlighted squares to their normal state. In addition to indicating the possible moves of a selected piece, highlight(bitmap) is also used to display the promotion menu.

The promotion menu is created as soon as a pawn is moved to the opponent's back rank. The four corners of the chessboard are highlighted, with each corner representing one of the four pieces to which the pawn can be promoted.