Skip to content

Commit 76bc73a

Browse files
Merge pull request #4 from StructuralWizard/include-day1
Day 1 written
2 parents a62ff57 + d9d0259 commit 76bc73a

File tree

12 files changed

+875
-0
lines changed

12 files changed

+875
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ _site/
99
.sass-cache/
1010
.jekyll-cache/
1111
.jekyll-metadata
12+
package-lock.json
13+
package.json
1214

1315
# Ignore folders generated by Bundler
1416
.bundle/
1517
vendor/
18+
19+
# Ignore folders for mermaid diagrams
20+
.assets/

_config.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,36 @@ url: https://just-the-docs.github.io
66

77
aux_links:
88
Template Repository: https://github.com/just-the-docs/just-the-docs-template
9+
10+
11+
collections:
12+
python_code:
13+
output: true
14+
15+
just_the_docs:
16+
collections:
17+
python_code:
18+
name: Python Code
19+
description: Python code examples for the 10 Days of Code
20+
nav_exclude: false # to exclude the entire collection from the main navigation
21+
nav_fold: true # to fold the collection, instead of showing links to all its top-level pages2
22+
search_exclude: false # to exclude all the collection pages from search results
23+
# For copy button on code
24+
enable_copy_code_button: true
25+
26+
# Back to top link
27+
back_to_top: true
28+
back_to_top_text: "Back to top"
29+
30+
31+
mermaid:
32+
# Version of mermaid library
33+
# Pick an available version from https://cdn.jsdelivr.net/npm/mermaid/
34+
version: "11.6.0"
35+
# for (v10+)
36+
path: "/assets/js/mermaid.esm.min.mjs"
37+
#path: "./node_modules/mermaid/dist/mermaid.esm.min.mjs"
38+
# for (<v10):
39+
# path: "/assets/js/mermaid.min.js"
40+
# Note: copy both `mermaid.esm.min.mjs` (v10+) or `mermaid.min.js` (<v10) and the associated
41+
# `.map` file from the specified version of `mermaid/dist` to `/assets/js/`.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "75e340c4",
6+
"metadata": {},
7+
"source": [
8+
"# 🧟‍♂️ Monster Maze Python Tutorial\n",
9+
"Welcome to the Monster Maze! In this tutorial, you’ll build a fun text-based game while learning core Python concepts."
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": null,
15+
"id": "a90a9c09",
16+
"metadata": {},
17+
"outputs": [],
18+
"source": [
19+
"\n",
20+
"import random\n",
21+
"\n",
22+
"# Constants\n",
23+
"ROOMS = [\"Hall\", \"Kitchen\", \"Dining Room\", \"Library\", \"Garden\"]\n",
24+
"ITEMS = [\"sword\", \"shield\", \"potion\"]\n",
25+
"MONSTERS = [\"goblin\", \"zombie\", \"troll\"]\n"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": null,
31+
"id": "06cb7692",
32+
"metadata": {},
33+
"outputs": [],
34+
"source": [
35+
"\n",
36+
"# Player dictionary\n",
37+
"player = {\n",
38+
" \"name\": \"Hero\",\n",
39+
" \"health\": 100,\n",
40+
" \"inventory\": []\n",
41+
"}\n",
42+
"\n",
43+
"# Functions\n",
44+
"def describe_room(room):\n",
45+
" print(f\"You are in the {room}.\")\n",
46+
" if random.random() < 0.3:\n",
47+
" item = random.choice(ITEMS)\n",
48+
" print(f\"You found a {item}!\")\n",
49+
" player[\"inventory\"].append(item)\n",
50+
" if random.random() < 0.3:\n",
51+
" monster = random.choice(MONSTERS)\n",
52+
" print(f\"A {monster} appears!\")\n",
53+
" fight(monster)\n",
54+
"\n",
55+
"def fight(monster):\n",
56+
" if \"sword\" in player[\"inventory\"]:\n",
57+
" print(f\"You defeated the {monster} with your sword!\")\n",
58+
" else:\n",
59+
" print(f\"The {monster} attacked you! You lose 20 health.\")\n",
60+
" player[\"health\"] -= 20\n"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": null,
66+
"id": "c4647a04",
67+
"metadata": {},
68+
"outputs": [],
69+
"source": [
70+
"\n",
71+
"# Game loop\n",
72+
"def play_game():\n",
73+
" print(\"Welcome to the Monster Maze!\")\n",
74+
" print(\"Find the exit without losing all your health.\")\n",
75+
"\n",
76+
" visited = []\n",
77+
" while player[\"health\"] > 0:\n",
78+
" room = random.choice(ROOMS)\n",
79+
" if room not in visited:\n",
80+
" visited.append(room)\n",
81+
" describe_room(room)\n",
82+
" print(f\"Health: {player['health']}, Inventory: {player['inventory']}\")\n",
83+
" if input(\"Continue? (y/n) \") != \"y\":\n",
84+
" break\n",
85+
"\n",
86+
" if player[\"health\"] <= 0:\n",
87+
" print(\"You have been defeated in the maze.\")\n",
88+
" else:\n",
89+
" print(\"You escaped the Monster Maze!\")\n",
90+
"\n",
91+
"play_game()\n"
92+
]
93+
},
94+
{
95+
"cell_type": "markdown",
96+
"id": "9d03cbce",
97+
"metadata": {},
98+
"source": [
99+
"\n",
100+
"## 📝 Reflection\n",
101+
"\n",
102+
"- What happens when you find an item?\n",
103+
"- How is randomness used in the game?\n",
104+
"- Try changing the monsters, items, or rooms. What happens?\n"
105+
]
106+
}
107+
],
108+
"metadata": {},
109+
"nbformat": 4,
110+
"nbformat_minor": 5
111+
}

_python_code/Day1/monster_maze.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import random
2+
3+
# Constants
4+
ROOMS = ["Hall", "Kitchen", "Library", "Dungeon", "Garden"]
5+
ITEMS = ["sword", "potion", "shield"]
6+
MONSTERS = ["Goblin", "Troll", "Skeleton"]
7+
8+
# Global variable
9+
found_key = False
10+
11+
def print_welcome():
12+
"""Prints the welcome message with ASCII art."""
13+
print("""
14+
🧟‍♂️ MONSTER MAZE 🧟‍♀️
15+
Escape the maze, defeat monsters, and find the key!
16+
""") # String manipulation and printing
17+
18+
def create_player(name):
19+
"""Returns a new player dictionary."""
20+
return {
21+
"name": name,
22+
"health": 100,
23+
"inventory": [],
24+
"location": random.choice(ROOMS) # Random module
25+
}
26+
27+
def describe_room(room):
28+
"""Describes the current room."""
29+
print(f"\nYou are now in the {room}.")
30+
if random.random() < 0.4: # Conditional statement
31+
item = random.choice(ITEMS)
32+
print(f"You found a {item}!")
33+
return item
34+
return None
35+
36+
def encounter_monster(player):
37+
"""Random monster encounter with chance of fight."""
38+
if random.random() < 0.3:
39+
monster = random.choice(MONSTERS)
40+
print(f"\n⚔️ A wild {monster} appears!")
41+
if "sword" in player["inventory"]:
42+
print("You defeat it with your sword!")
43+
else:
44+
player["health"] -= 20
45+
print("You have no sword! You got hurt!")
46+
print(f"Health: {player['health']}")
47+
if player["health"] <= 0:
48+
print("💀 You have died. Game Over.")
49+
exit()
50+
51+
def move_to_new_room(player):
52+
"""Moves the player to a new random room."""
53+
previous = player["location"]
54+
player["location"] = random.choice([r for r in ROOMS if r != previous])
55+
56+
def check_for_key(player):
57+
"""Checks if the player finds the key."""
58+
global found_key
59+
if not found_key and random.random() < 0.2:
60+
found_key = True
61+
print("🔑 You found the magic key!")
62+
player["inventory"].append("magic key")
63+
64+
def game_loop(player):
65+
"""Main game loop using recursion."""
66+
if found_key:
67+
print(f"\n🎉 Congratulations, {player['name']}! You escaped the maze!")
68+
return # End the game if key is found
69+
70+
item = describe_room(player["location"])
71+
if item:
72+
player["inventory"].append(item)
73+
74+
encounter_monster(player)
75+
check_for_key(player)
76+
77+
# While loop & string formatting with f-strings
78+
while True:
79+
choice = input("\nDo you want to move to another room? (yes/no): ").lower()
80+
if choice in ["yes", "y"]:
81+
move_to_new_room(player)
82+
game_loop(player) # Recursion
83+
break
84+
elif choice in ["no", "n"]:
85+
print("🛌 You chose to rest. Game Over.")
86+
break
87+
else:
88+
print("Please answer yes or no.")
89+
90+
# Main program
91+
def main():
92+
"""Starts the game."""
93+
print_welcome()
94+
name = input("Enter your name, adventurer: ")
95+
player = create_player(name) # Function with inputs/outputs
96+
game_loop(player)
97+
98+
if __name__ == "__main__":
99+
main()

0 commit comments

Comments
 (0)