微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

正确打印十六进制游戏板内容

如何解决正确打印十六进制游戏板内容

最近我一直在开发一个用 Python 找到的十六进制棋盘游戏 https://en.wikipedia.org/wiki/Hex_(board_game) 项目。然而,由于我生疏的编码技能,我很难找出一个函数来接收电路板内容(存储在 N * N 数组中)并以正确的格式打印它们。输出应该是这样的:

enter image description here

大小从 4 到 21 不等,但这我可以处理。然而,考虑到最终六边形内必须有棋盘内容(W 代表白色,B 代表黑色),我需要一种方法来动态打印棋盘内容。为此,我使用了一个列表 (board_print),在游戏开始时初始化,其中每行包含一个字符串,并根据所进行的移动(A3、B2 等)动态更改其内容

# 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

效果很好:

enter image description here

但后来我意识到我需要实现一个 undo() 和 load() 函数(moves 和 move_print 列表包含所有用它们各自的 board_print 字符串进行的移动),这意味着这种愚蠢的方法不再有效。我需要一种方法将板内容正确映射到打印到控制台的十六进制网格。作为参考,棋盘被表示为一个长度为 N 的列表列表,每个子列表代表一行(board[i][j] = 0(无片)或 1(白色)或 2(黑色))。目前我像这样初始化输出和 board_print 列表:

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)

抱歉,格式/缩进不正确,这是一个草稿,我无法从 PyCharm 复制代码

解决方法

似乎是一个有趣的小练习。这是我想出来的

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)

这仅用于根据您的规格印刷电路板(0 表示空单元格,1 表示白色,2 表示黑色)。

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

产生以下输出

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

如果有什么不清楚的请告诉我

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?