Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python visual output for chessboard

Tags:

python

2d

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>
like image 592
Tweakimp Avatar asked Nov 07 '22 12:11

Tweakimp


1 Answers

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.

like image 141
Rafael Valdez Avatar answered Nov 14 '22 21:11

Rafael Valdez