-
Notifications
You must be signed in to change notification settings - Fork 0
Game Implementation
So, basically we want a program to start a new chess game, and then send it a bunch of moves. For each move, it should first check whether the move is valid and legal, if so, make the move, and then check if the game is finished, and if so, announce who won.
For this, we first need to keep track of a bunch of information. The most obvious and important one is the board representation, i.e. which pieces are on which squares. There are also other information we need to store, but let's start with the chessboard.
We can use different data structures to store the board, e.g. bitboards are a very efficient way, but a bit(!) more elaborate to implement. For now, I'm going to stick with an array.
So, one option is to have an array (list) of pieces on the board, labeled by their positions. But the chessboard is so obviously a grid, so we can also implement it as an 8x8 matrix (2D array). Each square of the board will then correspond to a matrix element, which tells us which piece (if any) is on that square. We can then represent the square information by a number in our matrix:
- 0: empty square
- 1: pawn
- 2: knight
- 3: bishop
- 4: rook,
- 5: queen
- 6: king
Also, to differentiate between black and white pieces, we can make the black's numbers negative, so for example -1 is black pawn, +1 is white pawn, -2 is black knight, +2 is white knight etc.
We will also use numpy
for our 2D-array, since we will need to manipulate our chessboard a lot to extract data when implementing the "judge", and numpy arrays are much more efficient and easier to manipulate.
In our 2D array, the 1st and 2nd dimensions will correspond to the ranks (rows) and files (columns) of the board, respectively. So, for example the element at index [0, 0] will be the a1 square, [0, 7] will be h1, and [7, 0] will be a8.
Having decided on all these details, we can now write the first piece of code; a function to create a new board with all pieces in the starting position. We put it in a module named game.py
:
def new_board() -> np.ndarray:
"""
Create a chessboard in starting position.
"""
board = np.zeros((8, 8), dtype=np.int8) # Initialize an all-zero 8x8 array
board[1, :] = 1 # Set white pawns on row 2
board[-2, :] = -1 # Set black pawns on row 7
board[0, :] = [4, 2, 3, 5, 6, 3, 2, 4] # Set white's main pieces on row 1
board[-1, :] = -board[0] # Set black's main pieces on row 8
return board
It creates an 8x8 array with all elements initialized to 0 (since our values are only between -6 and 6, we can use the smallest integer data-type numpy has, i.e. int8, to save memory). It then assigns the piece values to rows 1, 2, 7, 8, and returns the array.
So this is how our board looks like; remember, the rows go from top to bottom (1 to 8), and the columns go from left to right (a to h), so the top-left element (index [0, 0]) is the a1 square:
[
[ 4, 2, 3, 5, 6, 3, 2, 4],
[ 1, 1, 1, 1, 1, 1, 1, 1],
[ 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0],
[-1, -1, -1, -1, -1, -1, -1, -1],
[-4, -2, -3, -5, -6, -3, -2, -4],
]