Home

LaMoSca v0.02

Pseudo-legal Move Generation

Objective

MonitorThis version of the program generates the list of possible moves for the current position of the pieces on the chessboard. Although the moves respect the way chess pieces move, their full legality is not checked (e.g. whether a check persists), which is why they are called pseudo-legal moves.

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 alternatives

A 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.
A case in which generating only some moves is still worthwhile is the generation of captures alone, used to look for stable game situations ("quiescence"), that is, positions in which no pieces are being exchanged between the two sides. This search is carried out to avoid misjudging positions because of merely temporary material advantages during long exchanges (the "horizon effect").

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 code

Movement vectors

In 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 squares

One 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 file

To 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: b8a6 b8c6 g8f6 g8h6 a7a6 a7a5 b7b6 b7b5 c7c6 c7c5 d7d6 d7d5 e7e6 e7e5 f7f6 f7f5 g7g6 g7g5 h7h6 h7h5
Mosse: e4d5 e4e5 a2a3 a2a4 b2b3 b2b4 c2c3 c2c4 d2d3 d2d4 f2f3 f2f4 g2g3 g2g4 h2h3 h2h4 b1a3 b1c3 d1e2 d1f3 d1g4 d1h5 e1e2 f1e2 f1d3 f1c4 f1b5 f1a6 g1f3 g1h3 g1e2

("Mosse" is Italian for "moves": the program's log is in Italian.)

Download

LaMoSca02.zip (4.43 k)

To do

  • generate legal moves;
  • generate special moves such as castling, promotions and en passant captures;
  • let the program make its own moves;
  • handle the graphical interface;
  • detect draws by repetition and by the 50-move rule.

References

  • Paolo Ciancarini:
    "I Giocatori Artificiali";
    Mursia (in Italian)

Links


Back Index Next