From 00cb4f5c71255db7fc3df1cb457905f8b2fe2632 Mon Sep 17 00:00:00 2001 From: Johnny Ilmo Koo Date: Wed, 9 Sep 2020 11:26:18 +0900 Subject: [PATCH 1/8] Make example work --- examples/history.txt | 2 +- examples/rock_paper_scissors.py | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) 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() From c82405cac195fcdc945d3f165daf96144716171a Mon Sep 17 00:00:00 2001 From: Johnny Ilmo Koo Date: Wed, 9 Sep 2020 11:26:49 +0900 Subject: [PATCH 2/8] Define Room class --- src/adv.py | 13 +++++++++++++ src/room.py | 21 ++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/adv.py b/src/adv.py index c9e26b0f85..6314e86ee8 100644 --- a/src/adv.py +++ b/src/adv.py @@ -23,6 +23,17 @@ # Link rooms together +""" +|---------------------| +|overlook - treasure | +| | | | +|foyer - narrow | +| | | +|outside | +|---------------------| + +""" + room['outside'].n_to = room['foyer'] room['foyer'].s_to = room['outside'] @@ -33,6 +44,8 @@ room['narrow'].n_to = room['treasure'] room['treasure'].s_to = room['narrow'] + + # # Main # diff --git a/src/room.py b/src/room.py index 24c07ad4c8..d8a57115e0 100644 --- a/src/room.py +++ b/src/room.py @@ -1,2 +1,21 @@ # 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, desc): + self.name = name + self.desc = desc + self.n_to = None + self.s_to = None + self.e_to = None + self.w_to = None + + def __str__(self): + return f''' + name: {self.name}\n + desc: {self.desc}\n + n_to: {self.n_to}\n + s_to: {self.s_to}\n + e_to: {self.e_to}\n + w_to: {self.w_to}\n''' \ No newline at end of file From ae8dc0557ae135c7b71446da2bfedf59ae76cc44 Mon Sep 17 00:00:00 2001 From: Johnny Ilmo Koo Date: Wed, 9 Sep 2020 23:37:29 +0900 Subject: [PATCH 3/8] Complete MVP for day 1 --- src/adv.py | 7 +++++++ src/player.py | 12 ++++++++++++ src/room.py | 24 +++++++++++++----------- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/adv.py b/src/adv.py index 6314e86ee8..4b41a9e9a8 100644 --- a/src/adv.py +++ b/src/adv.py @@ -1,4 +1,5 @@ from room import Room +from player import Player # Declare all the rooms @@ -52,6 +53,12 @@ # Make a new player object that is currently in the 'outside' room. +outside = room['outside'] +player = Player('koo', outside) +print(player) +print(room['outside']) +# print(room['outside'].n_to) + # Write a loop that: # # * Prints the current room name diff --git a/src/player.py b/src/player.py index d79a175029..e8557811de 100644 --- a/src/player.py +++ b/src/player.py @@ -1,2 +1,14 @@ # Write a class to hold player information, e.g. what room they are in # currently. + +class Player: + def __init__(self, name, cur_rm): + self.name = name + self.cur_rm = cur_rm + def __str__(self): + return f''' + ===== player ====== + name: {self.name}\n + current room: {self.cur_rm.name}\n + =================== + ''' \ No newline at end of file diff --git a/src/room.py b/src/room.py index d8a57115e0..df13c7c564 100644 --- a/src/room.py +++ b/src/room.py @@ -6,16 +6,18 @@ class Room: def __init__(self, name, desc): self.name = name self.desc = desc - self.n_to = None - self.s_to = None - self.e_to = None - self.w_to = None + # self.n_to = None + # self.s_to = None + # self.e_to = None + # self.w_to = None def __str__(self): - return f''' - name: {self.name}\n - desc: {self.desc}\n - n_to: {self.n_to}\n - s_to: {self.s_to}\n - e_to: {self.e_to}\n - w_to: {self.w_to}\n''' \ No newline at end of file + output = "\n" + output += f"{self.name}\n" + output += f"{self.desc}\n" + output += "Exits to the: " + output += f" [North] {self.n_to.name}" if hasattr(self, "n_to") else "" + output += f" [South] {self.s_to.name}" if hasattr(self, "s_to") else "" + output += f" [East] {self.e_to.name}" if hasattr(self, "e_to") else "" + output += f" [West] {self.w_to.name}" if hasattr(self, "w_to") else "" + return output \ No newline at end of file From 98dae6e2569dc742e9ad46dcc5696242005d90ff Mon Sep 17 00:00:00 2001 From: Johnny Ilmo Koo Date: Thu, 10 Sep 2020 12:20:05 +0900 Subject: [PATCH 4/8] Add move method to player class and prepare init code for adv.py --- src/adv.py | 25 +++++++++++++++++++++---- src/player.py | 27 ++++++++++++++++++++------- src/room.py | 16 ++++++++-------- 3 files changed, 49 insertions(+), 19 deletions(-) diff --git a/src/adv.py b/src/adv.py index 4b41a9e9a8..913203ed42 100644 --- a/src/adv.py +++ b/src/adv.py @@ -53,10 +53,7 @@ # Make a new player object that is currently in the 'outside' room. -outside = room['outside'] -player = Player('koo', outside) -print(player) -print(room['outside']) + # print(room['outside'].n_to) # Write a loop that: @@ -69,3 +66,23 @@ # Print an error message if the movement isn't allowed. # # If the user enters "q", quit the game. + +current_room = room['outside'] +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(player) + print(f'Keyboard Hit#: {i}\n') + i += 1 + + user = input("[n] north [e] east [s] south [w] west [q] Quit\n") + + 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') \ No newline at end of file diff --git a/src/player.py b/src/player.py index e8557811de..fbc8aafce1 100644 --- a/src/player.py +++ b/src/player.py @@ -2,13 +2,26 @@ # currently. class Player: - def __init__(self, name, cur_rm): + def __init__(self, name, current_room): self.name = name - self.cur_rm = cur_rm + self.current_room = current_room def __str__(self): return f''' - ===== player ====== - name: {self.name}\n - current room: {self.cur_rm.name}\n - =================== - ''' \ No newline at end of file +===== 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) \ No newline at end of file diff --git a/src/room.py b/src/room.py index df13c7c564..f4f41ec140 100644 --- a/src/room.py +++ b/src/room.py @@ -3,9 +3,9 @@ class Room: - def __init__(self, name, desc): + def __init__(self, name, description): self.name = name - self.desc = desc + self.description = description # self.n_to = None # self.s_to = None # self.e_to = None @@ -13,11 +13,11 @@ def __init__(self, name, desc): def __str__(self): output = "\n" - output += f"{self.name}\n" - output += f"{self.desc}\n" + output += f"room name: {self.name}\n" + output += f"room description: {self.description}\n\n" output += "Exits to the: " - output += f" [North] {self.n_to.name}" if hasattr(self, "n_to") else "" - output += f" [South] {self.s_to.name}" if hasattr(self, "s_to") else "" - output += f" [East] {self.e_to.name}" if hasattr(self, "e_to") else "" - output += f" [West] {self.w_to.name}" if hasattr(self, "w_to") else "" + 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 \ No newline at end of file From f3037cc083d6c3fd2beb0d3fd9b71f1fc1bd4b22 Mon Sep 17 00:00:00 2001 From: Johnny Ilmo Koo Date: Fri, 11 Sep 2020 11:23:46 +0900 Subject: [PATCH 5/8] Remove warning when quit --- src/adv.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/adv.py b/src/adv.py index 913203ed42..c8fad1da6c 100644 --- a/src/adv.py +++ b/src/adv.py @@ -81,8 +81,9 @@ user = input("[n] north [e] east [s] south [w] west [q] Quit\n") - print('\n ...Loading\n Please Wait\n') - player.move(user) + if(user != 'q'): + 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') \ No newline at end of file From e5882532d432ef8cccebefbc9347e7bfc4e1b4d1 Mon Sep 17 00:00:00 2001 From: Johnny Ilmo Koo Date: Fri, 11 Sep 2020 14:35:53 +0900 Subject: [PATCH 6/8] Update early while loop escape quit logic --- src/adv.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/adv.py b/src/adv.py index c8fad1da6c..ed81c62a02 100644 --- a/src/adv.py +++ b/src/adv.py @@ -81,9 +81,10 @@ user = input("[n] north [e] east [s] south [w] west [q] Quit\n") - if(user != 'q'): - print('\n ...Loading\n Please Wait\n') - player.move(user) + if(user == 'q'): + break + 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') \ No newline at end of file From 65f00d0f718e63a994badd35efd899a2fcaf4bb7 Mon Sep 17 00:00:00 2001 From: Johnny Ilmo Koo Date: Sat, 12 Sep 2020 00:46:09 +0900 Subject: [PATCH 7/8] Add item print out in the room --- src/adv.py | 9 +++++++-- src/item.py | 7 +++++++ src/room.py | 38 +++++++++++++++++++++++++++----------- 3 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 src/item.py diff --git a/src/adv.py b/src/adv.py index ed81c62a02..cbe7c5cf02 100644 --- a/src/adv.py +++ b/src/adv.py @@ -1,5 +1,6 @@ from room import Room from player import Player +from item import Item # Declare all the rooms @@ -46,7 +47,6 @@ room['treasure'].s_to = room['narrow'] - # # Main # @@ -68,12 +68,17 @@ # If the user enters "q", quit the game. current_room = room['outside'] +# current_room.list[Item('sword', 'this is sharp')] +current_room.append_item(Item('sword', 'this is sharp')) +current_room.append_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(player) print(f'Keyboard Hit#: {i}\n') @@ -87,4 +92,4 @@ player.move(user) print('\n'*3, '==== END OF THE ADVENTURE ====\n') -print('Thanks for enjoying the adventure!\nSee you next time!\n') \ No newline at end of file +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/room.py b/src/room.py index f4f41ec140..6839576d5b 100644 --- a/src/room.py +++ b/src/room.py @@ -1,23 +1,39 @@ # Implement a class to hold room information. This should have name and # description attributes. +# * Add the ability to add items to rooms. + +# * The `Room` class should be extended with a `list` that holds the `Item`s +# that are currently in that room. + +# * Add functionality to the main loop that prints out all the items that are +# visible to the player when they are in that room. + class Room: - + def __init__(self, name, description): self.name = name self.description = description - # self.n_to = None - # self.s_to = None - # self.e_to = None - # self.w_to = None - + self.list = [] + def __str__(self): output = "\n" output += f"room name: {self.name}\n" - output += f"room description: {self.description}\n\n" - output += "Exits 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"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 \ No newline at end of file + return output + + def append_item(self, item): + self.list.append(item) + print(f'Item {item.name} added to {self.name}\n') From 3d17b151753fff2f3595c78c83ae97e9a5e06847 Mon Sep 17 00:00:00 2001 From: Johnny Ilmo Koo Date: Sat, 12 Sep 2020 22:00:44 +0900 Subject: [PATCH 8/8] Complete Assignment --- src/adv.py | 41 ++++++++++++++++++++++++++++++++++------- src/player.py | 19 ++++++++++++++++++- src/room.py | 19 ++++++++++--------- 3 files changed, 62 insertions(+), 17 deletions(-) diff --git a/src/adv.py b/src/adv.py index cbe7c5cf02..1e92a8e863 100644 --- a/src/adv.py +++ b/src/adv.py @@ -67,10 +67,17 @@ # # 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.list[Item('sword', 'this is sharp')] -current_room.append_item(Item('sword', 'this is sharp')) -current_room.append_item(Item('boots', 'this is shoes')) +current_room.add_item(Item('sword', 'this is sharp')) +current_room.add_item(Item('boots', 'this is shoes')) player = Player('student', current_room) user = '' @@ -80,16 +87,36 @@ while not user == 'q': print(f'\nPlayer currently in \n{player.current_room}\n') - # print(player) + 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'): + if (user == 'q'): break - print('\n ...Loading\n Please Wait\n') - player.move(user) + + 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/player.py b/src/player.py index fbc8aafce1..45b1f7610f 100644 --- a/src/player.py +++ b/src/player.py @@ -5,6 +5,8 @@ class Player: def __init__(self, name, current_room): self.name = name self.current_room = current_room + self.inventory = [] + def __str__(self): return f''' ===== player ====== @@ -12,6 +14,7 @@ def __str__(self): current room: {self.current_room.name}\n =================== ''' + def move(self, dir): print(f'player tries to move to {dir}') print('|||| opening the door ...||||') @@ -19,9 +22,23 @@ def move(self, dir): 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) \ No newline at end of file + 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 6839576d5b..ba913f69c9 100644 --- a/src/room.py +++ b/src/room.py @@ -1,14 +1,6 @@ # Implement a class to hold room information. This should have name and # description attributes. -# * Add the ability to add items to rooms. - -# * The `Room` class should be extended with a `list` that holds the `Item`s -# that are currently in that room. - -# * Add functionality to the main loop that prints out all the items that are -# visible to the player when they are in that room. - class Room: def __init__(self, name, description): @@ -34,6 +26,15 @@ def __str__(self): output += f"\n[West] {self.w_to.name}" if hasattr(self, "w_to") else "" return output - def append_item(self, item): + 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