diff --git a/My_code.ipynb b/My_code.ipynb new file mode 100644 index 00000000..e543ea63 --- /dev/null +++ b/My_code.ipynb @@ -0,0 +1,586 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: pyttsx3 in c:\\users\\tg\\anaconda3\\lib\\site-packages (2.90)\n", + "Requirement already satisfied: pywin32; platform_system == \"Windows\" in c:\\users\\tg\\anaconda3\\lib\\site-packages (from pyttsx3) (223)\n", + "Requirement already satisfied: comtypes; platform_system == \"Windows\" in c:\\users\\tg\\anaconda3\\lib\\site-packages (from pyttsx3) (1.1.4)\n", + "Requirement already satisfied: pypiwin32; platform_system == \"Windows\" in c:\\users\\tg\\anaconda3\\lib\\site-packages (from pyttsx3) (223)\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "jupyterlab 2.1.0 has requirement jupyterlab_server>=1.1.0, but you'll have jupyterlab-server 1.0.0 which is incompatible.\n", + "jupyterlab-server 1.0.0 has requirement jsonschema>=3.0.1, but you'll have jsonschema 2.6.0 which is incompatible.\n", + "You are using pip version 10.0.1, however version 20.3b1 is available.\n", + "You should consider upgrading via the 'python -m pip install --upgrade pip' command.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: SpeechRecognition in c:\\users\\tg\\anaconda3\\lib\\site-packages (3.8.1)\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "jupyterlab 2.1.0 has requirement jupyterlab_server>=1.1.0, but you'll have jupyterlab-server 1.0.0 which is incompatible.\n", + "jupyterlab-server 1.0.0 has requirement jsonschema>=3.0.1, but you'll have jsonschema 2.6.0 which is incompatible.\n", + "You are using pip version 10.0.1, however version 20.3b1 is available.\n", + "You should consider upgrading via the 'python -m pip install --upgrade pip' command.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: pygame in c:\\users\\tg\\anaconda3\\lib\\site-packages (2.0.0)\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "jupyterlab 2.1.0 has requirement jupyterlab_server>=1.1.0, but you'll have jupyterlab-server 1.0.0 which is incompatible.\n", + "jupyterlab-server 1.0.0 has requirement jsonschema>=3.0.1, but you'll have jsonschema 2.6.0 which is incompatible.\n", + "You are using pip version 10.0.1, however version 20.3b1 is available.\n", + "You should consider upgrading via the 'python -m pip install --upgrade pip' command.\n" + ] + } + ], + "source": [ + "!pip install pyttsx3\n", + "!pip install SpeechRecognition\n", + "!pip install pygame" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "# define rooms and items\n", + "\n", + "# Furniture Elements\n", + "\n", + "couch = {\n", + " \"name\": \"couch\",\n", + " \"type\": \"furniture\"\n", + "}\n", + "\n", + "piano = {\n", + " \"name\": \"piano\",\n", + " \"type\": \"furniture\"\n", + "}\n", + "\n", + "king_bed = {\n", + " \"name\": \"king bed\",\n", + " \"type\": \"furniture\"\n", + "}\n", + "\n", + "table = {\n", + " \"name\": \"table\",\n", + " \"type\": \"furniture\"\n", + "}\n", + "\n", + "piano = {\n", + " \"name\": \"piano\",\n", + " \"type\": \"furniture\"\n", + "}\n", + "\n", + "# Doors\n", + "\n", + "gate = {\n", + " \"name\": \"gate\",\n", + " \"type\": \"door\"\n", + "}\n", + "\n", + "door = {\n", + " \"name\": \"door\",\n", + " \"type\": \"door\"\n", + "}\n", + "\n", + "exit = {\n", + " \"name\": \"exit\",\n", + " \"type\": \"door\"\n", + "}\n", + "\n", + "# Keys\n", + "\n", + "key_a = {\n", + " \"name\": \"key for gate\",\n", + " \"type\": \"key\",\n", + " \"target\": gate\n", + "}\n", + "\n", + "key_c = {\n", + " \"name\": \"key for door\",\n", + " \"type\": \"key\",\n", + " \"target\": door\n", + "}\n", + "\n", + "key_d = {\n", + " \"name\": \"key for exit\",\n", + " \"type\": \"key\",\n", + " \"target\": exit\n", + "}\n", + "\n", + "#Rooms\n", + " \n", + "game_room = {\n", + " \"name\": \"game room\",\n", + " \"type\": \"room\"\n", + "}\n", + "\n", + "bedroom_1 = {\n", + " \"name\": \"bedroom 1\",\n", + " \"type\": \"room\"\n", + "}\n", + "\n", + "living_room = {\n", + " \"name\": \"living room\",\n", + " \"type\": \"room\"\n", + "}\n", + "\n", + "outside = {\n", + " \"name\": \"outside\"\n", + "}\n", + " \n", + "all_rooms = [game_room, bedroom_1, living_room, outside] # removed bedroom_2\n", + " \n", + "all_doors = [gate, door, exit] # removed access, door for bedroom_2\n", + "\n", + "all_keys = [key_a, key_c, key_d] # removed key_b, door for bedroom_2\n", + " \n", + "# define which items/rooms are related\n", + " \n", + "object_relations = {\n", + " \"game room\": [couch, piano, gate],\n", + " \"bedroom 1\": [king_bed, gate, door], # removed access, door for bedroom_2\n", + " #\"bedroom 2\": [double_bed, dresser, access],\n", + " \"living room\": [table, door, exit],\n", + " \"piano\": [key_a],\n", + " #\"double bed\": [key_c],\n", + " \"king bed\": [key_c], #replaced b with c\n", + " \"table\": [key_d],\n", + " \"outside\": [exit],\n", + " \"gate\": [game_room, bedroom_1],\n", + " #\"access\": [bedroom_1, bedroom_2],\n", + " \"door\": [bedroom_1, living_room],\n", + " \"exit\": [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\": outside\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "###IMPORTAÇÃO DE PACOTES\n", + "import pyttsx3\n", + "import speech_recognition as sr\n", + "import pygame\n", + "from pygame import mixer\n", + "\n", + "###DEFINIR FUNCTIONS PARA SOM DA PORTA\n", + "def doorsound():\n", + " mixer.init()\n", + " a = mixer.Sound('scary_door.wav')\n", + " b = mixer.Sound.play(a)\n", + " while b.get_busy():\n", + " pygame.time.delay(10)\n", + "\n", + "###DEFINIR FUNCTIONS PARA SOM QUANDO CHEGAMOS AO TARGET ROOM=OUTSIDE##### \n", + "def escaped():\n", + " mixer.init()\n", + " a = mixer.Sound('you_win.wav')\n", + " b = mixer.Sound.play(a)\n", + " while b.get_busy():\n", + " pygame.time.delay(10)\n", + "\n", + "###########################################\n", + "#COLORAMA\n", + "from colorama import Fore, Back, Style \n", + "\n", + "###########################################\n", + "\n", + "def linebreak():\n", + " \"\"\"\n", + " Print a line break\n", + " \"\"\"\n", + " print(\"\\n\\n\")\n", + "def start_game():\n", + " \"\"\"\n", + " Start the game\n", + " \"\"\"\n", + "#Text to speech called via engine\n", + " \n", + " engine = pyttsx3.init() \n", + " engine.say(\"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, NOW!\") #Engine receives the text\n", + " engine.runAndWait() \n", + " engine.stop() \n", + " \n", + " print(Fore.RED + \"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, NOW!\")\n", + " \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\"] == game_state[\"target_room\"]):\n", + " print(\"Congrats! You escaped the room!\")\n", + " escaped()\n", + " engine = pyttsx3.init() \n", + " engine.say(\"Congrats! You escaped the room! Mind the gap, there is a cliff\")\n", + " engine.runAndWait()\n", + " engine.stop()\n", + " \n", + " print(\"Wanna play again? Yes or No\")\n", + " r = sr.Recognizer()\n", + " with sr.Microphone() as source:\n", + " engine = pyttsx3.init() \n", + " engine.say(\"Wanna play again? Yes or No\")\n", + " engine.runAndWait()\n", + " engine.stop() \n", + " r.adjust_for_ambient_noise(source)\n", + " data = None\n", + " while data is None: \n", + " try:\n", + " data = r.record(source, duration=3)\n", + " intended_action = r.recognize_google(data,language=\"en-US\")\n", + " intended_action = str(intended_action.replace(\" \", \"\").lower())\n", + " print(intended_action)\n", + " \n", + " except:\n", + " data = None\n", + " print('Please, repeat...')\n", + " engine = pyttsx3.init()\n", + " engine.say('Please, repeat...')\n", + " engine.runAndWait()\n", + " engine.stop()\n", + " \n", + " if intended_action == \"yes\":\n", + " engine = pyttsx3.init()\n", + " engine.say(\"Just kidding, this doesn't work. Did you pay attention to the last slide of Group One presentation?\")\n", + " engine.runAndWait()\n", + " engine.stop()\n", + " exit()\n", + " \n", + " if intended_action == \"no\":\n", + " exit()\n", + " \n", + " else:\n", + " print('Please, repeat...')\n", + " engine = pyttsx3.init()\n", + " engine.say('Please, repeat...')\n", + " engine.runAndWait()\n", + " engine.stop()\n", + " \n", + " else:\n", + " print(\"You are now in \" + room[\"name\"])\n", + " \n", + " #Text to speech called via engine\n", + " engine = pyttsx3.init()\n", + " engine.say(\"You are now in \" + room[\"name\"])\n", + " engine.runAndWait()\n", + " engine.stop() \n", + " \n", + " r = sr.Recognizer()\n", + " with sr.Microphone() as source:\n", + " engine = pyttsx3.init() #Speech to text init with recognizer\n", + " engine.say(\"What would you like to do? See or Go?\") \n", + " r.adjust_for_ambient_noise(source)\n", + " engine.say(\"Speak Now!\")\n", + " engine.runAndWait()\n", + " engine.stop() \n", + " #Speech to text\n", + " data = None\n", + " while data is None:\n", + " try:\n", + " data = r.record(source, duration=3)\n", + " intended_action = r.recognize_google(data,language=\"en-US\")\n", + " intended_action = str(intended_action.replace(\" \", \"\").lower())\n", + " print(intended_action)\n", + " except:\n", + " data = None\n", + " print('Please, repeat...')\n", + " engine = pyttsx3.init()\n", + " engine.say('Please, repeat...')\n", + " engine.runAndWait()\n", + " engine.stop()\n", + " \n", + " if \"see\" == intended_action:\n", + " explore_room(room)\n", + " play_room(room)\n", + " \n", + " elif \"go\" == intended_action: \n", + " r = sr.Recognizer()\n", + " with sr.Microphone() as source:\n", + " engine = pyttsx3.init()\n", + " engine.say(\"Where do you wanna go?\") \n", + " r.adjust_for_ambient_noise(source)\n", + " engine.say(\"Speak Now!\") \n", + " engine.runAndWait()\n", + " engine.stop() \n", + " data = None\n", + " while data is None: \n", + " try:\n", + " data = r.record(source, duration=3)\n", + " intended_action = r.recognize_google(data,language=\"en-US\")\n", + " intended_action = str(intended_action.replace(\" \", \"\").lower())\n", + " print(intended_action)\n", + " \n", + " except:\n", + " data = None\n", + " print('Please, repeat...')\n", + " engine = pyttsx3.init()\n", + " engine.say('Please, repeat...')\n", + " engine.runAndWait()\n", + " engine.stop()\n", + " \n", + " examine_item(intended_action)\n", + " play_room(room)\n", + " \n", + " else:\n", + " print(\"In English Please! Not sure what you mean. Say See or Go.\")\n", + " engine = pyttsx3.init()\n", + " engine.say(\"In English Please! Not sure what you mean. Say See or Go.\")\n", + " engine.runAndWait()\n", + " engine.stop()\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(\"Exploring the \" + room[\"name\"] + \". You find \" + \", \".join(items))\n", + " \n", + " engine = pyttsx3.init()\n", + " engine.say(\"Exploring the \" + room[\"name\"] + \". You find \" + \", \".join(items))\n", + " engine.runAndWait()\n", + " engine.stop()\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):\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", + " for item in object_relations[current_room['name']]:\n", + " if(item[\"name\"].replace(\" \", \"\") == item_name):\n", + " output = \"You examine \" + item_name + \". \"\n", + " \n", + " engine = pyttsx3.init()\n", + " engine.say(\"You examine \" + item_name) \n", + " engine.runAndWait()\n", + " engine.stop()\n", + " \n", + " if(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", + " \n", + " if(have_key):\n", + " output += \"You unlock it with a key you have. \"\n", + " engine = pyttsx3.init()\n", + " engine.say(str(output))\n", + " engine.runAndWait()\n", + " engine.stop()\n", + " next_room = get_next_room_of_door(item, current_room)\n", + " \n", + " else:\n", + " output += \"How silly you are? It's locked, you don't have the key.\"\n", + " engine = pyttsx3.init()\n", + " engine.say(\"How silly you are? It's locked, you don't have the key.\")\n", + " engine.runAndWait()\n", + " engine.stop() \n", + " \n", + " else:\n", + " \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", + " engine = pyttsx3.init()\n", + " engine.say(str(output))\n", + " engine.runAndWait()\n", + " engine.stop()\n", + " \n", + " else:\n", + " output += \" Ah , Ah , Ah! You nailed it! There isn't anything interesting about it.\"\n", + " engine = pyttsx3.init()\n", + " engine.say(str(output))\n", + " engine.runAndWait()\n", + " engine.stop()\n", + " print(output)\n", + " break\n", + " \n", + " if(output is None):\n", + " print(\"Come on, you can do better than this! Please request only items available in the current room.\")\n", + " \n", + " engine = pyttsx3.init()\n", + " engine.say(\"Come on! You can do better than this! Please request only items available in the current room.\")\n", + " engine.runAndWait()\n", + " engine.stop()\n", + " \n", + " if(next_room and have_key == True): \n", + " doorsound()\n", + " play_room(next_room)\n", + " \n", + " else:\n", + " play_room(current_room)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[31mYou 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, NOW!\n", + "You are now in game room\n", + "esternocleidomastoideo\n", + "In English Please! Not sure what you mean. Say See or Go.\n", + "You are now in game room\n", + "see\n", + "Exploring the game room. You find couch, piano, gate\n", + "You are now in game room\n", + "go\n", + "piano\n", + "You examine piano. You find key for gate. \n", + "You are now in game room\n", + "go\n", + "gate\n", + "You examine gate. You unlock it with a key you have. \n", + "You are now in bedroom 1\n", + "see\n", + "Exploring the bedroom 1. You find king bed, gate, door\n", + "You are now in bedroom 1\n", + "go\n", + "kingboo\n", + "Come on, you can do better than this! Please request only items available in the current room.\n", + "You are now in bedroom 1\n", + "kingbedgo\n", + "In English Please! Not sure what you mean. Say See or Go.\n", + "You are now in bedroom 1\n", + "go\n", + "kingboo\n", + "Come on, you can do better than this! Please request only items available in the current room.\n", + "You are now in bedroom 1\n", + "go\n", + "kingbed\n", + "You examine kingbed. You find key for door. \n", + "You are now in bedroom 1\n", + "go\n", + "door\n", + "You examine door. You unlock it with a key you have. \n", + "You are now in living room\n", + "see\n", + "Exploring the living room. You find table, door, exit\n", + "You are now in living room\n", + "go\n", + "table\n", + "You examine table. You find key for exit. \n", + "You are now in living room\n", + "go\n", + "exit\n", + "You examine exit. You unlock it with a key you have. \n", + "Congrats! You escaped the room!\n", + "Wanna play again? Yes or No\n", + "nononono\n", + "You are now in living room\n", + "Please, repeat...\n", + "Please, repeat...\n" + ] + } + ], + "source": [ + "game_state = INIT_GAME_STATE.copy()\n", + "\n", + "start_game()\n" + ] + }, + { + "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.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/escape-room-plan (1).jpg b/escape-room-plan (1).jpg new file mode 100644 index 00000000..05518613 Binary files /dev/null and b/escape-room-plan (1).jpg differ diff --git a/scary_door.wav b/scary_door.wav new file mode 100644 index 00000000..cc2947a9 Binary files /dev/null and b/scary_door.wav differ diff --git a/you_win.wav b/you_win.wav new file mode 100644 index 00000000..c1cafb1b Binary files /dev/null and b/you_win.wav differ diff --git a/your-code/.ipynb_checkpoints/sample-code-checkpoint.ipynb b/your-code/.ipynb_checkpoints/sample-code-checkpoint.ipynb new file mode 100644 index 00000000..ed52e170 --- /dev/null +++ b/your-code/.ipynb_checkpoints/sample-code-checkpoint.ipynb @@ -0,0 +1,248 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# define rooms and items\n", + "\n", + "couch = {\n", + " \"name\": \"couch\",\n", + " \"type\": \"furniture\",\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", + "}\n", + "\n", + "game_room = {\n", + " \"name\": \"game room\",\n", + " \"type\": \"room\",\n", + "}\n", + "\n", + "outside = {\n", + " \"name\": \"outside\"\n", + "}\n", + "\n", + "all_rooms = [game_room, outside]\n", + "\n", + "all_doors = [door_a]\n", + "\n", + "# define which items/rooms are related\n", + "\n", + "object_relations = {\n", + " \"game room\": [couch, piano, door_a],\n", + " \"piano\": [key_a],\n", + " \"outside\": [door_a],\n", + " \"door a\": [game_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\": outside\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, NOW!\")\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\"] == game_state[\"target_room\"]):\n", + " print(\"Congrats! You escaped the room!\")\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()\n", + " if intended_action == \"explore\":\n", + " explore_room(room)\n", + " play_room(room)\n", + " elif intended_action == \"examine\":\n", + " examine_item(input(\"What would you like to examine?\").strip())\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):\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", + " for item in object_relations[current_room[\"name\"]]:\n", + " if(item[\"name\"] == item_name):\n", + " output = \"You examine \" + item_name + \". \"\n", + " if(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", + " 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", + " else:\n", + " output += \"There isn't anything interesting about it.\"\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? Ener 'yes' or 'no'\").strip() == 'yes'):\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, NOW!\n", + "You are now in game room\n", + "What would you like to do? Type 'explore' or 'examine'?explore\n", + "You explore the room. This is game room. You find couch, piano, 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?door a\n", + "You examine door a. It is locked but you don't have the key.\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?piano\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?door a\n", + "You examine door a. You unlock it with a key you have.\n", + "Do you want to go to the next room? Ener 'yes' or 'no'yes\n", + "Congrats! You escaped the room!\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.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/sample-code.ipynb b/your-code/sample-code.ipynb index a6f8a94d..ed52e170 100644 --- a/your-code/sample-code.ipynb +++ b/your-code/sample-code.ipynb @@ -240,7 +240,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.6.5" } }, "nbformat": 4,