CHESS PRACTICE            
Updated: March 2008  

- ANTOINE BRUNEAU'S BLOG -

Chess and Programming


HOW CHESS
COMPUTERS WORK



1o) Algorithms used in Chess Programming

The alpha-beta algorithm is the one mainly used by chess-playing programs. It is a pruning technique based on the minimax algorithm, applied to the game tree.
The game tree is the tree of all legal moves, containing all possible lines of play starting from the initial position. Each node represents a position and all branches starting from this node represent all possible moves from this position. It is constructed recursively, with a limited depth because of the limits of machines.

«Chess with Lasers -
Image copyright © Susan Polgar Chess Blog
Artists Rights Society (ARS), New York / ADAGP, Paris
(cf. Susan Polgar Chess Blog - It's Chess with lasers)

             'Chess with lasers' 
        Original Photograph puplished
        on the Susan Polgar Chess Blog



The minimax algorithm evaluates first the position in each leaf of the tree. The basic principle is to choose the best move when it's your turn to move and to suppose that your adversary will do likewise. So you will always choose the move that corresponds to the maximal evaluation and suppose that your adversary will choose the move that corresponds to the minimal evaluation (for you). Les us suppose it's White to move. The algorithm proceeds from the leaf to the root, it goes back up to the nodes above the leaves and associates the minimum of the values of the leaves that depends on it if the level of leaves corresponds to black moves, or the maximum if this level corresponds to white moves. It goes back up one step again and does the same thing with the tree with a depth decreased by one unit, where the nodes of the previous step serve as leaves at this step. So it alternates taking the minimum of node values, and then the maximum, etc... as far as the root.

«Normalized Cuts segmentation
results obtained on the Berkeley
Segmentation Dataset (Research Projects)

          Normalized Cuts segmentation 
        results obtained on the Berkeley   
        Segmentation Dataset (Research Projects)


The alpha-beta algorithm works like the minimax algorithm but refrains from evaluating some nodes when it is not necessary. It avoids continuing the analysis of a node if this node is less interesting than a node already analysed. For example: let us suppose that it's White to move and that the leaves of the tree correspond to White moves. So we must associate to each node the maximum of the values of the leaves that depend on it. If in the evaluation of one node from the leaves we find one leaf which value is superior to the value of a node already evaluated, then computing the minimum of the maxima will not require evaluating the remaining leaves because whatever the values of the remaining leaves may be, the value of the node will still be superior to the previous node. This principle is applied to the entire tree from the root.

2o) The Generator of Legal Moves

For creating the game tree we need to implement a function which generates all legal moves from a given position for the color which has to move. The chessboard will be represented by a table 8x8, each piece will be located by its coordinates in the table, and a move will be given by two couples of coordinates (for ex. e2-e4). Legal moves computed with this function are put in a list. This function is also used by the interface with the player: the player enters a move to play and the computer checks if the move is in the list for deciding if it is legal.

We browse the pieces and generate all possible moves of each piece according to its specific mode of move. An elementary move is represented by a vector, for example the vector (1,2) represents one of the 8 possible movements of the Knight. We must take into account the fact that some pieces have a long range (like Queen, Rook and Bishop) and others haven't: this means that their elementary moves can be continued in all authorized directions. Each kind of piece is defined with a table of directions (vectors) and with a variable which indicates if it has long range or not. Of course, the pawns need a particular treatment (the direction depends on the color, rule for first move, capture en passant, capture and move are in different ways, promotion).

«Computer Art - Image generated
by Sterling Fractal- Image not
copyrighted (Public Domain)

             Image generated by  
        Sterling Fractal (cf.Wikipedia   
           the free Encyclopedia


After having generated all possible moves by tanslating the mode of move of each piece, the work is not finished, because the rules specify that the King mustn't be in check after the move for the move to be legal. We need a function that enables us to know if a square is attacked by an opposite piece. This function browses the chessboard from this square in all directions and verifies if there is an opposite piece which can attack in this direction. This function is also used for checking if it is legally possible to castle.

A solution would be for each move generated to check if after playing it the King is in check and in this case to remove this move from the list. The problem of this approach is that the function that verifies if the King is in check is rather "expensive", and will be used a lot of times. A more economic method is to limit this verification to the case where the King is already in check (this case is relatively rare, and in this case this approach is almost obliged), and to prevent from moving a piece which would place the King in check. When the King is not already in check the only way to put him in check, except the fact of moving the King itself, is to move a pinned piece (and it may depend on directions). So it suffices to make precede the analysis by a function that starts from the position of the king and browses all directions to specify which pieces are pinned and in which directions. The great advantage of this approach is that this function is applied once for all.

Of course if the list is empty this means that the King is Stalemated or Mated.

© Antoine Bruneau - April 2008

[This page was conceived by
Antoine Bruneau - John E Hawkes
has contributed to the English
presentation of this article -
All data is copyrighted by: ©
Antoine Bruneau & Chess-Theory]


************

   «Images taken from the Thinking Machine 4 -
Thinking Machine 4 explores the invisible,
Play chess against a transparent intelligence,
its evolving thought process visible on the board
before you. - Image copyright © Thinking Machine 4
Artists Rights Society (ARS), New York / ADAGP, Paris»


***


FOR PLEASANT SURFING AROUND THIS SITE:

  • We suggest these best paths:

  •   «LASTEST UPDATES»
      «GENERAL SITE PLAN»
      «GENERAL SITE MENU»
      «LINK COLLECTION HOME PAGE»
      «ECO CODES BASE»
      «CHESS THEORY FORUMS»
      «SEARCH ENGINE»
      «VIRTUAL ART MUSEUM»


    ***


    FOR DISCOVERING WHO WE ARE:

  • ... and also expressing freely your opinion:

  •   «CHESS-THEORY: GUEST BOOK»
      «CHESS-THEORY: ABOUT US»    
      «CHESS-THEORY: COPYRIGHT»





               * DON'T NEGLECT TO CONTACT US HERE! *           
    This page, created by Michel Bruneau, is
    Copyright: © Michel Bruneau «Chess-Theory»
    Webmaster - All rights reserved 2004-2008


    Thank to  you  visitors  and friends!...
    Please notice  that  you  help us a  lot
    by giving your opinion and comments
    about  this  page as well as all  other
    pages that you have recently visited
    in  the   «Chess-Theory»   Website:


      Express yourself
on our Guest Book  

    My Dear  Friends!...  Without  at   least
    your  moral  support, formulated  here
    or   on  our  forums, this  site can close
    definitively without advance warning!
    ... Humour? Maybe, but it is also true
    that   a   Webmaster,  most of  the time
    alone facing his computers, experien-
    ces  sometimes,   in  his  projects  and
    realizations, discouragement and doubt
    ... HELP ME BY WRITING YOUR FEELINGS!


    The «Chess-Theory» Website receives
    more than
    90, 000 different visitors
    per month, coming  from  about
    145
    different   countries   in   the   world
    (daily statistics generated by awstats)






    * «CHESS-THEORY FOUNDATION» WEB SITES *
    The  «Chess-Theory  Foundation»,  currently
    still  unofficial,   under  the  responsibility   of
    Michel Bruneau, the «Chess-Theory»  Web-
    master,   puts   to  your  disposal  the   three
    following   complementary  Web   Addresses:


    * CHESS-THEORY.COM *

    ~ CHESS-THEORY.COM ~
    This is our main site, fully dedicated
    to Chess Theory, Chess Training, Chess
    Analysis and Chess Practice; but presenting
    also the first version of our Virtual Art
    Museum. This bilingual Site owns about 2 000
    English pages, 2 000 French pages, more than
    10 000 linked images, many hundreds of chess
    diagrams and more than 110 analyzed games
    presented with the "Chess-Theory" Viewer!...

                   ~ 'CHESS-THEORY.COM' ~
      This is our main site, fully dedicated 
      to Chess Theory, Chess Training, Chess 
      Analysis and Chess Practice; but presenting
      also the first version of our Virtual Art 
      Museum. This bilingual Site owns about 2 000 
      English pages, 2 000 French pages, more than  
      10 000 linked images, many hundreds of chess 
      diagrams and more than 110 analyzed games    
      presented with the 'Chess-Theory' Viewer!...


    * VIRTUAL-ART-MUSEUM.COM *

    ~ VIRTUAL-ART-MUSEUM.COM ~
    You will recover here, in a surprising
    new look design, all galleries and
    linked images of the "Chess-Theory"
    Virtual Art Museum ... but, rather soon,
    you will discover also many new beautiful
    galleries presenting a rich collection
    of unexpected Hight Definition Images,
    Royalty Free Photos and so more ....

             ~ 'VIRTUAL-ART-MUSEUM.COM' ~
      You will recover here, in a surprising
      new look design, all galleries and
      linked images of the 'Chess-Theory'
      Virtual Art Museum ... but, rather soon,
      you will discover also many new beautiful 
      galleries presenting a rich collection 
      of unexpected  Hight Definition Images, 
      Royalty Free Photos and so more ....


    * FROM-THE-WHOLE-WORLD.COM *

    ~ FROM-THE-WHOLE-WORLD.COM ~
    This Web Site, currently under
    construction, will deal with all
    cultural, intellectual or moral
    subject untreated by other ones

     ~ 'FROM-THE-WHOLE-WORLD.COM' ~
      This Web Site, currently under
      construction, will deal with all
      cultural, intellectual or moral
      subjects not treated by other ones




    «Michel  Bruneau  the  "Chess-Theory" Webmaster
    ...  presumably   when  he  was  a   little  younger
    and    still    full    of    Illusions   and     Dreams! »
    Photograph and Montage by Jean-Pierre Bruneau
    Copyright © 2008 Jean-Pierre Bruneau & "Chess-Theory"
    Nevertheless this image is available for Link Exchange!

         Michel Bruneau the 'Chess-Theory' Webmaster 
         ...presumably when he was a little younger   
                and still full of Illusions and Dreams!


    ******** ©-«Chess-Theory.com»-2004-2008 ********


                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
    Listen Music Now