def test_correct_path(database_dir): for pid in PuzzleManager.getPuzzleIds(): p_cls = PuzzleManager.getPuzzleClass(pid) for variant in p_cls.test_variants: s_cls = PuzzleManager.getSolverClass(pid, test=True) solver = s_cls(p_cls.generateStartPosition(variant), dir_path=database_dir) puzzle = p_cls.generateStartPosition(variant) solver.solve() if puzzle.numPositions: assert puzzle.numPositions >= len( solver._remoteness ), "{} defined numPositions to be {} but solver calculated {}".format( puzzle.name, puzzle.numPositions, len(solver)) while puzzle.primitive() != PuzzleValue.SOLVABLE: assert (solver.getValue(puzzle) == PuzzleValue.SOLVABLE ), "{} not SOLVABLE".format( puzzle.toString(mode="minimal")) positions = generateMovePositions(puzzle) prev_remote = solver.getRemoteness(puzzle) b = False for pos in positions: next_remote = solver.getRemoteness(pos[1]) if (next_remote != PuzzleValue.UNSOLVABLE and next_remote < prev_remote): puzzle = pos[1] b = True if not b: raise AssertionError( "Puzzle {} has {} has no moves to reach solution". format(puzzle.__class__.name, puzzle.toString(mode="minimal")))
def init_data(): for p_cls in PuzzleManager.getPuzzleClasses(): if data["TESTING"]: variants = p_cls.test_variants else: variants = p_cls.variants for variant in variants: s_cls = PuzzleManager.getSolverClass(p_cls.id, variant) puzzle = p_cls.generateStartPosition(variant) solver = s_cls(puzzle, dir_path=data['DATABASE_DIR']) solver.solve(verbose=True)
def server_start(): # Check which Puzzles have been solved or not solved for p_cls in PuzzleManager.getPuzzleClasses(): if p_cls.id not in puzzle_solved_variants: puzzle_solved_variants[p_cls.id] = {} variants = p_cls.variants for variant in variants: p_cls = PuzzleManager.getPuzzleClass(p_cls.id) s_cls = PuzzleManager.getSolverClass(p_cls.id, variant) puzzle = p_cls.generateStartPosition(variant) solver = s_cls(puzzle, dir_path=app.config['DATABASE_DIR']) if os.path.exists(solver.path): puzzle_solved_variants[p_cls.id][variant] = solver
def database_dir(tmpdir): global db_dir if not db_dir is None: return db_dir for p_cls in PuzzleManager.getPuzzleClasses(): variants = p_cls.test_variants if not variants: warnings.warn( UserWarning( "{} does not have any test variants. It's correctness may vary." .format(p_cls.name))) for variant in variants: s_cls = PuzzleManager.getSolverClass(p_cls.id, variant) puzzle = p_cls.generateStartPosition(variant) solver = s_cls(puzzle, dir_path=tmpdir) solver.solve() db_dir = tmpdir return db_dir
def check_available(puzzle_id, variant=None): if puzzle_id not in puzzle_solved_variants: puzzle_solved_variants[puzzle_id] = {} if variant is None: return "unknown" elif len(puzzle_solved_variants[puzzle_id]) == 0: return "unavailable" elif variant is None or variant in puzzle_solved_variants[puzzle_id]: return "available" p_cls = PuzzleManager.getPuzzleClass(puzzle_id) s_cls = PuzzleManager.getSolverClass(p_cls.id, variant) puzzle = p_cls.generateStartPosition(variant) solver = s_cls(puzzle, dir_path=app.config['DATABASE_DIR']) import os if os.path.exists(solver.path): puzzle_solved_variants[puzzle_id][variant] = solver return "available" return "not available"
def puzzle_position(puzzle_id, variant_id, position): validate(puzzle_id, variant_id, position) puzzle = PuzzleManager.getPuzzleClass(puzzle_id).fromString(position) solver_cls = PuzzleManager.getSolverClass(puzzle_id, variant_id, app.config['TESTING']) s = solver_cls(puzzle, dir_path=app.config['DATABASE_DIR']) moves = generateMovePositions(puzzle) response = { "position": puzzle.toString(), "remoteness": s.getRemoteness(puzzle), "value": s.getValue(puzzle), "moves": { str(move[0]): { "position": move[1].toString(), "remoteness": s.getRemoteness(move[1]), "value": s.getValue(move[1]) } for move in moves } } return format_response(response)
help="Puzzle plays itself") parser.add_argument("-l", "--list", action="store_true", help="Lists puzzles and their ids") args = parser.parse_args() if not PuzzleManager.hasPuzzleId(args.puzzleid): print("Possible puzzles:") print("\n".join(PuzzleManager.getPuzzleIds())) raise Exception("Puzzleid is not recorded in PuzzleList") p_cls = PuzzleManager.getPuzzleClass(args.puzzleid) puzzle = None if args.variant: puzzle = p_cls.generateStartPosition(args.variant) if args.position: puzzle = p_cls.deserialize(args.position) if not puzzle: puzzle = p_cls() if args.info or args.auto: s_cls = PuzzleManager.getSolverClass(args.puzzleid, args.variant) solver = s_cls(puzzle) else: solver = None TUI(puzzle, solver=solver, info=args.info, auto=args.auto).play()