-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.py
179 lines (114 loc) · 4.48 KB
/
backend.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
import glob
import os
from colors import Color
def get_folder_file_list(folder_name):
folder_location = folder_name + "/*.sud"
file_list = glob.glob(folder_location)
for index in range(len(file_list)):
file_list[index] = os.path.basename(file_list[index])
return file_list
def get_file_location(folder_location, file_name):
return folder_location + "/" + file_name
def get_file_string(file_location):
file_string = open(file_location, "r").read()
final_string = ""
for line in file_string:
final_string = final_string + line.strip()
return final_string
def get_puzzle_category_list(file_list):
easy_list = []
medium_list = []
hard_list = []
for file_name in file_list:
if "easy" in file_name:
easy_list.append(file_name)
if "medium" in file_name:
medium_list.append(file_name)
elif "hard" in file_name:
hard_list.append(file_name)
return [easy_list, medium_list, hard_list]
def get_puzzle_answer_name(puzzle_name):
return puzzle_name.replace(".sud", "") + "_answer.sud"
def get_grid(sudoku_string):
grid = []
j = 0
row_string = ""
for i in range(len(sudoku_string)):
j = j + 1
row_string = row_string + sudoku_string[i]
if j == 9:
row_list = list(row_string)
for index in range(len(row_list)):
if row_list[index] == ".":
row_list[index] = " "
grid.append(row_list)
row_string = ""
j = 0
return grid
def put_to_grid(value, position, grid):
print(position)
x = position[0]
y = position[1]
if value == "0":
grid[y][x] = " "
else:
grid[y][x] = Color.GREEN + str(value) + Color.END
return grid
def colorize_grid(grid, color):
color_end = Color.END
for y in range(len(grid)):
for x in range(len(grid)):
if grid[y][x] != " ":
grid[y][x] = color + grid[y][x] + color_end
return grid
def decolorize_grid(grid):
for y in range(len(grid)):
for x in range(len(grid[y])):
if Color.END in grid[y][x] or Color.UNDERLINE in grid[y][x]:
grid[y][x] = grid[y][x].replace(Color.END, "")
grid[y][x] = grid[y][x].replace(Color.UNDERLINE, "")
if Color.CYAN in grid[y][x] or Color.GREEN in grid[y][x]:
grid[y][x] = grid[y][x].replace(Color.CYAN, "")
grid[y][x] = grid[y][x].replace(Color.GREEN, "")
if Color.YELLOW in grid[y][x] or Color.RED in grid[y][x]:
grid[y][x] = grid[y][x].replace(Color.YELLOW, "")
grid[y][x] = grid[y][x].replace(Color.RED, "")
return grid
# counts number of blanks in grid
def count_blank(grid):
blanks = 0
for grid_row in grid:
for cell in grid_row:
if cell == " ":
blanks = blanks + 1
return blanks
def get_checked_grid(puzzle_grid, answer_grid):
puzzle_grid = decolorize_grid(puzzle_grid)
answer_grid = decolorize_grid(answer_grid)
for y in range(len(puzzle_grid)):
for x in range(len(puzzle_grid[y])):
if puzzle_grid[y][x] != answer_grid[y][x]:
if puzzle_grid[y][x] == " ":
puzzle_grid[y][x] = "_"
puzzle_grid[y][x] = Color.RED + puzzle_grid[y][x] + Color.END
else:
if puzzle_grid[y][x] == answer_grid[y][x]:
puzzle_grid[y][x] = Color.CYAN + puzzle_grid[y][x] + Color.END
return puzzle_grid
def count_mistakes(checked_grid):
mistakes = 0
for y in range(len(checked_grid)):
for x in range(len(checked_grid[y])):
if Color.RED in checked_grid[y][x]:
mistakes = mistakes + 1
return mistakes
def fill_grid(puzzle_name, puzzle_grid):
# USE puzzle_grid backend.fill_grid(puzzle_name, puzzle_grid) TO ACCESS FUNCTION
answer_grid_name = get_puzzle_answer_name(puzzle_name)
answer_location = get_file_location("puzzles_answers", answer_grid_name)
answer_string = get_file_string(answer_location)
answer_grid = get_grid(answer_string)
for y in range(len(puzzle_grid)):
for x in range(len(puzzle_grid)):
puzzle_grid[y][x] = answer_grid[y][x]
return answer_grid