6

I'm building a chess program in python. Currently, my board looks like this:

 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  

This is the current output, but I don't really like it much. You can't tell if a square is black or white, and using caps and lower case letters for the pieces is also not that great. Do you guys maybe have a better idea of how to visualize the board without using third-party libraries?

I tried the chess Unicode characters and colorization in the console, but that doesn't work on Windows.

print("\u2657")
>>> UnicodeEncodeError: 'charmap' codec can't encode character '\u2657' in position 0: character maps to <undefined>
aschultz
  • 1,658
  • 3
  • 20
  • 30
Tweakimp
  • 393
  • 5
  • 16
  • If you are stuck with cmd.exe, some of the information [here](https://stackoverflow.com/questions/1259084/what-encoding-code-page-is-cmd-exe-using) might help. – Galen Dec 21 '17 at 01:16
  • 3
    You can use [colorama](https://pypi.python.org/pypi/colorama) on windows, to show the colors. It works on all platforms. And what is the problem with unicode chess characters? – thuyein Dec 21 '17 at 01:18
  • I am not able to print these: https://en.wikipedia.org/wiki/Chess_symbols_in_Unicode . i got it to work on repl.it which runs on linux though. – Tweakimp Dec 21 '17 at 01:36
  • @ThuYeinTun I edited the post to show you what is happening. – Tweakimp Dec 21 '17 at 01:41

2 Answers2

1

I think your color idea is fine. You can use ANSI escape codes to add color.

Here's an Example I found:

print("\033[1;32;40m Bright Green  \n")

This will change the color of the text to Bright Green with a Black background. The format is:

\033[ = Escape code, this is always the same

1 = Style, 1 for normal.

32 = Text colour, 32 for bright green.

40m = Background colour, 40 is for black.

Here's a link to the site I found. http://ozzmaker.com/add-colour-to-text-in-python/

The site also has codes for differing colors.

Note: I was using Windows Powershell when running the python code and things seemed to work well.

  • Thank you for your answer. What does >> print('\033[6m' + 'Blink!' + '\x1b[0m') << do on your system? On https://repl.it/repls/VigilantCadetblueSerpent, it blinks, on mine it doesnt. – Tweakimp Dec 21 '17 at 07:27
  • Doesn't seem to be blinking on mine either – Rafael Valdez Dec 21 '17 at 08:10
0

To do this you need to use ANSI escape codes. Here is a great website which shows how to use them:

http://ozzmaker.com/add-colour-to-text-in-python/

This website is mentioned in the above answer and I strongly recommend that you check it out. Also something else you might like to try for your game is to use the escape code \033c. This will clear the terminal so each move you can update you chess board.

Here is some more info on escape codes:

https://en.wikipedia.org/wiki/ANSI_escape_code

I hope this helps!

Alex Hawking
  • 1,125
  • 5
  • 19
  • 35