The Mages Castle Adventure

Language: Python

Task:

This objective was to create a small word adventure program using interacting functions..

Efficiency and improvement

There are several areas I could improve on including:

  1. Using CSV files as databases rather than dictionary variables. This would decrease the size of the strings that the program has stored.
  2. Using CSV files to save and load the players position. This would be useful for those who wish to switch off the program and carry on later.
  3. Use more loops to decrease some of the repeated code. For instance, creating a list of the item names would allow me to loop through the itemsInventory dictionary checking to see if they are present when the ‘inventory’ action has been used.
def move(room, rooms, choice):
"""This function asks for the direction the player wants to move and changes the room according to the result"""

new_choice = input("Which way would you like to go? North (n), south (s), west (w) or east (e)?")

# The first four rooms (2, 1, 3 and -1) have special rules that must be tested if the player wants to choose a
# direction for example, the minotaur must first be defeated before the player moves whichever way they want.

if room == 2 and rooms[2][1] == "3" and new_choice == "n":
room = -1
return room, choice
if room == 1 and rooms[1][1] == "0" and choice == "n":
print("The minotaur blocks your way. You must leave the same way that you entered")
room = 4
return room, choice
if room == 1 and rooms[1][1] == "0" and choice == "w":
print("The minotaur blocks your way. You must leave the same way that you entered")
room = 2
return room, choice
if room == 3 and rooms[3][1] == "0" and choice == "e":
print("The swordsman blocks your way. You must leave the same way that you entered")
room = 2
return room, choice
if room == 3 and rooms[3][1] == "0" and choice == "n":
print("The swordsman blocks your way. You must leave the same way that you entered")
room = 6
return room, choice
if room == -1 and new_choice != "s":
print("There is no other way to go except south")
return room, choice
if room == 2 and choice == "n" and rooms[2][1] == "1":
room = -1
return room, choice

# The next tests change the room according to the direction the person has taken, stopping the move if the person
# cannot go that way

if new_choice == "w":
if room == 1 or room == 4 or room == 7 or room == -1:
print("You cannot go that way")
else:
room -= 1
choice = new_choice
return room, choice
if new_choice == "n":
if room == 1 or room == 2 or room == 3 or room == -1:
print("You cannot go that way")
else:
room -= 3
choice = new_choice
return room, choice
if new_choice == "e":
if room == 3 or room == 6 or room == 9 or room == -1:
print("You cannot go that way")
else:
room += 1
choice = new_choice
return room, choice
if new_choice == "s":
if room == 7 or room == 8 or room == 9:
print("You cannot go that way")
else:
room += 3
choice = new_choice
return room, choice
return room, choice


def use(room, rooms, items, items_inventory):
"""This functions allows an item to be used"""

import sys
equipment = input("What would you like to use?")

# First it checks to see if the object is in the players inventory. It also checks if the player has typed in the
# correct spelling using try/except

try:
if items_inventory[equipment] == 0:
print("You do not have that")
return rooms, items, items_inventory
except:
return rooms, items, items_inventory

# Next it checks to see if the item being used can be used in the current room. It makes the correct changes if it
# can e.g. the status of the room being complete (i.e. rooms[1][1] being 1 or 0)
if room == 1 and equipment != "potion":
print("That will be of no use.")
return rooms, items, items_inventory
if room == 1 and equipment == "potion":
print(items[equipment][1])
equipment = input("Is there anything else you would like to use?")
try:
if items_inventory[equipment] == 0:
return rooms, items, items_inventory
except:
print ("A mistake has been made")
return rooms, items, items_inventory
if equipment != "sword":
print("You will need something else")
return rooms, items, items_inventory
else:
print(items[equipment][1])
rooms[1][1] = "1"
items_inventory["gem"] = "1"
return rooms, items, items_inventory
if room == 3 and equipment != "book":
print("That will be of no use.")
return rooms, items, items_inventory
if room == 3 and equipment == "book":

# When two objects are needed it puts an if statement inside the if statement to ask for the second object

equipment = input("Is there anything else you would like to use?")
try:
if items_inventory[equipment] == 0:
return rooms, items, items_inventory
except:
print ("A mistake has been made.")
return rooms, items, items_inventory
if equipment != "bracelet":
print("You will need something else")
return rooms, items, items_inventory
else:
print(items[equipment][1])
rooms[3][1] = "1"
items_inventory["sword"] = 1
return rooms, items, items_inventory
if room == 5 and equipment != "gem":
print("That will be of no use.")
return rooms, items, items_inventory
if room == 5 and equipment == "gem":
print(items[equipment][1])
sys.exit()
if room == 8 and equipment != "key":
print("That will be of no use.")
return rooms, items, items_inventory
if room == 8 and equipment == "key":
print(items[equipment][1])
rooms[8][1] = "1"
items_inventory["hairbrush"] = "1"
return rooms, items, items_inventory
if room == 9 and equipment != "hairbrush":
print("That will be of no use.")
return rooms, items, items_inventory
if room == 9 and equipment == "hairbrush":
print(items[equipment][1])
equipment = input("Is there anything else you would like to use?")
try:
if equipment != "components":
print("There is something else you will need to use.")
return rooms, items, items_inventory
except:
print ("A mistake has been made")
return rooms, items, items_inventory
else:
print(items[equipment][1])
items_inventory["potion"] = "1"
return rooms, items, items_inventory
if room == -1 and equipment != "pale":
print("That will be of no use here")
return rooms, items, items_inventory
if room == -1 and equipment == "pale":
print(items[equipment][1])
items_inventory["bracelet"] = "1"
rooms[-1][1] = "1"
return rooms, items, items_inventory
print("Try again")
return rooms, items, items_inventory


def examine(items_inventory, items):
"""This function allows the player to look at the object and get its description (i.e. items[1])"""

equipment = input("What would you like to examine?")

# Uses a try/except statement incase an incorrect word is entered

try:
if items_inventory[equipment] == "0":
return 0
if items_inventory[equipment] == "1":
print(items[equipment][0])
return 0
except:
print("Incorrect")
return 0


def search(room, rooms, items_inventory):
"""Allows a room to be searched and any chances that are required to occur"""

# These statements will check to see which room the player is in (room) and if the room has been complete
# (rooms [1][1] being in the state of 1 or 0)

if room == 1 and rooms[1][1] == "0":
print("Like you're going to be able to search anything with a minotaur in here!")
return rooms, items_inventory
if room == 1 and rooms[1][1] == "1":
print("Nothing more to find I'm afraid.")
return rooms, items_inventory

# If an object is found the items_inventory is altered to show the player has the item. This particular
# room as three outcomes as multiple searches as made so it changed the room status three times to keep track
# of how many searches have been made.

if room == 2 and rooms[2][1] == "0":
print("You find an interesting pamphlet")
items_inventory["pamphlet"] = "1"
rooms[2][1] = "1"
return rooms, items_inventory
if room == 2 and rooms[2][1] == "1":
print("You find an interesting book")
items_inventory["book"] = "1"
rooms[2][1] = "2"
return rooms, items_inventory
if room == 2 and rooms[2][1] == "2":
print("As you pull out a book you hear a click and a bookcase in the north wall swings aside. A secret door!")
rooms[2][1] = "3"
return rooms, items_inventory
if room == 2 and rooms[2][1] == "3":
print("Nothing more to find I'm afraid")
return rooms, items_inventory
if room == 3 and rooms[3][1] == "0":
print("You'll have to defeat the swordsman first!")
return rooms, items_inventory
if room == 3 and rooms[3][1] == "1":
print("Nothing more to find I'm afraid.")
return rooms, items_inventory

# The if statement within the next if statement is an activity of a specific room, testing the player to solve a
# puzzle

if room == 4 and rooms[4][1] == "0":
answer = input(
"The orb glows and a cackling voice comes from it's centre. 'You find me in December but not in any other"
"month.' It waits for you answer.")
if answer == "D" or answer == "d":
print(
"'You are correct!' Shrieks the voice, 'Here is your prize and make search the library thoroughly.' A"
"small key, big enough to find a casket, materialises in your pocket.")
items_inventory["key"] = "1"
return rooms, items_inventory
else:
print("The voice cackles mockingly, 'Better luck next time!'")
return rooms, items_inventory
if room == 4 and rooms[4][1] == "1":
print("The orb fails to glow.")
return rooms, items_inventory
if room == 5:
print("It looks like there could be some sort of mechanism in the floor")
return rooms, items_inventory
if room == 6 and rooms[6][1] == "0":
print(
"Going over to the well you wind the mechanism until a rickety bucket of water reaches the top. This could"
" come in useful.")
items_inventory["pale"] = "1"
return rooms, items_inventory
if room == 6 and rooms[6][1] == "1":
print("No more buckets to fill")
return rooms, items_inventory
if room == 7 and rooms[7][1] == "0":
print("Searching amongst the spices and animal parts you gather a few spell components")
items_inventory["components"] = "1"
return rooms, items_inventory
if room == 7 and rooms[7][1] == "0":
print("There are lots of magical ingredients but they are beyond your understanding")
return rooms, items_inventory
if room == 8 and rooms[8][1] == "0":
print("On the table sits a small casket")
return rooms, items_inventory
if room == 8 and rooms[8][1] == "1":
print("The casket is now empty")
return rooms, items_inventory
if room == 9 and rooms[9][1] == "0":
print("The witch won't let you search through HER room!")
return rooms, items_inventory
if room == 9 and rooms[9][1] == "1":
print("The witch stirs her cauldron, mumbling about her revenge to come")
return rooms, items_inventory
if room == -1 and rooms[-1][1] == "0":
print("The blob beast slurps across the floor toward you")
return rooms, items_inventory
if room == -1 and rooms[-1][1] == "1":
print("A puddle of black goo lies on the floor")
return rooms, items_inventory
return rooms, items_inventory


def inventory(items_inventory):
"""This function checks the items in the inventory and lists them if the player has them"""

if items_inventory["hairbrush"] == "1":
print("hairbrush")
if items_inventory["components"] == "1":
print("components")
if items_inventory["potion"] == "1":
print("potion")
if items_inventory["sword"] == "1":
print("sword")
if items_inventory["gem"] == "1":
print("gem")
if items_inventory["key"] == "1":
print("key")
if items_inventory["pamphlet"] == "1":
print("pamphlet")
if items_inventory["book"] == "1":
print("book")
if items_inventory["bracelet"] == "1":
print("bracelet")
if items_inventory["treasure"] == "1":
print("treasure")
if items_inventory["pale"] == "1":
print("pale")
return 0


def set_variables():
"""Sets the initial variables. These include:
room --> Each room has a number and the room variables states where the player is.
choice --> Stating the last direction the player came from. This is used in the minotaur and swordsman rooms to make
the player go back the way they came if they haven't completed the room
items_inventory --> a dictionary that states whether the objects have been collects ("1") or not ("0")
items --> a dictionary to give a description of each item and what happens when they are used successfully
rooms --> a dictionary giving a description of each of the rooms.
"""

room = 8
choice = "na"
items_inventory = {"hairbrush": 0, "components": 0, "potion": 0, "sword": 0, "gem": 0, "key": 0, "pamphlet": 0,
"bracelet": 0, "book": 0, "treasure": 0, "pale": 0}
items = {"hairbrush": ("a simple wooden hairbrush, frequently used by some aged person",
"She cackles and takes the hair from the brush. 'Now I'll have the control around here!'"),
"components": ("strange spell components that smell spicey and foreign",
"'Fantastic.' The hag smiles, dropping them into the couldron. 'Now I can make you your "
"potion'."),
"potion": ("a bottle whose odour is marvelous, steam rising from the pink liquid",
"Gulping down the frothing liquid, your muscles bulge and you have a surge of energy"),
"sword": ("a weapon with a fine blade and could be used to fight the most hardy of monsters",
"your charge forward, striking forward and battering the minotaur aside. Slashing out it bellows"
" in pain before your sword plunges into its chest and kills the snorting beast. As it crashes "
"to the ground a glowing gem falls from its grasp."),
"gem": ("a large green gem that gleams with a magical energy that draws you in",
"Fixing the glowing jewel into the gaping hole. you hear the sound of gears and mechanisms "
"turning. A spiralling stair case slowly rises from the ground, making you jump back in surprise. "
"In shock you realise you have found the way out and finally will escape the dungeon! Your quest "
"is at an end!"),
"key": ("a small brass key for a very small lock",
"With a click the casket opens. Within is a hairbrush with tufts of greying hair caught in its "
"teeth."),
"book": ("a small pamphlet that reads 'Beginner swordsman to duelling master in under a minute!'",
"Now you understand the true skills of swordsmanship! Now you are Zorro!"),
"bracelet": (
"a bracelet whose interwoven runes form a delicate pattern of cheetahs sprinting across its surface.",
"You feel a glow within and feel faster, lighter. The swordsman gives a nervous swollow as you swish "
"the blade back and fore at the speed of light. Easily out doing him he raises his hands as you prick "
"his chest with the blade. 'Okay, okay I surrender! But you can have this!' With great reluctance he "
"hands over a beautifully engraved sword."),
"pamphlet": (
"a book that goes that into the fascinating depths of mythical monsters such as the monstrous minotaur"
" that can only be defeated by the strongest of foes or the simple use of the water to fight the "
"bilious blob.", ""),
"treasure": ("a pile of glittering gems and golden coins! You're rich!", ""),
"pale": ("a small rickety bucket holds water collected from the well",
"Tossing the cold water onto the creature, it screams out in pain for dissolving into a pool of "
"black liquid. A quick search of the room reveals a pile of treasure that includes a sparkling "
"bracelet.")}
rooms = {1: ["The walls of this cell-like room are bare stone, splatters of old blood painting the dry surface. "
"Bones litter the floor and before you can say anything you look up to see a\n creature snorting and "
"preparing to charge you. It has the huge muscular body of a man but its head is that of a bull, "
"steam rising from its nostrils and pointed horns ready to\n stab. Beware the minotaur! There is an "
"exit south and east.", "0"],
2: ["You cannot see anything except rows of bookcases in this room, carved from ornate oak. The surface is"
" smooth to the touch and the musty smell of old books and papers meets you.\n The shelves hold row"
" upon row of ancient texts, some very difficult to understand and written in encrypted magical words."
" There is an exit west, south and east.", "0"],
3: ["As you step into the room an extravagantly dressed figure pounces toward you. He has a velvet tunic "
"that is adorned with frills and lace but underneath you can see the leather\n jerkin of a"
" professional. A handle bar moustache adorns his face and his eyes sparkle. “I am the bodyguard of"
" the mage and I challenge you to a duel!” He tosses a sword to your\n feet and draws his own. There"
" are exits south and west.", "0"],
4: ["There is nothing in this room except a tall, thin, black alter in the centre."
"Upon this is a large orb, a claw shaped hand holding it up. For a second you swear you hear a "
"faint\n cackling voice but then it is gone. There are exits north, east and south.", "0"],
5: ["This huge chamber is empty except a wide circle that is etched into the centre. There are exits"
" north, east, south and west.", "0", ],
6: ["The room is cold and damp, water dripping from the ceiling. In the centre of the room there is a"
" stone well, a wooden handle being fixed to the lifting mechanism. Turning it a few\n times obviously"
" brings up the bucket from who knows how far below. There is an exit north, west and south.", "0"],
7: ["The room is clustered with all sorts of shapes and smells. The morbid forms of dead bats and rats"
" hang upside down from the ceiling whilst herbs and spices lay scattered on the\n tables. There are"
" some strange, magical objects that are beyond your understanding and most are covered in a thick"
" layer of dust. Dark shadows hide the corners and you are sure\n you hear something scuttling around."
" There is an exit north and an exit west.", "0"],
8: ["You have entered the first chamber of the evil mage. This is obviously his living quarters; a"
" threadbare armchair showing tufts of feathers through numerous rips; a roaring fire\n jumping and"
" dancing in a magnificent fireplace; and a small table across one side adorned with small items,"
" figurines and papers. This is a thick rug on the floor that has been\n scared and burned by the"
" fire’s spitting and a large picture of the ancient mage is hanging on the wall.", "0"],
9: ["The pungent odour of a large cauldron’s content hits your nostrils"
" and almost makes you throw up. Seeing the colour drain from your face, a warty witch cackles as she "
"stirs her\n mixture. Her black hat is bent to one side and you see misshapen teeth when she smiles."
" Pulling her shawl around her she goes back to her spell, frowning at the potion. “If only \nI had"
" some of that wizard’s hair for my spell. I could control his mind and have all the power!”.", "0"],
-1: ["Coins glitter all over the floor and ornate items are scattered here and there. As you look up, a "
"huge, gelatinous, black blob slithers toward you, leaving a sticky gooey\n trail behind it. The"
" stone dissolves beneath its path of mucus,", "0"]}
return room, choice, items_inventory, items, rooms


def turn_loop(room, choice, items_inventory, items, rooms):
"""This function runs the 'turn' asking for an action and calling the correct function"""

while True:
if room == -1 and rooms[-1][1] == "1":
print("The room is empty except for a black, gooey puddle.")
else:
print(rooms[room][0])
print("\n")
action = input("What would you like to do? move, use, examine, search or inventory?")
if action == "move":
room, choice = move(room, rooms, choice)
if action == "use":
rooms, items, items_inventory = use(room, rooms, items, items_inventory)
if action == "examine":
examine(items_inventory, items)
if action == "search":
rooms, items_inventory = search(room, rooms, items_inventory)
if action == "inventory":
inventory(items_inventory)
else:
continue


print("You creep down the corridor of the wizards castle. Who knows what awaits. Opening the door ahead, you creep in.")
print("\n")
room, choice, items_inventory, items, rooms = set_variables()
turn_loop(room, choice, items_inventory, items, rooms)