-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
207 lines (179 loc) · 6.95 KB
/
solver.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
import clips
import os
import re
from board import Board
from bruteforce import Bruteforce
from constant import Constant, is_bomb, is_safe, is_undef
class MinesweeperSolver():
def __init__(self, n, bombCnt, bombPos):
self.board = Board(n, bombCnt, bombPos)
def solve(self):
def count_bomb(i, j):
ret = 0
for ni, nj in self.board.adjList[i][j]:
if (self.board.board[ni][nj] == Constant.BOMB):
ret += 1
return ret
def count_slot(i, j):
ret = 0
for ni, nj in self.board.adjList[i][j]:
if (self.board.board[ni][nj] == Constant.UNDEF):
ret += 1
return ret
env = clips.Environment()
env.load('minesweeper.clp')
def setup_env():
env.reset()
template_index = '-1'
for i in range(self.board.size):
template_index += ' ' + str(i)
template_index += ' -1'
template_string = '(index-x ' + template_index + ')'
env.assert_string(template_string)
template_string = '(index-y ' + template_index + ')'
env.assert_string(template_string)
env.define_function(count_bomb)
env.define_function(count_slot)
rule = """
(defrule detect-bomb
(index-x $? ?x $?)
(index-y $? ?y $?)
(safe-pos (x ?x) (y ?y) (val ?val))
(test (> (count_slot ?x ?y) 0))
(test (eq ?val (+ (count_bomb ?x ?y) (count_slot ?x ?y))))
=>
(assert (is-unsafe-around (x ?x) (y ?y)))
)
"""
env.build(rule)
rule = """
(defrule detect-slot
(index-x $? ?x $?)
(index-y $? ?y $?)
(safe-pos (x ?x) (y ?y) (val ?val))
(test (> (count_slot ?x ?y) 0))
(test (eq ?val (count_bomb ?x ?y)))
=>
(assert (is-safe-around (x ?x) (y ?y)))
)
"""
env.build(rule)
for rule in tuple(env.rules()):
rule.watch_firings = True
def find_bomb():
setup_env()
template = env.find_template('bomb-pos')
for x in self.board.bombFound:
new_fact = template.new_fact()
new_fact['x'] = x[0]
new_fact['y'] = x[1]
template = env.find_template('empty-slot')
for i in range(self.board.size):
for j in range(self.board.size):
if (self.board.board[i][j] != Constant.UNDEF):
continue
new_fact = template.new_fact()
new_fact['x'] = i
new_fact['y'] = j
new_fact.assertit()
template = env.find_template('safe-pos')
for i in range(self.board.size):
for j in range(self.board.size):
if (not is_safe(self.board.board[i][j])):
continue
new_fact = template.new_fact()
new_fact['x'] = i
new_fact['y'] = j
new_fact['val'] = self.board.board[i][j]
new_fact.assertit()
ret_move = []
ret_agenda = []
agenda = ''
try:
activations = tuple(env.activations())
for act in activations:
agenda += str(act) + '\n'
env.run()
for fact in env.facts():
strfact = str(fact).replace('(', ' ').replace(')', ' ')
words = strfact.split(' ')
print(strfact)
bomb = False
safe = False
val = []
bef = ''
for word in words:
if (word == "bomb-at"):
bomb = True
elif (word == "safe-at"):
safe = True
if (bomb or safe):
if (bef == 'x' or bef == 'y'):
val.append(int(word))
bef = word
if (safe or bomb):
if (self.board.board[val[0]][val[1]] == Constant.UNDEF):
ret_move.append(self.board.make_assert(val[0], val[1], bomb))
ret_agenda.append(agenda)
except Exception as e:
return (ret_move, ret_agenda)
return (ret_move, ret_agenda)
def use_bruteforce():
bf = Bruteforce(self.board)
ret_move = bf.run()
ret_agenda = []
for i in range(len(ret_move)):
ret_agenda.append('bruteforce')
return (ret_move, ret_agenda)
# MAIN PROCESS
ret_move = []
ret_agenda = []
step = 0
while (len(self.board.bombFound) < self.board.bombCnt and step < 101):
try:
if (step == 0):
ret_move.append(self.board.make_assert(0, 0, False))
ret_agenda.append('initial move: click on (0, 0)')
else:
progress = find_bomb()
if (len(progress[0]) == 0):
progress = use_bruteforce()
ret_move = ret_move + progress[0]
ret_agenda = ret_agenda + progress[1]
step += 1
except Exception as e:
print('Error:', e)
step += 1
if (len(self.board.bombFound) == self.board.bombCnt):
print('SELURUH BOMB BERHASIL DITEMUKAN!!')
else:
print('TIDAK SEMUA BOMB BERHASIL DITEMUKAN.')
print(f'DISELESAIKAN DALAM: {step} step')
ret_facts = []
for fact in env.facts():
strfact = str(fact).replace('(', ' ').replace(')', ' ')
ret_facts.append(strfact)
# print(f'Moves\n{ret_move}')
return (ret_move, ret_agenda,ret_facts)
if __name__ == '__main__':
pass
n = 4
bombCnt = 3
bombPos = [(0,3),(1,2),(1, 3)]
solver = MinesweeperSolver(n, bombCnt, bombPos)
temp = solver.solve()
# ==== Kalo mau input manual ====
# n = int(input())
# bombCnt = int(input())
# bombPos = []
# for i in range(bombCnt):
# x, y = [int(a) for a in input().split(',')]
# bombPos.append((x, y))
# ==== Kalo malas input manual ====
# for i in range(len(temp[0])):
# print('Iteration:', i)
# print('move:')
# print(temp[0][i])
# print('agenda:')
# print(temp[1][i])
# print()