|
2 | 2 | "cells": [ |
3 | 3 | { |
4 | 4 | "cell_type": "code", |
5 | | - "execution_count": null, |
| 5 | + "execution_count": 3, |
6 | 6 | "metadata": {}, |
7 | 7 | "outputs": [], |
8 | 8 | "source": [ |
9 | 9 | "# define rooms and items\n", |
| 10 | + "import random\n", |
| 11 | + "\n", |
| 12 | + "weather = {\n", |
| 13 | + " \"sunny\": \"sunglasses\",\n", |
| 14 | + " \"rainy\": \"umbrella\",\n", |
| 15 | + " \"snowing\": \"snow boots\",\n", |
| 16 | + "}\n", |
| 17 | + "\n", |
| 18 | + "sunglasses = {\n", |
| 19 | + " \"name\": \"sunglasses\",\n", |
| 20 | + " \"type\": \"object\",\n", |
| 21 | + "}\n", |
| 22 | + "\n", |
| 23 | + "umbrella = {\n", |
| 24 | + " \"name\": \"umbrella\",\n", |
| 25 | + " \"type\": \"object\",\n", |
| 26 | + "}\n", |
| 27 | + "\n", |
| 28 | + "snow_boots = {\n", |
| 29 | + " \"name\": \"snow boots\",\n", |
| 30 | + " \"type\": \"object\",\n", |
| 31 | + "}\n", |
| 32 | + "\n", |
| 33 | + "coat_hanger = {\n", |
| 34 | + " \"name\": \"coat hanger\",\n", |
| 35 | + " \"type\": \"furniture\"\n", |
| 36 | + "}\n", |
10 | 37 | "\n", |
11 | 38 | "couch = {\n", |
12 | 39 | " \"name\": \"couch\",\n", |
|
126 | 153 | " \"dresser\": [key_d],\n", |
127 | 154 | " \"door a\": [game_room, bedroom_1],\n", |
128 | 155 | " \"door b\": [bedroom_1, bedroom_2],\n", |
129 | | - " \"living room\": [dining_table, door_d, door_c]\n", |
| 156 | + " \"living room\": [dining_table, door_d, door_c, coat_hanger],\n", |
| 157 | + " \"dining table\": [sunglasses],\n", |
| 158 | + " \"couch\": [snow_boots],\n", |
| 159 | + " \"coat_hanger\": [umbrella]\n", |
130 | 160 | "}\n", |
131 | 161 | "\n", |
132 | 162 | "# define game state. Do not directly change this dict. \n", |
|
137 | 167 | "INIT_GAME_STATE = {\n", |
138 | 168 | " \"current_room\": game_room,\n", |
139 | 169 | " \"keys_collected\": [],\n", |
| 170 | + " \"items_collected\": [],\n", |
140 | 171 | " \"target_room\": outside,\n", |
141 | 172 | "}\n", |
142 | 173 | "\n", |
|
231 | 262 | " else:\n", |
232 | 263 | " if(item[\"name\"] in object_relations and len(object_relations[item[\"name\"]])>0):\n", |
233 | 264 | " item_found = object_relations[item[\"name\"]].pop()\n", |
234 | | - " game_state[\"keys_collected\"].append(item_found)\n", |
| 265 | + " if item_found[\"type\"] == \"key\":\n", |
| 266 | + " game_state[\"keys_collected\"].append(item_found)\n", |
| 267 | + " else:\n", |
| 268 | + " game_state[\"items_collected\"].append(item_found)\n", |
235 | 269 | " output += \"You find \" + item_found[\"name\"] + \".\"\n", |
236 | 270 | " else:\n", |
237 | 271 | " output += \"There isn't anything interesting about it.\"\n", |
|
240 | 274 | "\n", |
241 | 275 | " if(output is None):\n", |
242 | 276 | " print(\"The item you requested is not found in the current room.\")\n", |
243 | | - " \n", |
244 | 277 | " if(next_room and input(\"Do you want to go to the next room? Enter 'yes' or 'no': \").strip() == 'yes'):\n", |
245 | | - " #game_state[\"current_room\"] = next_room\n", |
| 278 | + " if(next_room == game_state[\"target_room\"]):\n", |
| 279 | + " item_needed = get_item_needed()\n", |
| 280 | + " if item_needed in game_state[\"items_collected\"]:\n", |
| 281 | + " game_state[\"current_room\"] = game_state[\"target_room\"]\n", |
| 282 | + " play_room(game_state)\n", |
| 283 | + " else:\n", |
| 284 | + " game_state[\"current_room\"] = next_room\n", |
| 285 | + " play_room(game_state)\n", |
| 286 | + " else:\n", |
246 | 287 | " play_room(game_state)\n", |
| 288 | + "\n", |
| 289 | + "def get_item_needed():\n", |
| 290 | + " t = random.randrange(-10, 40, 1)\n", |
| 291 | + " lst = weather.keys()\n", |
| 292 | + " if t<0:\n", |
| 293 | + " print(\"Congrats! You're about to go outside but you open the door and see that it's \" + lst[0] +\".\")\n", |
| 294 | + " print(\"Why not put on some snow boots?\")\n", |
| 295 | + " return weather[0]\n", |
| 296 | + " elif t>20:\n", |
| 297 | + " print(\"Congrats! You're about to go outside but you open the door and see that it's \" + lst[2] +\".\")\n", |
| 298 | + " print(\"Put on some sunglasses.\")\n", |
| 299 | + " return weather[2]\n", |
247 | 300 | " else:\n", |
248 | | - " play_room(game_state)" |
| 301 | + " print(\"Congrats! You're about to go outside but you open the door and see that it's \" + lst[1] +\".\")\n", |
| 302 | + " print(\"Don't forget your umbrella!\")\n", |
| 303 | + " return weather[1]\n" |
249 | 304 | ] |
250 | 305 | }, |
251 | 306 | { |
252 | 307 | "cell_type": "code", |
253 | 308 | "execution_count": null, |
254 | 309 | "metadata": {}, |
255 | | - "outputs": [], |
| 310 | + "outputs": [ |
| 311 | + { |
| 312 | + "name": "stdout", |
| 313 | + "output_type": "stream", |
| 314 | + "text": [ |
| 315 | + "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", |
| 316 | + "You are now in game room\n", |
| 317 | + "What would you like to do? Type 'explore' or 'examine'? examine\n", |
| 318 | + "What would you like to examine? couch\n", |
| 319 | + "You examine couch. You find snow boots.\n", |
| 320 | + "You are now in game room\n", |
| 321 | + "What would you like to do? Type 'explore' or 'examine'? examine\n", |
| 322 | + "What would you like to examine? piano\n", |
| 323 | + "You examine piano. You find key for door a.\n", |
| 324 | + "You are now in game room\n", |
| 325 | + "What would you like to do? Type 'explore' or 'examine'? examine\n", |
| 326 | + "What would you like to examine? door a\n", |
| 327 | + "You examine door a. You unlock it with a key you have.\n", |
| 328 | + "Do you want to go to the next room? Enter 'yes' or 'no': yes\n", |
| 329 | + "You are now in bedroom 1\n", |
| 330 | + "What would you like to do? Type 'explore' or 'examine'? explore\n", |
| 331 | + "You explore the room. This is bedroom 1. You find door a, door b, door c, queen bed\n", |
| 332 | + "You are now in bedroom 1\n", |
| 333 | + "What would you like to do? Type 'explore' or 'examine'? examine\n", |
| 334 | + "What would you like to examine? queen bed\n", |
| 335 | + "You examine queen bed. You find key for door b.\n", |
| 336 | + "You are now in bedroom 1\n", |
| 337 | + "What would you like to do? Type 'explore' or 'examine'? examine\n", |
| 338 | + "What would you like to examine? door b\n", |
| 339 | + "You examine door b. You unlock it with a key you have.\n", |
| 340 | + "Do you want to go to the next room? Enter 'yes' or 'no': yes\n", |
| 341 | + "You are now in bedroom 2\n", |
| 342 | + "What would you like to do? Type 'explore' or 'examine'? explore\n", |
| 343 | + "You explore the room. This is bedroom 2. You find double bed, dresser, door b\n", |
| 344 | + "You are now in bedroom 2\n", |
| 345 | + "What would you like to do? Type 'explore' or 'examine'? examine\n", |
| 346 | + "What would you like to examine? double bed\n", |
| 347 | + "You examine double bed. You find key for door c.\n", |
| 348 | + "You are now in bedroom 2\n", |
| 349 | + "What would you like to do? Type 'explore' or 'examine'? examine\n", |
| 350 | + "What would you like to examine? dresser\n", |
| 351 | + "You examine dresser. You find key for door d.\n", |
| 352 | + "You are now in bedroom 2\n" |
| 353 | + ] |
| 354 | + } |
| 355 | + ], |
256 | 356 | "source": [ |
257 | 357 | "start_game()" |
258 | 358 | ] |
|
0 commit comments