# Computation and Cognition

IA et Robotique

23 févr. 2014 (il y a 4 années et 4 mois)

89 vue(s)

C SC 483
Chess and AI:
Computation
and Cognition
Lecture

3
September 10th
Programming Project

There are lots of resources and open
source code available for chess

t simply copy and paste

Learn by writing your own code!

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

course webpage

bmp.zip

64 x

64 pixel
bitmaps
Chessboard

Example
:

done in
tcl/tk

page of code

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
Bitboard
Representation

64 bits

integers: typically 32 bits (4 bytes)

use 64 bit integers: C

s (unsigned) long long, uint64_t

Example
:

initial position: black pawns

0x000000000000FF00 (hex)
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
0
0
0
0
0
0
0
0
one hex digit
use any reasonable mapping
you like: of course, as

long as
you are consistent
Bitboard
Representation
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
=
lsb
msb
byte

1
byte

8
byte1
byte2
byte3
byte4
byte5
byte6
byte7
byte8
Bitboard
Representation

Encoding a

1 step move (
by black pawns
):

0x000000000824C300

> 0x0000000824C30000

left shift by 8 positions (=1 row)

i.e.
0x000000000824C300 << 8
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
1
0
0
1
0
0
1
1
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
1
0
0
1
0
0
1
1
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Bitboard
Representation

Integer representation

signed (2

s complement)

32 bit version
-1

= 0xFFFFFFFF
-2147483648

= 0x80000000
2147483647

= 0x7FFFFFFF
1

= 0x00000001
0

= 0x00000000

this usually means we
need to be careful
with right shifts

because of sign bit
preservation

example:

0x80000000 >> 1

=

0xC0000000
Bitboard
Representation

How many
bitboards
?

one for each type of
piece K, Q, R, B, N, P

for each side

= 12

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

0x0800000000000000

K

0x0000000000000008

q

0x1000000000000000

Q

0x0000000000000010

r

0x8100000000000000

R
0x0000000000000081

b

0x2400000000000000

B
0x0000000000000024

n

0x4200000000000000

N
0x0000000000000042

p
0x00FF000000000000

P
0x000000000000FF00
Bitboard
Representation

http://www.
onjava
.com/pub/a/onjava/2005/02/02/bitsets.html?page=2
another
view
Bitboard
Representation

white = k | q | r | b | n | p (
always
)

white = 0xFFFF000000000000
(
initially
)

black = K | Q | R | B | N | P (
always
)

black = 0x000000000000FFFF
(initially)

empty

= ~ (

white | black ) (
always
)

empty = ~ 0xFFFF00000000FFFF
(
initially
)

empty =

0x0000FFFFFFFF0000

Initially
:

k
0x0800000000000000

q
0x1000000000000000

r
0x8100000000000000

b
0x2400000000000000

n

0x4200000000000000

p
0x00FF000000000000

K
0x0000000000000008

Q
0x0000000000000010

R
0x0000000000000081

B
0x0000000000000024

N
0x0000000000000042

P
0x000000000000FF00
Bitboard
Representation

(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

occupied
squares
P << 8 & empty
similar considerations for p
Bitboard
Representation

Bitboard
implementation
:

forward two ranks if not
moved (
and not blocked
)

define

rank7 =
0x000000000000FF00

then
((P & rank7) << 8 & empty) <<
8 & empty

similar considerations for
p

rank2 =
0x00FF000000000000

Bitboard
implementation
:

capture diagonally

define

filea
=
0x8080808080808080

fileh
=
0x0101010101010101

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
Representation

Bitboard
implementation
:

capture diagonally

define

filea
=
0x8080808080808080

fileh
=
0x0101010101010101

for black

attacks down and to the
right
(P &
~fileh
) << 7

Why

~fileh
?
Bitboard
Representation

Bitboard
implementation
:

capture diagonally

define

filea
=
0x8080808080808080

fileh
=
0x0101010101010101

for black

attacks down and to the
right
(P &
~fileh
) << 7

Why

~fileh
?
fileh
Bitboard
Representation

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
p1 = p & rank5

0
P1 = P* << 1 & p1
P2 = P* >> 1 & p1
P1 or P2
non-zero
P* >> 8

& empty

0

similar considerations for black
Bitboard
Representation

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

could pre-compute possible
next moves

and use an associative
array (hash table)

for lookup

bit operations:
>>8
<<8
<<7
<<9
>>9
>>7
>>1
<<1
k_moves
(k) &

( black

| empty )
Bitboard
Representation

King moves

one step in any direction
to an empty square or
opponent

s square that is
not attacked
(excluding castling)

Logic:

let
bb
be the
bitboard
associated with a king
(black or white)

squares are:
(bb & ~ rank8) >> 8 | (bb &
~ rank1) << 8 | (bb & ~
fileh
)
>> 1 | (bb & ~
filea
) << 1 |
(bb & ~ ( rank8 |
filea
)) >> 7
| (bb & ~ ( rank8 |
fileh
)) >>
9 | (bb & ~ (rank1 |
fileh
))
<< 7 | (bb & ~ (rank1 |
filea
)) << 9
>>8
<<8
<<7
<<9
>>9
>>7
>>1
<<1
Bitboard
Representation

Logic:

let
bb
be the
bitboard
associated with a king (black
or white)

squares are:
(bb & ~ rank8) >> 8
| (bb &
~ rank1) << 8 | (bb & ~
fileh
)
>> 1 | (bb & ~
filea
) << 1 |
(bb & ~ ( rank8 |
filea
)) >> 7 |
(bb & ~ ( rank8 |
fileh
)) >> 9 |
(bb & ~ (rank1 |
fileh
)) << 7 |
(bb & ~ (rank1 |
filea
)) << 9

sign extension error:
0x8000000000000000
Bitboard
Representation

Knight moves

L-shaped pattern

can

hop over pieces

bit operations:
<<17
<<15
<<10
<<6
>>6
>>10
>>15
>>17
let
bb
be the
bitboard
associated with

knights
(bb & ~ (rank1 |
filegh
)) << 6 |

(bb & ~ (rank8 |
filegh
)) >> 10 |

(bb & ~ (rank1 |
fileab
)) << 10 |

(bb & ~ (rank8 |
fileab
)) >> 6 |

(bb & ~ (rank12 |
fileh
)) << 15 |

(bb & ~ (rank12 |
filea
)) << 17 |

(bb & ~ (rank78 |
fileh
)) >> 17 |

(bb & ~ (rank78 |
filea
)) >> 15
where, e.g.
filegh
=
fileg
|

fileh
Bitboard
Representation

Sliding pieces

e.g. Rook

may move along file
or rank

extent: as far
unoccupied

capture: opposing
piece on

first non-
empty 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
Bitboard
Representation

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 =

11001010
2
8
= 256 possible bit patterns
possible attack squares 01110110
can mask out white (later) to eliminate
attacking own piece, as in:
Bitboard
Representation

Rank pattern and piece lookup
(11110010,32) -> 01010000
(10011110,16) -> 11101000
(11000001,1) -> 01111110
(11001011,1) -> 00000010
(11001011,2) -> 00001101
(00111000,32) -> 11010000
(00110000,16) -> 00101111
(01101100,64) -> 10100000
(11110101,1) -> 00000110
(11001011,8) -> 01110110
(11110101,4) -> 00011011
(11111010,64) -> 10100000
(11111001,64) -> 10100000
(01000000,64) -> 10111111
(00100100,4) -> 00111011
(11101010,32) -> 01011000
(10010111,16) -> 11101100
(11100100,128) -> 01000000
(01111011,1) -> 00000010
(01111011,2) -> 00001101
(00110000,32) -> 11010000
(01001110,2) -> 00000101
(01001110,4) -> 00001010
(01100101,64) -> 10100000
(10111100,128) -> 01100000
(10100101,1) -> 00000110
(01111011,8) -> 00010110
(01001110,8) -> 01110100
(10100101,4) -> 00111011
(10011110,128) -> 01110000
(11110010,64) -> 10100000
(11011001,1) -> 00001110
(10000000,128) -> 01111111
(11100010,32) -> 01011110
(11011010,16) -> 01101000
Bitboard
Representation

Example
:

on

rank 7

white rook: 0x10 (decimal 16)

occupied:
11010001 (decimal
209)

look up

through the

pair rook
0x10 and pattern 209
the pre-computed attack
squares: 01101111 (decimal 111
or 0x6F)

bitboard
:

0x0000000000006F00
=

0x6F << 8

board:
Bitboard
Representation

Example
:

on

rank 7

white rook: 0x10 (decimal 16)

occupied:
11010001
(decimal 209)

look up

through the

pair rook
0x10 and pattern 209
the pre-computed attack
squares: 01101111 (decimal
111 or 0x6F)

bitboard
:

0x0000000000006E00
=

(0x6F << 8) & ~ white
i.e

board:
Bitboard
Representation

Note
:
logic given so far
doesn

t handle two

rooks

on
the same rank

Example

on

rank 7

white rooks: 0x11 (decimal
17)

occupied:
11010001
(decimal 209)

lookup must be made for both
pairs

rook 0x10 and pattern 209

rook 0x01 and pattern 209

off white pieces

board:
Bitboard
Representation

Similarly, lookup must
be done disjunctively for
rooks on different ranks

rook 0x10 and pattern
209

rook 0x08 and pattern

8

board:
Bitboard
Representation

Sliding pieces

e.g. Rook

may move along file
or rank

extent: as far
unoccupied

capture: opposing
piece on

first non-
empty square on file
or rank

Base Case

along a rank

Non-Base Case

along a file

problem: bits are not
contiguous anymore

degree rotated board
representation as
well
Bitboard
Representation

rank order

(current representation)

file order

(can convert: but best way is to
maintain both representations)
lsb
msb
byte

1
byte

8
byte

1
Bitboard
Representation

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 H8 H7 H6 H5 H4 H3 H2 H1
A7 B7 C7 D7 E7 F7 G7 H7 G8 G7 G6 G5 G4 G3 G2 G1
A6 B6 C6 D6 E6 F6 G6 H6 F8 F7 F6 F5 F4 F3 F2 F1
A5 B5 C5 D5 E5 F5 G5 H5 E8 E7 E6 E5 E4 E3 E2 E1
A4 B4 C4 D4 E4 F4 G4 H4 D8 D7 D6 D5 D4 D3 D
2 D1
A3 B3 C3 D3 E3 F3 G3 H3 C8 C7 C6 C5 C4 C3 C2 C1
A2 B2 C2 D2 E2 F2 G2 H2 B8 B7 B6 B5 B4 B3 B2 B1
A1 B1 C1 D1 E1 F1 G1 H1 A8 A7 A6 A5 A4 A3 A2 A1
Bitboard
Representation

to be continued next time...
Summary

construct
layout/setup
graphical display

compute

attacks/moves

captures

for all pieces
Show me your code and working program:

in class in two weeks
CHESS HOMEWORK
LECTURE #3
PUZZLE #1
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
1. 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(15 Pts)

1.Take 1 of the words in the

TERMINOLOGY

section under the

.

2. It should be a topic (word) that you find
interesting for your own chess development.

3. Write a 1-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

Champion ( under the section of
Undisputed World Champions
1886

1993
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.
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