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:
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):
def _determineIfSolverState(self, line):
@ -1221,6 +1230,21 @@ class BlackHoleSolver_Hint(Base_Solver_Hint):
board = self.calcBoardString()
if DEBUG:
print('--------------------\n', board, '--------------------')
if use_bh_solve_lib:
global bh_solve_lib_obj
bh_solve_lib_obj = bh_solve_lib_obj.new_bhs_user_handle()
bh_solve_lib_obj.read_board(
board=board,
game_type=game_type['preset'],
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'])]
@ -1230,13 +1254,17 @@ class BlackHoleSolver_Hint(Base_Solver_Hint):
args += ['--wrap-ranks']
command = self.BLACK_HOLE_SOLVER_COMMAND + ' ' + ' '.join(args)
pout, perr = self.run_solver(command, board)
if DEBUG:
start_time = time.time()
result = ''
if use_bh_solve_lib:
ret_code = bh_solve_lib_obj.resume_solution()
else:
pout, perr = self.run_solver(command, board)
for sbytes in pout:
s = six.text_type(sbytes, encoding='utf-8')
if DEBUG >= 5:
@ -1248,9 +1276,29 @@ class BlackHoleSolver_Hint(Base_Solver_Hint):
break
self._setText(iter=0, depth=0, states=0)
self.solver_state = result.lower()
hints = []
if use_bh_solve_lib:
self.solver_state = (
'solved' if ret_code == 0 else
('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]
hints.append([1, src, None])
else:
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:
@ -1260,7 +1308,8 @@ class BlackHoleSolver_Hint(Base_Solver_Hint):
hints.append([1, game.s.talon, None])
continue
m = re.match('Total number of states checked is ([0-9]+)\\.', s)
m = re.match(
'Total number of states checked is ([0-9]+)\\.', s)
if m:
self._setText(iter=int(m.group(1)))
continue
@ -1280,6 +1329,8 @@ class BlackHoleSolver_Hint(Base_Solver_Hint):
src = game.s.rows[found_stack_idx]
hints.append([1, src, None])
pout.close()
perr.close()
if DEBUG:
print('time:', time.time()-start_time)
@ -1287,9 +1338,6 @@ class BlackHoleSolver_Hint(Base_Solver_Hint):
hints.append(None)
self.hints = hints
pout.close()
perr.close()
class FreeCellSolverWrapper: