Question

I was wondering what would be the best pythonic way to print a console chess board using a dictionary.

Should I use list ? but what happens if their is no piece on the board on a certain row?

Edit : Exemple

Was it helpful?

Solution

In chess programs (typically for computing best moves, solve riddles, etc.) internally a 10×10 board is used where the middle 8×8 fields are used by the visible and playable board. (The margin helps keep the code simple.) To simplify memory management, typically a linear memory is used to store this, i. e. 100 fields. The position of a field of the board is then computed using board[(y+1) * 10 + (x+1)] with x, y in the range 0..7. (Init by board = [ 0 ] * 100.)

So you might want to use this, just in case your program later should expand to also compute moves or stay compatible with a library doing this.

As for graphics, I propose to use this board:

     A       B       C       D       E       F       G       H
  ------- ------- ------- ------- ------- ------- ------- -------
 | @___@ |  %~b  |  .@.  | \o*o/ | __+__ |  .@.  |  %~b  | @___@ |
8|  @@@  | `'dX  |  @@@  |  @@@  | `@@@' |  @@@  | `'dX  |  @@@  |8
 | d@@@b |  d@@b | ./A\. | d@@@b | d@@@b | ./A\. |  d@@b | d@@@b |
  ------- ------- ------- ------- ------- ------- ------- -------
 |   _   |   _   |   _   |   _   |   _   |   _   |   _   |   _   |
7|  (@)  |  (@)  |  (@)  |  (@)  |  (@)  |  (@)  |  (@)  |  (@)  |7
 |  d@b  |  d@b  |  d@b  |  d@b  |  d@b  |  d@b  |  d@b  |  d@b  |
  ------- ------- ------- ------- ------- ------- ------- -------
 |       | . . . |       | . . . |       | . . . |       | . . . |
6|       | . . . |       | . . . |       | . . . |       | . . . |6
 |       | . . . |       | . . . |       | . . . |       | . . . |
  ------- ------- ------- ------- ------- ------- ------- -------
 | . . . |       | . . . |       | . . . |       | . . . |       |
5| . . . |       | . . . |       | . . . |       | . . . |       |5
 | . . . |       | . . . |       | . . . |       | . . . |       |
  ------- ------- ------- ------- ------- ------- ------- -------
 |       | . . . |       | . . . |       | . . . |       | . . . |
4|       | . . . |       | . . . |       | . . . |       | . . . |4
 |       | . . . |       | . . . |       | . . . |       | . . . |
  ------- ------- ------- ------- ------- ------- ------- -------
 | . . . |       | . . . |       | . . . |       | . . . |       |
3| . . . |       | . . . |       | . . . |       | . . . |       |3
 | . . . |       | . . . |       | . . . |       | . . . |       |
  ------- ------- ------- ------- ------- ------- ------- -------
 |   _   |   _   |   _   |   _   |   _   |   _   |   _   |   _   |
2|  ( )  |  ( )  |  ( )  |  ( )  |  ( )  |  ( )  |  ( )  |  ( )  |2
 |  /_\  |  /_\  |  /_\  |  /_\  |  /_\  |  /_\  |  /_\  |  /_\  |
  ------- ------- ------- ------- ------- ------- ------- -------
 | [___] |  %~\  |  .O.  | \o^o/ | __+__ |  .O.  |  %~\  | [___] |
1|  [ ]  | `')(  |  \ /  |  [ ]  | `. .' |  \ /  | `')(  |  [ ]  |1
 | /___\ |  <__> |  /_\  | /___\ | /___\ |  /_\  |  <__> | /___\ |
  ------- ------- ------- ------- ------- ------- ------- -------
     A       B       C       D       E       F       G       H

I made these ASCII graphics back in the 90s for Tubmud. Feel free to use them.

OTHER TIPS

I would use a nested list for that problem. In chess, you have an 8x8 grid, which can easily built by python list comprehension magic.

grid = [ [" "] * 8 for unused in range(8)]

You than can access and alter the elements by indexing an array:

grid[0][0] = 'T'

You can use lower case letters for white and upper case letters for black figures, and their first letter or so for the kind, like 'Q' for black queen.

Dictionary looks not good for me, since it is hard to make operations for checking valid moves or sth, because you have no other way to determine the position of a figure by iterating over all values. The grid is most natural.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top