(Solved) : Using Python Build Game Requires Player Find Treasures House Player Use N E S W Keys Move Q42683929 . . .

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:


    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.

Build the house (see build_house() function above)
# the player’s start location:
Set startrow and startcol to be indices of an empty space in thehouse
Set num_treasures to the number of t’s in the house
Set tcount to 0   # how many treasures found so far
Loop while tcount is less that num_treasures
    Print the house
    Print the directions that the user can go(n,e,s,w), anywhere not a wall
    Read input from user into variable calledcommand
    # next, trow and tcol will represent where theplayer wants to go
    # make sure you call check_north, check_south,… as appropriate
    if command is “n” set trow to startrow-1 andtcol to startcol
    however if command is “e” set trow to startrowand tcol to startcol+1
    however if command is “s” set trow to startrow+1and tcol to startcol
    however if command is “w” set trow to startrowand tcol to startcol-1
    however if command is “q” then break out of theloop
    if the character at index trow and tcol of thehouse is a * then
        tell the user that theycannot go that way and continue the loop
    # *** right now we are not going to handle doorsor keys
    # The IF statement below should be coded intoget_treasure()
    #    except for the print (do notprint anything in get_treasure())
    if calling get_treasure() passing it trow andtcol returns True then
        # the treasure is pickedup:
        # get_treasure shouldreplace the “t” at index trow and tcol with a
        # space if a treasurewas found there
        add one to tcount
        tell the user that theyhave found a treasure, and how many they have
    set startrow to trow and startcol to tcol
if tcount equals num_treasures then
      congratulate the user

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
     # Note: doors are 5-9, keys are 0-4. Key 0unlocks door 5, so if we are
     #      trying to go through door 5, then the value of unlocked at index0
     #      should be True if we have already found the key that unlocks thedoor       
     if calling can_unlock and passing ithouse, unlocked, trow, and tcol returns False.

# player can’t go through the door
        set trow to startrow andtcol = startcol   # Keeps player from moving
        tell player “Sorry, thedoor is locked and you do not have the key”
     otherwise tell the player ”You unlockedthe door.”)
else if calling get_key() and passing it house, unlocked, trow, andtcol returs True
        # found a key!
        # get_key() should setunlocked at index house[trow][tcol] to True
        #   andreplace character at index trow and tcol of house to a space
        tell player “You found akey!”
else # do the code for treasure in the above algorithm

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…

Leave a Comment


We are the best freelance writing portal. Looking for online writing, editing or proofreading jobs? We have plenty of writing assignments to handle.

Quick Links

Browse Solutions

Place Order

About Us

× How can I help you?