1
0
Fork 0
mirror of https://github.com/shlomif/PySolFC.git synced 2025-04-05 00:02:29 -04:00

implement black_hole_solver lib.

This commit is contained in:
Shlomi Fish 2020-06-19 16:19:12 +03:00
parent 7eb29a216f
commit 18a460a1e7

View file

@ -836,6 +836,15 @@ try:
except BaseException: except BaseException:
pass pass
use_bh_solve_lib = False
try:
import black_hole_solver
bh_solve_lib_obj = black_hole_solver.BlackHoleSolver()
use_bh_solve_lib = True
except BaseException:
pass
class FreeCellSolver_Hint(Base_Solver_Hint): class FreeCellSolver_Hint(Base_Solver_Hint):
def _determineIfSolverState(self, line): def _determineIfSolverState(self, line):
@ -1221,65 +1230,107 @@ class BlackHoleSolver_Hint(Base_Solver_Hint):
board = self.calcBoardString() board = self.calcBoardString()
if DEBUG: if DEBUG:
print('--------------------\n', board, '--------------------') print('--------------------\n', board, '--------------------')
args = [] if use_bh_solve_lib:
args += ['--game', game_type['preset'], '--rank-reach-prune'] global bh_solve_lib_obj
args += ['--max-iters', str(self.options['max_iters'])] bh_solve_lib_obj = bh_solve_lib_obj.new_bhs_user_handle()
if 'queens_on_kings' in game_type: bh_solve_lib_obj.read_board(
args += ['--queens-on-kings'] board=board,
if 'wrap_ranks' in game_type: game_type=game_type['preset'],
args += ['--wrap-ranks'] place_queens_on_kings=(
game_type['queens_on_kings']
if ('queens_on_kings' in game_type) else True),
wrap_ranks=(
game_type['wrap_ranks']
if ('wrap_ranks' in game_type) else True),
)
bh_solve_lib_obj.limit_iterations(self.options['max_iters'])
else:
args = []
args += ['--game', game_type['preset'], '--rank-reach-prune']
args += ['--max-iters', str(self.options['max_iters'])]
if 'queens_on_kings' in game_type:
args += ['--queens-on-kings']
if 'wrap_ranks' in game_type:
args += ['--wrap-ranks']
command = self.BLACK_HOLE_SOLVER_COMMAND + ' ' + ' '.join(args) command = self.BLACK_HOLE_SOLVER_COMMAND + ' ' + ' '.join(args)
pout, perr = self.run_solver(command, board)
if DEBUG: if DEBUG:
start_time = time.time() start_time = time.time()
result = '' result = ''
for sbytes in pout: if use_bh_solve_lib:
s = six.text_type(sbytes, encoding='utf-8') ret_code = bh_solve_lib_obj.resume_solution()
if DEBUG >= 5: else:
print(s) pout, perr = self.run_solver(command, board)
m = re.search('^(Intractable|Unsolved|Solved)!', s.rstrip()) for sbytes in pout:
if m: s = six.text_type(sbytes, encoding='utf-8')
result = m.group(1) if DEBUG >= 5:
break print(s)
m = re.search('^(Intractable|Unsolved|Solved)!', s.rstrip())
if m:
result = m.group(1)
break
self._setText(iter=0, depth=0, states=0) self._setText(iter=0, depth=0, states=0)
self.solver_state = result.lower()
hints = [] hints = []
for sbytes in pout: if use_bh_solve_lib:
s = six.text_type(sbytes, encoding='utf-8') self.solver_state = (
if DEBUG: 'solved' if ret_code == 0 else
print(s) ('intractable'
if bh_solve_lib_obj.ret_code_is_suspend(ret_code)
else 'unsolved'))
self._setText(iter=bh_solve_lib_obj.get_num_times())
self._setText(
states=bh_solve_lib_obj.get_num_states_in_collection())
if self.solver_state == 'solved':
m = bh_solve_lib_obj.get_next_move()
while m:
found_stack_idx = m.get_column_idx()
if len(game.s.rows) > found_stack_idx >= 0:
src = game.s.rows[found_stack_idx]
if s.strip() == 'Deal talon': hints.append([1, src, None])
hints.append([1, game.s.talon, None]) else:
continue hints.append([1, game.s.talon, None])
m = bh_solve_lib_obj.get_next_move()
else:
self.solver_state = result.lower()
for sbytes in pout:
s = six.text_type(sbytes, encoding='utf-8')
if DEBUG:
print(s)
m = re.match('Total number of states checked is ([0-9]+)\\.', s) if s.strip() == 'Deal talon':
if m: hints.append([1, game.s.talon, None])
self._setText(iter=int(m.group(1))) continue
continue
m = re.match('This scan generated ([0-9]+) states\\.', s) m = re.match(
'Total number of states checked is ([0-9]+)\\.', s)
if m:
self._setText(iter=int(m.group(1)))
continue
if m: m = re.match('This scan generated ([0-9]+) states\\.', s)
self._setText(states=int(m.group(1)))
continue
m = re.match( if m:
'Move a card from stack ([0-9]+) to the foundations', s) self._setText(states=int(m.group(1)))
if not m: continue
continue
found_stack_idx = int(m.group(1)) m = re.match(
src = game.s.rows[found_stack_idx] 'Move a card from stack ([0-9]+) to the foundations', s)
if not m:
continue
hints.append([1, src, None]) found_stack_idx = int(m.group(1))
src = game.s.rows[found_stack_idx]
hints.append([1, src, None])
pout.close()
perr.close()
if DEBUG: if DEBUG:
print('time:', time.time()-start_time) print('time:', time.time()-start_time)
@ -1287,9 +1338,6 @@ class BlackHoleSolver_Hint(Base_Solver_Hint):
hints.append(None) hints.append(None)
self.hints = hints self.hints = hints
pout.close()
perr.close()
class FreeCellSolverWrapper: class FreeCellSolverWrapper: