-
Notifications
You must be signed in to change notification settings - Fork 0
/
kakuro_explorer.py
182 lines (116 loc) · 4.2 KB
/
kakuro_explorer.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
import matplotlib.pyplot as plt
import matplotlib.collections as collections
import numpy as np
import sys
b = 0.025
def fill_one_empty(ax, row, col):
row = row - 1
col = col - 1
s_c = [col, col+1, col+1, col]
s_r = [row, row, row+1, row+1]
ax.fill(s_c, s_r, 'k')
return ax
def fill_one_empty_g(ax, row, col):
row = row - 1
col = col - 1
s_c = [col, col+1, col+1, col]
s_r = [row, row, row+1, row+1]
ax.fill(s_c, s_r, 'g')
return ax
def fill_one_hint(ax, row, col, hint1, hint2):
s_hint1 = str(hint1)
s_hint2 = str(hint2)
ax.fill([col-1+b,col,col], [row-1,row-1,row-b], 'k')
ax.fill([col-1,col-1,col-b], [row-1+b,row,row], 'k')
ax.text(col-1+0.5, row+-1+0.35, s_hint1, color='w', fontsize=10, fontweight='bold')
ax.text(col-1+0.2, row+-1+0.8, s_hint2, color='w', fontsize=10, fontweight='bold')
return ax
def fill_hints(ax, row, col, hintS):
hint = hintS[0]
_type = hintS[1]
ax.fill([col-1+b,col,col], [row-1,row-1,row-b], 'tab:grey')
ax.fill([col-1,col-1,col-b], [row-1+b,row,row], 'tab:grey')
if(_type == "down"):
ax.text(col-1+0.1, row+-1+0.9, hint[0], color='w', fontsize=10, fontweight='bold')
if(_type == "right"):
ax.text(col-1+0.6, row+-1+0.25, hint[0], color='w', fontsize=10, fontweight='bold')
if(_type == "combined"):
ax.text(col-1+0.1, row+-1+0.9, hint[0], color='w', fontsize=10, fontweight='bold')
ax.text(col-1+0.6, row+-1+0.25, hint[1], color='w', fontsize=10, fontweight='bold')
def write_value(ax, row, col, val):
row = row - 1
col = col -1
ax.text(col + 0.5, row + 0.5, val, color='k', fontsize=12, fontweight='bold', ha='center', va='center')
return ax
def write_mark(ax, row, col):
row = row - 1
col = col -1
ax.text(col + 0.5, row + 0.5, "?", color='g', fontsize=12, fontweight='bold', ha='center', va='center')
def write_value_s(ax, row, col, val):
row = row - 1
col = col -1
ax.text(col + 0.5, row + 0.5, val, color='k', fontsize=9, fontweight='bold', ha='center', va='center')
return ax
def create_matrix(n):
fig, ax = plt.subplots()
ax.invert_yaxis()
for i in range(n+1):
ax.plot([0,n], [i,i], 'k-')
ax.plot([i,i], [0,n], 'k-')
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
return fig, ax
def assess_hint(hint):
hint = hint / 10
if(hint % 100 == 0):
return [[int(hint/100)], "right"]
else:
div = int(hint / 100)
rem = int(hint % 100)
if(div == 0 and rem != 0):
return [[rem], "down"]
if(div != 0 and rem != 0):
return [[rem, div], "combined"] ##[0] down [1] right
def read_file(_file):
reader = open(_file, "r")
readlines = reader.readlines()
m, n = readlines[0].strip("\n").split(" ")
m = int(m)
n = int(n)
board = [[] for i in range(m)]
print("board: ", board)
for i in range(1, len(readlines)):
vals = readlines[i].strip("\n").split(" ")
vals = [x for x in vals if x != ""]
print("i: ", i , "vals: ", vals)
for val in vals:
board[i-1].append(int(val))
return board, m, n
class Kakuro:
def __init__(self, _file):
self. board, self.m, self.n = read_file(_file)
self.fig, self.ax = create_matrix(self.m)
self.size = self.m
def print_matrix(self):
plt.show()
def generate_board(self):
boardSol = self.board
i = 0
for row in boardSol:
j = 0
for item in row:
if(item == -1):
fill_one_empty(self.ax, i+1, j+1)
elif(item > 0 and item <= 9):
write_value(self.ax, i+1, j+1, item)
elif(item == -2):
write_mark(self.ax, i+1, j+1)
else:
fill_hints(self.ax, i+1, j+1, assess_hint(item))
j = j + 1
i = i + 1
plt.show()
if __name__ == "__main__":
_file = sys.argv[1]
k = Kakuro(_file)
k.generate_board()