-
Notifications
You must be signed in to change notification settings - Fork 0
/
saveAndLoad.py
92 lines (83 loc) · 3.37 KB
/
saveAndLoad.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
from os.path import exists
def write_nurikabe(ind):
"""
User friendly way for saving nurikabe. The description of the nurikabe is saved to 'nurikabe<ind>.txt'.
:param ind: index of the nurikabe.
:return: width, heigh, island stems, where island stems is a list of form [(stem_x, stem_y, island size), ...].
"""
width = int(input("Width: "))
height = int(input("Height: "))
print("Enter the island stems. For a given coordinate y, enter size<space>x, for each island.")
print("Once you are done with the current y, enter the empty string''.")
stems = []
for y in range(height):
end = False
print("y =", y)
while not end:
try:
a = input("size<space>x: ")
if a == "":
end = True
else:
size, x = [int(t) for t in a.split(" ")]
stems.append((x, y, size))
except ValueError:
print("Something went frong, the last description has no effect.")
with open("nurikabe{}.txt".format(ind), "w") as f:
print("width;" + str(width), file=f)
print("height;" + str(height), file=f)
print("stems;" + str(stems), file=f)
return width, height, stems
def load_nurikabe(ind):
"""
Loads nurikabe from the file 'nurikabe<ind>.txt'.
:param ind: index of the nurikabe
:return: Description of the nurikabe.
"""
nuri = []
with open("nurikabe{}.txt".format(ind)) as f:
for x in f:
nuri.append(eval(x[x.find(";") + 1:].strip()))
return nuri
def write_solution(width, height, ind):
"""
User friendly way for saving the pencil-paper solution.
The solution is saved to the file 'nurikabe<ind>solution.txt'.
:param width: width of the nurikabe
:param height: height of the nurikabe
:param ind: index of the nurikabe
:return: The solution
"""
a = [["" for _ in range(width)] for _ in range(height)]
print("Enter the solution of dimensions {} x {}".format(width, height))
print("If a line in the solution is '7 2 sea sea 3 unknown 1', enter '72003-1'.")
print("If the size of an island is greater than 9, i.e. needs more than one place,")
print("enter first a string s for which len(s) != width and than follow the example:")
print("For a line '7 12 sea sea unknown 123' enter '7,12,0,0,-,123'")
for i in range(height):
vrsta = input("y = {}: ".format(i))
u = vrsta.strip()
while len(u) != width:
print("Wrong width:", len(u), "try again, now coma-separated!")
vrsta = input("y = {}: ".format(i))
u = vrsta.strip().split(",")
for j in range(width):
a[i][j] = "" if u[j] == "-" else int(u[j])
out = "nurikabe{}solution.txt".format(ind)
if exists(out):
ali = bool(input("Solution already exists. Do you want to overwrite it [true/false]? "))
else:
ali = True
if ali:
with open(out, "w") as f:
print(a, file=f)
return a
def load_solution(ind):
"""
Loads solution for nurikabe with given index.
:param ind: index of the nurikabe
:return: solution of the nurikabe
"""
solution = "nurikabe{}solution.txt".format(ind)
with open(solution) as f:
return eval(f.readline())