我试图打印一个 Reversi 宽,给定用户定义的行和列。在实现和打印电路板时,我在找到中心四件时遇到了一些麻烦。这是我到目前为止所拥有的:
def new_game_board(columns,rows) -> [[str]]:
''' Creates a new game board. Initially, a game board has the size
BOARD_COLUMNS x BOARD_ROWS and is comprised only of strings with the
value NONE
'''
board = []
for col in range(columns):
board.append([])
for row in range(rows):
board[-1].append('*')
black = (rows+1)*columns//2
white = rows//2
white = columns//2
return board
def drawBoard(board,columns,rows):
print(' '.join(map(lambda x: str(x + 1), range(columns))))
for y in range(rows):
print(' '.join(board[x][y] for x in range(columns)))
如何找到根据用户输入放置的新中心件?最终的板应如下所示:
1 2 3 4 5 6
. . . . . .
. . . . . .
. . B W . .
. . W B . .
. . . . . .
. . . . . .
你有大小为columns x rows
的矩形,所以中间在每个轴的中间:columns/2
和rows/2
。
import math
middle_start_row = math.floor(rows/2)
middle_start_col = math.floor(columns/2)