Question
I need help with completeing a python function in highltened in bold def make_grid(w: int, h: int, player_coord: Tuple[int, int], gold_coord: Tuple[int, int]) -> List[List[str]]:
I need help with completeing a python function in highltened in bold
def make_grid(w: int, h: int, player_coord: Tuple[int, int],
gold_coord: Tuple[int, int]) -> List[List[str]]:
"""
>>> make_grid(2, 3, (0, 0), (1, 2))
[['(x)', '(_)'], ['(_)','(_)'], ['(_)', '(o)']]
"""
grid = [['(_)' for i in range(w)] for j in range(h)]
x, y = player_coord
grid[y][x] = '(x)'
x, y = gold_coord
grid[y][x] = '(o)'
return grid
def print_grid(grid: List[List[str]]) -> None:
"""
e.g. if grid = [['(x)', '(_)'], ['(_)','(_)'], ['(_)', '(o)']]
this function should print out:
(x)(_)
(_)(_)
(_)(o)
"""
s = ''
for row in range(len(grid)):
s += ''.join(element for element in grid[row]) + " "
print(s)
def update_grid(grid: List[List[str]], w: int, h: int, px: int, py: int,
dx: int, dy: int) -> Optional[Tuple[int, int]]:
"""
Given the player's current position as px and py, and the directional changes in dx and dy, update the given grid to change the player's x-coordinate by dx, and their y-coordinate by dy.
More information:
Use the given w and h (representing the grid's width and height) to figure out whether or not the move is valid. If the move is not valid (that is, if it is outside of the grid's boundaries), then NO change occurs to the grid. The grid stays the same, and nothing is returned. If the move IS possible, then the grid is updated by adding dx to the player's x-coordinate, and adding dy to the player's y-coordinate. The new position in the grid is changed to the player icon '(x)', and the old position the player used to be in is changed to an empty space '(_)'. The new x- and y- coordinates of the player is returned as a tuple.
This function does NOT create or return a new grid. It modifies the "grid" list that is passed into it directly.
>>> L = [['(x)', '(_)'], ['(_)', '(o)'], ['(_)', '(_)']]
>>> update_grid(L, 2, 3, 0, 0, 1, 0)
(1, 0)
>>> L
[['(_)', '(x)'], ['(_)', '(o)'], ['(_)', '(_)']]
>>> L = [['(x)', '(_)'], ['(_)', '(o)'], ['(_)', '(_)']]
>>> update_grid(L, 2, 3, 0, 0, 0, 1)
(0, 1)
>>> L
[['(_)', '(_)'], ['(x)', '(o)'], ['(_)', '(_)']]
>>> L = [['(x)', '(_)'], ['(_)', '(o)'], ['(_)', '(_)']]
>>> print(update_grid(L, 2, 3, 0, 0, -1, 0))
None
>>> L
[['(x)', '(_)'], ['(_)', '(o)'], ['(_)', '(_)']]
"""
# COMPLETE THIS FUNCTION #
pass
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started