python - Incorrect placement of token in 2D grid -
i'm making treasure hunt game on changeable 8 x 8, 10 x 10 or 12 x 12 grid part of assignment school , need help. whenever make move on grid doesn't move desired position. can me or explain me why doesn't work?
the code below have far.
def makemove(): global board, numberofmoves, tokenboard, playerscore, tokenboard playercoords = getplayerposition() currentrow = playercoords[0] #sets var playercoords[0] currentcol = playercoords[1] #sets var playercoords[1] directionud = input("up/down?") #sets var 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 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 calculatepoints function if newcol > gridsize or newrow > gridsize: print("that move place outside of grid. please try again.") board[newcol][newrow] = "x" #sets new position "x" board[currentrow][currentcol] = "-" numberofmoves = numberofmoves + 1 #adds 1 numberofmoves displayboard() #calls displayboard function
okay, feel idiot. took reproducing work scratch before noticed problem - i'm confusing columns rows. think might issue well.
in game, should determine player's horizontal movement ('left/right?') changing current column , player's vertical movement ('up/down?') changing current row.
this because when determine value newrow
differs currentrow
, you're moving across rows rather along them. similar logic applies columns.
here's code below reference:
def create_board(size): return [['_' _ in range(size)] _ in range(size)] def print_board(board): row in board: print(row) def get_board_dimensions(board): # pre: board has @ least 1 row , 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 row {} on board height {}'.format(new_row, height)) elif not (0 <= new_column < width): raise valueerror('cannot move column {} on board width {}'.format(new_column, width)) board[curr_row][curr_column] = '_' board[new_row][new_column] = 'x' return board if __name__ == '__main__': # set 8 x 8 board player @ 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 size {} , player @ ({}, {})'.format(board_size, *player_position)) print_board(board) # allow player make move. print('player, make move:\n') board = make_move(board, player_position) # show new board state print('board after making move') print_board(board)
starting game
initialised board size 8 , player @ (1, 2) ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', 'x', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] player, make move:
moving 1 tile down
up/down? d how many places? 1 left/right? r how many places? 0 board after making move ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', 'x', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_']
moving 1 tile right
up/down? d how many places? 0 left/right? r how many places? 1 board after making move ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', 'x', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_']
moving 1 tile up
up/down? u how many places? 1 left/right? l how many places? 0 board after making move ['_', '_', 'x', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_']
moving 1 tile left
up/down? d how many places? 0 left/right? l how many places? 1 board after making move ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', 'x', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_']
moving diagonally down , right
up/down? d how many places? 1 left/right? r how many places? 1 board after making move ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', 'x', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_'] ['_', '_', '_', '_', '_', '_', '_', '_']
making illegal move
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 row {} on board height {}'.format(new_row, height)) valueerror: cannot move row -2 on board height 8
Comments
Post a Comment