Skip to content
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

Merged
merged 7 commits into from
Jun 12, 2022
Merged

Conversation

kieferro
Copy link
Contributor

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

@kieferro kieferro marked this pull request as ready for review June 11, 2022 16:32
@zhelyabuzhsky zhelyabuzhsky changed the title get_board_visual from either side Added parameter to get_board_visual to see the board from black's point of view Jun 12, 2022
@zhelyabuzhsky zhelyabuzhsky self-requested a review June 12, 2022 03:21
Copy link
Owner

@zhelyabuzhsky zhelyabuzhsky left a 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 )

@zhelyabuzhsky zhelyabuzhsky merged commit 61102a1 into zhelyabuzhsky:master Jun 12, 2022
@kieferro kieferro deleted the visual_for_black branch June 12, 2022 06:50
@FalcoGer
Copy link

FalcoGer commented Aug 9, 2022

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

image

@kieferro
Copy link
Contributor Author

kieferro commented Aug 9, 2022

Looks interesting. I would also be willing to create a PR to add this to the existing code.
@zhelyabuzhsky has to decide if he wants to include it in the library.

@johndoknjas
Copy link
Contributor

@FalcoGer I got it running on powershell in Windows, but the graphics are messed up:

image

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).

@kieferro
Copy link
Contributor Author

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.

@FalcoGer
Copy link

i think it would be too much to add the coloring as a dependency to a chess algoritm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants