1
0
Fork 0
This repository has been archived on 2024-01-30. You can view files and clone it, but cannot push or open issues or pull requests.
adaptive-game-assistant/adaptive_game_module/hint_giver.py

55 lines
2.1 KiB
Python
Raw Normal View History

2021-03-23 17:24:35 +01:00
import yaml # reads hint configurations from a .yml file
class HintGiver:
2021-03-30 14:27:37 +02:00
"""Class holding all the possible hints for all levels, and giving them
2021-03-30 14:27:59 +02:00
out when asked for.
Also keeps track of previously given hints, and total given hints in a game."""
2021-03-30 14:27:37 +02:00
2021-03-23 17:24:35 +01:00
def __init__(self, hint_filename):
2021-03-30 14:27:37 +02:00
"""Create a hint giver from file `filename`."""
2021-03-23 17:24:35 +01:00
self.total_hints_taken = 0
self.hints_taken = {} # dict, level : [hint_name, hint_name]
self.read_hint_file(hint_filename)
def read_hint_file(self, hint_filename): # raises OSError on file problems
"""Read dict of hints from the adaptive game from a YAML file."""
with open(hint_filename) as f:
self.hints = yaml.load(f, Loader=yaml.FullLoader)
def show_possible_hints(self, level, branch):
2021-03-30 14:27:37 +02:00
"""Return all possible hints for given `level` and `branch`."""
2021-03-23 17:24:35 +01:00
return list(self.hints[level][level+branch])
def take_hint(self, level, branch, hint_name):
2021-03-30 14:27:37 +02:00
"""Show hint text for a specific hint. Add it to hint log and counter."""
2021-03-23 17:24:35 +01:00
if not level+branch in self.hints_taken:
self.hints_taken[level+branch] = []
if hint_name not in self.hints_taken[level+branch]:
self.total_hints_taken += 1
self.hints_taken[level+branch].append(hint_name)
return self.hints[level][level+branch][hint_name]
def show_taken_hints(self, level, branch):
2021-03-30 14:27:37 +02:00
"""Show all previously taken hints for `level` and `branch`."""
2021-03-23 17:40:33 +01:00
if not level+branch in self.hints_taken:
return []
2021-03-23 17:24:35 +01:00
return self.hints_taken[level+branch]
def is_hint_name(self, level, branch, hint_name):
2021-03-30 14:27:37 +02:00
"""Check if `hint_name` is a name of a hint for `level` and `branch`."""
2021-03-23 17:24:35 +01:00
if hint_name in self.hints[level][level+branch]:
return True
2021-03-24 17:37:22 +01:00
return False
2021-03-24 17:53:56 +01:00
def restart_game(self):
2021-03-30 14:27:37 +02:00
"""Reset hint counter and hint log to zero."""
2021-03-24 17:53:56 +01:00
self.total_hints_taken = 0
self.hints_taken = {}
2021-03-24 17:37:22 +01:00
def log_to_file(self, filename):
2021-03-30 14:27:37 +02:00
"""Write taken hints to a file."""
2021-03-24 17:37:22 +01:00
with open(filename, 'a') as f:
2021-03-30 16:31:24 +02:00
yaml.dump(self.hints_taken, f)