user6142489
user6142489

Reputation: 532

Python Chess Implementation

I'm beginning work on a chess implementation and before going too far down the rabbit hole, I wanted to get the community's input if you wouldn't mind since I'm already at a dead end ha. I'm struggling to figure out the best way to associate the pieces with the coordinates.

Right now, I have a list of list with the various pieces where each list represents a board.

For the coordinates, I used this list comprehension

coordinates = [[(i,j) for i in range(0,8)] for j in range(0,8)]

which gives me a table like this

[(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0)]
[(0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1)]
[(0, 2), (1, 2), (2, 2), (3, 2), (4, 2), (5, 2), (6, 2), (7, 2)]
[(0, 3), (1, 3), (2, 3), (3, 3), (4, 3), (5, 3), (6, 3), (7, 3)]
[(0, 4), (1, 4), (2, 4), (3, 4), (4, 4), (5, 4), (6, 4), (7, 4)]
[(0, 5), (1, 5), (2, 5), (3, 5), (4, 5), (5, 5), (6, 5), (7, 5)]
[(0, 6), (1, 6), (2, 6), (3, 6), (4, 6), (5, 6), (6, 6), (7, 6)]
[(0, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7), (7, 7)]

Any strong thoughts on how I can associate the piece with their coordinate to find potential moves? I was thinking dictionary at first, but you have multiple pieces of the same type (eg. two knights) and don't think this would be ideal as the board evolved.

Thanks as always.

Upvotes: 2

Views: 1511

Answers (3)

Rainy
Rainy

Reputation: 1106

As someone mentioned, the most obvious simple implementation is a list of lists, for example in my implementation this logic creates the board, and then pieces are added to it using the add() method:

https://github.com/akulakov/pychess/blob/7176b168568000af721e79887981bcd6467cfbc0/chess.py#L141

Upvotes: 0

Joe Iddon
Joe Iddon

Reputation: 20414

Funnily enough, I have just been working on exactly this! Previously, I wrote a chess AI but in javascript, however today I have been converting that code into Python for use with a bigger project so the knowledge is fresh in my mind.

Originally, in the JS version, I stored the board effectively as an 8x8 array of strings for each piece (in reality this was inside an object with other data such as castling but that is not important).

However, this method of using an array (list in Python) led to problems due to the way they are passed by reference. The issue was that passing the board state through the negamax algorithm meant that for each move to be considered, the whole array (in JS) would have to be copied to stop the move being made to the original board state.

I got around this by storing the board states as strings which are immutable in Python. I would advise you to start off using lists though as they are much simpler to access and change values even though they will probably end up leading to slowness (from making copies of them) later down the line when you come to optimising.

The actual trick to storing the board state is to use one character for each piece and use upper and lowercase to represent the white and black sides. I stole this technique from the widely used FEN notation and it turns out to be really useful for both displaying and doing operations on the board state!

To see what I mean, you could initialise the starting state with:

state = ["RNBQKBNR", "PPPPPPPP", "        ", "        ", "        ", "        ", "pppppppp", "rnbqkbnr"]
state = [list(r) for r in state]

and then you can easily create a display function with:

def display(state):
   for r in reversed(state):
      print(''.join(r))

then whenever you want to display a given state, you can call display(state) which gives:

rnbqkbnr
pppppppp




PPPPPPPP
RNBQKBNR

Hopefully this helps you out! You can look at the code for my full implementation of a chess AI on github: in Python and in javascript :)

Upvotes: 2

A. Meshu
A. Meshu

Reputation: 4148

OK it goes like this: You got 64 cells, traditionally coordinated with letter and digit. Name each cell numeric so that cell will coordinated: "a1" will be 11, h5 will be 85 etc.

Now for the moves:

  1. Up: (cell value) + 1, Down: (cell value) - 1, Right: (cell value) + 10, Left: (cell value) - 10,
  2. Diagnose: Up-Left: (cell value) - 9, Up-Right: (cell value) + 11, Down-Left: (cell value) - 11, Down-Right: ((cell value) + 9,
  3. And for the Knight: (cell value) + 21, (cell value) - 21, (cell value) + 12, (cell value) - 12, (cell value) + 8, (cell value) - 8, (cell value) + 19, (cell value) – 19.

As you can understand, I recently build one by myself ( based on JS if you mind) ha ha.

Good Luck!

Upvotes: 1

Related Questions