T0nyX1ang / noqx

Extended logic puzzle solver of noq.
https://noqx.tonyxiang.site/
GNU General Public License v3.0
4 stars 0 forks source link

[Refactor/Feature] Support solver inheritance #30

Closed T0nyX1ang closed 6 months ago

T0nyX1ang commented 6 months ago

This feature aims to make solvers behave like plugins.

The following code might be used:

def solve(E: encoding) -> str:
    # do anything or use the default behavior
T0nyX1ang commented 6 months ago

We "simulated" the inheritance. As a result, there is a higher level function run() to call:

def run(puzzle_type: str, puzzle_content: str) -> str:
    """Run the solver."""
    module = modules[puzzle_type]

    if not hasattr(module, "encode"):
        module.encode = encode

    if not hasattr(module, "solve"):
        raise NotImplementedError("Solver not implemented.")

    if not hasattr(module, "decode"):
        module.decode = decode

    puzzle_encoded: Encoding = module.encode(puzzle_content)
    solutions_encoded: List[Dict[str, str]] = module.solve(puzzle_encoded)
    solutions_decoded: str = module.decode(solutions_encoded)
    return solutions_decoded