Class Solver

java.lang.Object
com.codename1.gaming.platformer.Solver

public final class Solver extends Object

Proves a level can be finished by finding a run that finishes it.

The simulation is deterministic, so a run is just its input, and any state a run reached can be reached again by replaying that input from the start. The solver keeps an archive of the places it has reached (a tile, whether the hero stood, the keys held and the switches thrown), each with the shortest input that got there. Every round it goes back to one of them -- favouring the ones nearest the goal and the ones it has tried least -- and presses buttons at random from there for a moment, archiving whatever is new. This is the "Go-Explore" idea, and platformers suit it well: being somewhere new is almost always progress.

Run it before a level is published (an editor, or a server checking a level a language model wrote), over every shipped level in a test, or offline to record the run a menu's attract mode plays.

A game whose progress is something the solver cannot see -- an ability picked up, a counter its GameRules keep -- says so with setProgress, so reaching the same tile with more of it counts as a new place.

It drives one player. A level for several players needs its own search.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    What a game counts as progress besides where the hero stands.
    static interface 
    Makes the world each run starts from, for a game whose levels need more than LevelData#createWorld(long): an EntityFactory, GameRules.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Solver(LevelData level, long seed, long searchSeed)
    A solver for one level and seed.
  • Method Summary

    Modifier and Type
    Method
    Description
    int[]
    Where the search came closest to the goal, for telling a level's author where it got stuck.
    int
    How many distinct places the search has reached.
    long
    Ticks simulated so far, the solver's measure of effort.
    void
    setChords(int[] chords)
    The button chords the search may hold, for a game whose input is not the whole pad -- an auto-runner holds right and only chooses to jump.
    void
    setMaxTicks(int ticks)
    The longest run the solver will consider, in ticks.
    void
    Adds the game's own progress to what makes a place new.
    void
    Builds each run's world with worlds instead of the level alone.
    solve(long budgetTicks)
    Searches for a finishing run.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Solver

      public Solver(LevelData level, long seed, long searchSeed)

      A solver for one level and seed.

      Parameters
      • level: the level
      • seed: the world seed runs use
      • searchSeed: seeds the solver's own choices, so a search repeats
  • Method Details

    • setChords

      public void setChords(int[] chords)

      The button chords the search may hold, for a game whose input is not the whole pad -- an auto-runner holds right and only chooses to jump.

      Parameters
      • chords: Input bit combinations; null restores the default set
    • setWorlds

      public void setWorlds(Solver.Worlds worlds)

      Builds each run's world with worlds instead of the level alone. The world must be the one the game plays, or a found route will not replay.

      Parameters
      • worlds: the world maker, or null for LevelData#createWorld(long)
    • setMaxTicks

      public void setMaxTicks(int ticks)

      The longest run the solver will consider, in ticks. Five minutes by default.

      Parameters
      • ticks: the cap
    • setProgress

      public void setProgress(Solver.Progress progress)

      Adds the game's own progress to what makes a place new.

      Parameters
      • progress: the game's measure, or null
    • getSimulatedTicks

      public long getSimulatedTicks()

      Ticks simulated so far, the solver's measure of effort.

      Returns

      the tick count

    • solve

      public InputLog solve(long budgetTicks)

      Searches for a finishing run.

      Parameters
      • budgetTicks: how many ticks of simulation to spend before giving up
      Returns

      the input of a run that reaches the goal, or null when none was found in the budget

    • getClosest

      public int[] getClosest()

      Where the search came closest to the goal, for telling a level's author where it got stuck.

      Returns

      {column, row, distance in tiles}, or null before a search

    • getExplored

      public int getExplored()

      How many distinct places the search has reached.

      Returns

      the archive size