Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Game WIP RM #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
394 changes: 394 additions & 0 deletions .ipynb_checkpoints/lab-python-flow-control-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,394 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "d3bfc191-8885-42ee-b0a0-bbab867c6f9f",
"metadata": {
"tags": []
},
"source": [
"# Lab | Flow Control"
]
},
{
"cell_type": "markdown",
"id": "303cceb0-f898-4c5f-99bd-c04e131e1ded",
"metadata": {},
"source": [
"Objective: Practice how to use programming constructs like if/else statements and loops to control the flow of a program's execution."
]
},
{
"cell_type": "markdown",
"id": "42fd260a-9eb2-4c39-9989-f2c8c70552ec",
"metadata": {},
"source": [
"## Challenge: The Haunted Mansion"
]
},
{
"cell_type": "markdown",
"id": "7086f8f7-ded2-48e5-b966-7ec44fb57ca5",
"metadata": {},
"source": [
"You are a brave adventurer who has decided to explore the Haunted Mansion, a decrepit old building that is rumored to be haunted by ghosts and spirits. Your objective is to find the treasure that is hidden somewhere in the mansion.\n",
"\n",
"## Requirements\n",
"\n",
"- Your script should have at least two functions: \"run_mansion()\" and \"encounter_ghost()\".\n",
"- Your script should use if-else statements, while loops, for loops, combined loops, or nested loops to control the flow of execution.\n",
"- Your script should prompt the user for input to make decisions on which path to take or what actions to perform.\n",
"- Your script should include random events and obstacles that can either help or hinder the adventurer in their quest.\n",
"- Your script should have an objective of finding the treasure at the end of the mansion.\n",
"\n",
"## Instructions\n",
"\n",
"- Begin by creating a list of items that the adventurer can pick up along the way. These items will be used to help the adventurer overcome obstacles and defeat ghosts. Examples of items can be weapons, potions, keys, etc.\n",
"\n",
"- Complete the function called \"run_mansion()\" that serves as the main function for the game. Within \"run_mansion()\", prompt the user to choose a path to take at each intersection. Each path should have its unique challenges and obstacles that the adventurer must overcome.\n",
"\n",
"- Use loops to check if the adventurer has enough health points to continue the game. If their health points drop to zero, the game is over.\n",
"\n",
"- Complete the function called \"encounter_ghost()\" that will be used to handle ghost encounters. The function should use random events to determine the outcome of the encounter, and the adventurer should use their items to help them defeat the ghost.\n",
"\n",
"- Use loops to generate random events or items along the way. These events can either help or hinder the adventurer, and the outcome should be based on random chance.\n",
"\n",
"- At the end of the mansion, the adventurer will find the treasure, and the game will end.\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "8846d61d-cf9d-4ad4-bbb8-1ecb3bb22005",
"metadata": {},
"source": [
"*Introduction to Functions*:\n",
"\n",
" Functions are blocks of code that perform a specific task. They are used to break up complex code into smaller, more manageable parts, which can make your code easier to read and understand. Functions can also be reused multiple times throughout your code, which can save you a lot of time and effort.\n",
"\n",
" Functions are defined using the def keyword, followed by the name of the function and a set of parentheses. Inside the parentheses, you can list any arguments that the function needs in order to perform its task. These arguments are optional, but they can be useful if you need to pass data into the function from outside.\n",
"\n",
" Once you have defined a function, you can call it from anywhere in your code using its name and passing any necessary arguments. When the function is called, the code inside it is executed, and any values that it returns are passed back to the calling code.\n",
"\n",
" In this exercise, we have defined a function called encounter_ghost that simulates a battle between the adventurer and a ghost, and run_mansion. Your task is to complete these functions by using flow control statements such as if, else, while, for loops, and nested loops. Remember to pay attention to the instructions and comments provided in the function to help guide you. Once you have completed the function encounter_ghost, you can call it from the main code to simulate the battle and test your implementation.\n",
"\n",
" Good luck!"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "685dc566-9f7c-43ff-92e5-f6ce9216de8d",
"metadata": {},
"outputs": [],
"source": [
"import random, copy, sys"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "8b8e8c3e-dbfd-4405-8bed-d03ef095b130",
"metadata": {},
"outputs": [],
"source": [
"INIT_GAME_STATE = {\n",
" \"mHP\": 10,\n",
" \"cHP\": 10, \n",
" \"inventory\": [],\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 137,
"id": "499552c8-9e30-46e1-a706-4ac5dc64670e",
"metadata": {},
"outputs": [],
"source": [
"def encounter_ghost(game_state):\n",
" \"\"\"\n",
" This function handles the encounter with a ghost. \n",
" The outcome of the battle is determined by a random number between 1 and 10.\n",
" If the random number is less than or equal to 5, the adventurer defeats the ghost. In this case, print the message \"You defeated the ghost!\" \n",
" and return something that indicates the adventurer defeated the ghost.\n",
" If the random number is greater than 5, the adventurer loses the battle. In this case, print \"You lost the battle...\"\n",
" and return something that indicates the ghost defeated the adventurer.\n",
" \"\"\"\n",
" inventory = game_state[\"inventory\"]\n",
" cck = True\n",
" while cck == True:\n",
" choice = input(\"You encounter a ghost! Will you run or will you fight?: \")\n",
" if choice == \"run\":\n",
" print(\"You start to run away but soon realize the ghost outpaces you. You have no choice but to turn around and face the ghost!\")\n",
" injured = fight_ghost()\n",
" if injured == True:\n",
" hp_check(game_state, modifier=-2)\n",
" else:\n",
" print(\"Searching the remains you find a Gold key!\")\n",
" inventory.append(\"Gold Key\")\n",
" break\n",
" elif choice == \"fight\":\n",
" print(\"You prepare to fight. Will you use any items?\")\n",
" cck = False\n",
" (minrange, maxrange) = inventory_use(inventory)\n",
" injured = fight_ghost(minrange, maxrange)\n",
" if injured == True:\n",
" hp_check(game_state, modifier=-2)\n",
" else:\n",
" inventory.append(\"Gold Key\")\n",
" break\n",
" else:\n",
" print(\"That option is unsupported, please try again\")\n",
"\n",
"def fight_ghost(minrange=0, maxrange=10):\n",
" \"\"\"\n",
" This function handles the ghost encounter. Player can use items to buff their odds.\n",
" \"\"\"\n",
" outcome = random.randint(minrange, maxrange)\n",
" if outcome > 5:\n",
" print(\"Congratulations, you defeated the ghost!\")\n",
" return 0\n",
" else:\n",
" print(\"The ghost outmaneuvers you and hits you! You lose 2 HP! After the hit the ghost loses interest and goes away!\")\n",
" return 1\n",
"\n",
"def inventory_use(inventory):\n",
" \"\"\"\n",
" This function allows players to use items from inventory to buff the odds of dealing with challenges. WIP atm.\n",
" \"\"\"\n",
" minrange=0\n",
" maxrange=10\n",
" return minrange, maxrange\n",
"\n",
"def left_option(state):\n",
" \"\"\"\n",
" Defines what happens when you go left\n",
" If they choose \"left\", a random event occurs. There is a 50% chance that the adventurer will find a potion and a 50% chance that they will \n",
" fall into a trap and lose 2 health points. If they find the potion, it is saved into the adventurer's items. \n",
" If they fall into a trap, 2 points are taken out of the adventurer's health points.\n",
" \"\"\"\n",
" print(\"As you turn left, you see a long dark corridor! You wonder what riches lie in those walls.\")\n",
" revent = random.randint(1,2)\n",
" if revent == 1:\n",
" print(\"\\nAs you wander through the wall, you find a strange red flask on top of a dresser. As you pick it up, you realize it's an HP potion!\\n\")\n",
" state[\"inventory\"].append(\"HP_potion\")\n",
" return 0\n",
" else:\n",
" print(\"\\nAs you walk amazed by the artworks on the walls you fail to see a trap! You get injured and lost 2 Hp!\\n\")\n",
" return -2\n",
" print(\"\\nAs you find the end of the corridor, you find nothing else interesting so you move back to the intersection!\\n\")\n",
" \n",
"def right_option(game_state):\n",
" \"\"\"\n",
" Defines right option\n",
" If they choose \"right\", the \"encounter_ghost()\" function is called to handle the battle between the adventurer and the ghost. \n",
" If the adventurer wins, they find a key which is saved into the adventurer's items. If they lose, they lose 2 health points.\n",
" Hint: remember to check what encounter_ghost() returned to make know if they won or lost.\n",
" \"\"\"\n",
" print(\"\\nAs you turn right, you see the corridor makes a sharp left turn. As you make the turn,\", \n",
" \"you see all the torches lighting the walls going out in smoke!\\n\")\n",
" encounter_ghost(game_state)\n",
" print(\"\\nAs you find the end of the corridor, you find nothing else interesting so you move back to the intersection!\\n\")\n",
" \n",
"\n",
"def door_check(game_state):\n",
" \"\"\"\n",
" Defines door option\n",
" If the adventurer has the key, they can unlock the door and find the Treasure. This message is printed \"You unlocked the door and found the \n",
" Treasure! Congratulations!\".\n",
" If they don't have the key, they are prompted to find it from the beginning.\n",
" \"\"\"\n",
" if 'Gold Key' in game_state[\"inventory\"]:\n",
" print(\"You unlocked the door and found the Treasure! Congratulations!\")\n",
" return 1\n",
" else:\n",
" return 0 #set 1 if key is present, 0 if its not\n",
"\n",
"def hp_check(state, modifier=0):\n",
" \"\"\"\n",
" If the adventurer's health points reach 0 or less, the message \"Game over, you lost all your health points.\" is printed.\n",
" \"\"\"\n",
" mHP = state[\"mHP\"]\n",
" cHP = state[\"cHP\"]\n",
" \n",
" cHP = cHP + modifier\n",
"\n",
" state[\"cHP\"]= cHP\n",
" \n",
" if cHP <= 0:\n",
" return 1\n",
" else:\n",
" return 0\n",
"\n",
"def hp_print(game_state):\n",
" mHP = game_state[\"mHP\"]\n",
" cHP = game_state[\"cHP\"]\n",
" print(f\"\\nCurrent HP: {cHP}/{mHP}\\n\\n\")\n",
"\n",
"def run_main(game_state):\n",
" poption = False\n",
" while poption == False:\n",
" if bool(hp_check(game_state)) == True:\n",
" hp_print(game_state)\n",
" print(\"You died! Your skeleton will remain in the house as a reminder for all those who come seek riches of others!\")\n",
" break\n",
" else:\n",
" hp_print(game_state)\n",
" path=input(\"Do you want to go left, right or try the door?\")\n",
" if \"left\" in path:\n",
" lifechange = left_option(game_state)\n",
" hp_check(game_state, lifechange)\n",
" elif \"right\" in path:\n",
" right_option(game_state)\n",
" elif \"door\" in path:\n",
" print(game_state[\"inventory\"])\n",
" check = door_check(game_state)\n",
" if bool(check) == True:\n",
" break\n",
" else:\n",
" print(\"Unrecognized option, please try again!\")\n"
]
},
{
"cell_type": "code",
"execution_count": 139,
"id": "d3e4076b-48cc-41ac-95ad-891743e775f5",
"metadata": {},
"outputs": [],
"source": [
"# main function for the game\n",
"def run_mansion():\n",
" \n",
" game_state=copy.deepcopy(INIT_GAME_STATE)\n",
" # game_state=INIT_GAME_STATE.copy() ## This also fixes issues with the project 1... to be incorporated\n",
" \n",
" print(\"Welcome to the Haunted Mansion!\", \n",
" \"As you explore it, you find yourself facing a door in the middle of the corridor.\",\n",
" \"You can choose to go left or right, or try opening the door.\")\n",
"\n",
" hp_check(game_state)\n",
"\n",
" run_main(game_state)\n",
" \n",
" \"\"\"\n",
" Simulates an adventure through a haunted mansion. The adventurer starts with 10 health points and no items.\n",
" Prompt the user to choose between two paths: \"left\" or \"right\". \n",
"\n",
" \"\"\"\n",
" # your code goes here"
]
},
{
"cell_type": "code",
"execution_count": 141,
"id": "f238dc90-0be2-4d8c-93e9-30a1dc8a5b72",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to the Haunted Mansion! As you explore it, you find yourself facing a door in the middle of the corridor. You can choose to go left or right, or try opening the door.\n",
"\n",
"Current HP: 10/10\n",
"\n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to go left, right or try the door? right\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"As you turn right, you see the corridor makes a sharp left turn. As you make the turn, you see all the torches lighting the walls going out in smoke!\n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"You encounter a ghost! Will you run or will you fight?: run\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"You start to run away but soon realize the ghost outpaces you. You have no choice but to turn around and face the ghost!\n",
"Congratulations, you defeated the ghost!\n",
"Searching the remains you find a Gold key!\n",
"\n",
"As you find the end of the corridor, you find nothing else interesting so you move back to the intersection!\n",
"\n",
"\n",
"Current HP: 10/10\n",
"\n",
"\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to go left, right or try the door? door\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Gold Key']\n",
"You unlocked the door and found the Treasure! Congratulations!\n"
]
}
],
"source": [
"run_mansion()"
]
},
{
"cell_type": "raw",
"id": "1bb61198-c921-4ac2-8b77-26e9b1b57057",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"id": "88212f63-3bdb-479f-bf6c-4ecd0685d39a",
"metadata": {},
"source": [
"This should print the game's narrative and prompt the user to make choices and fight ghosts. The game ends when the adventurer finds the key or loses all their health points. "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading