-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.py
145 lines (116 loc) · 4.13 KB
/
state.py
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
"""state module"""
from random import randint
from typing import List
from events import Events, post
class State:
"""
State class contains state of the game.
State properties:
- answers - 2-dimensional list of answers (12x4)
- feedback - 2-dimensional list of feedback (12x4)
- current_row - current row, starts at the end and decrements,
if game reaches row 0 a game over event is triggered (see game.logic module)
- active_indices - a list that toggles (using SPACEBAR) which element
of the current row is selected
- input_enabled - determines if input is currently enabled
- cheater - is used to determine which game rules should be used
"""
combination: List[int]
answers: List[List[int]]
feedback: List[List[int]]
active_indices: List[int]
current_row: int
def __init__(self):
self.combination = [randint(1, 6) for i in range(4)]
self.answers = [[0 for j in range(4)] for i in range(12)]
self.feedback = [[] for i in range(12)]
self.active_indices = [1 if i == 0 else 0 for i in range(4)]
self.current_row = len(self.answers) - 1
self.input_enabled = True
self.cheater = bool(randint(0, 1))
print('Combination:', self.combination)
print('Rules:', 'Correct rules enabled.' if not self.cheater else 'CHEATER ENABLED')
post(Events.AFTER_GAME_RESET, {'state': self})
def reset(self):
"""
Game reset.
:return: None
"""
self.__init__()
post(Events.GAME_RESET, {})
def get_active_index(self):
"""
Return currently activated index.
:return: currently active index
"""
return self.active_indices.index(1)
def change_active_index(self):
"""
Change active index to the next element.
In case if no next element is available,
proceed to the first element.
:return: None
"""
index = self.get_active_index()
next_index = 0
self.active_indices[index] = 0
if index + 1 < len(self.active_indices):
next_index = index + 1
self.active_indices[next_index] = 1
def get_feedback(self, row: int):
"""
Get list from the feedback row.
:param row: feedback row
:return: list with feedback for the row
"""
return self.feedback[row]
def get_feedback_digit(self, row: int, col: int):
"""
Get digit of the feedback for the row at col index.
:param row: feedback row
:param col: feedback col
:return: feedback digit of the row at col index
"""
return self.get_feedback(row)[col]
def append_feedback_digit(self, digit: int, row: int = None):
"""
Append digit to the feedback row.
In case if no row is passed, current_row is used.
:param digit: digit to set, this later used to determine color
:param row: feedback row
:return: None
"""
self.feedback[row if row else self.current_row].append(digit)
def get_answer(self, row):
"""
Get answer for the given row.
:param row: answer row
:return list of the answers for the given row
"""
return self.answers[row]
def get_answer_digit(self, row, col):
"""
Get answer digit for the given row and col.
:param row: answer row
:param col: answer col
:return digit of the answer at given row and col
:return: None
"""
return self.get_answer(row)[col]
def set_answer_digit(self, digit: int, row: int = None, col: int = None):
"""
Set answer digit for the given row and col.
:param digit: answer digit
:param row: answer row
:param col: answer col
:return: None
"""
row = row if row else self.current_row
col = col if col else self.get_active_index()
self.answers[row][col] = digit
def disable_input(self):
"""
Disable input and wait for the reset.
:return: None
"""
self.input_enabled = False