Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

258 changes: 257 additions & 1 deletion src/adv.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from room import Room
import random

from room import Room
from player import Player
from item import Item
from monster import Monster
# Declare all the rooms

room = {
Expand All @@ -21,7 +25,19 @@
earlier adventurers. The only exit is to the south."""),
}

items = {
'sword': Item('Sword', 'Sharp blade allows the wielder to cut their foes cleanly.'),
'apple': Item('Apple', 'An apple that the is able to be eaten. I wonder if it taste good?'),
'gold': Item('Gold', 'Looks like this is a currency of some type.'),
'shield': Item('Shield', 'A wooden shield that is great for blocking weak attacks')
}

monsters = {
'goblin' : Monster('Goblin', 5, 2, 10),
'orc': Monster('Orc', 10, 10, 20),
'wolf': Monster('Wolf', 10, 10, 10),
'snake': Monster('Snake', 2, 2, 2)
}
# Link rooms together

room['outside'].n_to = room['foyer']
Expand All @@ -33,11 +49,89 @@
room['narrow'].n_to = room['treasure']
room['treasure'].s_to = room['narrow']

# Add items to the rooms
room['outside'].addItem(items['sword'])
room['foyer'].addItem(items['apple'])
room['overlook'].addItem(items['shield'])
room['narrow'].addItem(items['apple'])
room['treasure'].addItem(items['gold'])

# Add monters to rooms

for i, r in room.items():
for m in range(random.randint(1,5)):
ran_num = random.randint(1,100)
if ran_num > 80:
r.monsters.append(monsters['goblin'])
elif ran_num > 50:
r.monsters.append(monsters['orc'])
elif ran_num > 30:
r.monsters.append(monsters['wolf'])
else:
r.monsters.append(monsters['snake'])

#
# Main
#
def changeRoom(direction, current_room):
if direction.lower() == 'north':
if current_room.n_to == '':
print('Invalid direction, please try again. Or type help')
return userInput[0] == 'paths'
else:
player.current_room = current_room.n_to

elif direction.lower() == 'south':
if current_room.s_to == '':
print('Invalid direction, please try again. Or type help')
return userInput[0] == 'paths'
else:
player.current_room = current_room.s_to

elif direction.lower() == 'east':
if current_room.e_to == '':
print('Invalid direction, please try again. Or type help')
return userInput[0] == 'paths'
else:
player.current_room = current_room.e_to

elif direction.lower() == 'west':
if current_room.w_to == '':
print('Invalid direction, please try again. Or type help')
return userInput[0] == 'paths'
else:
player.current_room = current_room.w_to
else:
print('Invalid direction, please try again. Or type help')
return userInput[0] == 'paths'

def getItem(item, room):
if item:
player.addItem(items[f'{item.lower()}'])
room.removeItem(items[f'{item.lower()}'])
else:
print('That item does not exist')

def dropItem(item, room):
isItem = player.removeItem(items[f'{item.lower()}'])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i love that you kept the items as dicts rather than making it an array. Makes lookup time faster. (We will get into this more)

if isItem != False:
room.addItem(items[f'{item.lower()}'])

def userInputField():
user = input('Check available paths? Check for items in current room or Quit\n')
userInputText = lowerCaseString(user.split())

return userInputText

def lowerCaseString(string):
for i, s in enumerate(string):
string[i] = s.lower()
return string

# Make a new player object that is currently in the 'outside' room.
player = Player('Drol', room['outside'])
print(f'Welcome {player.name}!!\nYou are currently in the room: {player.current_room}\nThere is {len(player.current_room.monsters)} monster(s) here. How would you like to proceed?')
userInput = userInputField()

# Write a loop that:
#
Expand All @@ -49,3 +143,165 @@
# Print an error message if the movement isn't allowed.
#
# If the user enters "q", quit the game.

while not userInput[0] == 'quit':
if userInput[0] == 'paths' or userInput[0] == 'check' and userInput[1] == 'paths':
paths = player.current_room.checkPaths()
routes = player.current_room.paths
if len(paths) == 1:
print(f'\nLooks like we can only go {paths[0]}')
print('What would you like to do now?')
print(f'Go {paths[0]}? Check for items in current room or Quit')
newInput = input('')
inputString = lowerCaseString(newInput.split())

if len(inputString) == 2:
if inputString[0] == 'go':
changeRoom(inputString[1], player.current_room)
print(f'\nYou are now in the {player.current_room}\nHow would you like to proceed?')
userInput = userInputField()
elif inputString[0] == 'check' and inputString[1] == 'items':
userInput[0] == 'items'
elif inputString[0] == 'drop':
userInput = inputString
else:
print('Invalid command, please try again. Or type help')
userInput[0] == 'paths'

else:
if inputString[0] == 'items':
userInput[0] = 'items'
elif inputString[0] == 'quit':
userInput[0] = 'quit'
else:
changeRoom(inputString[0], player.current_room)
print(f'\nYou are now in the {player.current_room}\nHow would you like to proceed?')
userInput = userInputField()

else :
print(f'\nLooks like we can go either {player.current_room.printPaths(paths)}')

if len(paths) == 4:
print(f'Go {paths[0]}? Go {paths[1]}? Go {paths[2]}? Go {paths[3]}? Check for items in current room or Quit')
newInput = input('')
inputString = lowerCaseString(newInput.split())

if len(inputString) == 2:
if inputString[0] == 'go':
changeRoom(inputString[1], player.current_room)
print(f'\nYou are now in the {player.current_room}\nHow would you like to proceed?')
userInput = userInputField()
elif inputString[0] == 'check' and inputString[1] == 'items':
userInput[0] == 'items'
elif inputString[0] == 'drop':
userInput = inputString
else:
print('Invalid command, please try again. Or type help')
userInput[0] == 'paths'

else:
if inputString[0] == 'items':
userInput[0] = 'items'
elif inputString[0] == 'quit':
userInput[0] = 'quit'
else:
changeRoom(inputString[0], player.current_room)
print(f'\nYou are now in the {player.current_room}\nHow would you like to proceed?')
userInput = userInputField()

elif len(paths) == 3:
print(f'Go {paths[0]}? Go {paths[1]}? Go {paths[2]}? Check for items in current room or Quit')
newInput = input('')
inputString = lowerCaseString(newInput.split())

if len(inputString) == 2:
if inputString[0] == 'go':
changeRoom(inputString[1], player.current_room)
print(f'\nYou are now in the {player.current_room}\nHow would you like to proceed?')
userInput = userInputField()
elif inputString[0] == 'check' and inputString[1] == 'items':
userInput[0] == 'items'
elif inputString[0] == 'drop':
userInput = inputString
else:
print('Invalid command, please try again. Or type help')
userInput[0] == 'paths'

else:
if inputString[0] == 'items':
userInput[0] = 'items'
elif inputString[0] == 'quit':
userInput[0] = 'quit'
else:
changeRoom(inputString[0], player.current_room)
print(f'\nYou are now in the {player.current_room}\nHow would you like to proceed?')
userInput = userInputField()

elif len(paths) == 2:
print(f'Go {paths[0]}? Go {paths[1]}? Check for items in current room or Quit')
newInput = input('')
inputString = lowerCaseString(newInput.split())

if len(inputString) == 2:
if inputString[0] == 'go':
changeRoom(inputString[1], player.current_room)
print(f'\nYou are now in the {player.current_room}\nHow would you like to proceed?')
userInput = userInputField()
elif inputString[0] == 'check' and inputString[1] == 'items':
userInput[0] == 'items'
elif inputString[0] == 'drop':
userInput = inputString
else:
print('Invalid command, please try again. Or type help')
userInput[0] == 'paths'

else:
if inputString[0] == 'items':
userInput[0] = 'items'
elif inputString[0] == 'quit':
userInput[0] = 'quit'
else:
changeRoom(inputString[0], player.current_room)
print(f'\nYou are now in the {player.current_room}\nHow would you like to proceed?')
userInput = userInputField()

elif userInput[0] == 'items' or userInput[0] == 'check' and userInput[1] == 'items':
if len(player.current_room.items) == 0:
print('There are no items in this room!')
userInput = userInputField()
else:
room_items = player.current_room.getItems()
print(f'Current items in the room:\n{room_items}')
newInput = input('Looks like theirs some items here, what would you like to do?\n')
inputString = lowerCaseString(newInput.split())

if len(inputString) == 2 and inputString[0] == 'get' or inputString[0] == 'take':
getItem(inputString[1], player.current_room)
userInput = userInputField()
elif inputString[0] == 'drop':
userInput = inputString
elif inputString[0] == 'quit':
userInput[0] = 'quit'
else:
print('Command not recognized, please try again')
userInput[0] = 'items'

elif userInput[0] == 'inventory' or userInput[0] == 'check' and userInput[1] == 'inventory':
print(f'Current inventory:\n{player.getInventory()}')
userInput = userInputField()

elif userInput[0] == 'drop':
dropItem(userInput[1], player.current_room)
userInput = userInputField()

elif userInput[0] == 'help':
print('Some command examples you can do are:\ncheck paths\ncheck items\ncheck inventory or inventory\ngo (some direction)')
userInput = userInputField()
elif userInput[0] == 'monsters' or userInput[0] == 'check' and userInput[1] == 'monsters':
print(f'Room Monsters: {player.current_room.printMonsters()}')
userInput = userInputField()
else:
print('Invalid input please try again.')
userInput = userInputField()
#print(f'{player.name}. You are in the {player.current_room} room. What would you like to do now?')
#user = int(input('[1] Check available paths [2] Check for items in current room [5] Quit\n'))
18 changes: 18 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Class to hold item information

class Item:
def __init__(self, name, desc):
self.name = name
self.desc = desc

def __repr__(self):
return ''+self.name+' '+self.desc+''

def __str__(self):
return ''+self.name+' '+self.desc+''

def on_take(self):
print(f'You added the {self.name} to your inventory!')

def on_drop(self):
print(f'You dropped the {self.name} onto the floor')
12 changes: 12 additions & 0 deletions src/monster.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Monster:
def __init__(self, name, attack, defense, health):
self.name = name
self.attack = attack
self.defense = defense
self.health = health

def __repr__(self):
return f'Name: '+self.name+'\nAttack: '+str(self.attack)+'\nDefense: '+str(self.defense)+'\nHealth: '+str(self.health)+''

def __str__(self):
return f'Name: '+self.name+'\nAttack: '+str(self.attack)+'\nDefense: '+str(self.defense)+'\nHealth: '+str(self.health)+''
31 changes: 31 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,33 @@
# Write a class to hold player information, e.g. what room they are in
# currently.

class Player:
def __init__(self, name, current_room):
self.name = name
self.current_room = current_room
self.inventory = []

def __repr__(self):
return ''+self.name+''

def __str__(self):
return ''+self.name+''

def addItem(self, item):
item.on_take()
self.inventory.append(item)

def removeItem(self, item):
if item in self.inventory:
self.inventory.remove(item)
item.on_drop()
else:
print("You don't have that item")
return False

def getInventory(self):
inventory = [str(i.name) for i in self.inventory]

res = ' '.join(inventory)

return res
Loading