-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSudoku_interface.py
More file actions
70 lines (57 loc) · 2.17 KB
/
Copy pathSudoku_interface.py
File metadata and controls
70 lines (57 loc) · 2.17 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
# Create by Packetsss
# Personal use is allowed
# Commercial use is prohibited
import random
import ast
class interface:
def __init__(self, solution=False, random=False):
self.shuffle = random
self.solution = solution
self.base = 3
self.side = self.base * self.base
def foundation(self):
if not self.shuffle:
with open("src\\puzzle1.txt") as f:
for line in f:
if len(line) < 10:
break
last_line = line
a = ast.literal_eval(last_line)
if not self.solution:
return a["puzzle"]
else:
return a["solution"]
else:
with open("src\\puzzle1.txt") as f:
ct = 0
for l in f:
ct += 1
rand = random.randint(1, ct)
with open("src\\puzzle1.txt") as f:
for i, line in enumerate(f):
if i == rand + 1:
l = line
break
a = ast.literal_eval(l)
if not self.solution:
return a["puzzle"]
else:
return a["solution"]
def expandLine(self, line):
return line[0] + line[5:9].join([line[1:5] * (self.base - 1)] * self.base) + line[9:13]
def print_board(self, board):
line0 = self.expandLine("╔═══╤═══╦═══╗")
line1 = self.expandLine("║ . │ . ║ . ║")
line2 = self.expandLine("╟───┼───╫───╢")
line3 = self.expandLine("╠═══╪═══╬═══╣")
line4 = self.expandLine("╚═══╧═══╩═══╝")
symbol = " 1234567890"
nums = [[""] + [symbol[n] for n in row] for row in board]
print(line0)
for r in range(1, self.side + 1):
print("".join(n + s for n, s in zip(nums[r - 1], line1.split("."))))
print([line2, line3, line4][(r % self.side == 0) + (r % self.base == 0)])
def main():
interface(solution=True).print_board(interface().foundation())
if "__main__" == __name__:
main()