473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

New Chess Module

I'm releasing a new python chess module called shatranj.
You can get it from www.employees.org/~stannous/shatranj
until I move the project to sourceforge or some other place.

It's a text based (bitboard) chess engine that implements
an alphabeta search with iterative deepening.
It also has a small opening book and stores most of
its information in dictionaries (hash tables). So for
a python module, it should be fast.

It currently has a very simple evaluation
function so don't expect strong play. My goal was to
implement a complete program using 64 bit numbers
(bitboards) as the main data structures...sacrificing
speed for code clarity. I'm hoping that non-programmers
interested in AI or advanced chess players will be able
to pick it up and add some intelligence to it. Being
written in Python, it's not blazingly fast...but Kasparov
doesn't even look at 2k nodes per second, does he? ;-)

Some things I could use some help with: interface for
xboard and winboard not to mention a better
evaluation function.

(comments and suggestions are welcome...
please email shatranjchess at gmail dot com)

Here's a small interactive session of how it works:

------------------
>>from shatranj import *
....reading startup data
....total time to read data 0.0774528980255
....found opening book shatranj-book.bin with 37848 positions
>>position = Position("r1bqk2r/pppp1ppp/2n5/5N2/2B1n3/8/PPP1QPPP/R1B1K2R")
all_pieces = position.piece_bb["b_occupied"] | position.piece_bb["w_occupied"]
other_pieces = position.piece_bb["b_occupied"]
from_square = c4
wtm = 1
mask = position.pinned(from_square,wtm)
ne_pieces = diag_mask_ne[from_square] & all_pieces
nw_pieces = diag_mask_nw[from_square] & all_pieces
moves = ((diag_attacks_ne[from_square][ne_pieces] & other_pieces) | \
.... (diag_attacks_ne[from_square][ne_pieces] & ~all_pieces)
| \
.... (diag_attacks_nw[from_square][nw_pieces] & other_pieces)
| \
.... (diag_attacks_nw[from_square][nw_pieces] & ~all_pieces))
& mask
>>>
moves
1275777090846720L
>>>
tobase(moves,2)
'1001000100001010000000000000101000000000000000000 00'
>>display(moves)
+---+---+---+---+---+---+---+---+
8 | | . | | . | | . | | . |
+---+---+---+---+---+---+---+---+
7 | . | | . | | . | 1 | . | |
+---+---+---+---+---+---+---+---+
6 | 1 | . | | . | 1 | . | | . |
+---+---+---+---+---+---+---+---+
5 | . | 1 | . | 1 | . | | . | |
+---+---+---+---+---+---+---+---+
4 | | . | | . | | . | | . |
+---+---+---+---+---+---+---+---+
3 | . | 1 | . | 1 | . | | . | |
+---+---+---+---+---+---+---+---+
2 | | . | | . | | . | | . |
+---+---+---+---+---+---+---+---+
1 | . | | . | | . | | . | |
+---+---+---+---+---+---+---+---+
a b c d e f g h
>>move_list = position.generate_moves(wtm)
moves,san_moves = position.get_move_list(move_list)
san_moves.values()
['Rg1', 'O-O', 'f3', 'a3', 'Rb1', 'f4', 'Ba6', 'Qe3', 'Bh6', 'Bd3',
'Qg4', 'Ng3', 'Ne7', 'Be6', 'Nxg7', 'Qxe4', 'Ne3', 'b4', 'b3', 'Be3',
'Bg5', 'g3', 'Kf1', 'Rf1', 'Nh6', 'a4', 'Nh4', 'Qh5', 'Kd1', 'h4',
'h3',
'c3', 'Bxf7', 'Nd6', 'Bb5', 'Nd4', 'Qf3', 'g4', 'Qf1', 'Bb3', 'Qd1',
'Qd3', 'Qd2', 'Bd5', 'Bd2', 'Bf4']
>>>
# now play a game!
play()
Shatranj version 1.0
g: switch sides m: show legal moves
n: new game l: list game record
d: display board b: show book moves
sd: change search depth (2-16) default=5
q: quit

Shatranj: d

+---+---+---+---+---+---+---+---+
8 | r | n | b | q | k | b | n | r |
+---+---+---+---+---+---+---+---+
7 | p | p | p | p | p | p | p | p |
+---+---+---+---+---+---+---+---+
6 | | . | | . | | . | | . |
+---+---+---+---+---+---+---+---+
5 | . | | . | | . | | . | |
+---+---+---+---+---+---+---+---+
4 | | . | | . | | . | | . |
+---+---+---+---+---+---+---+---+
3 | . | | . | | . | | . | |
+---+---+---+---+---+---+---+---+
2 | P | P | P | P | P | P | P | P |
+---+---+---+---+---+---+---+---+
1 | R | N | B | Q | K | B | N | R |
+---+---+---+---+---+---+---+---+
a b c d e f g h

Shatranj:
-----------------------------------

Enjoy,
Sam

Mar 15 '07 #1
2 2008
sh***********@gmail.com writes:
written in Python, it's not blazingly fast...but Kasparov
doesn't even look at 2k nodes per second, does he? ;-)
Wow, cool. Out of curiosity how many nodes per second does it look
at?
Mar 15 '07 #2
On Mar 15, 4:46 pm, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
shatranjch...@gmail.com writes:
written in Python, it's not blazingly fast...but Kasparov
doesn't even look at 2k nodes per second, does he? ;-)

Wow, cool. Out of curiosity how many nodes per second does it look
at?
depends on your processor speed...on a recent machine (~2.3GHz)
I can get about 2k nps without the psyco boost...but my goal was not
to beat the compiled programs with fast code. I'd like to develop a
strong program with good evaluation and move ordering. And perhaps
try search methods other than alphabeta. Using bitboards should
make the first part easier and using python should help with the
second
part.

--Sam

Mar 15 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Will McGugan | last post by:
Hi folks, I've written a Python chess module that does the following. * Reads / Writes PGN files * Write FEN files * Validates moves * Lists legal moves * Detects check / mate / stalemate /...
5
by: derian | last post by:
Do you guys know if there is a program that can generate chess moves? I recently wrote a chess game but its only 2 player... I deceided it was too difficult to come up with the logic for the AI...
11
by: Gregc. | last post by:
G'day I am writing a chess program. Here is my code: #include <stdio.h> #include <stdlib.h> bool isInCheck (int krow, int kcol, int qrow, int qcol) { double check;
1
by: Will McGugan | last post by:
Hi folks, I have just blogged about a Python chess module of mine that I wrote a while back. I plan on using it for a commerical project, but making the module open source. So I would be...
5
by: Paolo Pantaleo | last post by:
Well Python is not a good language for writing a chess engine (even if a chess engine exists: http://www.kolumbus.fi/jyrki.alakuijala/pychess.html), but it could be grat for chess interfaces, for...
1
by: Varun Hiremath | last post by:
Hello, I have written a chess client using python which is a graphic interface to play chess. It is at present a two player version, players move their peices by clicking two squares on the board...
63
by: biyubi | last post by:
Hi, a year ago I won the 2005 Best Game categoryof the International Obfuscated C Code Contestwith a chess program. http://www.ioccc.org/whowon2005.html...
2
by: CoreyWhite | last post by:
When playing games, perhaps the most simple is tic-tac-toe. The game has two simple strategies, one is defensive and the other offensive. It is not hard at first to learn how to tie games when...
10
by: sam_cit | last post by:
Hi Everyone, I'm working on developing a chess game and i decided to use c++ for its object oriented approach. I have a bass class unit and is inherited to distinct number of units (like king,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.