AIGameScene

@available(macOS 10.15, *)
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 ten moves 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 strategist that will be playing this scene.

    Declaration

    Swift

    var strategist: AIGameStrategist?
  • 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 ten moves.

    Declaration

    Swift

    override func sceneDidLoad()
  • 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 getPredeterminedStrategy(max 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)
  • 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.

    Important

    This function has been renamed to AIGameScene.apply(_ action:).

    Declaration

    Swift

    @available(*, deprecated, renamed: "AIGameScene.apply")
    func setUpdate(_ state: AIGameDecision)

    Parameters

    state

    The action that will be performed to change the state.

  • 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

    @available(macOS 10.15, *)
    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.