diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..bc67497c Binary files /dev/null and b/.DS_Store differ diff --git a/.ipynb_checkpoints/sample-code-2-checkpoint.ipynb b/.ipynb_checkpoints/sample-code-2-checkpoint.ipynb new file mode 100644 index 00000000..52f1ece0 --- /dev/null +++ b/.ipynb_checkpoints/sample-code-2-checkpoint.ipynb @@ -0,0 +1,373 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "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", + "bedroom_1 = {\n", + " \"name\": \"bedroom 1\",\n", + " \"type\": \"room\",\n", + "}\n", + "\n", + "queen_bed = {\n", + " \"name\": \"queen bed\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "closet = {\n", + " \"name\": \"closet\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "monster = {\n", + " \"name\": \"monster\",\n", + " \"type\": \"furniture\",\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", + "door_c = {\n", + " \"name\": \"door c\",\n", + " \"type\": \"door\",\n", + "}\n", + "\n", + "bedroom_2 = {\n", + " \"name\": \"bedroom 2\",\n", + " \"type\": \"room\",\n", + "}\n", + "\n", + "double_bed = {\n", + " \"name\": \"double bed\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "dresser = {\n", + " \"name\": \"dresser\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "kitchen = {\n", + " \"name\": \"kitchen\",\n", + " \"type\": \"room\",\n", + "}\n", + "\n", + "cup_board = {\n", + " \"name\": \"cup board\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "fridge = {\n", + " \"name\": \"fridge\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "cooker = {\n", + " \"name\": \"cooker\",\n", + " \"type\": \"furniture\",\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", + "living_room = {\n", + " \"name\": \"living room\",\n", + " \"type\": \"room\",\n", + "}\n", + "\n", + "dining_table = {\n", + " \"name\": \"dining table\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "door_c = {\n", + " \"name\": \"door c\",\n", + " \"type\": \"door\",\n", + "}\n", + "\n", + "key_c = {\n", + " \"name\": \"key for door c\",\n", + " \"type\": \"key\",\n", + " \"target\": door_c,\n", + "}\n", + "\n", + "door_e = {\n", + " \"name\": \"door e\",\n", + " \"type\": \"door\",\n", + "}\n", + "\n", + "key_e = {\n", + " \"name\": \"key for door d\",\n", + " \"type\": \"key\",\n", + " \"target\": door_e,\n", + "}\n", + "\n", + "\n", + "\n", + "outside = {\n", + " \"name\": \"outside\"\n", + "}\n", + "\n", + "all_rooms = [game_room, outside,bedroom_1,bedroom_2,kitchen,living_room]\n", + "\n", + "all_doors = [door_a,door_b,door_c,door_d,door_d]\n", + "\n", + "# define which items/rooms are related\n", + "\n", + "object_relations = {\n", + " \"game room\": [couch, piano, door_a],\n", + " \"bedroom 1\": [queen_bed, closet, door_a, door_b, door_c],\n", + " \"bedroom 2\": [double_bed, dresser, door_b],\n", + " \"kitchen\": [cup_board, fridge, cooker, door_c, door_d],\n", + " \"living room\":[dining_table, door_d, door_e],\n", + " \"closet\": [monster],\n", + " \"piano\": [key_a],\n", + " \"queen bed\": [key_b],\n", + " \"double bed\": [key_c],\n", + " \"dresser\": [key_d],\n", + " \"cup board\": [key_e],\n", + " \"outside\": [door_e],\n", + " \"door a\": [game_room, bedroom_1],\n", + " \"door b\": [bedroom_1, bedroom_2],\n", + " \"door c\": [bedroom_1, kitchen],\n", + " \"door d\": [kitchen, living_room],\n", + " \"door e\": [living_room, outside],\n", + "\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", + " \n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "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", + " \n", + "def new_start_game():\n", + "# \"\"\"\n", + "# Start the game\n", + "# \"\"\"\n", + " print(\"There's a monster at the closet, and now you will go back to the Game Room! Don`t be sad, you didn't lose your keys.\")\n", + " INIT_GAME_STATE = {\"current_room\": game_room,\"keys_collected\": [],\"target_room\": outside}\n", + " game_state = INIT_GAME_STATE.copy()\n", + " play_room(game_state[\"current_room\"])\n", + " \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'?\").lower().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?\").lower().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", + " elif(item[\"name\"] == \"closet\"):\n", + " #new_start_game()\n", + " #output = \"\\nThere's a monster at the closet, and now you will go back to the Game Room! Don`t be sad, you didn't lose your keys\"\n", + " return print(\"\\nThere's a monster at the closet, and now you will go back to the Game Room!\") \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? Enter 'yes' or 'no'\").strip() == 'yes'):\n", + " play_room(next_room)\n", + " else:\n", + " play_room(current_room)\n", + "\n", + " \n", + "while True:\n", + " INIT_GAME_STATE = {\"current_room\": game_room,\"keys_collected\": [],\"target_room\": outside}\n", + " game_state = INIT_GAME_STATE.copy() \n", + " start_game()\n", + " object_relations = {\n", + " \"game room\": [couch, piano, door_a],\n", + " \"bedroom 1\": [queen_bed, closet, door_a, door_b, door_c],\n", + " \"bedroom 2\": [double_bed, dresser, door_b],\n", + " \"kitchen\": [cup_board, fridge, cooker, door_c, door_d],\n", + " \"living room\":[dining_table, door_d, door_e],\n", + " \"closet\": [monster],\n", + " \"piano\": [key_a],\n", + " \"queen bed\": [key_b],\n", + " \"double bed\": [key_c],\n", + " \"dresser\": [key_d],\n", + " \"cup board\": [key_e],\n", + " \"outside\": [door_e],\n", + " \"door a\": [game_room, bedroom_1],\n", + " \"door b\": [bedroom_1, bedroom_2],\n", + " \"door c\": [bedroom_1, kitchen],\n", + " \"door d\": [kitchen, living_room],\n", + " \"door e\": [living_room, outside],\n", + "\n", + "}\n", + " INIT_GAME_STATE = {\"current_room\": game_room,\"keys_collected\": [],\"target_room\": outside}\n", + " game_state = INIT_GAME_STATE.copy()\n", + " start_game()\n", + " restart = input(\"New Game: Yes or No?\").lower().strip()\n", + " if restart == \"No\":\n", + " break\n", + " elif restart == \"Yes\":\n", + " continue" + ] + } + ], + "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.8.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/new-escape_room.jpg b/new-escape_room.jpg new file mode 100644 index 00000000..f5675b73 Binary files /dev/null and b/new-escape_room.jpg differ diff --git a/your-code/.DS_Store b/your-code/.DS_Store new file mode 100644 index 00000000..7e4844fa Binary files /dev/null and b/your-code/.DS_Store differ diff --git a/your-code/sample-code.ipynb b/your-code/.ipynb_checkpoints/sample-code-checkpoint.ipynb similarity index 75% rename from your-code/sample-code.ipynb rename to your-code/.ipynb_checkpoints/sample-code-checkpoint.ipynb index a6f8a94d..5069a335 100644 --- a/your-code/sample-code.ipynb +++ b/your-code/.ipynb_checkpoints/sample-code-checkpoint.ipynb @@ -34,21 +34,105 @@ " \"type\": \"room\",\n", "}\n", "\n", + "bedroom_1 = {\n", + " \"name\": \"bedroom 1\",\n", + " \"type\": \"room\",\n", + "}\n", + "\n", + "queen_bed = {\n", + " \"name\": \"queen bed\",\n", + " \"type\": \"furniture\",\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", + "door_c = {\n", + " \"name\": \"door c\",\n", + " \"type\": \"door\",\n", + "}\n", + "\n", + "bedroom_2 = {\n", + " \"name\": \"bedroom 2\",\n", + " \"type\": \"room\",\n", + "}\n", + "\n", + "double_bed = {\n", + " \"name\": \"double bed\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "dresser = {\n", + " \"name\": \"dresser\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "\n", + "living_room = {\n", + " \"name\": \"living room\",\n", + " \"type\": \"room\",\n", + "}\n", + "\n", + "dining_table = {\n", + " \"name\": \"dining table\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "door_c = {\n", + " \"name\": \"door c\",\n", + " \"type\": \"door\",\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", "outside = {\n", " \"name\": \"outside\"\n", "}\n", "\n", - "all_rooms = [game_room, outside]\n", + "all_rooms = [game_room, outside,bedroom_1,bedroom_2,living_room]\n", "\n", - "all_doors = [door_a]\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],\n", + " \"bedroom 1\": [queen_bed, door_a, door_b, door_c],\n", + " \"bedroom 2\": [double_bed, dresser, door_b],\n", + " \"living room\":[dining_table, door_c, door_d],\n", " \"piano\": [key_a],\n", + " \"queen bed\": [key_b],\n", + " \"double bed\": [key_c],\n", + " \"dresser\": [key_d],\n", " \"outside\": [door_a],\n", - " \"door a\": [game_room, outside]\n", + " \"door a\": [game_room, bedroom_1],\n", + " \"door b\": [bedroom_1, bedroom_2],\n", + " \"door c\": [bedroom_1, living_room],\n", + " \"door d\": [living_room, outside],\n", + "\n", "}\n", "\n", "# define game state. Do not directly change this dict. \n", @@ -162,7 +246,7 @@ " 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", + " if(next_room and input(\"Do you want to go to the next room? Enter 'yes' or 'no'\").strip() == 'yes'):\n", " play_room(next_room)\n", " else:\n", " play_room(current_room)" @@ -170,7 +254,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -178,35 +262,7 @@ "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" + "You are now in game room\n" ] } ], @@ -240,7 +296,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.2" } }, "nbformat": 4, diff --git a/your-code/__pycache__/code.cpython-38.pyc b/your-code/__pycache__/code.cpython-38.pyc new file mode 100644 index 00000000..4318079e Binary files /dev/null and b/your-code/__pycache__/code.cpython-38.pyc differ diff --git a/your-code/__pycache__/code.cpython-39.pyc b/your-code/__pycache__/code.cpython-39.pyc new file mode 100644 index 00000000..3e7cabe0 Binary files /dev/null and b/your-code/__pycache__/code.cpython-39.pyc differ diff --git a/your-code/code.py b/your-code/code.py new file mode 100644 index 00000000..dd0192c3 --- /dev/null +++ b/your-code/code.py @@ -0,0 +1,396 @@ +#Game + +#Imports +import random + +# define rooms and items + +couch = { + "name": "couch", + "type": "furniture", +} + +door_a = { + "name": "door a", + "type": "door", +} + +key_a = { + "name": "key for door a", + "type": "key", + "target": door_a, +} + +piano = { + "name": "piano", + "type": "furniture", +} + +game_room = { + "name": "game room", + "type": "room", +} + +bedroom_1 = { + "name": "bedroom 1", + "type": "room", +} + +queen_bed = { + "name": "queen bed", + "type": "furniture", +} + +closet = { + "name": "closet", + "type": "furniture", +} + +monster = { + "name": "monster", + "type": "furniture", +} + +door_b = { + "name": "door b", + "type": "door", +} + +key_b = { + "name": "key for door b", + "type": "key", + "target": door_b, +} + +door_c = { + "name": "door c", + "type": "door", +} + +bedroom_2 = { + "name": "bedroom 2", + "type": "room", +} + +double_bed = { + "name": "double bed", + "type": "furniture", +} + +dresser = { + "name": "dresser", + "type": "furniture", +} + +kitchen = { + "name": "kitchen", + "type": "room", +} + +cup_board = { + "name": "cup board", + "type": "furniture", +} + +fridge = { + "name": "fridge", + "type": "furniture", +} + +cooker = { + "name": "cooker", + "type": "furniture", +} + +door_d = { + "name": "door d", + "type": "door", +} + +key_d = { + "name": "key for door d", + "type": "key", + "target": door_d, +} + +living_room = { + "name": "living room", + "type": "room", +} + +dining_table = { + "name": "dining table", + "type": "furniture", +} + +door_c = { + "name": "door c", + "type": "door", +} + +key_c = { + "name": "key for door c", + "type": "key", + "target": door_c, +} + +door_e = { + "name": "door e", + "type": "door", +} + +key_e = { + "name": "key for door d", + "type": "key", + "target": door_e, +} + +outside = { + "name": "outside" +} + +all_rooms = [game_room, outside,bedroom_1,bedroom_2,kitchen,living_room] + +all_doors = [door_a,door_b,door_c,door_d,door_d] + +# define which items/rooms are related + +object_relations = { + "game room": [couch, piano, door_a], + "bedroom 1": [queen_bed, closet, door_a, door_b, door_c], + "bedroom 2": [double_bed, dresser, door_b], + "kitchen": [cup_board, fridge, cooker, door_c, door_d], + "living room":[dining_table, door_d, door_e], + "closet": [monster], + "piano": [key_a], + "queen bed": [key_b], + "double bed": [key_c], + "dresser": [key_d], + "cup board": [key_e], + "outside": [door_e], + "door a": [game_room, bedroom_1], + "door b": [bedroom_1, bedroom_2], + "door c": [bedroom_1, kitchen], + "door d": [kitchen, living_room], + "door e": [living_room, outside], +} + + +# define game state. Do not directly change this dict. +# Instead, when a new game starts, make a copy of this +# dict and use the copy to store gameplay state. This +# way you can replay the game multiple times. + +INIT_GAME_STATE = { + "current_room": game_room, + "keys_collected": [], + "target_room": outside +} + +#Tic Tac Toe + +#define board +board = [i for i in range(0,9)] +winner=[] + +# Function to print the board +def print_board(): + print(' ', board[0], ' | ', board[1], ' | ', board[2], ' ') + print('-----------') + print(' ', board[3], ' | ', board[4], ' | ', board[5], ' ') + print('-----------') + print(' ', board[6], ' | ', board[7], ' | ', board[8], ' ') + +# Function to check if win condition is satisfied. +def win_check(): + if ((board[0] == "X" and board[4] == "X" and board[8] == "X") or (board[0] == "X" and board[3] == "X" and board[6] == "X") or (board[1] == "X" and board[4] == "X" and board[7] == "X") or (board[2] == "X" and board[5] == "X" and board[8] == "X") or (board[2] == "X" and board[4] == "X" and board[6] == "X") or (board[0] == "X" and board[1] == "X" and board[2] == "X") or (board[3] == "X" and board[4] == "X" and board[5] == "X") or (board[6] == "X" and board[7] == "X" and board[8] == "X")): + print_board() + winner.append("player") + elif ((board[0] == "O" and board[4] == "O" and board[8] == "O") or (board[0] == "O" and board[3] == "O" and board[6] == "O") or (board[1] == "O" and board[4] == "O" and board[7] == "O") or (board[2] == "O" and board[5] == "O" and board[8] == "O") or (board[2] == "O" and board[4] == "O" and board[6] == "O") or (board[0] == "O" and board[1] == "O" and board[2] == "O") or (board[3] == "O" and board[4] == "O" and board[5] == "O") or (board[6] == "O" and board[7] == "O" and board[8] == "O")): + print_board() + winner.append("game") + elif len([i for i in board if i != "X" and i != "O"]) == 0: + print_board() + winner.append("tie") + else: + current_player() + + +# define move +def current_player(): + print_board() + a = board.count("X") + b = board.count("O") + if (int(a) > int(b)): + possible_moves = [i for i in board if i != "X" and i != "O"] + move = random.choice(possible_moves) + monster_move = int(move) + board[monster_move]="O" + win_check() + elif (int(a) == int(b) or int(a) == 0 ): + player_choices = [i for i in board if i != "X" and i != "O" ] + player_move = int(input("Make your move [0-8]: ")) + if player_move not in player_choices: + print("Position not possible!") + else: + board[player_move]="X" + win_check() + else: + print("error here") + +def linebreak(): + """ + Print a line break + """ + print("\n\n") + +def start_game(): + """ + Start the game + """ + 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!") + play_room(game_state["current_room"]) + + +def new_start_game(): +# """ +# Start the game +# """ + print("There's a monster at the closet, and now you will go back to the Game Room! Don`t be sad, you didn't lose your keys.") + INIT_GAME_STATE = {"current_room": game_room,"keys_collected": [],"target_room": outside} + game_state = INIT_GAME_STATE.copy() + play_room(game_state["current_room"]) + +def play_room(room): + """ + Play a room. First check if the room being played is the target room. + If it is, the game will end with success. Otherwise, let player either + explore (list all items in this room) or examine an item found here. + """ + game_state["current_room"] = room + if(game_state["current_room"] == game_state["target_room"]): + print("Congrats! You escaped the room!") + else: + print("You are now in " + room["name"]) + intended_action = input("What would you like to do? Type 'explore' or 'examine'?").lower().strip() + if intended_action == "explore": + explore_room(room) + play_room(room) + elif intended_action == "examine": + examine_item(input("What would you like to examine?").lower().strip()) + else: + print("Not sure what you mean. Type 'explore' or 'examine'.") + play_room(room) + linebreak() + + +def explore_room(room): + """ + Explore a room. List all items belonging to this room. + """ + items = [i["name"] for i in object_relations[room["name"]]] + print("You explore the room. This is " + room["name"] + ". You find " + ", ".join(items)) + +def get_next_room_of_door(door, current_room): + """ + From object_relations, find the two rooms connected to the given door. + Return the room that is not the current_room. + """ + connected_rooms = object_relations[door["name"]] + for room in connected_rooms: + if(not current_room == room): + return room + + +def examine_item(item_name): + """ + Examine an item which can be a door or furniture. + First make sure the intended item belongs to the current room. + Then check if the item is a door. Tell player if key hasn't been + collected yet. Otherwise ask player if they want to go to the next + room. If the item is not a door, then check if it contains keys. + Collect the key if found and update the game state. At the end, + play either the current or the next room depending on the game state + to keep playing. + """ + current_room = game_state["current_room"] + next_room = "" + output = None + for item in object_relations[current_room["name"]]: + if(item["name"] == item_name): + output = "You examine " + item_name + ". " + if(item["type"] == "door"): + have_key = False + for key in game_state["keys_collected"]: + if(key["target"] == item): + have_key = True + if(have_key): + output += "You unlock it with a key you have." + next_room = get_next_room_of_door(item, current_room) + else: + output += "It is locked but you don't have the key." + elif(item["name"] == "closet"): + #new_start_game() + #output = "\nThere's a monster at the closet, and now you will go back to the Game Room! Don`t be sad, you didn't lose your keys" + return print("\nThere's a monster at the closet! You lose and now you will go back to the Game Room!") + elif(item["name"] == "dining table"): + print("The Monster invites you for one last challenge!") + while len(winner) == 0: + current_player() + if len(winner) != 0: + if winner[0] == "player": + print("Congratulations! You defeat the Monster") + elif winner [0] == "game": + print("Nooo! The Monster win! Run!") + elif winner[0] == "tie": + print("It's a Tie!") + else: + if(item["name"] in object_relations and len(object_relations[item["name"]])>0): + item_found = object_relations[item["name"]].pop() + game_state["keys_collected"].append(item_found) + output += "You find " + item_found["name"] + "." + else: + output += "There isn't anything interesting about it." + print(output) + break + + if(output is None): + print("The item you requested is not found in the current room.") + + if(next_room and input("Do you want to go to the next room? Enter 'yes' or 'no'").strip() == 'yes'): + play_room(next_room) + else: + play_room(current_room) + + +while True: + INIT_GAME_STATE = {"current_room": game_room,"keys_collected": [],"target_room": outside} + game_state = INIT_GAME_STATE.copy() + start_game() + object_relations = { + "game room": [couch, piano, door_a], + "bedroom 1": [queen_bed, closet, door_a, door_b, door_c], + "bedroom 2": [double_bed, dresser, door_b], + "kitchen": [cup_board, fridge, cooker, door_c, door_d], + "living room":[dining_table, door_d, door_e], + "closet": [monster], + "piano": [key_a], + "queen bed": [key_b], + "double bed": [key_c], + "dresser": [key_d], + "cup board": [key_e], + "outside": [door_e], + "door a": [game_room, bedroom_1], + "door b": [bedroom_1, bedroom_2], + "door c": [bedroom_1, kitchen], + "door d": [kitchen, living_room], + "door e": [living_room, outside], +} + #INIT_GAME_STATE = {"current_room": game_room,"keys_collected": [],"target_room": outside} + #game_state = INIT_GAME_STATE.copy() + #start_game() + restart = input("New Game: Yes or No?").lower().strip() + if restart == "no": + break + elif restart == "yes": + continue diff --git a/your-code/main.ipynb b/your-code/main.ipynb new file mode 100644 index 00000000..d585f57f --- /dev/null +++ b/your-code/main.ipynb @@ -0,0 +1,460 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "#Game\n", + "\n", + "#Imports\n", + "import random\n", + "\n", + "# 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", + "bedroom_1 = {\n", + " \"name\": \"bedroom 1\",\n", + " \"type\": \"room\",\n", + "}\n", + "\n", + "queen_bed = {\n", + " \"name\": \"queen bed\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "closet = {\n", + " \"name\": \"closet\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "monster = {\n", + " \"name\": \"monster\",\n", + " \"type\": \"furniture\",\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", + "door_c = {\n", + " \"name\": \"door c\",\n", + " \"type\": \"door\",\n", + "}\n", + "\n", + "bedroom_2 = {\n", + " \"name\": \"bedroom 2\",\n", + " \"type\": \"room\",\n", + "}\n", + "\n", + "double_bed = {\n", + " \"name\": \"double bed\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "dresser = {\n", + " \"name\": \"dresser\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "kitchen = {\n", + " \"name\": \"kitchen\",\n", + " \"type\": \"room\",\n", + "}\n", + "\n", + "cup_board = {\n", + " \"name\": \"cup board\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "fridge = {\n", + " \"name\": \"fridge\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "cooker = {\n", + " \"name\": \"cooker\",\n", + " \"type\": \"furniture\",\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", + "living_room = {\n", + " \"name\": \"living room\",\n", + " \"type\": \"room\",\n", + "}\n", + "\n", + "dining_table = {\n", + " \"name\": \"dining table\",\n", + " \"type\": \"furniture\",\n", + "}\n", + "\n", + "door_c = {\n", + " \"name\": \"door c\",\n", + " \"type\": \"door\",\n", + "}\n", + "\n", + "key_c = {\n", + " \"name\": \"key for door c\",\n", + " \"type\": \"key\",\n", + " \"target\": door_c,\n", + "}\n", + "\n", + "door_e = {\n", + " \"name\": \"door e\",\n", + " \"type\": \"door\",\n", + "}\n", + "\n", + "key_e = {\n", + " \"name\": \"key for door d\",\n", + " \"type\": \"key\",\n", + " \"target\": door_e,\n", + "}\n", + "\n", + "\n", + "\n", + "outside = {\n", + " \"name\": \"outside\"\n", + "}\n", + "\n", + "all_rooms = [game_room, outside,bedroom_1,bedroom_2,kitchen,living_room]\n", + "\n", + "all_doors = [door_a,door_b,door_c,door_d,door_d]\n", + "\n", + "# define which items/rooms are related\n", + "\n", + "object_relations = {\n", + " \"game room\": [couch, piano, door_a],\n", + " \"bedroom 1\": [queen_bed, closet, door_a, door_b, door_c],\n", + " \"bedroom 2\": [double_bed, dresser, door_b],\n", + " \"kitchen\": [cup_board, fridge, cooker, door_c, door_d],\n", + " \"living room\":[dining_table, door_d, door_e],\n", + " \"closet\": [monster],\n", + " \"piano\": [key_a],\n", + " \"queen bed\": [key_b],\n", + " \"double bed\": [key_c],\n", + " \"dresser\": [key_d],\n", + " \"cup board\": [key_e],\n", + " \"outside\": [door_e],\n", + " \"door a\": [game_room, bedroom_1],\n", + " \"door b\": [bedroom_1, bedroom_2],\n", + " \"door c\": [bedroom_1, kitchen],\n", + " \"door d\": [kitchen, living_room],\n", + " \"door e\": [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\": outside\n", + " \n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "#Tic Tac Toe\n", + "\n", + "#define board\n", + "board = [i for i in range(0,9)]\n", + "winner=[]\n", + "\n", + "# Function to print the board\n", + "def print_board():\n", + " print(' ', board[0], ' | ', board[1], ' | ', board[2], ' ')\n", + " print('-----------')\n", + " print(' ', board[3], ' | ', board[4], ' | ', board[5], ' ')\n", + " print('-----------')\n", + " print(' ', board[6], ' | ', board[7], ' | ', board[8], ' ')\n", + "\n", + "# Function to check if win condition is satisfied.\n", + "def win_check():\n", + " if ((board[0] == \"X\" and board[4] == \"X\" and board[8] == \"X\") or (board[0] == \"X\" and board[3] == \"X\" and board[6] == \"X\") or (board[1] == \"X\" and board[4] == \"X\" and board[7] == \"X\") or (board[2] == \"X\" and board[5] == \"X\" and board[8] == \"X\") or (board[2] == \"X\" and board[4] == \"X\" and board[6] == \"X\") or (board[0] == \"X\" and board[1] == \"X\" and board[2] == \"X\") or (board[3] == \"X\" and board[4] == \"X\" and board[5] == \"X\") or (board[6] == \"X\" and board[7] == \"X\" and board[8] == \"X\")):\n", + " print_board()\n", + " winner.append(\"player\")\n", + " elif ((board[0] == \"O\" and board[4] == \"O\" and board[8] == \"O\") or (board[0] == \"O\" and board[3] == \"O\" and board[6] == \"O\") or (board[1] == \"O\" and board[4] == \"O\" and board[7] == \"O\") or (board[2] == \"O\" and board[5] == \"O\" and board[8] == \"O\") or (board[2] == \"O\" and board[4] == \"O\" and board[6] == \"O\") or (board[0] == \"O\" and board[1] == \"O\" and board[2] == \"O\") or (board[3] == \"O\" and board[4] == \"O\" and board[5] == \"O\") or (board[6] == \"O\" and board[7] == \"O\" and board[8] == \"O\")):\n", + " print_board()\n", + " winner.append(\"game\")\n", + " elif len([i for i in board if i != \"X\" and i != \"O\"]) == 0:\n", + " print_board()\n", + " winner.append(\"tie\")\n", + " else:\n", + " current_player()\n", + "\n", + "# define move\n", + "def current_player():\n", + " print_board()\n", + " a = board.count(\"X\")\n", + " b = board.count(\"O\")\n", + " if (int(a) > int(b)):\n", + " possible_moves = [i for i in board if i != \"X\" and i != \"O\"]\n", + " move = random.choice(possible_moves)\n", + " monster_move = int(move)\n", + " board[monster_move]=\"O\"\n", + " win_check()\n", + " elif (int(a) == int(b) or int(a) == 0 ):\n", + " player_choices = [i for i in board if i != \"X\" and i != \"O\" ]\n", + " player_move = int(input(\"Make your move [0-8]: \"))\n", + " if player_move not in player_choices:\n", + " print(\"Position not possible!\")\n", + " else:\n", + " board[player_move]=\"X\"\n", + " win_check()\n", + " else:\n", + " print(\"error here\")\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "tags": [] + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "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!\nYou are now in game room\n" + ] + } + ], + "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", + " \n", + "def new_start_game():\n", + "# \"\"\"\n", + "# Start the game\n", + "# \"\"\"\n", + " print(\"There's a monster at the closet, and now you will go back to the Game Room! Don`t be sad, you didn't lose your keys.\")\n", + " INIT_GAME_STATE = {\"current_room\": game_room,\"keys_collected\": [],\"target_room\": outside}\n", + " game_state = INIT_GAME_STATE.copy()\n", + " play_room(game_state[\"current_room\"])\n", + " \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'?\").lower().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?\").lower().strip())\n", + " else:\n", + " print(\"Not sure what you mean. Type 'explore' or 'examine'.\")\n", + " play_room(room)\n", + " linebreak()\n", + "\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", + "\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", + " 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", + " elif(item[\"name\"] == \"closet\"):\n", + " #new_start_game()\n", + " #output = \"\\nThere's a monster at the closet, and now you will go back to the Game Room! Don`t be sad, you didn't lose your keys\"\n", + " return print(\"\\nThere's a monster at the closet! You lose and now you will go back to the Game Room!\")\n", + " elif(item[\"name\"] == \"dining table\"):\n", + " print(\"The Monster invites you for one last challenge!\")\n", + " while len(winner) == 0:\n", + " current_player()\n", + " if len(winner) != 0:\n", + " if winner[0] == \"player\":\n", + " print(\"Congratulations! You defeat the Monster\")\n", + " elif winner [0] == \"game\":\n", + " print(\"Nooo! The Monster win! Run!\")\n", + " elif winner[0] == \"tie\":\n", + " print(\"It's a Tie!\")\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? Enter 'yes' or 'no'\").strip() == 'yes'):\n", + " play_room(next_room)\n", + " else:\n", + " play_room(current_room)\n", + "\n", + " \n", + " \n", + "while True:\n", + " INIT_GAME_STATE = {\"current_room\": game_room,\"keys_collected\": [],\"target_room\": outside}\n", + " game_state = INIT_GAME_STATE.copy() \n", + " start_game()\n", + " object_relations = {\n", + " \"game room\": [couch, piano, door_a],\n", + " \"bedroom 1\": [queen_bed, closet, door_a, door_b, door_c],\n", + " \"bedroom 2\": [double_bed, dresser, door_b],\n", + " \"kitchen\": [cup_board, fridge, cooker, door_c, door_d],\n", + " \"living room\":[dining_table, door_d, door_e],\n", + " \"closet\": [monster],\n", + " \"piano\": [key_a],\n", + " \"queen bed\": [key_b],\n", + " \"double bed\": [key_c],\n", + " \"dresser\": [key_d],\n", + " \"cup board\": [key_e],\n", + " \"outside\": [door_e],\n", + " \"door a\": [game_room, bedroom_1],\n", + " \"door b\": [bedroom_1, bedroom_2],\n", + " \"door c\": [bedroom_1, kitchen],\n", + " \"door d\": [kitchen, living_room],\n", + " \"door e\": [living_room, outside],\n", + "}\n", + " #INIT_GAME_STATE = {\"current_room\": game_room,\"keys_collected\": [],\"target_room\": outside}\n", + " #game_state = INIT_GAME_STATE.copy()\n", + " #start_game()\n", + " restart = input(\"New Game: Yes or No?\").lower().strip()\n", + " if restart == \"no\":\n", + " break\n", + " elif restart == \"yes\":\n", + " continue\n" + ] + } + ], + "metadata": { + "kernelspec": { + "name": "python392jvsc74a57bd07101ece57323ae226d3ee33127f20849298572704d5292ba75a6e287de47f3f2", + "display_name": "Python 3.9.2 64-bit" + }, + "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.9.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} \ No newline at end of file