最近我一直在做一个十六进制棋盘游戏https://en.wikipedia.org/wiki/Hex_(board_game)项目,我在Python语言中找到了这个项目。然而,由于我生疏的编码技巧,我真的很难找到一个函数来接受电路板内容(存储在一个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和moves_print列表包含用它们各自的board_print字符串进行的所有移动),这意味着这种愚蠢的方法不再有效。我需要一种方法来正确映射电路板内容到十六进制网格,这是打印到控制台。为了便于参考,电路板被表示为长度为N的列表,每个子列表代表一行(电路板i=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复制代码时遇到了问题。
发布于 2020-12-22 02:09:35
看起来是个有趣的小练习。这是我想出来的
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如果有什么不清楚的地方,请告诉我
https://stackoverflow.com/questions/65396231
复制相似问题