Python: Sun­fish chess en­gine

Jonni Bid­well analy­ses the in­nards of a small but per­fectly formed chess en­gine that bests him with alarm­ing reg­u­lar­ity.

Linux Format - - PYTHON -

Leg­end tells of one Sissa ibn Dahir who in­vented the game of Chess for an In­dian king. So im­pressed was that king that he of­fered Sissa any­thing he de­sired as a re­ward. Be­ing of a cal­cu­lat­ing bent, Sissa replied “Then I wish that one grain of wheat shall be put on the first square of the chess­board, two on the sec­ond, and that the num­ber of grains shall be dou­bled un­til the last square is reached: what­ever the quan­tity this might be, I de­sire to re­ceive it”. The king soon re­alised that there was not enough wheat in the world to ful­fil this de­mand, and once again was im­pressed. There are var­i­ous end­ings to this story, in one Sissa is given a po­si­tion within the king’s court, in an­other he is ex­e­cuted for be­ing a smart arse. Hope­fully this tu­to­rial’s chess treat­ment will fea­ture nei­ther ex­e­cu­tion nor LXF tow­ers be­ing buried in moun­tains of wheat.

Chess is a com­pli­cated game – all the pieces move dif­fer­ently de­pend­ing on their cir­cum­stances, there are var­i­ous ex­tra­or­di­nary moves (eg en passent pawn cap­ture, castling) and pawns get pro­moted if they make it all the way to the other side. As a re­sult, a sur­feit of pit­falls present them­selves to the chess-pro­gram­ming dilet­tante, so rather than spend­ing a whole tu­to­rial fall­ing into traps we’re go­ing to bor­row the code from Thomas Ahle’s Sun­fish – a com­plete chess en­gine pro­grammed in Python. There’s no short­age of chess en­gines: from the clas­sic GNU Chess to the Kas­parovbeat­ing Deep Blue (1997) to the pack-lead­ing Stock­fish. Chess en­gines on their own gen­er­ally do not come with their own GUI, their code be­ing mostly de­voted to the not in­con­sid­er­able prob­lem of find­ing the best move for a given po­si­tion. Some (in­clud­ing Sun­fish) al­low you to play via a text con­sole, but most will talk to an ex­ter­nal GUI, such as xboard, via a pro­to­col such as the Univer­sal Chess In­ter­face (UCI) or WinBoard. Be­sides pro­vid­ing nice pic­tures of the ac­tion, this en­ables us to play against dif­fer­ent en­gines from a sin­gle pro­gram. Fur­ther­more, we can pit en­gine against en­gine and enjoy chess as a spec­ta­tor sport.

The Sun­fish en­gine

We’ll as­sume that you know how to play chess, but if you don’t you can prac­tice by play­ing against Thomas’s Sun­fish en­gine. You’ll find the code on the LXFDVD in the Tu­to­ri­als/ Chess direc­tory. Copy this direc­tory to your home folder, and then run it with: $ cd ~/Chess $ python sun­

The pro­gram uses Uni­code glyphs to dis­play the pieces in the ter­mi­nal, making it look a lit­tle more chess-like than GNU Chess. Check the box ( seeIn­stallingXb­oun­dandIn­ter­fac­ing with­Sun­fish) to see how to enjoy graph­i­cal play. Moves are in­putted by spec­i­fy­ing the start­ing and end­ing co­or­di­nates, so the ag­gres­sive open­ing which moves the king’s pawn to e4 would be in­putted e2e4. Note that this is slightly longer than the more com­mon al­ge­braic no­ta­tion (in which the pre­vi­ous move would be writ­ten e4 ), but makes it much eas­ier for

Newspapers in English

Newspapers from Australia

© PressReader. All rights reserved.