![]() |
LaMoSca v0.02Pseudo-legal Move Generation |
Objective
For now the list is only used to check whether the moves typed in by the user are allowed, by comparing them with those generated by the software. The alternativesA program can follow different strategies when generating its own moves, depending on whether it generates all the possible moves and then discards the illegal and weaker ones, or concentrates on a small number of good candidates.
Today most chess programs follow the first approach: the computer generates all the possible moves
of the pieces, whether or not they are legal (e.g. whether they leave the king in check); these moves are
called "pseudo-legal". The software then discards the invalid ones while searching for its best move. Another choice is between generating moves algorithmically, following the rules of each piece, and precompiling libraries or tables of moves from which the software selects according to the current position on the board, with large savings in processing time. The second approach applies first of all to the opening, with opening libraries for the first phase of the game, before switching to algorithmic generation in the middlegame. The other field is the endgame, with tables that identify theoretically solved endings. Algorithmic move generation can also "precompile" tables with the moves the pieces can make, so that generating the moves becomes a matter of looking up these tables, using the current position as the key. The codeMovement vectorsIn the program the chessboard is stored as a continuous array of squares, with indices running from "0" for A8 to 63 for H1. To generate moves efficiently, the computer can store for each piece a vector with the offsets of all the possible destination squares from the current square. For a knight, for example, the movement vector would be -17, -15, -10, -6, 6, 10, 15, 17 |
|
With the piece on square 36, the possible destination squares, to be saved on a dedicated stack, would be 19, 21, 26, 30, 42, 46, 51, 53 For pieces that can move more than one square (e.g. the bishop) the movement vector is applied repeatedly until an occupied square is found, and one move is saved on the stack for each square visited. |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63 |
Checking with border squaresOne problem in chess move generation is checking that the destination square lies inside the board. For example, all the moves of a king on H1 going down or to the right must be discarded. |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, -1, -1, 8, 9, 10, 11, 12, 13, 14, 15, -1, -1, 16, 17, 18, 19, 20, 21, 22, 23, -1, -1, 24, 25, 26, 27, 28, 29, 30, 31, -1, -1, 32, 33, 34, 35, 36, 37, 38, 39, -1, -1, 40, 41, 42, 43, 44, 45, 46, 47, -1, -1, 48, 49, 50, 51, 52, 53, 54, 55, -1, -1, 56, 57, 58, 59, 60, 61, 62, 63, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1. 21, 22, 23, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 41, 42, 43, 44, 45, 46, 47, 48, 51, 52, 53, 54, 55, 56, 57, 58, 61, 62, 63, 64, 65, 66, 67, 68, 71, 72, 73, 74, 75, 76, 77, 78, 81, 82, 83, 84, 85, 86, 87, 88, 91, 92, 93, 94, 95, 96, 97, 98 |
One technique is that of "border squares". A matrix is created that has conventional values (e.g. -1) outside the 8x8 squares of the board. The number of extra rows is dictated by the knight, which can jump two squares beyond its own. Two rows are therefore needed above and below the ordinary board, while a single column is enough on the right and on the left, since each of them can be treated as the continuation of the other. The matrix is therefore 10x12, like the one shown alongside. To access this matrix the software uses a second, 8x8 matrix that maps the indices of the 8x8 matrix to those of the 10x12 one. During the game, the destination square is inside the board if the value read from the 10x12 matrix is not "-1". |
The log fileTo keep track of how the chess program works during the game, this version can write a log file, which is enabled by passing the parameter "log" when the program is started. For example, the file "LaMoScaLogFile.txt" shows that the moves generated after the inputs "e2e4", "d7d5" and "e4e5" are:
Mosse: a2a3 a2a4 b2b3 b2b4 c2c3 c2c4 d2d3 d2d4 e2e3
e2e4 f2f3 f2f4 g2g3 g2g4 h2h3 h2h4 b1a3 b1c3 g1f3 g1h3 ("Mosse" is Italian for "moves": the program's log is in Italian.) |
DownloadTo do
|
References
Links |
| Back | Index | Next |