Added the ability for verbs to be a dict, for future use by an AI.
This commit is contained in:
@@ -1,14 +1,38 @@
|
|||||||
from Simulation import Simulation
|
from Simulation import Simulation
|
||||||
class HotdogSimulation(Simulation):
|
class HotdogSimulation(Simulation):
|
||||||
def __init__(self) -> None:
|
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):
|
def start(self):
|
||||||
print(f"DEBUG: simulation started.")
|
print(f"DEBUG: simulation started.")
|
||||||
def use(self):
|
def use(self, *args, **kwargs) -> bool:
|
||||||
|
if 'eat' in args:
|
||||||
print(f"DEBUG: someone ate the hot dog! 🌭")
|
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):
|
def tick(self):
|
||||||
self.ticks_left -= 1
|
self.ticks_left -= 1
|
||||||
|
self.temperature -= 10
|
||||||
if self.ticks_left == 0:
|
if self.ticks_left == 0:
|
||||||
print(f"DEBUG: simulation out of ticks, expiring.")
|
print(f"DEBUG: simulation out of ticks, expiring.")
|
||||||
return False
|
return False
|
||||||
|
|||||||
@@ -4,5 +4,5 @@ isn't specified on the command line. It will then:
|
|||||||
|
|
||||||
* Run the simulation class's `start()` method,
|
* Run the simulation class's `start()` method,
|
||||||
* Call its `tick()` method,
|
* Call its `tick()` method,
|
||||||
* Call its `use()` method,
|
* Call its `use()` method with random uses from `get_uses()`,
|
||||||
* then call its `stop()` method once `tick()` returns false.
|
* then call its `stop()` method once `tick()` or `use()` returns false.
|
||||||
@@ -16,12 +16,23 @@ class Simulation(ABC):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@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):
|
def use(self, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Use the simulated object.
|
Use the simulated object.
|
||||||
Args:
|
Args:
|
||||||
args (tuple): A tuple of arguments.
|
args (tuple): A tuple of verbs to be used on the object.
|
||||||
kwargs (dict): A dictionary of key-value pairs of arguments.
|
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
|
pass
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ from importlib import import_module
|
|||||||
import tkinter as tk
|
import tkinter as tk
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
from random import choice
|
||||||
from time import sleep
|
from time import sleep
|
||||||
from tkinter import filedialog
|
from tkinter import filedialog
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
@@ -10,8 +11,9 @@ class SimulationRunner:
|
|||||||
def __init__(self, simulation) -> None:
|
def __init__(self, simulation) -> None:
|
||||||
self.simulation = simulation()
|
self.simulation = simulation()
|
||||||
self.simulation.start()
|
self.simulation.start()
|
||||||
while self.simulation.tick():
|
uses = list(self.simulation.get_uses().keys())
|
||||||
self.simulation.use()
|
while self.simulation.tick() and self.simulation.use(choice(uses)):
|
||||||
|
pass
|
||||||
self.simulation.stop()
|
self.simulation.stop()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Reference in New Issue
Block a user