Where to start with GKMinmaxStrategy?

I was wondering if anyone here had any luck with the GKMinmaxStrategy. This class / function was shown at WWDC, but most of the example code was in Objective-C, which was a disappointment.

Another Stone Flipper game (Reversi / Othello) appeared in WWDC videos for GameplayKit, but they haven't posted the code yet (yet?).

Has anyone had any luck with this? I was hoping to try this with a simple tic-tac-toe game, but I'm not sure how to start.

+3


source to share


2 answers


I agree that this is a tricky structure to learn - I just finished writing a tutorial about GameplayKit and GKMinmaxStrategy and it wasn't the best feat. If you follow the guide, he builds the complete game from scratch, explaining how it all fits together. You may find this useful as a starting point, at least.

I hope Apple improves its documentation before iOS 9 is final!

If you want to dive right in, here's the least you need to know:



  • Make sure your game model (data) and view (layouts) are saved separately.
  • Make your model an implementation of the protocol NSCopying

    , because it will be copied many times as the AI ​​runs.
  • You should also make it implement a protocol GKGameModel

    that requires you to be able to enumerate the available moves, apply movement to copies of the board (actually, not really), and then evaluate the players' ratings after that.
  • Every "move" (for whatever it is in your game) must follow a protocol GKGameModelUpdate

    , so it will be a class you create that defines a particular move. You will get this back when you have chosen the best move, so it will contain something like "move the knight to E4".
  • If your game is not rated (in my tutorial I used Four in a Row, which has exactly this problem), then you need to come up with a heuristic that estimates roughly how good a move was.
  • Run the AI ​​on a background thread to make sure your UI remains responsive, then push the result back to the foreground thread when you're ready to make changes to the UI.

If you find that the AI ​​is slow, either limit the number of moves it can take or reduce the depth of view.

+3


source


Here's a tutorial on GKMinmaxStrategy TicTacToe in Swift .

This should explain how things work and provide some guidance on how to make good AI. The strategist is certainly not a template for any kind of gaming software, it just provides structure. 95% of the work is still on your shoulders.;)



The code is available here . Please note that it requires not only Xcode 7 but also OS X 10.11. It would be easy to adapt to iOS 9 though.

+2


source







All Articles