Colorama / termcolor colored output not working completely

74 Views Asked by At

I've got an issue in my code. I want to print to colored boards to the terminal of a fleet battle game.

This is my code:

# function to print the hole board while player is playing the game
def print_leaked_board(board):
    """
    function to print the board with visible ships
    Args:
        board (List): The board on which the ships are placed
    """
    # creates the first row with letters to locate the ship positon (horizontal)
    print("  ".join(letterRow))
    for i, row in enumerate(board):
        # creates the first column with letters to locate the ship positon (vertical)
        print(str(i + 1).zfill(2), end="  ")  # zfill to format number  (0digit)
        # replace function to optical replace 1 = ship position, 0 = free space (water), 6 = placement blocker for following player ships
        print(
            "  ".join(
                str(elem).replace("1", "\033[37m#").replace("0", "\033[34m~").replace("6", "\033[31mX")
                for elem in row
            )
        )


# function to print the hole board while player is placing the ships
def print_hidden_board(board):
    """Function to print the board without showing the ships, so the opponent cannot see the ships of the other player

    Args:
        board (List): The board on which the ships are placed
    """
    # creates the first row with letters to locate the ship positon (horizontal)
    print("  ".join(letterRow))
    for i, row in enumerate(board):
        # creates the first column with letters to locate the ship positon (vertical)
        print(str(i + 1).zfill(2), end="  ")  # zfill to format number  (0digit)
        # replace function to optical replace
        # 1 = ship filed but hidden (shown as water),
        # 2 = free space (water),
        # 2 = shot spot without hit,
        # 3 = shot spot with hit, 4 = eleminated ship (complete)
        print(
            "  ".join(
                str(elem).replace("1", "~").replace("0", "~").replace("2", "O").replace("3", "x").replace("4", "#")
                for elem in row
            )
        )

In the first function the colored output is working fine. In the second print function, which is basically the same as the one above, i only get the '7m#' printed and i don't know how to fix it. Any ideas?

Thanks in advance.

0

There are 0 best solutions below