MSc C++ Programming – Assessed Exercise No. 3
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
Department of Computing
MSc C++ Programming – Assessed Exercise No. 3
The Exercise
You are required to write a program that simulates and manages chess games. Your program should load chess game states from Forsyth–Edwards Notation (FEN) strings. Your program should accept moves in the format (source square, destination square), where a square is referred to using a letter in the range A-H and a number in the range 1-8. It should validate each move and keep track of the state of the game, detecting when the game is over and producing appropriate output to the user.
Write appropriate class definitions in C++ which correctly interface with the main pro- gram given in the file ChessMain.cpp. This main program contains code which tries to give various kinds of erroneous input and an example chess game, which your solution should handle as indicated below in the sample output. Your submission must include a makefile that compiles your program and creates an executable file with the name chess.
The chess board is initially set up as per the following diagram:
Each row (numbered 1-8) is called a rank, and each column (labelled A to H) is called a file. White moves first, and thereafter players take it in turns to make moves. Pieces are moved to either an unoccupied square or one occupied by an opponent’s piece, which iscaptured and removed from play. Each piece moves according to the following rules:
固 The king moves one square in any direction.
The rook (or castle) can move any number of squares along any rank or file, but may not leap over other pieces.
里 The bishop can move any number of squares diagonally, but may not leap over other pieces.
The queen combines the power of the rook and bishop and can move any number of squares along rank, file, or diagonal, but it may not leap over other pieces.
The knight moves to any of the closest squares that are not on the same rank, file, or diagonal, thus the move forms an ‘L’-shape: two squares vertically and one square horizontally, or two squares horizontally and one square vertically. The knight is the only piece that can leap over other pieces. The pawn may move forward to the unoccupied square immediately in front of it on the same file; or on its first move it may advance two squares along the same file provided both squares are unoccupied; or it may move to a square occupied by an opponent’s piece which is diagonally in front of it on an adjacent file, capturing that piece.
When the king is under immediate attack by one or more of the opponent’s pieces, it is said to be in check. A response to a check is a legal move if it results in a position where the king is no longer under direct attack (i.e., not in check). This can involve capturing the checking piece; interposing a piece between the checking piece and the king (which is possible only if the attacking piece is not a knight and there is a square between it and the king); or moving the king to a square where it is not under attack. It is illegal for a player to make a move that would put or leave his own king in check.
The object of the game is to checkmate the opponent; this occurs when the opponent’s king is in check, and there is no legal way to remove it from attack. When checkmate occurs, the game is over and the player whose king is checkmated loses. The game may also end in a stalemate: this is where the player whose turn it is to move is not in check but has no legal move that they can make.
N.B. While you are not required to implement any of the chess rules of castling, en passant, or pawn promotion, a correct and elegant implementation of castling will gain you a few bonus points.
A FEN record completely specifies the state of a chess game, in one line of text and using only the ASCII character set.
A full FEN record contains six fields, each separated by a space. For this exercise we shall only require the first three fields, but you may include as many of the others as you wish.
1. Piece placement data: Each rank is described, starting with rank 8 and ending with rank 1, with a ‘/’ between each one; within each rank, the contents of the squares are described in order from the a-file to the h-file. Each piece is identified by a single letter taken from the standard English names in algebraic notation (pawn = ‘P’, knight = ‘N’, bishop = ‘B’, rook = ‘R’, queen = ‘Q’, and king = ‘K’). White pieces are designated using uppercase letters ("PNBRQK"), while black pieces use lowercase letters ("pnbrqk"). A set of one or more consecutive empty squares within a rank is denoted by a digit from ”1” to ”8”, corresponding to the number of squares.
2. Active color: ‘w’ means that White is to move, and ‘b’ means that Black is to move.
3. Castling availability: If neither side has the ability to castle, this field uses the character ‘-’ . Otherwise, this field contains one or more letters: ‘K’ if White can castle kingside, ‘Q’ if White can castle queenside, ‘k’ if Black can castle kingside, and ‘q’ if Black can castle queenside. A situation that temporarily prevents castling does not prevent the use of this notation.
For example, here is the FEN for the starting position, containing only the first three fields:
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq
For the purposes of this exercise, you may assume that any FEN string you will be provided with will be a valid example of the first three fields, and will contain at minimum exactly one white king and one black king.
N.B. You are not required to implement the logic for the third field (castling availability) in order to achieve full marks. It is included simply to allow for those attempting to acquire some bonus marks for a castling implementation.
Example of a Chess Game
The specific game which is simulated in the main function found in ChessMain.cpp is a short game played between Alexander Alekhine, and his opponent Milorad Vasic Miles in 1931. In algebraic chess notation, the game played out as follows:
1. e4 e6
2. d4 d5
3. Nc3 Bb4
4. Bd3 Bxc3+
5. bxc3 h6
6. Ba3 Nd7
7. Qe2 dxe4
8. Bxe4 Ngf6
9. Bd3 b6
10. Qxe6+ fxe6
11. Bg6#
The Output
========================
Testing the Chess Engine
========================
A new board state is loaded!
It is not Black’s turn to move!
There is no piece at position D4!
White’s Pawn moves from D2 to D4
Black’s Bishop cannot move to B4!
=========================
Alekhine vs . Vasic (1931)
=========================
A new board state is loaded!
White’s Pawn moves from E2 to E4
Black’s Pawn moves from E7 to E6
White’s Pawn moves from D2 to D4
Black’s Pawn moves from D7 to D5
White’s Knight moves from B1 to C3
Black’s Bishop moves from F8 to B4
White’s Bishop moves from F1 to D3
Black’s Bishop moves from B4 to C3 taking White’s Knight
White is in check
White’s Pawn moves from B2 to C3 taking Black’s Bishop
Black’s Pawn moves from H7 to H6
White’s Bishop moves from C1 to A3
Black’s Knight moves from B8 to D7
White’s Queen moves from D1 to E2
Black’s Pawn moves from D5 to E4 taking White’s Pawn
White’s Bishop moves from D3 to E4 taking Black’s Pawn
Black’s Knight moves from G8 to F6
White’s Bishop moves from E4 to D3
Black’s Pawn moves from B7 to B6
White’s Queen moves from E2 to E6 taking Black’s Pawn
Black is in check
Black’s Pawn moves from F7 to E6 taking White’s Queen
White’s Bishop moves from D3 to G6
Black is in checkmate
Approach
A suitable way to approach the exercise could be as follows:
. Create classes for each different kind of chess piece - each class will be responsible for determining which moves are valid for itself.
. Provide an interface through which a ChessBoard class interacts with its pieces.
You might like to ask yourself the following questions:
. What information does the ChessBoard class require from each of its pieces to be able to validate the moves that are submitted to it?
. What information does a chess piece need to know in order to determine which moves it can make?
. What conditions must be checked after each piece has moved?
If you wish, you may use the C++ Standard Template Library (STL).
2023-12-15