Skip to content

Instantly share code, notes, and snippets.

@LearnCocos2D
Last active May 31, 2017 17:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LearnCocos2D/4acd1eb6733192aab180 to your computer and use it in GitHub Desktop.
Save LearnCocos2D/4acd1eb6733192aab180 to your computer and use it in GitHub Desktop.
GKMinmaxStrategist stub classes implementing the GKGameModel* protocols for Swift 2.0
/*
Swift classes implementing the GKMinmaxStrategizer protocols, without any logic.
You can use these as a template, copy & paste them into your project and start working. ;)
This gist is provided by http://tilemapkit.com and released into public domain.
Here's my tutorial about GKMinmaxStrategist: http://tilemapkit.com/2015/07/gkminmaxstrategist-build-tictactoe-ai/
*/
class TheGameModel: NSObject, NSCopying, GKGameModel {
required override init() { // init required due to use of dynamicType() in copyWithZone
}
// ===========================================================================
// NSCopying Protocol
// ===========================================================================
func copyWithZone(zone: NSZone) -> AnyObject { // NSCopying protocol function
let copy = self.dynamicType.init()
copy.setGameModel(self) // this copies the game model state
return copy
}
// ===========================================================================
// GKGameModel Protocol
// ===========================================================================
private var _players : [GKGameModelPlayer]?
var players: [GKGameModelPlayer]? {
get {
// return the list of all players (both human and AI)
return _players
}
}
private var _activePlayer : GKGameModelPlayer?
var activePlayer: GKGameModelPlayer? {
get {
// return the player set to make the next move
return _activePlayer
}
}
func setGameModel(gameModel: GKGameModel) {
// copy the gameModel's state into ours (ie self.xyz = gameModel.xyz)
}
func gameModelUpdatesForPlayer(player: GKGameModelPlayer) -> [GKGameModelUpdate]? {
// create instances of GKGameModelUpdate for every possible move, then return them in an array
return nil
}
func applyGameModelUpdate(gameModelUpdate: GKGameModelUpdate) {
// have activePlayer change the game model according to the move described by gameModelUpdate
// then switch self.activePlayer to the next (other) player
}
func scoreForPlayer(player: GKGameModelPlayer) -> Int {
// From the perspective of player, calculate how desirable the current game model state is.
// Higher values indicate higher desirability. Use negative values to indicate disadvantage. Zero is neutral.
// Return Int.min (NSIntegerMin) if the move is not valid.
return 0
}
}
class TheGameModelPlayer : NSObject, GKGameModelPlayer {
// an integer uniquely identifying a specific player
private var _playerId: Int = 0
var playerId: Int {
get {
return _playerId;
}
set {
_playerId = newValue
}
}
}
class TheGameModelUpdate : NSObject, GKGameModelUpdate {
// value rates desirability of the move
private var _value: Int = 0
var value: Int {
get {
return _value
}
set {
_value = newValue
}
}
}
class SomeClass {
func setupStrategist() {
let strategist = GKMinmaxStrategist()
strategist.maxLookAheadDepth = 3
strategist.randomSource = GKARC4RandomSource()
strategist.gameModel = TheGameModel() // optionally call a custom initializer providing initial model state
}
}
@BiggestShoelaces
Copy link

class TheGameModel: NSObject, NSCopying, GKGameModel { Doesn't work. is there new code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment