Search code examples
pythondictionarychess

Pythonic way to print a chess board in console


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


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.