【问题标题】:Incorrect placement of token in 2D grid [closed]令牌在 2D 网格中的错误放置 [关闭]
【发布时间】:2017-01-14 16:42:54
【问题描述】:

作为学校作业的一部分,我正在可变的 8 x 8、10 x 10 或 12 x 12 网格上制作寻宝游戏,我需要一些帮助。每当我在网格上移动时,它并不总是移动到所需的位置。有人可以帮助我或向我解释为什么它不起作用吗?

下面的代码是我目前所拥有的。

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

【问题讨论】:

  • 仅从视觉上检查您当前的代码,没有发现明显错误(还有改进的余地,但似乎没有导致意外行为)。您能否包括代码或calculatePoints 并详细说明`它并不总是移动到所需位置`?
  • 顺便说一句,您的评论#sets var to the input of "Up/down?" 是重复的。第二次是“左/右”。
  • 你的条件if newCol > gridSize or newRow > gridSize: 应该使用>= 不是吗?它还应该返回或引发异常。

标签: python grid token


【解决方案1】:

好吧,我觉得自己像个白痴。在我注意到问题之前,我需要从头开始复制您的工作 - 我将 columnsrows 混淆了。我想这也可能是你的问题。

在您的游戏中,您应该通过更改当前和玩家的垂直来确定玩家的水平移动('左/右?') 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

【讨论】:

    猜你喜欢
    • 2013-05-21
    • 2014-12-03
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 2014-01-10
    • 2023-03-31
    • 2012-08-02
    • 1970-01-01
    相关资源
    最近更新 更多