Queer European MD passionate about IT
Browse Source

Create Mission882Solutions.ipynb

acstrahl 1 year ago
parent
commit
5ad8f21318
1 changed files with 310 additions and 0 deletions
  1. 310 0
      Mission882Solutions.ipynb

+ 310 - 0
Mission882Solutions.ipynb

@@ -0,0 +1,310 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "# Grow a Garden!\n",
+    "\n",
+    "- Players will simulate the experience of gardening by planting, growing, and harvesting virtual plants.\n",
+    "- Players will choose which plants to grow, tend to them, and eventually harvest them.\n",
+    "- The game will incorporate various stages of plant growth, from seeds to mature plants, and players will need to care for their plants at each stage.\n",
+    "\n",
+    "**Features:**\n",
+    "- Planting: Choose a plant from your inventory and plant it.\n",
+    "- Tending: Care for your plants to help them grow.\n",
+    "- Harvesting: Once a plant is mature, harvest it to add to your inventory.\n",
+    "- Foraging: Look for new seeds to expand your plant collection."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 125,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import random"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "### The Plant Class"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 126,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "class Plant:\n",
+    "    def __init__(self, name, harvest_yield):\n",
+    "        self.name = name\n",
+    "        self.harvest_yield = harvest_yield\n",
+    "        self.growth_stages = [\"seed\", \"sprout\", \"mature\", \"flower\", \"fruit\", \"harvest-ready\"]\n",
+    "        self.current_growth_stage = self.growth_stages[0] # Initial growth stage is seed\n",
+    "        self.harvestable = False\n",
+    "\n",
+    "    def grow(self):\n",
+    "        current_index = self.growth_stages.index(self.current_growth_stage)\n",
+    "        if self.current_growth_stage == self.growth_stages[-1]:\n",
+    "            print(f\"{self.name} is already fully grown!\")\n",
+    "        elif current_index < len(self.growth_stages) - 1:\n",
+    "            self.current_growth_stage = self.growth_stages[current_index + 1]\n",
+    "            if self.current_growth_stage == \"harvest-ready\":\n",
+    "                self.harvestable = True\n",
+    "\n",
+    "    def harvest(self):\n",
+    "        if self.harvestable:\n",
+    "            self.harvestable = False\n",
+    "            return self.harvest_yield\n",
+    "        else:\n",
+    "            return None"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "### Plant Subclasses"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 127,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "class Tomato(Plant):\n",
+    "    def __init__(self):\n",
+    "        super().__init__(\"Tomato\", 10)\n",
+    "\n",
+    "class Lettuce(Plant):\n",
+    "    def __init__(self):\n",
+    "        super().__init__(\"Lettuce\", 5)\n",
+    "        self.growth_stages = [\"seed\", \"sprout\", \"mature\", \"harvest-ready\"]\n",
+    "\n",
+    "class Carrot(Plant):\n",
+    "    def __init__(self):\n",
+    "        super().__init__(\"Carrot\", 8)\n",
+    "        self.growth_stages = [\"seed\", \"sprout\", \"mature\", \"harvest-ready\"]"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "### Select Item Helper Function"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 128,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "def select_item(items):\n",
+    "    # Determine if items is a dictionary or a list\n",
+    "    if type(items) == dict:\n",
+    "        item_list = list(items.keys())\n",
+    "    elif type(items) == list:\n",
+    "        item_list = items\n",
+    "    else:\n",
+    "        print(\"Invalid items type.\")\n",
+    "        return None\n",
+    "    # Print out the items\n",
+    "    for i in range(len(item_list)):\n",
+    "        try:\n",
+    "            item_name = item_list[i].name\n",
+    "        except:\n",
+    "            item_name = item_list[i]\n",
+    "        print(f\"{i + 1}. {item_name}\")\n",
+    "\n",
+    "    # Get user input\n",
+    "    while True:\n",
+    "        user_input = input(\"Select an item: \")\n",
+    "        try:\n",
+    "            user_input = int(user_input)\n",
+    "            if 0 < user_input <= len(item_list):\n",
+    "                return item_list[user_input - 1]\n",
+    "            else:\n",
+    "                print(\"Invalid input.\")\n",
+    "        except:\n",
+    "            print(\"Invalid input.\")\n"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "### The Gardener Class"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 129,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "class Gardener:\n",
+    "    \"\"\"Represents a gardener who can plant and harvest plants.\"\"\"\n",
+    "\n",
+    "    plant_dict = {\"tomato\": Tomato, \"lettuce\": Lettuce, \"carrot\": Carrot}\n",
+    "\n",
+    "    def __init__(self, name):\n",
+    "        self.name = name\n",
+    "        self.planted_plants = []\n",
+    "        self.inventory = {}\n",
+    "\n",
+    "    def plant(self):\n",
+    "        selected_plant = select_item(self.inventory)\n",
+    "        if selected_plant in self.inventory and self.inventory[selected_plant] > 0:\n",
+    "            self.inventory[selected_plant] -= 1\n",
+    "            if self.inventory[selected_plant] == 0:\n",
+    "                del self.inventory[selected_plant]\n",
+    "            new_plant = self.plant_dict[selected_plant]()\n",
+    "            self.planted_plants.append(new_plant)\n",
+    "            print(f\"{self.name} planted a {selected_plant}!\")\n",
+    "        else:\n",
+    "            print(f\"{self.name} doesn't have any {selected_plant} to plant!\")\n",
+    "\n",
+    "    def tend(self):\n",
+    "        for plant in self.planted_plants:\n",
+    "            if plant.harvestable:\n",
+    "                print(f\"{plant.name} is ready to be harvested!\")\n",
+    "            else:\n",
+    "                plant.grow()\n",
+    "                print(f\"{plant.name} is now a {plant.current_growth_stage}!\")\n",
+    "    \n",
+    "    def harvest(self):\n",
+    "        selected_plant = select_item(self.planted_plants)\n",
+    "        if selected_plant.harvestable == True:\n",
+    "            if selected_plant.name in self.inventory:\n",
+    "                self.inventory[selected_plant.name] += selected_plant.harvest()\n",
+    "            else:\n",
+    "                self.inventory[selected_plant.name] = selected_plant.harvest()\n",
+    "            print(f\"You harvested a {selected_plant.name}!\")\n",
+    "            self.planted_plants.remove(selected_plant)\n",
+    "        else:\n",
+    "            print(f\"You can't harvest a {selected_plant.name}!\")\n",
+    "\n",
+    "    def forage_for_seeds(self):\n",
+    "        seed = random.choice(all_plant_types)\n",
+    "        if seed in self.inventory:\n",
+    "            self.inventory[seed] += 1\n",
+    "        else:\n",
+    "            self.inventory[seed] = 1\n",
+    "        print(f\"{self.name} found a {seed} seed!\")\n",
+    "\n",
+    "        "
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "### Defining Game-Level Variables"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 130,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "all_plant_types = [\"tomato\", \"lettuce\", \"carrot\"]\n",
+    "valid_commands = [\"plant\", \"tend\", \"harvest\", \"forage\", \"help\", \"quit\"]"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "### Main Game Loop"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 131,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Welcome to the garden! You will act as a virtual gardener.\n",
+      "Forage for new seeds, plant them, and then watch them grow!\n",
+      "Start by entering your name.\n",
+      "Welcome, Anna! Let's get gardening!\n",
+      "Type 'help' for a list of commands.\n",
+      "Anna found a lettuce seed!\n",
+      "1. lettuce\n",
+      "Anna planted a lettuce!\n",
+      "Lettuce is now a sprout!\n",
+      "Lettuce is now a mature!\n",
+      "Lettuce is now a harvest-ready!\n",
+      "1. Lettuce\n",
+      "You harvested a Lettuce!\n",
+      "Goodbye!\n"
+     ]
+    }
+   ],
+   "source": [
+    "# Print welcome message\n",
+    "print(\"Welcome to the garden! You will act as a virtual gardener.\\nForage for new seeds, plant them, and then watch them grow!\\nStart by entering your name.\")\n",
+    "\n",
+    "# Create gardener\n",
+    "gardener_name = input(\"What is your name? \")\n",
+    "print(f\"Welcome, {gardener_name}! Let's get gardening!\\nType 'help' for a list of commands.\")\n",
+    "gardener = Gardener(gardener_name)\n",
+    "\n",
+    "# Main game loop\n",
+    "while True:\n",
+    "    player_action = input(\"What would you like to do? \")\n",
+    "    player_action = player_action.lower()\n",
+    "    if player_action in valid_commands:\n",
+    "        if player_action == \"plant\":\n",
+    "            gardener.plant()\n",
+    "        elif player_action == \"tend\":\n",
+    "            gardener.tend()\n",
+    "        elif player_action == \"harvest\":\n",
+    "            gardener.harvest()\n",
+    "        elif player_action == \"forage\":\n",
+    "            gardener.forage_for_seeds()\n",
+    "        elif player_action == \"help\":\n",
+    "            print(\"*** Commands ***\")\n",
+    "            for command in valid_commands:\n",
+    "                print(command)\n",
+    "        elif player_action == \"quit\":\n",
+    "            print(\"Goodbye!\")\n",
+    "            break\n",
+    "    else:\n",
+    "        print(\"Invalid command.\")"
+   ]
+  }
+ ],
+ "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.11.3"
+  },
+  "orig_nbformat": 4
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}