令牌在2D网格中的位置不正确

时间:2017-01-14 16:42:55

标签: python grid token

我在一个可更换的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

1 个答案:

答案 0 :(得分:1)

好吧,我觉得自己像个白痴。在我注意到问题之前,我从头开始重现你的工作 - 我把混淆了。我认为这也可能是你的问题。

在游戏中,你应该通过更改当前的和玩家的垂直来确定玩家的水平运动('左/右?')通过更改当前来运动('上/下?')。

这是因为当您确定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