From 14893814d94648d83c690e689570915fcd2a84e7 Mon Sep 17 00:00:00 2001 From: Young Won Date: Thu, 7 May 2020 07:15:42 -0700 Subject: [PATCH 1/3] first one --- src/adv.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/player.py | 8 ++++++++ src/room.py | 16 +++++++++++++++- 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/src/adv.py b/src/adv.py index c9e26b0f85..634c25330d 100644 --- a/src/adv.py +++ b/src/adv.py @@ -1,4 +1,6 @@ from room import Room +from player import Player +import textwrap # Declare all the rooms @@ -38,6 +40,7 @@ # # Make a new player object that is currently in the 'outside' room. +player = Player('John', 'outside') # Write a loop that: # @@ -49,3 +52,53 @@ # Print an error message if the movement isn't allowed. # # If the user enters "q", quit the game. + +def find_key(value, my_dict): + key_list = list(my_dict.keys()) + value_list = list(my_dict.values()) + return key_list[value_list.index(value)] + +while True: + print('\n','I am in the room {}'.format(player.current_room), '\n') + # text = f'I am in the room of {player.current_room}. I do not know where to go now However. I will explore around to find a room for treasure.' + # print(textwrap.wrap(text)[0]) + user_input = input('Please enter the direction to go (for exit, enter "q"): ') + + if user_input == 'q': + break + # print('user_input',user_input) + if user_input == 'n': + print('\n', 'going north') + if room[player.current_room].s_to != None: + player.current_room = find_key(room[player.current_room].s_to, room) + print('\n',f'now I am in {player.current_room}') + else: + print('\n', 'no room is in the direction, please choose others') + + elif user_input == 's': + print('\n', 'going south') + if room[player.current_room].n_to != None: + player.current_room = find_key(room[player.current_room].n_to, room) + print('\n',f'now I am in {player.current_room}') + else: + print('\n', 'no room is in the direction, please choose others') + + elif user_input == 'e': + print('\n', 'going east') + if room[player.current_room].w_to != None: + player.current_room = find_key(room[player.current_room].w_to, room) + print('\n',f'now I am in {player.current_room}') + else: + print('\n', 'no room is in the direction, please choose others') + + elif user_input == 'w': + print('\n', 'going west') + if room[player.current_room].e_to != None: + player.current_room = find_key(room[player.current_room].e_to, room) + print('\n',f'now I am in {player.current_room}') + else: + print('\n', 'no room is in the direction, please choose others') + else: + print('Wrong key is entered, please try again') + + diff --git a/src/player.py b/src/player.py index d79a175029..fb1b4ddde9 100644 --- a/src/player.py +++ b/src/player.py @@ -1,2 +1,10 @@ # 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 + + + def __str__(self): + return f"" \ No newline at end of file diff --git a/src/room.py b/src/room.py index 24c07ad4c8..786dafe24b 100644 --- a/src/room.py +++ b/src/room.py @@ -1,2 +1,16 @@ # 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, n_to=None, e_to=None, s_to=None, w_to=None): + self.name = name + self.description = description + self.n_to = n_to + self.e_to = e_to + self.s_to = s_to + self.w_to = w_to + + def __str__(self): + return f"" + + \ No newline at end of file From 1cb86c57607b660e3c7fdad7d8ea4b615a5b4e49 Mon Sep 17 00:00:00 2001 From: Young Won Date: Thu, 7 May 2020 16:04:28 -0700 Subject: [PATCH 2/3] day2 --- src/adv.py | 39 +++++++++++++++++++++++++++------------ src/item.py | 5 +++++ src/player.py | 11 +++++++++-- src/room.py | 14 ++++++++++---- 4 files changed, 51 insertions(+), 18 deletions(-) create mode 100644 src/item.py diff --git a/src/adv.py b/src/adv.py index 634c25330d..76130f5767 100644 --- a/src/adv.py +++ b/src/adv.py @@ -1,26 +1,33 @@ from room import Room from player import Player +from item import Item import textwrap # Declare all the rooms +# knife = Item('knife', 'small knife') +# map = Item('map', 'map for navigation to room') +# sword = Item('sword', ' long sward for battle') +# bike = Item('bike', 'bike for fast navigation') +# shoe = Item('shoe', 'magic shoe') + room = { - 'outside': Room("Outside Cave Entrance", - "North of you, the cave mount beckons"), + 'outside': Room(name="Outside Cave Entrance", + description="North of you, the cave mount beckons", items=['knife1', 'map1']), - 'foyer': Room("Foyer", """Dim light filters in from the south. Dusty -passages run north and east."""), + 'foyer': Room(name="Foyer", description="""Dim light filters in from the south. Dusty +passages run north and east.""", items=['sword1', 'magic_shoe1']), - 'overlook': Room("Grand Overlook", """A steep cliff appears before you, falling + 'overlook': Room(name="Grand Overlook", description="""A steep cliff appears before you, falling into the darkness. Ahead to the north, a light flickers in -the distance, but there is no way across the chasm."""), +the distance, but there is no way across the chasm.""", items=['power_booster', 'magic_mirror']), - 'narrow': Room("Narrow Passage", """The narrow passage bends here from west -to north. The smell of gold permeates the air."""), + 'narrow': Room(name="Narrow Passage", description="""The narrow passage bends here from west +to north. The smell of gold permeates the air.""", items=['map2', 'lightening_wand']), - 'treasure': Room("Treasure Chamber", """You've found the long-lost treasure + 'treasure': Room(name="Treasure Chamber", description="""You've found the long-lost treasure chamber! Sadly, it has already been completely emptied by -earlier adventurers. The only exit is to the south."""), +earlier adventurers. The only exit is to the south.""", items=['majic_key', 'sword2']), } @@ -42,6 +49,12 @@ # Make a new player object that is currently in the 'outside' room. player = Player('John', 'outside') +knife = Item('knife', 'small knife') +map = Item('map', 'map for navigation to room') +sword = Item('sword', ' long sward for battle') +bike = Item('bike', 'bike for fast navigation') +shoe = Item('shoe', 'magic shoe') + # Write a loop that: # # * Prints the current room name @@ -59,11 +72,12 @@ def find_key(value, my_dict): return key_list[value_list.index(value)] while True: - print('\n','I am in the room {}'.format(player.current_room), '\n') + print('\n','I am in the room called {}'.format(player.current_room), '\n') + room[player.current_room].get_items() # text = f'I am in the room of {player.current_room}. I do not know where to go now However. I will explore around to find a room for treasure.' # print(textwrap.wrap(text)[0]) user_input = input('Please enter the direction to go (for exit, enter "q"): ') - + if user_input == 'q': break # print('user_input',user_input) @@ -72,6 +86,7 @@ def find_key(value, my_dict): if room[player.current_room].s_to != None: player.current_room = find_key(room[player.current_room].s_to, room) print('\n',f'now I am in {player.current_room}') + room[player.current_room].get_items() else: print('\n', 'no room is in the direction, please choose others') diff --git a/src/item.py b/src/item.py new file mode 100644 index 0000000000..5ab1036f73 --- /dev/null +++ b/src/item.py @@ -0,0 +1,5 @@ + +class Item: + def __init__(self, name, description=""): + self.name = name + self.desciption = description diff --git a/src/player.py b/src/player.py index fb1b4ddde9..8a8af53754 100644 --- a/src/player.py +++ b/src/player.py @@ -1,10 +1,17 @@ # Write a class to hold player information, e.g. what room they are in # currently. class Player: - def __init__(self, name, current_room): + def __init__(self, name, current_room, inventory=[]): self.name = name self.current_room = current_room + self.inventory = inventory def __str__(self): - return f"" \ No newline at end of file + return f"" + + def pickup_items(*items): + print('Player picked up items...') + for item in items: + self.inventory.append(item) + print(f'{item}') \ No newline at end of file diff --git a/src/room.py b/src/room.py index 786dafe24b..df7b571ba3 100644 --- a/src/room.py +++ b/src/room.py @@ -1,16 +1,22 @@ # Implement a class to hold room information. This should have name and # description attributes. +from item import Item class Room: - def __init__(self, name, description, n_to=None, e_to=None, s_to=None, w_to=None): + def __init__(self, name, description, items=[], n_to=None, e_to=None, s_to=None, w_to=None): self.name = name self.description = description self.n_to = n_to self.e_to = e_to self.s_to = s_to self.w_to = w_to - + self.items = [Item(p) for p in items] def __str__(self): - return f"" + return f"\n" - \ No newline at end of file + def get_items(self): + print(f'{self.name} room has items as follows...') + for item in self.items: + print(f'{item.name}') + print('\n') + \ No newline at end of file From e9cda31b0ec0296186928418bd1cd4b8b61ff310 Mon Sep 17 00:00:00 2001 From: Young Won Date: Thu, 7 May 2020 19:01:22 -0700 Subject: [PATCH 3/3] 2nd push --- src/adv.py | 22 ++++++++++++++++------ src/player.py | 9 ++++++--- src/room.py | 6 +++++- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/adv.py b/src/adv.py index 76130f5767..597fcd1447 100644 --- a/src/adv.py +++ b/src/adv.py @@ -49,11 +49,11 @@ # Make a new player object that is currently in the 'outside' room. player = Player('John', 'outside') -knife = Item('knife', 'small knife') -map = Item('map', 'map for navigation to room') -sword = Item('sword', ' long sward for battle') -bike = Item('bike', 'bike for fast navigation') -shoe = Item('shoe', 'magic shoe') +# knife = Item('knife', 'small knife') +# map = Item('map', 'map for navigation to room') +# sword = Item('sword', ' long sward for battle') +# bike = Item('bike', 'bike for fast navigation') +# shoe = Item('shoe', 'magic shoe') # Write a loop that: # @@ -74,6 +74,17 @@ def find_key(value, my_dict): while True: print('\n','I am in the room called {}'.format(player.current_room), '\n') room[player.current_room].get_items() + user_item = input('Please enter "take item1 item2" or drop item": ') + item_arr = user_item.split(' ') + + action = item_arr[0] + items = item_arr[1:] + print('item_arr', items) + if item_arr[0] == 'take': + player.pickup_items(*items) + for item in items: + room[player.current_room].delete_items(item) + # text = f'I am in the room of {player.current_room}. I do not know where to go now However. I will explore around to find a room for treasure.' # print(textwrap.wrap(text)[0]) user_input = input('Please enter the direction to go (for exit, enter "q"): ') @@ -86,7 +97,6 @@ def find_key(value, my_dict): if room[player.current_room].s_to != None: player.current_room = find_key(room[player.current_room].s_to, room) print('\n',f'now I am in {player.current_room}') - room[player.current_room].get_items() else: print('\n', 'no room is in the direction, please choose others') diff --git a/src/player.py b/src/player.py index 8a8af53754..087a021e5d 100644 --- a/src/player.py +++ b/src/player.py @@ -10,8 +10,11 @@ def __init__(self, name, current_room, inventory=[]): def __str__(self): return f"" - def pickup_items(*items): - print('Player picked up items...') + def pickup_items(self, *items): + print('\nPlayer picked up items...\n') + for item in items: self.inventory.append(item) - print(f'{item}') \ No newline at end of file + print(f'{item}') + print('\n') + print('self.inventory', self.inventory) \ No newline at end of file diff --git a/src/room.py b/src/room.py index df7b571ba3..beaf55aef8 100644 --- a/src/room.py +++ b/src/room.py @@ -19,4 +19,8 @@ def get_items(self): for item in self.items: print(f'{item.name}') print('\n') - \ No newline at end of file + + def delete_items(self, item): + + self.items = [item1 for item1 in self.items if item1['name'] != item] + print(f'item deleted in the room: {item}') \ No newline at end of file