好吧,我觉得自己像个白痴。在我注意到问题之前,我需要从头开始复制您的工作 - 我将 columns 与 rows 混淆了。我想这也可能是你的问题。
在您的游戏中,您应该通过更改当前列和玩家的垂直来确定玩家的水平移动('左/右?') em> 移动('Up/Down?')通过改变当前行。
这是因为当您确定newRow 的值不同于currentRow 时,您是在跨 行而不是沿 行移动。类似的逻辑适用于列。
下面是我的代码供参考:
def create_board(size):
return [['_' for _ in range(size)] for _ in range(size)]
def print_board(board):
for row in board:
print(row)
def get_board_dimensions(board):
# PRE: board has at least one row and column
return len(board), len(board[0])
def make_move(board, player_position):
def query_user_for_movement():
while True:
try:
return int(input('How many places? '))
except ValueError:
continue
def query_user_for_direction(query, *valid_directions):
while True:
direction = input(query)
if direction in valid_directions:
return direction
vertical_direction = query_user_for_direction('Up/Down? ', 'u', 'd')
vertical_displacement = query_user_for_movement()
horizontal_direction = query_user_for_direction('Left/Right? ', 'l', 'r')
horizontal_displacement = query_user_for_movement()
curr_row, curr_column = player_position
new_row = curr_row + vertical_displacement * (1 if vertical_direction == 'd' else -1)
new_column = curr_column + horizontal_displacement * (1 if horizontal_direction == 'r' else -1)
width, height = get_board_dimensions(board)
if not (0 <= new_row < height):
raise ValueError('Cannot move to row {} on board with height {}'.format(new_row, height))
elif not (0 <= new_column < width):
raise ValueError('Cannot move to column {} on board with width {}'.format(new_column, width))
board[curr_row][curr_column] = '_'
board[new_row][new_column] = 'X'
return board
if __name__ == '__main__':
# Set up an 8 x 8 board with the player at a specific location
board_size = 8
player_position = (1, 2)
board = create_board(board_size)
board[player_position[0]][player_position[1]] = 'X'
print('Initialised board with size {} and player at ({}, {})'.format(board_size, *player_position))
print_board(board)
# Allow the player to make their move.
print('Player, make your move:\n')
board = make_move(board, player_position)
# Show the new board state
print('Board after making move')
print_board(board)
开始游戏
Initialised board with size 8 and player at (1, 2)
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', 'X', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
Player, make your move:
向下移动一个图块
Up/Down? d
How many places? 1
Left/Right? r
How many places? 0
Board after making move
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', 'X', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
向右移动一个图块
Up/Down? d
How many places? 0
Left/Right? r
How many places? 1
Board after making move
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', 'X', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
向上移动一个图块
Up/Down? u
How many places? 1
Left/Right? l
How many places? 0
Board after making move
['_', '_', 'X', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
向左移动一格
Up/Down? d
How many places? 0
Left/Right? l
How many places? 1
Board after making move
['_', '_', '_', '_', '_', '_', '_', '_']
['_', 'X', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
对角线向下和向右移动
Up/Down? d
How many places? 1
Left/Right? r
How many places? 1
Board after making move
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', 'X', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
非法移动
Up/Down? u
How many places? 3
Left/Right? r
How many places? 0
Traceback (most recent call last):
File "C:/Users/<<me>>/.PyCharmCE2016.3/config/scratches/scratch_2.py", line 62, in <module>
board = make_move(board, player_position)
File "C:/Users/<<me>>/.PyCharmCE2016.3/config/scratches/scratch_2.py", line 41, in make_move
raise ValueError('Cannot move to row {} on board with height {}'.format(new_row, height))
ValueError: Cannot move to row -2 on board with height 8