diff --git a/examples/history.txt b/examples/history.txt index aa178b017c..ca8db41327 100644 --- a/examples/history.txt +++ b/examples/history.txt @@ -1 +1 @@ -3,3,0 \ No newline at end of file +3,7,2 \ No newline at end of file diff --git a/examples/rock_paper_scissors.py b/examples/rock_paper_scissors.py index 4a1899f900..76a5a7c01e 100644 --- a/examples/rock_paper_scissors.py +++ b/examples/rock_paper_scissors.py @@ -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() diff --git a/src/adv.py b/src/adv.py index c9e26b0f85..1e92a8e863 100644 --- a/src/adv.py +++ b/src/adv.py @@ -1,4 +1,6 @@ from room import Room +from player import Player +from item import Item # Declare all the rooms @@ -23,6 +25,17 @@ # Link rooms together +""" +|---------------------| +|overlook - treasure | +| | | | +|foyer - narrow | +| | | +|outside | +|---------------------| + +""" + room['outside'].n_to = room['foyer'] room['foyer'].s_to = room['outside'] @@ -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 @@ -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') diff --git a/src/item.py b/src/item.py new file mode 100644 index 0000000000..fa4ebbb723 --- /dev/null +++ b/src/item.py @@ -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' diff --git a/src/player.py b/src/player.py index d79a175029..45b1f7610f 100644 --- a/src/player.py +++ b/src/player.py @@ -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 diff --git a/src/room.py b/src/room.py index 24c07ad4c8..ba913f69c9 100644 --- a/src/room.py +++ b/src/room.py @@ -1,2 +1,40 @@ # Implement a class to hold room information. This should have name and -# description attributes. \ No newline at end of file +# 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