-
Notifications
You must be signed in to change notification settings - Fork 0
/
puzzle.py
223 lines (175 loc) · 6.04 KB
/
puzzle.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
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
213
214
215
216
217
218
219
220
221
222
223
"""
Repository link https://github.com/Kolotose/lab0_task2.git
"""
def read_board(path: str):
"""
Read game board file from path.
Return list of str.
>>> read_board("board.txt")
["**** ****", "***1 ****", "** 3****", \
"* 4 1****", " 9 5 ", " 6 83 *", \
"3 1 **", " 8 2***", " 2 ****"]
"""
with open(path, 'r', encoding='utf-8') as file:
board = file.readlines()
file.close()
return board
def check_board_complete(board: list):
"""
Checks if the board is completed with all the numbers
>>> check_board_complete(["**** ****", "***1 ****", "** 3****", \
"* 4 1****", " 9 5 ", " 6 83 *", \
"3 1 **", " 8 2***", " 2 ****"])
False
"""
for line in board:
for element in line:
if element == ' ':
return False
return True
def check_board_size(board:list):
"""
Used in validate_board()
Checks if the board is 9x9
>>> check_board_size(["**** ****", "***1 ****", "** 3****", \
"* 4 1****", " 9 5 ", " 6 83 *", \
"3 1 **", " 8 2***", " 2 ****"])
True
"""
if len(board) != 9:
return False
for line in board:
if len(line) != 9:
return False
return True
def check_board_form(board: list):
"""
Used in validate_board()
Checks if the board is correct form
>>> check_board_form(["**** ****", "***1 ****", "** 3****", \
"* 4 1****", " 9 5 ", " 6 83 *", \
"3 1 **", " 8 2***", " 2 ****"])
True
"""
# Checking if stars are not placed where they do not need to be
for i in range(5):
# 5, because we have to have (8, 0), (7, 1), ..., (4, 4) be filled
# and it is 5 cells. We will be leaning on them (shape similar to L)
if board[8-i][i] == '*':
return False
for j in range(1, 5):
if board[8-i-j][i] == '*' or board[8-i][i+j] == '*':
return False
# Checking if the tiles are not placed where they do not need to be
for i in range(4):
# Top right square
for j in range(4):
if board[i][8-j] != '*':
return False
# Top left and bottom right square
for j in range(4-i):
if board[i][j] != '*' or board[8-i][8-j] != '*':
return False
return True
def check_numbers(board: list):
"""
Used in validate_board()
Checks if the numbers in same columns, rows and colours are different
>>> check_numbers(["**** ****", "***1 ****", "** 3****", \
"* 4 1****", " 9 5 ", " 6 83 *", \
"3 1 **", " 8 2***", " 2 ****"])
False
>>> check_numbers(["**** ****", "***1 ****", "** 3****", \
"* 4 1****", " 9 5 ", " 6 83 *", \
"3 **", " 8 2***", " 2 ****"])
True
"""
if rows_check(board) and \
columns_check(board) and \
color_check(board):
# All conditions are satisfied
return True
return False
def rows_check(board: list):
"""
Used in check_numbers()
Checks if the numbers in same rows are different
>>> rows_check(["**** ****", "***1 ****", "** 3****", \
"* 4 1****", " 9 5 ", " 6 83 *", \
"3 1 **", " 8 2***", " 2 ****"])
True
"""
for line in board:
numbers_in_line = []
for element in line:
if element.isnumeric():
numbers_in_line.append(element)
if check_uniqueness(numbers_in_line) == False:
return False
return True
def columns_check(board: list):
"""
Used in check_numbers()
Checks if the numbers in same columns are different
>>> columns_check(["**** ****", "***1 ****", "** 3****", \
"* 4 1****", " 9 5 ", " 6 83 *", \
"3 1 **", " 8 2***", " 2 ****"])
False
"""
for j in range(9):
numbers_in_column = []
for i in range(9):
if board[i][j].isnumeric():
numbers_in_column.append(board[i][j])
if check_uniqueness(numbers_in_column) == False:
return False
return True
def color_check(board: list):
"""
Used in check_numbers()
Checks if the numbers in same columns are different
>>> color_check(["**** ****", "***1 ****", "** 3****", \
"* 4 1****", " 9 5 ", " 6 83 *", \
"3 1 **", " 8 2***", " 2 ****"])
True
"""
for i in range(5):
numbers_in_colour = []
numbers_in_colour.append(board[8-i][i])
for j in range(1, 5):
numbers_in_colour.append(board[8-i-j][i])
numbers_in_colour.append(board[8-i][i+j])
if check_uniqueness(numbers_in_colour) == False:
return False
return True
def check_uniqueness(list_of_numbers: list):
"""
Used in color_check(), rows_check(), columns_check()
Checks if the numbers in list are unique
>>> check_uniqueness(['1', '2', '3', '4', '6', '7', '8', '9'])
True
>>> check_uniqueness(['1', '2', '3', '4', '5', '6', '7', '1'])
False
"""
for number in range(1, 10):
amount = list_of_numbers.count(str(number))
if amount > 1:
return False
return True
def validate_board(board: list):
"""
Main function
Checks if the board is ready for the game
>>> validate_board(["**** ****", "***1 ****", "** 3****", \
"* 4 1****", " 9 5 ", " 6 83 *", \
"3 1 **", " 8 2***", " 2 ****"])
False
"""
if check_board_size(board) and \
check_board_form(board) and \
check_numbers(board):
return True
return False
if __name__ == "__main__":
import doctest
doctest.testmod()