python - 正确打印十六进制游戏板内容
问题描述
最近我一直在研究我在 Python 中找到的一个十六进制棋盘游戏https://en.wikipedia.org/wiki/Hex_(board_game)项目。但是,由于我生疏的编码技能,我很难找出一个函数来接收板内容(存储在 N * N 数组中)并以正确的格式打印它们。输出应该如下所示:
尺寸从 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
效果很好:
但后来我意识到我需要实现一个 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,0],[0,0,0,1],[0,0,0,2],[1,2,0,0],[0,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
让我知道是否有任何不清楚的地方
推荐阅读
- kubernetes - 向图表模板添加密钥
- html - Facebook 按钮减慢页面速度
- docker - docker:未经授权:Windows 10 需要身份验证
- javascript - 如何从此页面刮取“cardinal_id”
- google-chrome - HTTP/2 服务器推送导致重复请求
- ios - 未调用 iOS 前台观察者
- android - 如何取消解析 back4app 获取/发布请求?
- laravel - 与参数的关系然后在 Eloquent Laravel 中使用 contains
- r - R函数/方法使用概率对数据帧进行采样,直到达到条件
- autocomplete - CentOS8上的Python3.8版本是否支持自动补全格式?