AIGameScene

class AIGameScene : ChallengeGameScene

A game scene that an AI agent can control.

In the game scene, the player can provide a move budget and strategy to solve the puzzle. A default random move strategist with a budget of one move is provided when these options aren’t available.

Important

Scenes that subclass the AI game scene must be running macOS 10.15 Catalina or higher.
  • The console view model that hosts all of the console messages.

    Declaration

    Swift

    var console: ConsoleViewModel
  • The strategist that will be playing this scene.

    Declaration

    Swift

    var strategist: AIGameStrategist?
  • The console window associated with this scene.

    Declaration

    Swift

    var consoleWindowController: NSWindowController?
  • Load the scene, set an initial state, and attempt to solve the puzzle.

    Agent testing mode will need to be enabled, and options for the agent type and move budget should be available. In cases where this isn’t available, the random move agent will be used and will have a budget of one move.

    Declaration

    Swift

    override func sceneDidLoad()
  • Close the simulator console and prevent scene-saving.

    Declaration

    Swift

    override func willMove(from view: SKView)
  • Close out of the current console and re-initialize the console.

    Declaration

    Swift

    override func didMove(to view: SKView)
  • Attempt to solve the level by generating batches of actions to run infinitely or until the puzzle is solved.

    To prevent the game from locking up due to infinite recursion, this function is designed to make a bunch of pre-determined moves in batches of the maximum agent budget, act on them, and repeat this process.

    Declaration

    Swift

    func solve(with rate: Int?)

    Parameters

    rate

    The maximum rate that the agent can make moves. If none is provided, it will use a default of 10.

  • Get a predetermined set of actions with a maximum budget.

    After every move, the state is reassesed. If the state resulting from an action causes the solution, no further actions will be generated.

    Complexity

    This method takes O(n) time since, at the very worst case, a list of actions with the max budget can be created.

    Declaration

    Swift

    func strategize(with budget: Int) -> [AIGameDecision]

    Parameters

    budget

    The maximum number of moves to get a strategy for.

    Return Value

    A list of actions for the agent to take.

  • Prevent the player from doing anything that could influence state updates.

    Declaration

    Swift

    func blockInput()
  • Prevent keyboard input.

    Declaration

    Swift

    override func keyDown(with event: NSEvent)
  • Run the AI’s version of didFinishUpdate.

    Declaration

    Swift

    override func didFinishUpdate()
  • Capture the current scene as a game state.

    Declaration

    Swift

    func getState() -> AIAbstractGameState?

    Return Value

    An abstract version of the world as a game state that the agent can use.

  • Apply a game state update to the scene.

    Declaration

    Swift

    func apply(_ action: AIGameDecision)

    Parameters

    action

    The action that will be performed to change the state.

  • Perform a set of actions and update the state of the scene.

    Declaration

    Swift

    func repeatAfterMe(_ moves: [AIGameDecision])

    Parameters

    moves

    The list of actions to perform.

  • Get the appropriate strategy based on the type of input passed.

    Important

    This function requires macOS 10.15 Catalina or later as it uses the new opaque type system.

    Declaration

    Swift

    func getStrategy(with state: AIAbstractGameState) -> some AIGameStrategist

    Parameters

    state

    The state to create a strategist from.

    Return Value

    An AI game strategist that will be used for the game scene. If none was provided, the random move strategist will be used instead.

  • Initialize the console window and begin streaming information to the console view model.

    Declaration

    Swift

    func initConsole()
  • Run any post-update logic and check input states.

    Declaration

    Swift

    func aiFinish()