-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiction.py
More file actions
212 lines (164 loc) · 6.99 KB
/
fiction.py
File metadata and controls
212 lines (164 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
from dataclasses import dataclass
from enum import Flag, auto
from typing import NamedTuple, Optional
from typeguard import typechecked
from words import is_word
class LetterResult(Flag):
NONE = 0
IN_WORD = auto()
CORRECT = auto()
class Lie(NamedTuple):
result : LetterResult
index : int
SUPPORTED_WORD_LENGTHS = [5]
@dataclass(init=False)
class GameSettings:
rounds : int = 10
checks : int = 3
wordLength : int = 5
@typechecked
def __init__(self, rounds : int = 10, checks : int = 3, wordLength : int = 5):
assert rounds > 0, f'Rounds \'{rounds}\' must be a positive integer'
assert checks >= 0, f'Check count \'{checks}\' cannot be negative'
assert wordLength in SUPPORTED_WORD_LENGTHS, f'World Length \'{wordLength}\' not supported. Available lengths are {SUPPORTED_WORD_LENGTHS}'
self.rounds = rounds
self.checks = checks
self.wordLength = wordLength
@dataclass(init=False)
class Guess:
word : str
lie : Lie
checkedIndex : int
def __init__(self, word : str):
self.word = word
self.lie = None
self.checkedIndex = -1
class GameState(Flag):
NONE = 0
IN_PROGRESS = auto()
GUESSER_WON = auto()
LIEBRARIAN_WON = auto()
ABANDONDED = auto()
@dataclass(init=False)
class Game:
winWord : str
settings : GameSettings
firstLetter : str
guesses : list[Guess]
state : GameState
@property
def lastGuess(self) -> Optional[Guess]:
return self.guesses[-1] if len(self.guesses) > 0 else None
@property
def wordLength(self) -> int:
return self.settings.wordLength
@property
def currentRound(self) -> int:
return len(self.guesses) + 1
@property
def remaingRounds(self) -> int:
return self.settings.rounds - len(self.guesses)
@property
def checksUsed(self) -> int:
count = 0
for guess in self.guesses:
if self.valid_index(guess.checkedIndex):
count += 1
return count
@property
def checksRemaing(self) -> int:
return self.settings.checks - self.checksUsed
@property
def hasWord(self) -> bool:
return self.winWord is not None and len(self.winWord) > 0
@property
def checkedFirstLetter(self) -> bool:
return self.firstLetter is not None and len(self.firstLetter) == 1
@typechecked
def __init__(self, settings : GameSettings = GameSettings()):
self.settings = settings
self.winWord = None
self.firstLetter = None
self.guesses = list()
self.state = GameState.IN_PROGRESS
@typechecked
def pick_word(self, winWord : str):
assert not self.hasWord, 'Word has already been selected'
assert len(winWord) == self.settings.wordLength, f'Word must be {self.settings.wordLength} characters long'
self.winWord = winWord.upper()
WORD_NOT_PICKED_MSG = 'The target word has not been selected yet'
@typechecked
def check_first_letter(self, letter : str) -> bool:
assert self.hasWord, self.WORD_NOT_PICKED_MSG
assert not self.checkedFirstLetter, f'Cannot check more than one letter. \'{self.firstLetter}\' has already been checked to{(" not" if self.firstLetter not in self.winWord else "")} be in the word'
assert len(letter) == 1, f'\'{letter}\' must be a single letter'
assert letter.isalpha(), f'\'{letter}\' must be an english letter'
self.firstLetter = letter.upper()
return letter in self.winWord
@typechecked
def make_guess(self, word : str) -> GameState:
assert self.hasWord, self.WORD_NOT_PICKED_MSG
assert self.checkedFirstLetter, 'First letter has not been verified yet'
assert len(word) == len(self.winWord), f'Word must be {len(self.winWord)} characters long'
assert len(self.guesses) < self.settings.rounds, f'Max guesses have been reached ({self.settings.rounds})'
word = word.upper()
def used_word() -> bool:
for guess in self.guesses:
if guess.word == word:
return True
return False
assert not used_word(), f'Already used word \"{word}\"'
assert is_word(word), f'"{word}" is not a valid word'
self.guesses.append(Guess(word))
if word == self.winWord:
self.state = GameState.GUESSER_WON
elif len(self.guesses) == self.settings.rounds:
self.state = GameState.LIEBRARIAN_WON
return self.state
NO_GUESSES_MSG = 'No guesses have been made'
def can_give_lie(self) -> bool:
assert self.winWord is not None, self.WORD_NOT_PICKED_MSG
assert self.lastGuess is not None, self.NO_GUESSES_MSG
assert self.lastGuess.lie is None, 'Already given a lie'
return True
@typechecked
def give_lie(self, lie : Lie):
assert self.winWord is not None, self.WORD_NOT_PICKED_MSG
assert self.lastGuess is not None, self.NO_GUESSES_MSG
assert self.valid_index(lie.index), f'Lie index \'{lie.index}\' is not within word length of \'{len}'
guess : Guess = self.lastGuess
assert guess.lie is None, 'Already given a lie'
assert lie != self.letter_result(guess.word[lie.index], lie.index), 'Not a real lie'
guess.lie = lie
def can_check_lie(self) -> bool:
assert self.winWord is not None, self.WORD_NOT_PICKED_MSG
assert self.has_guess(), self.NO_GUESSES_MSG
assert self.checksUsed < self.settings.checks, f'All checks have been used ({self.settings.checks})'
guess = self.lastGuess
assert guess.lie is not None, 'Clue has not been given yet'
assert not self.valid_index(guess.checkedIndex), f'Already checked most recent guess ({guess.word})'
return True
@typechecked
def check_is_lie(self, index : int) -> bool:
if not self.can_check_lie():
return False
assert self.valid_index(index), f'{index} is not a valid index'
guess = self.lastGuess
guess.checkedIndex = index
return guess.lie.index == index
@typechecked
def letter_result(self, letter : str, index : int) -> LetterResult:
assert self.winWord is not None, self.WORD_NOT_PICKED_MSG
assert(self.valid_index(index))
assert(len(letter) == 1)
letter = letter.upper()
if self.winWord[index] == letter:
return LetterResult.CORRECT
if letter in self.winWord:
return LetterResult.IN_WORD
return LetterResult.NONE
def has_guess(self) -> bool:
return len(self.guesses) > 0
@typechecked
def valid_index(self, index : Optional[int]) -> bool:
return index is not None and index >= 0 and index < len(self.winWord)