diff --git a/your-code/main.ipynb b/your-code/main.ipynb new file mode 100644 index 00000000..081c3b3a --- /dev/null +++ b/your-code/main.ipynb @@ -0,0 +1,532 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# define rooms and items\n", + "\n", + "#game room\n", + "\n", + "couch = {\n", + " \"name\": \"couch\",\n", + " \"type\": \"furniture\",\n", + " \"description\": \"It's just a couch. There isn't anything interesting about it.\"\n", + "}\n", + "\n", + "door_a = {\n", + " \"name\": \"door a\",\n", + " \"type\": \"door\",\n", + "}\n", + "\n", + "key_a = {\n", + " \"name\": \"key for door a\",\n", + " \"type\": \"key\",\n", + " \"target\": door_a,\n", + "}\n", + "\n", + "piano = {\n", + " \"name\": \"piano\",\n", + " \"type\": \"furniture\",\n", + " \"description\": \"It's just a piano. There isn't anything interesting about it.\"\n", + "}\n", + "\n", + "game_room = {\n", + " \"name\": \"game room\",\n", + " \"type\": \"room\",\n", + "}\n", + "\n", + "chess_table = {\n", + " \"name\": \"chess table\",\n", + " \"type\": \"furniture\",\n", + " \"description\": \"It's just a chess table. There is no time to play.\"\n", + "}\n", + "\n", + "#bedroom_1\n", + "\n", + "bedroom_1 = {\n", + " \"name\": \"bedroom 1\",\n", + " \"type\": \"room\",\n", + "}\n", + "\n", + "door_b = {\n", + " \"name\": \"door b\",\n", + " \"type\": \"door\",\n", + "} \n", + "\n", + "key_b = {\n", + " \"name\": \"key for door b\",\n", + " \"type\": \"key\",\n", + " \"target\": door_b,\n", + "}\n", + "\n", + "queen_bed = {\n", + " \"name\": \"queen bed\",\n", + " \"type\": \"furniture\",\n", + " \"description\": \"It's just a large bed. There is no time for a nap.\"\n", + "}\n", + "\n", + "lamp = {\n", + " \"name\": \"lamp\",\n", + " \"type\": \"furniture\",\n", + " \"description\": \"It's just a lamp. There isn't anything interesting about it.\"\n", + "}\n", + "\n", + "\n", + "door_c = {\n", + " \"name\": \"door c\",\n", + " \"type\": \"door\",\n", + "}\n", + "\n", + "#bedroom_2\n", + "\n", + "bedroom_2 = {\n", + " \"name\": \"bedroom 2\",\n", + " \"type\": \"room\",\n", + "}\n", + "\n", + "key_c = {\n", + " \"name\": \"key for door c\",\n", + " \"type\": \"key\",\n", + " \"target\": door_c,\n", + "}\n", + "\n", + "door_d = {\n", + " \"name\": \"door d\",\n", + " \"type\": \"door\",\n", + "}\n", + "\n", + "key_d = {\n", + " \"name\": \"key for door d\",\n", + " \"type\": \"key\",\n", + " \"target\": door_d,\n", + "}\n", + "\n", + "double_bed = {\n", + " \"name\": \"double bed\",\n", + " \"type\": \"furniture\",\n", + " \"description\": \"It's just a bed. There is no time for a nap.\"\n", + "}\n", + "\n", + "dresser = {\n", + " \"name\": \"dresser\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "portrait = {\n", + " \"name\": \"portrait\",\n", + " \"type\": \"furniture\",\n", + " \"description\": \"You found a piece of paper behind it that reads '385'.\"\n", + "}\n", + "\n", + "chest = {\n", + " \"name\": \"chest\",\n", + " \"type\": \"furniture\",\n", + " \"description\": \"The chest is locked with a code.\"\n", + "}\n", + "\n", + "#living_room\n", + "\n", + "living_room = {\n", + " \"name\": \"living room\",\n", + " \"type\": \"room\",\n", + "}\n", + "\n", + "bedroom_2 = {\n", + " \"name\": \"bedroom 2\",\n", + " \"type\": \"room\",\n", + "}\n", + "\n", + "dining_table = {\n", + " \"name\": \"dining table\",\n", + " \"type\": \"furniture\",\n", + " \"description\": \"It's just a table. There isn't anything interesting about it.\"\n", + "}\n", + "\n", + "door_e = {\n", + " \"name\": \"door e\",\n", + " \"type\": \"trap\",\n", + "}\n", + "\n", + "#outside\n", + "\n", + "outside = {\n", + " \"name\": \"outside\"\n", + "}\n", + "\n", + "all_rooms = [game_room, bedroom_1, bedroom_2, living_room, outside]\n", + "\n", + "all_doors = [door_a, door_b, door_c, door_d]\n", + "\n", + "# define which items/rooms are related\n", + "\n", + "object_relations = {\n", + " \"game room\": [couch, piano, door_a, chess_table],\n", + " \"piano\": [key_a],\n", + " \"bedroom 1\": [door_a, door_b, door_c, queen_bed, lamp],\n", + " \"door a\": [game_room, bedroom_1],\n", + " \"queen bed\": [key_b],\n", + " \"door b\": [bedroom_1, bedroom_2],\n", + " \"bedroom 2\": [double_bed, dresser, door_b, chest, portrait],\n", + " \"double bed\": [key_c],\n", + " \"dresser\": [key_d],\n", + " \"living room\": [dining_table, door_d, door_c, door_e],\n", + " \"door c\": [bedroom_1, living_room],\n", + " \"door d\": [living_room, outside]\n", + "}\n", + "\n", + "# define game state. Do not directly change this dict. \n", + "# Instead, when a new game starts, make a copy of this\n", + "# dict and use the copy to store gameplay state. This \n", + "# way you can replay the game multiple times.\n", + "\n", + "INIT_GAME_STATE = {\n", + " \"current_room\": game_room,\n", + " \"keys_collected\": [],\n", + " \"target_room\": bedroom_1\n", + "}\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "def linebreak():\n", + " \"\"\"\n", + " Print a line break\n", + " \"\"\"\n", + " print(\"\\n\\n\")\n", + "\n", + "def start_game():\n", + " \"\"\"\n", + " Start the game\n", + " \"\"\"\n", + " print(\"You wake up on a couch and find yourself in a strange house with no windows which you have never been to before. You don't remember why you are here and what had happened before. You feel some unknown danger is approaching and you must get out of the house. You have 12 turns before your host arrives, examine the items wisely!\")\n", + " play_room(game_state[\"current_room\"])\n", + "\n", + "def play_room(room):\n", + " \"\"\"\n", + " Play a room. First check if the room being played is the target room.\n", + " If it is, the game will end with success. Otherwise, let player either \n", + " explore (list all items in this room) or examine an item found here.\n", + " \"\"\"\n", + " game_state[\"current_room\"] = room\n", + " if(game_state[\"current_room\"] == outside):\n", + " print(\"Congrats! You are free!\")\n", + " else:\n", + " print(\"You are now in \" + room[\"name\"])\n", + " intended_action = input(\"What would you like to do? Type 'explore' or 'examine'?\").strip().lower()\n", + " if intended_action == \"explore\":\n", + " explore_room(room)\n", + " play_room(room)\n", + " elif intended_action == \"examine\":\n", + " # added list of items to examine by the input\n", + " room_var = game_state[\"current_room\"]\n", + " item_found_lst = [item[\"name\"] for item in object_relations[room_var[\"name\"]]]\n", + " examine_item(input(\"What would you like to examine? \" + str(item_found_lst)).strip().lower())\n", + " else:\n", + " print(\"Not sure what you mean. Type 'explore' or 'examine'.\")\n", + " play_room(room)\n", + " linebreak()\n", + "\n", + "def explore_room(room):\n", + " \"\"\"\n", + " Explore a room. List all items belonging to this room.\n", + " \"\"\"\n", + " items = [i[\"name\"] for i in object_relations[room[\"name\"]]]\n", + " print(\"You explore the room. This is \" + room[\"name\"] + \". You find \" + \", \".join(items))\n", + "\n", + "def get_next_room_of_door(door, current_room):\n", + " \"\"\"\n", + " From object_relations, find the two rooms connected to the given door.\n", + " Return the room that is not the current_room.\n", + " \"\"\"\n", + " connected_rooms = object_relations[door[\"name\"]]\n", + " for room in connected_rooms:\n", + " if(not current_room == room):\n", + " return room\n", + "\n", + "def examine_item(item_name, turns=[0]):\n", + " \"\"\"\n", + " Examine an item which can be a door or furniture.\n", + " First make sure the intended item belongs to the current room.\n", + " Then check if the item is a door. Tell player if key hasn't been \n", + " collected yet. Otherwise ask player if they want to go to the next\n", + " room. If the item is not a door, then check if it contains keys.\n", + " Collect the key if found and update the game state. At the end,\n", + " play either the current or the next room depending on the game state\n", + " to keep playing.\n", + " \"\"\"\n", + " current_room = game_state[\"current_room\"]\n", + " next_room = ''\n", + " output = None\n", + " \n", + " ## counts how many turns you have left\n", + " turns[0] += 1\n", + " turns_left = 12 - turns[0]\n", + " if turns_left < 1:\n", + " print(\"Your host has arrived! You're dead! Try again!\")\n", + " \n", + " else:\n", + " print(\"You have {} turns before your host arrive.\".format(turns_left))\n", + " for item in object_relations[current_room[\"name\"]]:\n", + " if(item[\"name\"] == item_name):\n", + " output = \"You examine \" + item_name + \". \"\n", + " \n", + " ## trap door_e\n", + " if(item[\"type\"] == \"trap\"):\n", + " input_trap = input(\"The door is unlocked. Enter at your own risk. Enter 'yes' or 'no'\").strip().lower()\n", + " if input_trap == 'yes':\n", + " print(\"It's a trap door! You're dead! Try again!\")\n", + " return\n", + " \n", + " elif(item[\"type\"] == \"door\"):\n", + " have_key = False\n", + " for key in game_state[\"keys_collected\"]:\n", + " if(key[\"target\"] == item):\n", + " have_key = True\n", + " if(have_key):\n", + " output += \"You unlock it with a key you have.\"\n", + " next_room = get_next_room_of_door(item, current_room)\n", + " else:\n", + " output += \"It is locked but you don't have the key.\"\n", + " \n", + " ##added the chest feature, if code is right, reset turns\n", + " elif item[\"name\"] == \"chest\":\n", + " output += item[\"description\"]\n", + " code = input(\"Enter 3 digit code:\").strip()\n", + " if code == '385':\n", + " print(\"You found a chocolate bar. Win 6 turns.\")\n", + " turns[0] = 0\n", + " print(\"Your host will be late. You have 12 turns left.\")\n", + " else:\n", + " print(\"That is not the code.\")\n", + " \n", + " else:\n", + " if(item[\"name\"] in object_relations and len(object_relations[item[\"name\"]])>0):\n", + " item_found = object_relations[item[\"name\"]].pop()\n", + " game_state[\"keys_collected\"].append(item_found)\n", + " output += \"You find \" + item_found[\"name\"] + \".\"\n", + " \n", + " #added inventory\n", + " inventory = [items[\"name\"] for items in game_state[\"keys_collected\"]]\n", + " print(\"INVENTORY:\", inventory)\n", + " else:\n", + " output += item[\"description\"] #changed descriptions\n", + " print(output)\n", + " break\n", + "\n", + " if(output is None):\n", + " print(\"The item you requested is not found in the current room.\")\n", + "\n", + " if(next_room and input(\"Do you want to go to the next room? Enter 'yes' or 'no'\").strip().lower() == 'yes'):\n", + " game_state[\"target_room\"] = next_room\n", + " play_room(next_room)\n", + " else:\n", + " play_room(current_room)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You wake up on a couch and find yourself in a strange house with no windows which you have never been to before. You don't remember why you are here and what had happened before. You feel some unknown danger is approaching and you must get out of the house. You have 12 turns before your host arrives, examine the items wisely!\n", + "You are now in game room\n", + "What would you like to do? Type 'explore' or 'examine'?examine\n", + "What would you like to examine? ['couch', 'piano', 'door a', 'chess table']piano\n", + "You have 11 turns before your host arrive.\n", + "INVENTORY: ['key for door a']\n", + "You examine piano. You find key for door a.\n", + "You are now in game room\n", + "What would you like to do? Type 'explore' or 'examine'?examine\n", + "What would you like to examine? ['couch', 'piano', 'door a', 'chess table']door a\n", + "You have 10 turns before your host arrive.\n", + "You examine door a. You unlock it with a key you have.\n", + "Do you want to go to the next room? Enter 'yes' or 'no'yes\n", + "You are now in bedroom 1\n", + "What would you like to do? Type 'explore' or 'examine'?examine\n", + "What would you like to examine? ['door a', 'door b', 'door c', 'queen bed', 'lamp']queen bed\n", + "You have 9 turns before your host arrive.\n", + "INVENTORY: ['key for door a', 'key for door b']\n", + "You examine queen bed. You find key for door b.\n", + "You are now in bedroom 1\n", + "What would you like to do? Type 'explore' or 'examine'?examine\n", + "What would you like to examine? ['door a', 'door b', 'door c', 'queen bed', 'lamp']door b\n", + "You have 8 turns before your host arrive.\n", + "You examine door b. You unlock it with a key you have.\n", + "Do you want to go to the next room? Enter 'yes' or 'no'yes\n", + "You are now in bedroom 2\n", + "What would you like to do? Type 'explore' or 'examine'?explore\n", + "You explore the room. This is bedroom 2. You find double bed, dresser, door b, chest, portrait\n", + "You are now in bedroom 2\n", + "What would you like to do? Type 'explore' or 'examine'?examine\n", + "What would you like to examine? ['double bed', 'dresser', 'door b', 'chest', 'portrait']chest\n", + "You have 7 turns before your host arrive.\n", + "Enter 3 digit code:234\n", + "That is not the code.\n", + "You examine chest. The chest is locked with a code.\n", + "You are now in bedroom 2\n", + "What would you like to do? Type 'explore' or 'examine'?examine\n", + "What would you like to examine? ['double bed', 'dresser', 'door b', 'chest', 'portrait']portrait\n", + "You have 6 turns before your host arrive.\n", + "You examine portrait. You found a piece of paper behind it that reads '385'.\n", + "You are now in bedroom 2\n", + "What would you like to do? Type 'explore' or 'examine'?examine\n", + "What would you like to examine? ['double bed', 'dresser', 'door b', 'chest', 'portrait']chest\n", + "You have 5 turns before your host arrive.\n", + "Enter 3 digit code:385\n", + "You found a chocolate bar. Win 6 turns.\n", + "Your host will be late. You have 12 turns left.\n", + "You examine chest. The chest is locked with a code.\n", + "You are now in bedroom 2\n", + "What would you like to do? Type 'explore' or 'examine'?examine\n", + "You have 11 turns before your host arrive.\n", + "The item you requested is not found in the current room.\n", + "You are now in bedroom 2\n", + "What would you like to do? Type 'explore' or 'examine'?examine\n", + "What would you like to examine? ['double bed', 'dresser', 'door b', 'chest', 'portrait']double bed\n", + "You have 10 turns before your host arrive.\n", + "INVENTORY: ['key for door a', 'key for door b', 'key for door c']\n", + "You examine double bed. You find key for door c.\n", + "You are now in bedroom 2\n", + "What would you like to do? Type 'explore' or 'examine'?examine\n", + "What would you like to examine? ['double bed', 'dresser', 'door b', 'chest', 'portrait']double bed\n", + "You have 9 turns before your host arrive.\n", + "You examine double bed. It's just a bed. There is no time for a nap.\n", + "You are now in bedroom 2\n", + "What would you like to do? Type 'explore' or 'examine'?examine dresser\n", + "Not sure what you mean. Type 'explore' or 'examine'.\n", + "You are now in bedroom 2\n", + "What would you like to do? Type 'explore' or 'examine'?examine\n", + "What would you like to examine? ['double bed', 'dresser', 'door b', 'chest', 'portrait']dresser\n", + "You have 8 turns before your host arrive.\n", + "INVENTORY: ['key for door a', 'key for door b', 'key for door c', 'key for door d']\n", + "You examine dresser. You find key for door d.\n", + "You are now in bedroom 2\n", + "What would you like to do? Type 'explore' or 'examine'?examine\n", + "What would you like to examine? ['double bed', 'dresser', 'door b', 'chest', 'portrait']door b\n", + "You have 7 turns before your host arrive.\n", + "You examine door b. You unlock it with a key you have.\n", + "Do you want to go to the next room? Enter 'yes' or 'no'yes\n", + "You are now in bedroom 1\n", + "What would you like to do? Type 'explore' or 'examine'?explore\n", + "You explore the room. This is bedroom 1. You find door a, door b, door c, queen bed, lamp\n", + "You are now in bedroom 1\n", + "What would you like to do? Type 'explore' or 'examine'?examine\n", + "What would you like to examine? ['door a', 'door b', 'door c', 'queen bed', 'lamp']door c\n", + "You have 6 turns before your host arrive.\n", + "You examine door c. You unlock it with a key you have.\n", + "Do you want to go to the next room? Enter 'yes' or 'no'yes\n", + "You are now in living room\n", + "What would you like to do? Type 'explore' or 'examine'?explore\n", + "You explore the room. This is living room. You find dining table, door d, door c, door e\n", + "You are now in living room\n", + "What would you like to do? Type 'explore' or 'examine'?examine\n", + "What would you like to examine? ['dining table', 'door d', 'door c', 'door e']door e\n", + "You have 5 turns before your host arrive.\n", + "The door is unlocked. Enter at your own risk. Enter 'yes' or 'no'yes\n", + "It's a trap door! You're dead! Try again!\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + } + ], + "source": [ + "game_state = INIT_GAME_STATE.copy()\n", + "\n", + "start_game()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}