Skip to content

Ilmo Koo #503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion examples/history.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3,3,0
3,7,2
10 changes: 7 additions & 3 deletions examples/rock_paper_scissors.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
#import module we need
import random

import os
print(os.getcwd())
#file i/o functions for historical results

path_history = os.getcwd()+"/examples/history.txt"

def load_results():
text_file = open("history.txt", "r")
text_file = open(path_history, "r")
history = text_file.read().split(",")
text_file.close()
return history

def save_results( w, t, l):
text_file = open("history.txt", "w")
text_file = open(path_history, "w")
text_file.write( str(w) + "," + str(t) + "," + str(l))
text_file.close()

Expand Down
71 changes: 71 additions & 0 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from room import Room
from player import Player
from item import Item

# Declare all the rooms

Expand All @@ -23,6 +25,17 @@


# Link rooms together
"""
|---------------------|
|overlook - treasure |
| | | |
|foyer - narrow |
| | |
|outside |
|---------------------|

"""


room['outside'].n_to = room['foyer']
room['foyer'].s_to = room['outside']
Expand All @@ -33,12 +46,16 @@
room['narrow'].n_to = room['treasure']
room['treasure'].s_to = room['narrow']


#
# Main
#

# Make a new player object that is currently in the 'outside' room.


# print(room['outside'].n_to)

# Write a loop that:
#
# * Prints the current room name
Expand All @@ -49,3 +66,57 @@
# Print an error message if the movement isn't allowed.
#
# If the user enters "q", quit the game.

def search_item(name, storage):
print('item name', name)
print('storage', storage)
result = next((item for item in storage if item.name == name), None)
print('item found! in the storage', result)
return result


current_room = room['outside']
current_room.add_item(Item('sword', 'this is sharp'))
current_room.add_item(Item('boots', 'this is shoes'))

player = Player('student', current_room)
user = ''
i = 0
print('\n==== WELCOME TO THE ADVENTURE ====', '\n'*3)

while not user == 'q':

print(f'\nPlayer currently in \n{player.current_room}\n')
print(
f'player currently has following items: {[item.name for item in player.inventory]}')
print(f'Keyboard Hit#: {i}\n')
i += 1

user = input("[n] north [e] east [s] south [w] west [q] Quit\n")

if (user == 'q'):
break

if (len(user) > 1):
user_split = user.split()
verb = user_split[0]
item_name = user_split[1]
if (verb == 'get'):
print(f'player try to get {item_name}!!')
item = search_item(item_name, player.current_room.list)
print('item found!! ', item)
print('item found!! name', item.name)
player.current_room.remove_item(item)
player.get_item(item)
elif (verb == 'drop'):
print(f'player try to drop {item_name}')
item = search_item(item_name, player.inventory)
print('item?', item)
player.drop_item(item)
player.current_room.add_item(item)
else:
print('\n ...Loading\n Please Wait\n')
player.move(user)

print('\n'*3, '==== END OF THE ADVENTURE ====\n')
print('Thanks for enjoying the adventure!\nSee you next time!\n')
7 changes: 7 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Item:
def __init__(self, name, description):
self.name = name
self.description = description

def __str__(self):
return f'Item name: {self.name}\nItem desc: {self.description}\n'
42 changes: 42 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,44 @@
# 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 __str__(self):
return f'''
===== player ======
name: {self.name}\n
current room: {self.current_room.name}\n
===================
'''

def move(self, dir):
print(f'player tries to move to {dir}')
print('|||| opening the door ...||||')
path = f'{dir}_to'
if hasattr(self.current_room, path):
self.current_room = getattr(self.current_room, path)
print(f'player is now in {self.current_room.name}')

else:
print('\n'*2)
print('x-x '*5, ' WARNING ', 'x-x '*5)
print('there is no exit towards given direction')
print('x-x '*5, ' WARNING ', 'x-x '*5)
print('\n'*2)

def get_item(self, item):
self.inventory.append(item)
print(f"{item.name} has been just added to the player's inventory")
return

def drop_item(self, item):
try:
self.inventory.remove(item)
print(f"{item.name} has been just removed from the player's inventory")
except:
print(f"there is no {item.name} in the inventory")
return
40 changes: 39 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,40 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.

class Room:

def __init__(self, name, description):
self.name = name
self.description = description
self.list = []

def __str__(self):
output = "\n"
output += f"room name: {self.name}\n"
output += f"room description: {self.description}\n"
if len(self.list):
output += f"room has the following items:\n"

for idx, item in enumerate(self.list):
output += f"{idx+1}. item name: {item.name}\n item desc: {item.description}\n"
output += "\nExits to the: "
output += f"\n[North] {self.n_to.name}" if hasattr(
self, "n_to") else ""
output += f"\n[South] {self.s_to.name}" if hasattr(
self, "s_to") else ""
output += f"\n[East] {self.e_to.name}" if hasattr(self, "e_to") else ""
output += f"\n[West] {self.w_to.name}" if hasattr(self, "w_to") else ""
return output

def add_item(self, item):
self.list.append(item)
print(f'Item {item.name} added to {self.name}\n')
return

def remove_item(self, item):
try:
self.list.remove(item)
print(f"{item.name} has been removed from {self.name}")
except:
print(f"{item.name} is not in the room.\n")
return