using python
You will build a game that requires the player to find all ofthe treasures in a house. The player will use the n,e,s,w keys tomove around. The player should not be able to go through walls.Rooms in the house may have locked doors. In order to do through alocked door, the player must have previously picked up a key. Towin the game, the player must navigate around the house, goingthrough locked doors as necessary, and pick up all thetreasures.
Initially, your program will read the house from a file. Forexample, a file with the contents shown below describes ahouse.
***************
*** ********************************
*** 0 6 *
*********** ************************** *
* * * *
********5*************** * t *
* 1 * ******
* t *
************************
To create your own house file, simply click on “new file” inrepl.it, name the file, and copy and paste the above text into it.In the file, the stars (*) represent impassable walls. Numbers 0through 4 represent a key, and the number 5 through 9 represent adoor. Key 0 opens door 5, key 1 opens door 6, and so on. Each “t”represents a treasure.
Below is a function that you will include in your program thatloads a file into your Python program and represents it as a listof lists. After calling the function below, you will be able tolook at any cell in the house to determine what is here. Forexample, if you use the following code:
house = build_house();
Then you will be able to inspect house[0][0] and see that it isa ‘*’, which means that it is a piece of a wall. Likewise, athouse[2][12] is a key (key 0) that will open the locked door athouse[5][12] (door 5).
def build_house():
print(“Please enter the house file: “)
house_file = input()
housefp = open(house_file, “r”)
myhouse = []
line = housefp.readline()
while line:
myhouse.append(list(line))
line = housefp.readline()
return myhouse
Part 1 Navigation
You will write a main driver that contains a loop that asks theplayer for a direction, and then attempts to move the player inthat direction. The algorithm for the main is below.
main() |
Figure 1: Pseudocode for the house game -navigation. |
Implement the above algorithms and test it. Also, implement thefollowing functions to help you implement the above algorithm (makesure that you use them in your code!).
print_house(house, row, col) – display the house and an “@” showingwhere the player is. This function is given below.
check_north(house, row, col) – returns true if the character athouse[row][col] is not a wall
Also implement functions check_south, check_east, andcheck_west
get_treasure(house, row, col) – returns true is house[row][col] isa “t”. Also replaces the “t” with a “ “ to indicate treasure waspicked up.
The code for print_house() is below:
import copy
def print_house(h,sr,sc):
th = copy.deepcopy(h)
th[sr][sc] = “@”
for i in th:
print(”.join(str(x) for xin i), end=”)
Part 2 – Handling Door and Keys
Note that in the previous algorithm, you did not handle unlockingdoors. Therefore, in this part of the of the assignment, you willimplement this missing feature.
First, you need a data structure to keep track of what doors youcan unlock (i.e. what keys you have found). So, at the top of yourmain() function, set a variable called unlocked to a listcontaining five False values.
Next, you will add the Python code that handles picking up keys andunlocking doors. A good spot to handle going through a door is toadd code under the comment in the algorithm above that starts withthree stars, i.e.
# *** right now we are not going to handle doors or keys
So, below that comment, add Python code that implements thefollowing pseudocode.
if calling function is_door() and passing it house, trow, andtcol is True # player can’t go through the door |
Figure 2: Pseudocode for the house game – doors andkeys |
When adding code to your Python program to implement the abovemodifications to the algorithm, implement the following functionsand use them in your implementation of the pseudocode.
is_door(house,row,col) – returns true is the character athouse[row][col] is a door (from 5 to 9), returns falseotherwise.
get_key(house,unlocked,row,col) – Sets the value at the appropriateindex of unlocked to True for the number found at house[row][col],if it is a number between 0 and 4 inclusive. In other words, itsets unlocked[house[row][col]] to True if house[row][col] is anumber between 0 and 4. It also replaces house[row][col] with aspace to represent that the key has been picked up.
can_unlock(house,unlocked,row,col) – returns true if unlocked forthe corresponding door at house[row][col] is true. Remember tosubtract 5 from the door number to index into the unlocked list atthe appropriate place
Expert Answer
Answer to using python You will build a game that requires the player to find all of the treasures in a house. The player will use…