Question: The Game Your task is to invent and implement a text-based adventure game. The game needs to be coded and tested in Visual Studio Code.

The Game Your task is to invent and implement a text-based adventure game. The game needs to be coded and tested in Visual Studio Code. The base functionality that you have to implement is: The game has several locations/rooms.

The player can walk through the locations. (This was already implemented in the code you have been given.) There are items in some rooms. Every room can hold any number of items. Some items can be picked up by the player, others cant. The player can carry some items with him. Every item has a weight. The player can carry items only up to a certain total weight. The player can win. There has to be some situation that is recognised as the end of the game where the player is informed that he/she has won. Implement a command back that takes you back to the last room youve been in. Add at least four new commands (in addition to those that were present in the code you got from me) 6.1. Challenge Tasks Add characters to your game. Characters are people or animals or monsters anything that moves, really. Characters are also in rooms (like the player and the items). Unlike items, characters can move around by themselves. NEW INFORMATION AS REQUESTED CODES

class Command(): """ This class holds information about a command that was issued by the user. A command currently consists of two strings: a command word and a second word (for example, if the command was "take map", then the two strings obviously are "take" and "map"). The way this is used is: Commands are already checked for being valid command words. If the user entered an invalid command (a word that is not known) then the command word is . If the command had only one word, then the second word is . """

def __init__( self, first_word, second_word ): """ Create a command object. First and second word must be supplied, but either one (or both) can be None. Parameters ---------- first_word: string The first word of the command. None if the command was not recognised. second_word: string The second word of the command. """ self.command_word = first_word self.second_word = second_word def get_command_word(self): """ Return the command word (the first word) of this command. If the command was not understood, the result is None. Returns the command word. """ return self.command_word

def get_second_word(self): """ Returns the second word of this command. Returns None if there was no second word. """ return self.second_word

def is_unknown(self): """ Returns true if this command was not understood. """ return (self.command_word == None) # None is a special Python value that says the variable contains nothing

def has_second_word(self): """ Returns true if the command has a second word. """ return (self.second_word != None) # None is a special Python value that says the variable contains nothing

from command import Command from command_words import CommandWords

class Parser(): """ This parser reads user input and tries to interpret it as an "Adventure" command. Every time it is called it reads a line from the terminal and tries to interpret the line as a two word command. It returns the command as an object of class Command.

The parser has a set of known command words. It checks user input against the known commands, and if the input is not one of the known commands, it returns a command object that is marked as an unknown command. """ def __init__(self): """ Create a parser to read from the terminal window """ self.commands = CommandWords()

def get_command(self): """ Returns The next command from the user """ # Initialise word1 and word2 to word1 = None # None is a special Python value that says the variable contains nothing word2 = None

input_line = input( "> " )

# Find up to two words on the line and set word1 and word2 appropriately tokens = input_line.strip().split( " " ) if len( tokens ) > 0: word1 = tokens[0] if len( tokens ) > 1: word2 = tokens[1]

# Now check whether this word is known. If so, create a command # with it. If not, create a command (for unknown command). if(self.commands.is_command(word1)): return Command(word1, word2) else: return Command(None, word2);

def show_commands(self): """ Print out a list of valid command words. """ self.commands.show_all()

GAME

from room import Room from command_parser import Parser

""" * This class is the main class of the "World of Zuul" application. * "World of Zuul" is a very simple, text based adventure game. Users * can walk around some scenery. That's all. It should really be extended * to make it more interesting! * * To play this game, create an instance of this class and call the "play" * method. * * This main class creates and initialises all the others: it creates all * rooms, creates the parser and starts the game. It also evaluates and * executes the commands that the parser returns. """

class Game(): """ To play this game, create an instance of this class and call the "play" method. This main class creates and initialises all the others: it creates all rooms, creates the parser and starts the game. It also evaluates and executes the commands that the parser returns. """ def __init__(self): """ Create the game and initialise its internal map. """ self.create_rooms() self.parser = Parser()

def create_rooms(self): """ Create all the rooms and link their exits together. """ # create the rooms outside = Room("outside the main entrance of the university") theater = Room("in a lecture theater") pub = Room("in the campus pub") lab = Room("in a computing lab") office = Room("in the computing admin office") # initialise room exits outside.set_exit("east", theater) outside.set_exit("south", lab) outside.set_exit("west", pub)

theater.set_exit("west", outside)

pub.set_exit("east", outside)

lab.set_exit("north", outside) lab.set_exit("east", office)

office.set_exit("west", lab)

self.current_room = outside; # start game outside

def play(self): """ Main play routine. Loops until end of play """ self.print_welcome()

# Enter the main command loop. Here we repeatedly read commands and # execute them until the game is over. finished = False while finished == False: command = self.parser.get_command() finished = self.process_command(command) print("Thank you for playing. Good bye.")

def print_welcome(self): """ Print out the opening message for the player """ print() print("Welcome to the World of Zuul!") print("World of Zuul is a new, incredibly boring adventure game.") print("Type 'help' if you need help.") print() print(self.current_room.get_long_description())

def process_command(self, command): """ Given a command, process (that is: execute) the command.

Parameters ---------- command: Command The command to be processed Returns true If the command ends the game, false otherwise. """ want_to_quit = False

if command.is_unknown(): print("I don't know what you mean...") return False

command_word = command.get_command_word() if command_word == "help": self.print_help() elif command_word == "go": self.go_room(command) elif command_word == "quit": want_to_quit = self.quit(command) return want_to_quit

# implementations of user commands:

def print_help(self): """ Print out some help information. """ print("You are lost. You are alone. You wander") print("around at the university.") print() print("Your command words are:") self.parser.show_commands()

""" * Try to in to one direction. If there is an exit, enter the new * room, otherwise print an error message. """ def go_room(self, command): """ Try to in to one direction. If there is an exit, enter the new room, otherwise print an error message.

Parameters ---------- command: Command The command to be processed """ if command.has_second_word() == False: # if there is no second word, we don't know where to go... print("Go where?") return

direction = command.get_second_word()

# Try to leave current room. next_room = self.current_room.get_exit(direction)

if next_room == None: # None is a special Python value that says the variable contains nothing print("There is no door!") else: self.current_room = next_room print(self.current_room.get_long_description())

def quit(self, command): """ "Quit" was entered. Check the rest of the command to see whether we really quit the game.

Parameters ---------- command: Command The command to be processed Returns true, if this command quits the game, false otherwise. """ if command.has_second_word(): print("Quit what?") return False else: return True # signal that we want to quit

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 Databases Questions!