C SC 483 Chess and AI: Computation and Cognition. Lecture 3 September 10th

Similar documents
C SC 483 Chess and AI: Computation and Cognition. Lecture 5 September 24th

If a pawn is still on its original square, it can move two squares or one square ahead. Pawn Movement

Movement of the pieces

YourTurnMyTurn.com: chess rules. Jan Willem Schoonhoven Copyright 2018 YourTurnMyTurn.com

C SC 483 Chess and AI: Computation and Cognition. Lecture 2 August 27th

Chess Handbook: Course One

USING BITBOARDS FOR MOVE GENERATION IN SHOGI

ChesServe Test Plan. ChesServe CS 451 Allan Caffee Charles Conroy Kyle Golrick Christopher Gore David Kerkeslager

a b c d e f g h i j k l m n

NSCL LUDI CHESS RULES

Perry High School. 2 nd Semester!

arxiv: v1 [cs.ds] 28 Apr 2007

After learning the Rules, What should beginners learn next?

LEARN TO PLAY CHESS CONTENTS 1 INTRODUCTION. Terry Marris December 2004

AVOIDING ROTATED BITBOARDS WITH DIRECT LOOKUP

Unit. The double attack. Types of double attack. With which pieces? Notes and observations

The game of Paco Ŝako

Google DeepMind s AlphaGo vs. world Go champion Lee Sedol

Monday, February 2, Is assigned today. Answers due by noon on Monday, February 9, 2015.

arxiv: v2 [cs.ai] 15 Jul 2016

Welcome to the Brain Games Chess Help File.

Algebraic Chess Notation

Chess, a mathematical definition

The Basic Rules of Chess

Essential Chess Basics (Updated Version) provided by Chessolutions.com

A1 Problem Statement Unit Pricing

CHESS SOLUTION PREP GUIDE.

Chess Rules- The Ultimate Guide for Beginners

3. Bishops b. The main objective of this lesson is to teach the rules of movement for the bishops.

Chess for Kids and Parents

EE292: Fundamentals of ECE

Microchess 2.0 gives you a unique and exciting way to use your Apple II to enjoy the intellectually stimulating game of chess. The complete program lo

THROUGH THE LOOKING GLASS CHESS

Official Problem Set 2017 ACM/ICPC. The 2017 ACM-ICPC Asia Kabul Regional Contest

Senior Math Circles February 10, 2010 Game Theory II

ENGR170 Assignment Problem Solving with Recursion Dr Michael M. Marefat

John Griffin Chess Club Rules and Etiquette

The Chess Set. The Chessboard

Chess Puzzle Mate in N-Moves Solver with Branch and Bound Algorithm

12 Special Moves - Stalemate, Pawn Promotion, Castling, En Passant capture

Homework Assignment #1

OCTAGON 5 IN 1 GAME SET

Boulder Chess. [0] Object of Game A. The Object of the Game is to fill the opposing Royal Chambers with Boulders. [1] The Board and the Pieces

Its topic is Chess for four players. The board for the version I will be discussing first

GICAA State Chess Tournament

ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #7. Project #1: Checkers, Due: Feb. 19th, 11:59p.m.

CS2212 PROGRAMMING CHALLENGE II EVALUATION FUNCTIONS N. H. N. D. DE SILVA

In the game of Chess a queen can move any number of spaces in any linear direction: horizontally, vertically, or along a diagonal.

District Fourteen Chess Fest 2012 Information Sheet

Homework 9: Software Design Considerations

The Game. Getting Sarted

Chapter 1: Positional Play

Your first step towards nobility

Triple Challenge.txt

Wednesday, February 1, 2017

EE 109 Midterm Review

Welcome & Introduction

Part IV Caro Kann Exchange Variation


DELUXE 3 IN 1 GAME SET

Rotated bitboards in FUSc#

2. Review of Pawns p

Chess Evolution 3. Artur Yusupov

Contents. Introduction 5 How to Study this Book 5

MOVE GENERATION WITH PERFECT HASH FUNCTIONS

Fun and Games on a Chess Board

Suggested by Joshua L. Mask. Written by Ken Mask, MD. Illustrated by Simmie Williams

Lines of Action - Wikipedia, the free encyclopedia

Software Requirements Specification

Overview... 3 Starting the Software... 3 Adding Your Profile... 3 Updating your Profile... 4

Draw Steffen Slumstrup Nielsen Lev Lepkyi st prize (Award published in June 2018:

Foundations of AI. 5. Board Games. Search Strategies for Games, Games with Chance, State of the Art. Wolfram Burgard and Luc De Raedt SA-1

Types of center. Unit 2. The center. Types of center

Computer Graphics: Graphics Output Primitives Primitives Attributes

Cover and Interior design Olena S. Sullivan Interior format and copyediting Luise Lee

Alpha Hex is a game of tactical card placement and capture. The player who owns the most cards when the board is full wins.

Know your energy. Modbus Register Map EB etactica Power Bar

All games have an opening. Most games have a middle game. Some games have an ending.

THE EFFECTIVENESS OF DAMATH IN ENHANCING THE LEARNING PROCESS OF FOUR FUNDAMENTAL OPERATIONS ON WHOLE NUMBERS

Chess and Python revisited

RGB COLORS. Connecting with Computer Science cs.ubc.ca/~hoos/cpsc101


CPSC 217 Assignment 3 Due Date: Friday March 30, 2018 at 11:59pm

Reality Chess. Yellow. White

NOVAG AGATE INSTRUCTION

Contents. Explanation of symbols Cast of Characters Introduction Chapter 1 Values of the Pieces The Quick Count...

UKPA Presents. March 12 13, 2011 INSTRUCTION BOOKLET.

Logic Masters India Presents

CMPUT 657: Heuristic Search

Fun and Games on a Chess Board II

Problem A. Ancient Keyboard

UNIT 13A AI: Games & Search Strategies. Announcements

CSE 573 Problem Set 1. Answers on 10/17/08

The 2013 British Informatics Olympiad

Adversarial Search. CMPSCI 383 September 29, 2011

Evaluation. General Differences

Lecture 2: Sum rule, partition method, difference method, bijection method, product rules

Dan Heisman. Is Your Move Safe? Boston

Lecture 6: Latin Squares and the n-queens Problem

A CLASSIFICATION OF QUADRATIC ROOK POLYNOMIALS

Transcription:

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