C SC 483 Chess and AI: Computation and Cognition Lecture 3 September th
Programming Project A series of tasks There are lots of resources and open source code available for chess Please don t simply copy and paste Learn by writing your own code!
Task Set up graphical representation of chess board allow you to set up chess positions allow you to move pieces recommend using the Tk toolkit for this project many variants: (see wiki Tk page)
Chess Pieces for the class, let s all use these pieces taken from Apple s Symbol font chess pieces are unicode characters 2654 to 265F
Chess Pieces download from course webpage bmp.zip 64 x 64 pixel bitmaps
Chessboard Example: done in tcl/tk about one page of code
Task 2 Compute possible legal moves and attacks
Underlying Representation Behind the scenes: use a bitboard to represent where pieces are reasons: fast bit operations easy to answer many questions fairly directly
64 bits integers: typically 32 bits (4 bytes) use 64 bit integers: C s (unsigned) long long, uint64_t Example: initial position: black pawns xff (hex) one hex digit use any reasonable mapping you like: of course, as long as you are consistent
you could use column major or row major order, count up from the bottom vs. from the top (or left vs. right) my chosen representation: lsb = least significant bit msb = most significant bit bitboard = msb byte byte 8 lsb byte8 byte7 byte6 byte5 byte4 byte3 byte2 byte
Encoding a step move (by black pawns): x824c3 > x824c3 left shift by 8 positions (= row) i.e. x824c3 << 8
Integer representation signed (2 s complement) 32 bit version - = xffffffff -247483648 = x8 247483647 = x7fffffff = x = x this usually means we need to be careful with right shifts because of sign bit preservation example: x8 >> = xc
How many bitboards? one for each type of piece K, Q, R, B, N, P for each side = 2 think of them as layers or overlays in Photoshop (I m going to use lowercase for white and uppercase for black in this presentation) Initially: k x8 K x8 q x Q x r x8 R x8 b x24 B x24 n x42 N x42 p xff P xff
http://www.onjava.com/pub/a/onjava/25/2/2/bitsets.html?page=2 another view
white = k q r b n p (always) white = xffff (initially) black = K Q R B N P (always) black = xffff (initially) empty = ~ ( white black ) (always) empty = ~ xffffffff (initially) empty = xffffffff Initially: k x8 q x r x8 b x24 n x42 p xff K x8 Q x R x8 B x24 N x42 P xff
(Legal) pawn moves forward one rank (if not blocked) forward two ranks if not moved (and not blocked) capture diagonally en passant need to check also if king is under attack after the move Bitboard implementation: forward one rank (if not blocked) we can operate on all the pawns at a time (bitparallel) P << 8 need to mask out occupied squares P << 8 & empty similar considerations for p
Bitboard implementation: forward two ranks if not moved (and not blocked) define rank7 = xff then ((P & rank7) << 8 & empty) << 8 & empty similar considerations for p rank2 = xff Bitboard implementation: capture diagonally define filea = x88888888 fileh = x for black attacks down and to the right (P & ~fileh) << 7 Attacks down and to the left (P & ~filea) << 9 similar considerations for p
Bitboard implementation: capture diagonally define filea = x88888888 fileh = x for black attacks down and to the right (P & ~fileh) << 7 Why ~fileh?
Bitboard implementation: capture diagonally define filea = x88888888 fileh = x for black attacks down and to the right (P & ~fileh) << 7 Why ~fileh? fileh
Bitboard implementation: en passant only get one chance, so need to know previous move suppose black moved pawn two squares last move, let P* be position then, for white p = p & rank5 P = P* << & p P2 = P* >> & p P or P2 non-zero P* >> 8 & empty similar considerations for black
King moves one step in any direction to an empty square or opponent s square that is not attacked (excluding castling) messy to compute bit operations at run time and mask off relevant edges could pre-compute possible next moves and use an associative array (hash table) for lookup bit operations: >>7 << <<9 >>8 >>9 >> <<8 <<7 k_moves(k) & ( black empty )
King moves one step in any direction to an empty square or opponent s square that is not attacked (excluding castling) >>7 << <<9 >>8 >>9 >> <<8 <<7 Logic: let bb be the bitboard associated with a king (black or white) squares are: (bb & ~ rank8) >> 8 (bb & ~ rank) << 8 (bb & ~ fileh) >> (bb & ~ filea) << (bb & ~ ( rank8 filea)) >> 7 (bb & ~ ( rank8 fileh)) >> 9 (bb & ~ (rank fileh )) << 7 (bb & ~ (rank filea )) << 9
Logic: let bb be the bitboard associated with a king (black or white) squares are: (bb & ~ rank8) >> 8 (bb & ~ rank) << 8 (bb & ~ fileh) >> (bb & ~ filea) << (bb & ~ ( rank8 filea)) >> 7 (bb & ~ ( rank8 fileh)) >> 9 (bb & ~ (rank fileh )) << 7 (bb & ~ (rank filea )) << 9 sign extension error: x8
Knight moves L-shaped pattern can hop over pieces let bb be the bitboard associated with knights (bb & ~ (rank filegh)) << 6 (bb & ~ (rank8 filegh)) >> (bb & ~ (rank fileab)) << (bb & ~ (rank8 fileab)) >> 6 (bb & ~ (rank2 fileh)) << 5 (bb & ~ (rank2 filea)) << 7 (bb & ~ (rank78 fileh)) >> 7 (bb & ~ (rank78 filea)) >> 5 bit operations: >>5 >>7 >>6 >> << <<6 <<7 <<5 where, e.g. filegh = fileg fileh
Sliding pieces e.g. Rook may move along file or rank extent: as far unoccupied capture: opposing piece on first nonempty square on file or rank Base Case along a rank per rook position per bit pattern of occupation pre-compute possible squares rook could move to
Base Case along a rank per rook position per bit pattern of occupation pre-compute possible squares rook could move to Example: occupation on rank8: white & black & rank8 = 2 8 = 256 possible bit patterns possible attack squares can mask out white (later) to eliminate attacking own piece, as in:
Rank pattern and piece lookup (,32) -> (,6) -> (,) -> (,) -> (,2) -> (,32) -> (,6) -> (,64) -> (,) -> (,8) -> (,4) -> (,64) -> (,64) -> (,64) -> (,4) -> (,32) -> (,6) -> (,28) -> (,) -> (,2) -> (,32) -> (,2) -> (,4) -> (,64) -> (,28) -> (,) -> (,8) -> (,8) -> (,4) -> (,28) -> (,64) -> (,) -> (,28) -> (,32) -> (,6) ->
Example: board: on rank 7 white rook: x (decimal 6) occupied: (decimal 29) look up through the pair rook x and pattern 29 the pre-computed attack squares: (decimal or x6f) bitboard: x6f = x6f << 8
Example: on rank 7 white rook: x (decimal 6) occupied: (decimal 29) look up through the pair rook x and pattern 29 the pre-computed attack squares: (decimal or x6f) bitboard: x6e = (x6f << 8) & ~ white i.e mask off white pieces board:
Note: logic given so far doesn t handle two rooks on the same rank Example on rank 7 white rooks: x (decimal 7) occupied: (decimal 29) lookup must be made for both pairs rook x and pattern 29 rook x and pattern 29 disjunctively, and then mask off white pieces board:
Similarly, lookup must be done disjunctively for rooks on different ranks rook x and pattern 29 rook x8 and pattern 8 board:
Sliding pieces e.g. Rook may move along file or rank extent: as far unoccupied capture: opposing piece on first nonempty square on file or rank Base Case along a rank Non-Base Case along a file problem: bits are not contiguous anymore solution: add a 9 degree rotated board representation as well
rank order (current representation) msb byte file order (can convert: but best way is to maintain both representations) lsb byte byte 8
Idea: now bits along files can be reduced to the rank lookup case http://www.cis.uab.edu/hyatt/bitmaps.html A8 B8 C8 D8 E8 F8 G8 H8 A7 B7 C7 D7 E7 F7 G7 H7 A6 B6 C6 D6 E6 F6 G6 H6 A5 B5 C5 D5 E5 F5 G5 H5 A4 B4 C4 D4 E4 F4 G4 H4 A3 B3 C3 D3 E3 F3 G3 H3 A2 B2 C2 D2 E2 F2 G2 H2 A B C D E F G H H8 H7 H6 H5 H4 H3 H2 H G8 G7 G6 G5 G4 G3 G2 G F8 F7 F6 F5 F4 F3 F2 F E8 E7 E6 E5 E4 E3 E2 E D8 D7 D6 D5 D4 D3 D2 D C8 C7 C6 C5 C4 C3 C2 C B8 B7 B6 B5 B4 B3 B2 B A8 A7 A6 A5 A4 A3 A2 A
to be continued next time...
Summary Task construct layout/setup graphical display Task 2 compute attacks/moves captures for all pieces Show me your code and working program: in class in two weeks
CHESS HOMEWORK LECTURE #3
PUZZLE # Write down 3 possible ways white can attack the F7 pawn from this position. (2 Pts) Write down good moves white can make in this position to fight for the Central Squares (e4,d4,e5,d5). (3 Pts).
PUZZLE #2. How many legal captures does White have in this position and what are they? (5 Pts) 2. How many legal captures does Black have in this position and what are they? (5 Pts)
RESEARCH PAPER(5 Pts).Take of the words in the TERMINOLOGY section under the REQUIRED READING. 2. It should be a topic (word) that you find interesting for your own chess development. 3. Write a -2 page research paper on that subject, using actual examples from articles, games, chessbase, on-line sites or any other sources. All the sources must be cited.
BONUS (5Pts) http://en.wikipedia.org/wiki/world_chess_championship Choose your favorite World Chess Champion ( under the section of Undisputed World Champions 886 993 on that website ) Write a short summary about his chess playing style, strengths and contribution to the chess world. Must be typed. Examples not necessary but welcome.
REQUIRED READING: Be familiar with the following terminology From G-P ( in Alphabetical order) ( Starting at GAMBIT and finishing at PUSH ) http://en.wikipedia.org/wiki/patzer
Quiz Time