Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. //
  2. // OXGameController.swift
  3. // NoughtsAndCrosses
  4. //
  5. // Created by Alejandro Castillejo on 6/2/16.
  6. // Copyright © 2016 Julian Hulme. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10.  
  11. class OXGameController {
  12.  
  13. var gameList:[OXGame]?
  14. private var currentGame: OXGame?
  15.  
  16.  
  17. class var sharedInstance: OXGameController {
  18. struct Static {
  19. static var instance:OXGameController?
  20. static var token: dispatch_once_t = 0
  21. }
  22.  
  23. dispatch_once(&Static.token) {
  24. Static.instance = OXGameController()
  25. }
  26. return Static.instance!
  27.  
  28. }
  29.  
  30. func getListOfGames() -> [OXGame]? {
  31. let random: Int = Int(arc4random_uniform(UInt32(9)) + 5)
  32. self.gameList = [OXGame](count: random, repeatedValue: OXGame())
  33. for game in self.gameList! {
  34. game.gameId = getRandomID()
  35. game.hostUser = User(email:"hostuser@gmail.com",password: "")
  36. }
  37.  
  38. return gameList
  39. }
  40.  
  41. func getCurrentGame() -> OXGame? {
  42. print("Getting current game")
  43. return currentGame
  44. }
  45.  
  46.  
  47. //Can only be called when there is an active game
  48. func playMove(index: Int) -> CellType{
  49. print("PlayingMove on 'network'")
  50. let cellType: CellType = (currentGame?.playMove(index))!
  51. return cellType
  52. }
  53.  
  54. //Simple random move, it will always try to play the first indexes
  55. func playRandomMove() -> (CellType, Int)? {
  56. print("Playing random move")
  57. if let count = currentGame?.board.count {
  58. for i in 0...count - 1 {
  59. if (currentGame?.board[i] == CellType.EMPTY){
  60. let cellType: CellType = (currentGame?.playMove(i))!
  61. print(cellType)
  62. print("Succesfully at: " + String(i))
  63. return (cellType, i)
  64. }
  65. }
  66. }
  67. print("Unsuccesfully")
  68. return nil
  69.  
  70. }
  71.  
  72. func createNewGame(hostUser:User) {
  73. print("Creating new network game")
  74. }
  75.  
  76.  
  77. func acceptGameNumber(gameId: String) -> OXGame? {
  78. print("Accepting network game")
  79. for game in self.gameList! {
  80. if (game.gameId == gameId) {
  81. currentGame = game
  82. print("Succesfully")
  83. return game
  84. }
  85.  
  86. }
  87. print("Not succesfully")
  88. return nil
  89.  
  90. }
  91.  
  92. func finishCurrentGame(){
  93. print("Finishing current game")
  94. currentGame = nil
  95. }
  96.  
  97. //Helper functions
  98. func getRandomID() -> String {
  99. let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  100. let len: Int = 10
  101. let randomString : NSMutableString = NSMutableString(capacity: len)
  102.  
  103. for _ in 1...len {
  104. let length = UInt32 (letters.length)
  105. let rand = arc4random_uniform(length)
  106. randomString.appendFormat("%C", letters.characterAtIndex(Int(rand)))
  107. }
  108.  
  109. return randomString as String
  110. }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement