Implement this protocol to describe your gameplay model so that a strategist object can plan game moves.
- iOS 9.0+
- macOS 10.11+
- Mac Catalyst 13.0+Beta
- tvOS 9.0+
You adopt this protocol to describe the gameplay of your turn-based game for use by a
GKStrategist object. The strategist uses your game model class (that is, the class you create to adopt this protocol), along with other custom classes you create (adopting the
GKGame protocols), to find optimal moves.
GameplayKit relies on your game model class for several parts of its strategy algorithm.
Identifying possible changes to the game state. Your
gamemethod and your move class (a custom class implementing the
GKGameprotocol) describe the set of moves available during a given player’s turn.
Simulating future moves on a copy of the game. Your
setmethod allows GameplayKit to work with a separate instance of the game model—that is, not the one representing the actual game in play—and your
apply(_:)uses the information in your move class to perform hypothetical moves on that separate copy of the game.
Rating the desirability of possible future states of the game. Each time GameplayKit performs a hypothetical move in its copy of the game model, it calls your
score(for:)method to evaluate that state of the game from the perspective of a particular player.
When you use a strategist to plan moves in your game, it uses your game model to combine these parts into a strategy: By identifying, performing, and rating the success of possible future moves, the strategist can choose a move most likely to result in a future win. This process involves using the
set methods to evaluate many possible states of a game model—for best results, ensure that your game model class contains only the information critical to describing your game and that it can copy that state quickly.