Question: In this exercise, you will write a Python program that helps you navigate a treasure map represented by a grid. Your task is to find

In this exercise, you will write a Python program that helps you navigate a "treasure map"
represented by a grid. Your task is to find the hidden treasure by following a series of directional
commands. This exercise will help you practice Python basics, including loops, conditionals, and
list manipulation.
Scenario:
You are on a treasure hunt, and you have a map represented by a 5x5 grid. The grid looks like
this:
[['.','.','.','.','.'],
['.','.','.','.','.'],
['.','.','X','.','.'],
['.','.','.','.','.'],
['.','.','.','.','.']]
The X marks the spot where the treasure is hidden, and you start at the top-left corner of the grid
(position [0,0]).
Instructions:
1. Map Initialization:
o Create a 5x5 grid filled with dots . except for the treasure, which is marked by X
at a random position (shown as [2][2] in the matrix above).
2. Navigating the Map:
o You will navigate the grid using a series of commands: up, down, left, and
right.
o Write a function move(position, direction) that takes the current position as
a list [row, col] and a direction as a string, and returns the new position after
moving one step in that direction.
o Ensure that your function doesn't allow movement outside the grid boundaries.
3. Finding the Treasure:
o This is a text input based game. You will start at position [0,0].
o Type commands (right,left,up,down) into the ipython console. Use the
move function you created to move to another location. You are searching for the
treasure.
o Have the program return a value giving how close you are to the treasure.
o Continue issuing commands until you reach the treasure, and print "Treasure
found!" along with the final position.
4. Plotting the Path:
o Use matplotlib to visualize the grid and the path you took to find the treasure.
o Plot the starting point, each move, and the treasure location.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!