From 49545ed01bfab16c5ac944f51718d9408a299122 Mon Sep 17 00:00:00 2001 From: cheezedoodle2 Date: Mon, 3 Apr 2023 23:49:36 -0700 Subject: [PATCH] Added the ability for verbs to be a dict, for future use by an AI. --- simulation/HotdogSimulation.py | 30 +++++++++++++++++++++++++++--- simulation/README.md | 4 ++-- simulation/Simulation.py | 15 +++++++++++++-- simulation/SimulationRunner.py | 6 ++++-- 4 files changed, 46 insertions(+), 9 deletions(-) diff --git a/simulation/HotdogSimulation.py b/simulation/HotdogSimulation.py index 1d137ff..fcb77e6 100644 --- a/simulation/HotdogSimulation.py +++ b/simulation/HotdogSimulation.py @@ -1,14 +1,38 @@ from Simulation import Simulation class HotdogSimulation(Simulation): def __init__(self) -> None: - self.ticks_left = 5 + self.bites_left = 5 + self.ticks_left = 10 + # Temperature in degC + self.temperature = 20 + def get_uses(self): + # The trailing comma is to ensure a tuple is returned instead of a string. + return { + 'eat': "Eat one bite of the hot dog.", + 'microwave': "Put the hot dog in the microwave for 30 seconds." + } def start(self): print(f"DEBUG: simulation started.") - def use(self): - print(f"DEBUG: someone ate the hot dog! 🌭") + def use(self, *args, **kwargs) -> bool: + if 'eat' in args: + print(f"DEBUG: someone ate the hot dog! 🌭") + self.bites_left -= 1 + if self.bites_left == 0: + print(f"DEBUG: no more hot dog left! 🌭") + return False + if 'microwave' in args: + print(f"DEBUG: someone microwaved the hot dog! 🌭") + self.temperature += 40 + if self.temperature > 100: + print(f"DEBUG: hot dog got too hot and exploded! 🌭") + return False + else: + print(f"DEBUG: someone used the hot dog! 🌭") + return True def tick(self): self.ticks_left -= 1 + self.temperature -= 10 if self.ticks_left == 0: print(f"DEBUG: simulation out of ticks, expiring.") return False diff --git a/simulation/README.md b/simulation/README.md index 7e42663..968810d 100644 --- a/simulation/README.md +++ b/simulation/README.md @@ -4,5 +4,5 @@ isn't specified on the command line. It will then: * Run the simulation class's `start()` method, * Call its `tick()` method, -* Call its `use()` method, -* then call its `stop()` method once `tick()` returns false. \ No newline at end of file +* Call its `use()` method with random uses from `get_uses()`, +* then call its `stop()` method once `tick()` or `use()` returns false. \ No newline at end of file diff --git a/simulation/Simulation.py b/simulation/Simulation.py index 9596120..113d5a5 100644 --- a/simulation/Simulation.py +++ b/simulation/Simulation.py @@ -16,12 +16,23 @@ class Simulation(ABC): pass @abstractmethod + def get_uses(self): + """ + Return the uses of the simulated object. These are verb strings which are passed to the `use()` method. + Returns: + tuple: A dict of key-value pairs, where the keys are strings of + verbs to be passed to `use`, and the values are strings explaining + what the verb does in English. + """ + @abstractmethod def use(self, *args, **kwargs): """ Use the simulated object. Args: - args (tuple): A tuple of arguments. - kwargs (dict): A dictionary of key-value pairs of arguments. + args (tuple): A tuple of verbs to be used on the object. + kwargs (dict): A dictionary of key-value pairs of arguments to the verb(s). + Returns: + True if the action succeeded, False if the action did not succeed and the object needs to be expired. """ pass diff --git a/simulation/SimulationRunner.py b/simulation/SimulationRunner.py index 302491c..43145d6 100644 --- a/simulation/SimulationRunner.py +++ b/simulation/SimulationRunner.py @@ -2,6 +2,7 @@ from importlib import import_module import tkinter as tk import os import sys +from random import choice from time import sleep from tkinter import filedialog from abc import ABC, abstractmethod @@ -10,8 +11,9 @@ class SimulationRunner: def __init__(self, simulation) -> None: self.simulation = simulation() self.simulation.start() - while self.simulation.tick(): - self.simulation.use() + uses = list(self.simulation.get_uses().keys()) + while self.simulation.tick() and self.simulation.use(choice(uses)): + pass self.simulation.stop() if __name__ == '__main__':