-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added parameter to get_board_visual to see the board from black's point of view #104
Added parameter to get_board_visual to see the board from black's point of view #104
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job
Thank you )
I have written something similar. You may or may not like it better. Just thought I'd share. I mainly did this for my own visualization using colors, highlighting moves and flipping the board. I wrote this after I tried flipping the default string provided by this library by hacking it apart and reassembling it and it just became a giant mess. I also wanted the row and file indicators on all sides. # required for colored output. possibly linux only, who knows.
from termcolor import colored
class ColorConst:
# red, green, yellow, blue, magenta, cyan, white.
# on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white
# bold, dark, underline, blink, reverse, concealed.
WHITE_PIECE = "red"
BLACK_PIECE = "blue"
BOARD_WHITE = "on_white"
BOARD_BLACK = "on_yellow"
FEN_BG = None
def getBoardStr(stockfish: Stockfish, flip: bool = False, highlight: list[str] = [], color = True) -> str:
# unfliped A8 is top left
range_ranks = range(1, 8 + 1) if flip else range(8, 1 - 1, -1)
range_files = range(ord('a'), ord('h') + 1) if not flip else range(ord('h'), ord('a') - 1, -1)
white_squre = True # first square is white regardless of orientation
lineSep = " " + (("+---" * 9)[:-3]) # the seperator between the lines on the board.
result = " " # spacing for header to line up right
# make files header
for file in range_files:
file = chr(file)
result = result + f" {file}"
result = result.rstrip() + "\n"
# print actual board
for rank in range_ranks:
result = result + lineSep + "\n"
result = result + f"{rank} |"
for file in range_files:
file = chr(file)
sqr = f"{file}{rank}"
piece = stockfish.get_what_is_on_square(sqr)
fieldStr = " " if piece == None else f" {piece.value} "
if color:
fg = None
bg = ColorConst.BOARD_WHITE if white_squre else ColorConst.BOARD_BLACK
attrs = ['bold']
if sqr in highlight:
attrs.append('reverse')
if piece != None:
if piece.value in ['r', 'n', 'b', 'q', 'k', 'p']:
fg = ColorConst.BLACK_PIECE
else:
fg = ColorConst.WHITE_PIECE
fieldStr = colored(fieldStr, color=fg, on_color=bg, attrs=attrs)
result = result + f"{fieldStr}|"
white_squre = not white_squre
result = result + f" {rank}\n"
white_squre = not white_squre
result = result + lineSep + "\n"
# make files footer
result = result + " " # spacing for footer to line up right
for file in range_files:
file = chr(file)
result = result + f" {file}"
result = result.rstrip() + "\n"
return result |
Looks interesting. I would also be willing to create a PR to add this to the existing code. |
@FalcoGer I got it running on powershell in Windows, but the graphics are messed up: Pretty sure it's just a windows problem since I got similar output with termcolor on another chess project (but on linux the output was fine). |
It should also work in Powershell if you do the following before outputting the text: import colorama
colorama.init() But I could imagine that this could be a basic problem, because we don't know for all systems if colouring works and it could confuse some people if you only get the escape characters displayed. |
i think it would be too much to add the coloring as a dependency to a chess algoritm |
Adds feature suggested in #103
The
get_board_visual
function can optionally be passed a parameter to see the board from black's point of view.To make this possible, the board part of the output from Stockfish is inverted both horizontally and vertically. The numbers at the edge and the letters at the bottom are added after the invert, so that they still remain on the right and bottom side respectively.
Test and REAMDE entry added