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>
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.
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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.