diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000000..44f81f8705 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/../../../../../:\CS2\Projects\Intro-Python-II\.idea/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/Intro-Python-II.iml b/.idea/Intro-Python-II.iml new file mode 100644 index 0000000000..d9e6024fc5 --- /dev/null +++ b/.idea/Intro-Python-II.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000000..105ce2da2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000..8d93904d9f --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000..287b09d13f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000..9661ac7134 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/adv.py b/src/adv.py index c9e26b0f85..8cb57699d2 100644 --- a/src/adv.py +++ b/src/adv.py @@ -1,37 +1,37 @@ -from room import Room +# from room import Room # Declare all the rooms - -room = { - 'outside': Room("Outside Cave Entrance", - "North of you, the cave mount beckons"), - - 'foyer': Room("Foyer", """Dim light filters in from the south. Dusty -passages run north and east."""), - - 'overlook': Room("Grand Overlook", """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."""), - - 'narrow': Room("Narrow Passage", """The narrow passage bends here from west -to north. The smell of gold permeates the air."""), - - 'treasure': Room("Treasure Chamber", """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."""), -} +# +# room = { +# 'outside': Room("Outside Cave Entrance", +# "North of you, the cave mount beckons"), +# +# 'foyer': Room("Foyer", """Dim light filters in from the south. Dusty +# passages run north and east."""), +# +# 'overlook': Room("Grand Overlook", """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."""), +# +# 'narrow': Room("Narrow Passage", """The narrow passage bends here from west +# to north. The smell of gold permeates the air."""), +# +# 'treasure': Room("Treasure Chamber", """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."""), +# } # Link rooms together -room['outside'].n_to = room['foyer'] -room['foyer'].s_to = room['outside'] -room['foyer'].n_to = room['overlook'] -room['foyer'].e_to = room['narrow'] -room['overlook'].s_to = room['foyer'] -room['narrow'].w_to = room['foyer'] -room['narrow'].n_to = room['treasure'] -room['treasure'].s_to = room['narrow'] +# room['outside'].n_to = room['foyer'] +# room['foyer'].s_to = room['outside'] +# room['foyer'].n_to = room['overlook'] +# room['foyer'].e_to = room['narrow'] +# room['overlook'].s_to = room['foyer'] +# room['narrow'].w_to = room['foyer'] +# room['narrow'].n_to = room['treasure'] +# room['treasure'].s_to = room['narrow'] # # Main @@ -49,3 +49,154 @@ # Print an error message if the movement isn't allowed. # # If the user enters "q", quit the game. + +from player import Player +from room import Room +from item import Item + +# DECLARE ITEMS +item_hat = Item("Hat", "A very very fancy hat") +item_bat = Item("Bat", "An unusually swole bat") +item_rat = Item("Rate", "This is NOT a bat, there are probably others.") +item_rope = Item("Rope", "For adventuring") +item_pole = Item("Pole", "You cannot adventure without a 10 foot pole") + +# DECLARE ROOMS +room_outside = Room( + "Outside Cave Entrance", + "North of you, the cave mount beckons", + {item_hat: 1}, +) +room_foyer = Room( + "Foyer", + """Dim light filters in from the south. Dusty passages run north and east.""", + {item_rat: 1}, +) +room_overlook = Room( + "Grand Overlook", + """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.""", + {item_rope: 1}, +) +room_narrow = Room( + "Narrow Passage", + """The narrow passage bends here from west to north. The smell of gold permeates the air.""", + {item_bat: 1} +) +room_treasure = Room( + "Treasure Chamber", + """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.""", + {item_pole: 1} +) + +# Link rooms together +room_outside.branches.update({ + "north": room_foyer, +}) +room_foyer.branches.update({ + "north": room_overlook, + "east": room_narrow, + "south": room_outside +}) +room_overlook.branches.update({ + "south": room_foyer +}) +room_narrow.branches.update({ + "south": room_foyer +}) +room_treasure.branches.update({ + "south": room_narrow, +}) + +# Main +if __name__ == '__main__': + + # Make a new player object that is currently in the 'outside' rooms_dict. + name = input("Please input name: ") + player = Player(name, room_outside) + + # Write a loop that: + # + direction = None + cur_room = None + print("You must go find the treasure because of reasons!") + input("Press any key to begin: ") + + # While game is playing + while direction != "q": + # If player has moved + if cur_room != player.current_room: + # Current rooms_dict updates to players new location + cur_room = player.current_room + + # Print player name & location + print(f"{player.name} is in {player.current_room.name}") + + # * Prints the current rooms_dict name + print(f"{player.current_room}") + + # * Waits for user input and decides what to do. + direction = input("What do you do? \nOPTIONS: [n, s, e, w, (a)ction] ").lower() + + # If the user enters a cardinal direction, attempt to move to the rooms_dict there. + if direction in ["n", "s", "e", "w", "q", "a"]: + if direction == "n": + # Update current rooms_dict to northern rooms_dict + cur_room = cur_room.branches["north"] + elif direction == "s": + # Update current rooms_dict to southern rooms_dict + cur_room = cur_room.branches["south"] + elif direction == "e": + # Update current rooms_dict to eastern rooms_dict + cur_room = cur_room.branches["east"] + elif direction == "w": + # Update current rooms_dict to western rooms_dict + cur_room = cur_room.branches["west"] + + elif direction == "a": + action = input("What would you like to do? (s)earch (i)nventory (b)ack") + if action.lower() == "s" and player.current_room.items: + items = cur_room.items + grabbed = [] + + for item in items: + print(f"{player.name} sees {item}") + + decide = input(f"Do you want {item.name}? (y)/(n)? ").lower() + + if decide == "y": + grabbed.append(item) + + elif decide == "n": + print("You don't want that.") + + for item in grabbed: + player.grab_item(item) + player.current_room.remove_item(item) + + # elif action == "s" and player.current_room.items in player.inventory: + # print("There is nothing to see here") + + if action.lower() == "i": + print(f"{player.inventory}") + + elif direction == "q": + quit() + + # If current rooms_dict has None, print a message + if cur_room is None: + print(f"There is nowhere for {player.name} to go.") + elif cur_room == player.current_room: + # If current rooms_dict has not updated, print a message + print("Think about your next move...") + else: + # If current rooms_dict available to update, update rooms_dict and move on + print(f"{player.name} moves with vigor to {cur_room.name}") + player.current_room = cur_room + + # Print an error message if the movement isn't allowed. + else: + print("Direction Not Valid") + + # If the user enters "q", quit the game. diff --git a/src/item.py b/src/item.py new file mode 100644 index 0000000000..92439c0b6c --- /dev/null +++ b/src/item.py @@ -0,0 +1,12 @@ +class Item: + def __init__(self, name, description): + self.name = name + self.description = description + + def __repr__(self): + str_rep = f"""Item({self.name}, {self.description})""" + return str_rep + + def __str__(self): + str_rep = f"{self.name}, {self.description}" + return str_rep \ No newline at end of file diff --git a/src/player.py b/src/player.py index d79a175029..fc3ed47bd0 100644 --- a/src/player.py +++ b/src/player.py @@ -1,2 +1,47 @@ # Write a class to hold player information, e.g. what room they are in # currently. + +from typing import Optional, Dict + +from room import Room +from item import Item + + +class Player: + def __init__(self, + name: str, + current_room: Optional[Room] = None, + inventory: Optional[Dict[Item, int]] = None + ): + self.name = name + self.current_room = current_room + self.inventory = inventory if inventory is not None else {} + + def __str__(self): + return f""" + ----------------- + Player + ----------------- + name: {self.name} + current_room: {self.current_room} + inventory: {self.inventory} + ----------------- + """ + + def grab_item(self, item: Item): + if item not in self.inventory: + self.inventory[item] = 0 + self.inventory[item] += 1 + + # print(f'\nYou now have the {item.name} in your inventory.') + # print(f' * Your current inventory:\n {self.inventory}') + + def drop_item(self, item: Item): + if item.name not in self.inventory: + return + + self.inventory[item] -= 1 + if self.inventory[item] == 0: + del self.inventory[item] + + print(f' * {item} has been removed from your inventory.') \ No newline at end of file diff --git a/src/room.py b/src/room.py index 24c07ad4c8..28750619ab 100644 --- a/src/room.py +++ b/src/room.py @@ -1,2 +1,67 @@ # Implement a class to hold room information. This should have name and -# description attributes. \ No newline at end of file +# description attributes. + +from typing import Dict, Optional +from item import Item + + +class Room: + def __init__(self, name: str, + description: Optional[str] = None, + items: Optional[Dict[Item, int]] = None + ): + self.name = name + self.description = description if description is not None else "No description given!" + self.items = items if items is not None else {} + self.branches = { + "north": None, + "east": None, + "south": None, + "west": None, + } + + def __repr__(self): + return f"Room({self.name}, {self.description}, {self.items})" + + def __str__(self): + n = self.branches["north"] + e = self.branches["east"] + s = self.branches["south"] + w = self.branches["west"] + + items_desc = ", ".join([f"{item}" for item in self.items]) + + return f""" +---------------------- +Room: {self.name} +- Description: {self.description} +- Items: [{items_desc}] +- Branches: + north={n.name if n is not None else "None"}, + east={e.name if e is not None else "None"}, + south={s.name if s is not None else "None"} + west={w.name if w is not None else "None"} + """ + + def add_item(self, item: Item): + if item not in self.items: + self.items[item] = 0 + self.items[item] += 1 + + def remove_item(self, item: Item): + if item not in self.items: + return + self.items[item] -= 1 + if self.items[item] == 0: + del self.items[item] + + def create_branch_map(self, + north: Optional["Room"], + east: Optional["Room"], + south: Optional["Room"], + west: Optional["Room"], + ): + self.branches["north"] = north + self.branches["east"] = east + self.branches["south"] = south + self.branches["west"] = west \ No newline at end of file