Tatami
Tatami

Reputation: 45

Print hex game board contents properly

Recently I've been working on a hex board game https://en.wikipedia.org/wiki/Hex_(board_game) project that I found in Python. However due to my rusty coding skills I'm having real trouble figuring out a function that takes in the board contents (which are stored inside a N * N array) and prints them in a proper format. The output is supposed to look like this: enter image description here

Size varies from 4 to 21, but this I can handle alright. However, considering that eventually there have to be board contents inside the hexagons (W for a white piece and B for a black), I needed a way to dynamically print the board contents. For this, I used a list (board_print), initialized in the beginning of the game that contains a string for each row and dynamically alters its contents based on the move played (A3,B2 etc):

# Function to change and print board after a move is played
def play(move, whose_turn):
    global turn
    color_string = 'W' if whose_turn == 0 else 'B'
    column = " ".join(re.findall("[a-zA-Z]+", move))
    row = int(re.findall(r'\d+', move)[0])
    # Check if there is a piece already placed there and move is in bounds
    if 0 <= row <= board_size and 0 <= col_index[column] <= board_size:
        if board[row - 1][col_index[column]] != 0:
            print('Error! There is a piece already placed there')
            return False
        else:
            board[row - 1][col_index[column]] = 1 if color_string == 'W' else 2
            moves.append((row, col_index[column]))
            # Modify board_print contents
            # 4-3 4-7 4-11 4-15...
            # 6-5 6-9 6-13    ...
            # A is 0 etc, board index to print index mapping is 1->4 2->6 3->8..,,

            for index, row_string in enumerate(board_print):
                if index == 2 * row + 2:
                    # Handle A differently because index starts from 0
                    if column == 'A':
                        new_string = row_string[:index] + color_string + row_string[index + 1:]
                    else:
                        # Because col_index starts from 0 , add 1 . then multiply the result by col index to match the
                        # print format
                        new_string = row_string[
                                 :index + (col_index[column] + 1) * col_index[
                                     column] + 2] + color_string + row_string[
                                                                   index + (
                                                                           col_index[
                                                                               column] + 1) *
                                                                   col_index[
                                                                       column] + 3:]
                    board_print[index] = new_string

        # Print board
        for row in board_print:
            print(row)
        print('Move played: ', move)
        moves_print.append(board_print)
        return True
    else:
        print('Error!Move is out of bounds')
        return False  # Don't change turn if move was illegal

which worked fine:

enter image description here

But then I realized that I needed to implement an undo() and load() function (moves and moves_print lists contain all the moves made with their respective board_print strings), meaning that this silly approach can no longer work. I need a way to properly map the board contents to the hex grid that is printed to the console. For reference, the board is represented as a length N list of lists, with each sublist representing a row (board[i][j] = 0(no piece) or 1(white) or 2(black)). Currently I initialize the output and the board_print list like this:

column_names = '    A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T'
def start_board(n, load_game):
    # Append each row to the board print array
    rows_index = 0  # to index rows
    number_index = 0  # For printing out the row numbers in the board
    if not load_game: print(' ' * (n + 3) + 'W H I T E')
    board_print.append(' ' * (n + 3) + 'W H I T E')
    # Format the top rows properly with spaces
    if not load_game: print(column_names[:(4 + (3 * n + 4))])
    board_print.append(column_names[:(4 + (3 * n + 4))])
    if not load_game: print('    ' + '-   ' * n)
    board_print.append('    ' + '-   ' * n)
    if not load_game: print('   ' + '/ \\_' * (n - 1) + '/ \\')
    board_print.append('   ' + '/ \\_' * (n - 1) + '/ \\')
    # Loop enough times to print entire board. That is, 2 * n times
    for i in range(2 * n):
        if i == 0 or i % 2 == 0:
        # Even pattern
            if not load_game: print(
            ' ' * number_index + (str(rows_index + 1) + ' ' + '|   ' * n + '| ' + str(rows_index + 1)))
            board_print.append(
            ' ' * number_index + (str(rows_index + 1) + ' ' + '|   ' * n + '| ' + str(rows_index + 1)))
            rows_index += 1
        else:
            # Odd pattern
            # Check if it's final row for proper formatting
            if i == (2 * n) - 1:
                if not load_game: print(' ' * (number_index + 2) + '\\_/ ' * n)
                board_print.append(
                ' ' * (number_index + 2) + '\\_/ ' * n)
            else:
                if not load_game: print(' ' * (number_index + 2) + '\\_/ ' * n + '\\')
                board_print.append(
                ' ' * (number_index + 2) + '\\_/ ' * n + '\\')

        number_index += 1
    # Print final row (column names and BLACK)
    if not load_game: print(' ' * 2 * (n - 1) + column_names[:(4 + (3 * n + 4))])
    board_print.append(
    ' ' * 2 * (n - 1) + column_names[:(4 + (3 * n + 4))])
    moves_print.append(board_print)

Sorry for the bad formatting/indentation, this is a rough draft and I'm having trouble copying the code from PyCharm.

Upvotes: 1

Views: 717

Answers (1)

Cereal
Cereal

Reputation: 156

Seems like a fun little exercise. Here's what I came up with

column_names = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

def print_board(board):
    rows = len(board)
    cols = len(board[0])
    indent = 0
    headings = " "*5+(" "*3).join(column_names[:cols])
    print(headings)
    tops = " "*5+(" "*3).join("-"*cols)
    print(tops)
    roof = " "*4+"/ \\"+"_/ \\"*(cols-1)
    print(roof)
    color_mapping = lambda i : " WB"[i]
    for r in range(rows):
        row_mid = " "*indent
        row_mid += " {} | ".format(r+1)
        row_mid += " | ".join(map(color_mapping,board[r]))
        row_mid += " | {} ".format(r+1)
        print(row_mid)
        row_bottom = " "*indent
        row_bottom += " "*3+" \\_/"*cols
        if r<rows-1:
            row_bottom += " \\"
        print(row_bottom)
        indent += 2
    headings = " "*(indent-2)+headings
    print(headings)

This is just for printing the board according to your specifications (0 for empty cells, 1 for white, 2 for black).

board=[[0,0,0,0],[0,0,0,1],[0,0,0,2],[1,2,0,0],[0,2,1,0]]
print_board(board)

Yields the following output

     A   B   C   D     
     -   -   -   -     
    / \_/ \_/ \_/ \    
 1 |   |   |   |   | 1 
    \_/ \_/ \_/ \_/ \  
   2 |   |   |   | W | 2
      \_/ \_/ \_/ \_/ \
     3 |   |   |   | B | 3 
        \_/ \_/ \_/ \_/ \
       4 | W | B |   |   | 4
          \_/ \_/ \_/ \_/ \
         5 |   | B | W |   | 5
            \_/ \_/ \_/ \_/
             A   B   C   D

Let me know if anything is unclear

Upvotes: 1

Related Questions