forked from SVIG/python-bootcamp
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
|
||||
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):
|
||||
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
|
||||
|
||||
@@ -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.
|
||||
* Call its `use()` method with random uses from `get_uses()`,
|
||||
* then call its `stop()` method once `tick()` or `use()` returns false.
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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__':
|
||||
|
||||
Reference in New Issue
Block a user