Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intelligence for Games and Puzzles1 Some Chess-specific Techniques Chess is played on an 8x8.

Similar presentations


Presentation on theme: "Intelligence for Games and Puzzles1 Some Chess-specific Techniques Chess is played on an 8x8."— Presentation transcript:

1 http://csiweb.ucd.ie/Staff/acater/comp4031.htmlArtificial Intelligence for Games and Puzzles1 Some Chess-specific Techniques Chess is played on an 8x8 board 8 is a power of two Computers are especially good at handling powers of two This happy accident has been exploited in chess programs in two main ways: move generation speedups bitboard representations

2 http://csiweb.ucd.ie/Staff/acater/comp4031.htmlArtificial Intelligence for Games and Puzzles2 Move generation: the obvious way A natural way to represent the board is with an 8x8 array, containing values 1..12 representing black pawn,... white king indexed by  rank running from 0 to 7  file running also from 0 to 7 When a chessman move is being considered, the new rank and file must satisfy rank ≥ 0 and rank ≤ 7 and file ≥ 0 and file ≤ 7 A rook can move in a ray over several empty squares, along a file {rank := rank ± n} or along a rank {file := file ± n} A bishop can move in a ray over several empty squares, along a diagonal {rank := rank ± n; file := file ± n} A queen can do either the above. Knights, Kings and (usually) Pawns do not move in rays.

3 http://csiweb.ucd.ie/Staff/acater/comp4031.htmlArtificial Intelligence for Games and Puzzles3 Move generation with vector-based board - 1/3 It is faster to access a 1-dimensional vector than a 2-dimensional array. 8x8 board may be linearised so that 64 elements of a vector [0…63] correspond to squares a1, b1, c1, d1, e1, f1, g1, h1, a2, b2, … g8, h8 Then a rook moves from square X to X - n (stopping after a multiple of 8) or to X + n (stopping short of multiple of 8) or to X ± 8n (stopping before going negative or going beyond 63) A bishop moves from square X to X ± 9n to X ± 7n A knight moves from square X to X±10, X±17, X±15, X±6 5657585960616263 4849505152535455 4041424344454647 3233343536373839 2425262728293031 1617181920212223 89101112131415 01234567

4 http://csiweb.ucd.ie/Staff/acater/comp4031.htmlArtificial Intelligence for Games and Puzzles4 Move generation with vector-based board - 2/3 For all piece types, the legality of moves must be checked. Checking a proposed move does not go off the top or bottom of the board is easy, just check {X ≥ 0 and X ≤ 63} Checking a proposed move does not go over the left or right edge of the board is not as easy as that; effectively you must decompose X into its rank & file components.  Can do this fairly cheaply since divide-by-8 and modulo-8 can be done with a rightshift operation and a bitwise-and operation, respectively. The top/bottom check can be made cheaper by exploiting these facts:  all numbers 0-63 have a ‘0’ in bit position 7 (from least significant)  negative numbers, and also numbers 64-127, have a ‘1’ in bit position 7  so whenever {X & 64} is non-zero then X is not valid

5 http://csiweb.ucd.ie/Staff/acater/comp4031.htmlArtificial Intelligence for Games and Puzzles5 Move generation with vector-based board - 3/3 The board vector need not be exactly the right size. It can be bigger than needed. It is useful for it have 128 elements. (Actually only 120 is enough!) Chessboard squares can be mapped to vector elements as follows: This allows both rank and file parts of an index to be checked in one operation: indices with a ‘1’ in bit 8 are negative or greater than 127 +ve indices with a ‘1’ in bit 4 are in the range 8-15, 24-31, 48-55, …  they correspond to vector elements not mapped to any square. When (X & #x88) is nonzero X is invalid. This is the “Hex-88 Trick”. a1,b1,c1,d1,e1,f1,g1,h1,,,,,,,,,a2,b2,c2,d2,e2,f2,g2,h2, 1st rank8 unused elements2nd rank

6 http://csiweb.ucd.ie/Staff/acater/comp4031.htmlArtificial Intelligence for Games and Puzzles6 Bitboards Rather than use a vector containing codes identifying chessmen, many programs use a collection of several 64-bit vectors, using one bit per chessboard square. The technique was pioneered by “Kaissa”, a Russian program of the 1970s which ran on a computer with 64-bit words; as today’s “Alpha” chips. Each “bitboard” uses ‘1’ bits to represent different things about a square. Making a move then involves changing at least three of these bitboards. This square is occupied. This square is occupied by a bishop. This square is occupied by white. This square is occupied by a rook. This square is occupied by black. This square is occupied by a queen. This square is occupied by a pawn. This square is occupied by a king. This square is occupied by a knight.

7 http://csiweb.ucd.ie/Staff/acater/comp4031.htmlArtificial Intelligence for Games and Puzzles7 “Ordinary” moves of knight and king These pieces move fixed distances in any of 8 directions. They capture any enemy piece occupying their target square. There is no need to consider any squares other than source and target. For each type of piece,  and for each source bit position, –up to eight bitmaps can be constructed and stored –to represent the up-to-8 possible targets for that piece type starting from that source square If say the “d5” bit is set in the “white” and “knight” bitboards, then consider the eight d5-knight bitmaps in turn check that {WhiteBitboard & currentbitmap} is zero, if so  zero the “d5” bit in the occupied, white, & knight bitboards  zero target bit in the black, pawn, bishop, rook, queen, & king bitboards  set to one the target bit in the occupied, white, & knight bitboards

8 http://csiweb.ucd.ie/Staff/acater/comp4031.htmlArtificial Intelligence for Games and Puzzles8 Rook moves Rooks (and Queens) can move in rays, along ranks or along files. all squares they pass over must be unoccupied the final square may be unoccupied or occupied by an enemy piece For each square, a bitmap can specify the other squares a rook (or queen) could reach if unimpeded. To see if a rook (queen) could actually reach a particular square involves checking the occupied bitboard against a substring of bits. (If a bitwise AND returns nonzero, the square cannot be reached) It is better then to use four separate bitmaps - attack bitboards - for the four different directions of movement.

9 http://csiweb.ucd.ie/Staff/acater/comp4031.htmlArtificial Intelligence for Games and Puzzles9 Flipped bitboards Checking substrings of bits involves forming substrings of bits. This is easily done by bit-shifting when the bits in a bitstring are consecutive in memory. For the bitboard organisation shown before, that makes it easy to manipulate attack bitboards for attacks along a rank, but not along a file. By using additional bitboards, flipped on their sides, this too is made easy. a8b8c8d8e8f8g8h8 a7b7c7d7e7f7g7h7 a6b6c6d6e6f6g6h6 a5b5c5d5e5f5g5h5 a4b4c4d4e4f4g4h4 a3b3c3d3e3f3g3h3 a2b2c2d2e2f2g2h2 a1b1c1d1e1f1g1h1 h2h3h4h5h6h7h8 g1g2g3g4g5g6g7g8 f1f2f3f4f5f6f7f8 e1e2e3e4e5e6e7e8 d1d2d3d4d5d6d7d8 c1c2c3c4c5c6c7c8 b1b2b3b4b5b6b7b8 a1a2a3a4a5a6a7a8

10 http://csiweb.ucd.ie/Staff/acater/comp4031.htmlArtificial Intelligence for Games and Puzzles10 Bishop moves Bishops (and Queens) can move in rays, along diagonals. Again, all squares they pass over must be unoccupied the final square may be unoccupied or occupied by an enemy piece For each square, a bitmap can specify the other squares a bishop (or queen) could reach if unimpeded. As before, in order to generate substrings of bits easily it would be necessary to transform the bitboards. This (complex) transformation has the name “rotated bitboard”. The term “rotated bitboard” has come to apply to flipped bitboards as well as the two transformations to follow:

11 http://csiweb.ucd.ie/Staff/acater/comp4031.htmlArtificial Intelligence for Games and Puzzles11 Rotating a bitboard for a1…h8 1. Cut bitboard along a diagonal a8b8c8d8e8f8g8h8 a7b7c7d7e7f7g7h7 a6b6c6d6e6f6g6h6 a5b5c5d5e5f5g5h5 a4b4c4d4e4f4g4h4 a3b3c3d3e3f3g3h3 a2b2c2d2e2f2g2h2 a1b1c1d1e1f1g1h1

12 http://csiweb.ucd.ie/Staff/acater/comp4031.htmlArtificial Intelligence for Games and Puzzles12 Rotating a bitboard for a1…h8 1. Cut bitboard along a diagonal 2. Rearrange halves to make parallelogram a8b8c8d8e8f8g8h8 a7b7c7d7e7f7g7h7 a6b6c6d6e6f6g6h6 a5b5c5d5e5f5g5h5 a4b4c4d4e4f4g4h4 a3b3c3d3e3f3g3h3 a2b2c2d2e2f2g2h2 a1b1c1d1e1f1g1h1 h7 g6h6 f5g5h5 e4f4g4h4 d3e3f3g3h3 c2d2e2f2g2h2 b1c1de1f1g1h1 a8b8c8d8e8f8g8h8 a7b7c7d7e7f7g7 a6b6c6d6e6f6 a5b5c5d5e5 a4b4c4d4 a3b3c3 a2b2 a1

13 http://csiweb.ucd.ie/Staff/acater/comp4031.htmlArtificial Intelligence for Games and Puzzles13 Rotating a bitboard for a1…h8 1. Cut bitboard along a diagonal 2. Rearrange halves to make parallelogram 3. Squash to make new square a8b8c8d8e8f8g8h8 a7b7c7d7e7f7g7h7 a6b6c6d6e6f6g6h6 a5b5c5d5e5f5g5h5 a4b4c4d4e4f4g4h4 a3b3c3d3e3f3g3h3 a2b2c2d2e2f2g2h2 a1b1c1d1e1f1g1h1 h7 g6h6 f5g5h5 e4f4g4h4 d3e3f3g3h3 c2d2e2f2g2h2 b1c1d1e1f1g1h1 a8b8c8d8e8f8g8h8 a7b7c7d7e7f7g7 a6b6c6d6e6f6 a5b5c5d5e5 a4b4c4d4 a3b3c3 a2b2 a1 a8b1c2d3e4f5g6h7 a7b8c1d2e3f4g5h6 a6b7c8d1e2f3g4h5 a5b6c7d8e1f2g3h4 a4b5c6d7e8f1g2h3 a3b4c5d6e7f8g1h2 a2b3c4d5e6f7g8h1 a1b2c3d4e5f6g7h8

14 http://csiweb.ucd.ie/Staff/acater/comp4031.htmlArtificial Intelligence for Games and Puzzles14 Rotating a bitboard for a8…h1 1. Cut bitboard along a diagonal 2. Rearrange halves to make parallelogram 3. Squash to make new square a8b8c8d8e8f8g8h8 a7b7c7d7e7f7g7h7 a6b6c6d6e6f6g6h6 a5b5c5d5e5f5g5h5 a4b4c4d4e4f4g4h4 a3b3c3d3e3f3g3h3 a2b2c2d2e2f2g2h2 a1b1c1d1e1f1g1h1 a7 a6b6 a5b5c5 a4b4c4d4 a3b3c3d3e3 a2b2c2d2e2f2 a1b1c1d1e1f1g1 a8b8c8d8e8f8g8h8 b7c7d7e7f7g7h7 c6d6e6f6g6h6 d5e5e6g5h5 e4f4g4h4 f3g3h3 g2h2 h1 a7b6c5d4e3f2g1h8 a6b5c4d3e2f1g8h7 a5b4c3d2e1f8g7h6 a4b3c2d1e8f7g6h5 a3b2c1d8e7f6g5h4 a2b1c8d7e6f5g4h3 a1b8c7d6e5f4g3h2 a8b7c6d5e4f3g2h1

15 http://csiweb.ucd.ie/Staff/acater/comp4031.htmlArtificial Intelligence for Games and Puzzles15 Using rotated bitboards Programs that use rotated bitboards do so for the sake of efficiency of the logic for move generation and attack detection. They use many precomputed bitboards, using memory to gain speed. Whenever a move is made, a few more bitboards must be kept up to date: Regular file-oriented bitboards, for occupied, white, black, pawn etc.  often separating further: blackpawn, whitepawn, blackknight, … Flipped, rank-oriented versions of all of those. Rotated a1-h8 versions, and Rotated a8-h1 versions With true 64-bit machine words, the bitwise operations are very fast. With 32-bit words, even if a language allows 64-bit integers, benefits are less.

16 http://csiweb.ucd.ie/Staff/acater/comp4031.htmlArtificial Intelligence for Games and Puzzles16 Some online references supertech.lcs.mit.edu/~heinz/dt/node8.html (beware, rank & file confused in 1st paragraph) www.onjava.com/pub/a/onjava/2005/02/02/bitsets.html?page=last&x-showcontent=text atlanchess.com/html/rotated_bitboards.html www.fzibi.com/cchess/bitboards.htm


Download ppt "Intelligence for Games and Puzzles1 Some Chess-specific Techniques Chess is played on an 8x8."

Similar presentations


Ads by Google