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...sa crificing
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:
------------------
....reading startup data
....total time to read data 0.0774528980255
....found opening book shatranj-book.bin with 37848 positions
.... (diag_attacks_n e[from_square][ne_pieces] & ~all_pieces)
| \
.... (diag_attacks_n w[from_square][nw_pieces] & other_pieces)
| \
.... (diag_attacks_n w[from_square][nw_pieces] & ~all_pieces))
& mask
127577709084672 0L
'10010001000010 100000000000001 010000000000000 0000000'
+---+---+---+---+---+---+---+---+
8 | | . | | . | | . | | . |
+---+---+---+---+---+---+---+---+
7 | . | | . | | . | 1 | . | |
+---+---+---+---+---+---+---+---+
6 | 1 | . | | . | 1 | . | | . |
+---+---+---+---+---+---+---+---+
5 | . | 1 | . | 1 | . | | . | |
+---+---+---+---+---+---+---+---+
4 | | . | | . | | . | | . |
+---+---+---+---+---+---+---+---+
3 | . | 1 | . | 1 | . | | . | |
+---+---+---+---+---+---+---+---+
2 | | . | | . | | . | | . |
+---+---+---+---+---+---+---+---+
1 | . | | . | | . | | . | |
+---+---+---+---+---+---+---+---+
a b c d e f g h
['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']
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
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...sa crificing
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 *
....total time to read data 0.0774528980255
....found opening book shatranj-book.bin with 37848 positions
>>position = Position("r1bqk 2r/pppp1ppp/2n5/5N2/2B1n3/8/PPP1QPPP/R1B1K2R")
>>all_pieces = position.piece_ bb["b_occupied "] | position.piece_ bb["w_occupied "]
>>other_piece s = position.piece_ bb["b_occupied "]
>>from_square = c4
>>wtm = 1
>>mask = position.pinned (from_square,wt m)
>>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) | \
>>all_pieces = position.piece_ bb["b_occupied "] | position.piece_ bb["w_occupied "]
>>other_piece s = position.piece_ bb["b_occupied "]
>>from_square = c4
>>wtm = 1
>>mask = position.pinned (from_square,wt m)
>>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_n w[from_square][nw_pieces] & other_pieces)
| \
.... (diag_attacks_n w[from_square][nw_pieces] & ~all_pieces))
& mask
>>>
>>moves
>>moves
>>>
>>tobase(moves, 2)
>>tobase(moves, 2)
>>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.genera te_moves(wtm)
>>moves,san_mov es = position.get_mo ve_list(move_li st)
>>san_moves.val ues()
>>moves,san_mov es = position.get_mo ve_list(move_li st)
>>san_moves.val ues()
'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()
>># now play a game!
>>play()
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
Comment