Python 二维网格中令牌的放置不正确
作为学校作业的一部分,我正在可变的8 x 8、10 x 10或12 x 12网格上制作一个寻宝游戏,我需要一些帮助。每当我在网格上移动时,它并不总是移动到所需的位置。有人能帮我或向我解释为什么它不起作用吗 下面的代码是我到目前为止的代码Python 二维网格中令牌的放置不正确,python,grid,token,Python,Grid,Token,作为学校作业的一部分,我正在可变的8 x 8、10 x 10或12 x 12网格上制作一个寻宝游戏,我需要一些帮助。每当我在网格上移动时,它并不总是移动到所需的位置。有人能帮我或向我解释为什么它不起作用吗 下面的代码是我到目前为止的代码 def makeMove(): global board, numberOfMoves, tokenBoard, playerScore, tokenBoard playerCoords = getPlayerPosition() curren
def makeMove():
global board, numberOfMoves, tokenBoard, playerScore, tokenBoard
playerCoords = getPlayerPosition()
currentRow = playerCoords[0] #sets var to playerCoords[0]
currentCol = playerCoords[1] #sets var to playerCoords[1]
directionUD = input("Up/Down?") #sets var to the input of "Up/down?"
movement_col = int(input("How many places?"))
if directionUD == "u": #checks if var equals "u"
newCol = currentCol - movement_col
print (newCol)
elif directionUD =="d": #checks if var equals "d"
newCol = currentCol + movement_col
directionLR = input("Left/Right?") #sets var to the input of "Up/down?"
movement_row = int(input("How many places?"))
if directionLR == "l": #checks if var equals "l"
newRow = currentRow - movement_row
print(newRow)
elif directionLR == "r": #checks if var equals "r"
newRow = currentRow + movement_row
print(newRow)
calculatePoints(newCol, newRow) #calls the calculatePoints function
if newCol > gridSize or newRow > gridSize:
print("That move will place you outside of the grid. Please try again.")
board[newCol][newRow] = "X" #sets the new position to "X"
board[currentRow][currentCol] = "-"
numberOfMoves = numberOfMoves + 1 #adds 1 to the numberOfMoves
displayBoard() #calls the displayBoard function
好吧,我觉得自己像个白痴。我花了很长时间才注意到问题——我把列和行搞混了。我想这也可能是你的问题 在游戏中,您应该通过更改当前列来确定玩家的水平移动(“左/右”),通过更改当前行来确定玩家的垂直移动(“上/下”) 这是因为当您确定与
currentRow
不同的newRow
值时,您是跨行移动,而不是沿行移动。类似的逻辑也适用于列
以下是我的代码供参考:
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)
向下移动一块瓷砖
向上/向下?D
有多少地方?1.
左/右?R
有多少地方?0
搬家后的董事会
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
[“uuu'、”uu'、”X'、”uu'、”uu'、”uu'、”uu'、”uu']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
向右移动一个磁贴
向上/向下?D
有多少地方?0
左/右?R
有多少地方?1.
搬家后的董事会
['_', '_', '_', '_', '_', '_', '_', '_']
[uu'、''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
向上移动一个磁贴
向上/向下?U
有多少地方?1.
左/右?L
有多少地方?0
搬家后的董事会
[“uuu'、”uu'、”X'、”uu'、”uu'、”uu'、”uu'、”uu']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
向左移动一个磁贴
向上/向下?D
有多少地方?0
左/右?L
有多少地方?1.
搬家后的董事会
['_', '_', '_', '_', '_', '_', '_', '_']
[“uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
沿对角线向下和向右移动
向上/向下?D
有多少地方?1.
左/右?R
有多少地方?1.
搬家后的董事会
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
[uu'、''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
['_', '_', '_', '_', '_', '_', '_', '_']
做出非法举动
向上/向下?U
有多少地方?3.
左/右?R
有多少地方?0
回溯(最近一次呼叫最后一次):
文件“C:/Users/.PyCharmCE2016.3/config/scratches/scratch_2.py”,第62行,在
棋盘=移动(棋盘、玩家位置)
文件“C:/Users/.PyCharmCE2016.3/config/scratches/scratch_2.py”,第41行,make_move
raise VALUERROR('无法移动到板上高度为{}的{}行。格式(新行,高度))
ValueError:无法移动到板上高度为8的第2行
仅通过目视检查您当前的代码,没有发现明显错误(有改进的余地,但似乎没有导致意外行为)。你能把代码或计算点
包括在内,并详细说明“它并不总是移动到所需的位置”吗?你的注释#将变量设置为“向上/向下”的输入?
是重复的。第二次是“左/右”。另外,如果newCol>gridSize或newRow>gridSize:您的条件应该使用=
,不是吗?它还应该返回或引发异常。