Advertisement
Guest User

Untitled

a guest
Jun 6th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 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. import UIKit
  11. import SwiftyJSON
  12.  
  13.  
  14. class OXGameController: WebService {
  15.  
  16. // var gameList:[OXGame]? = []
  17. private var currentGame: OXGame = OXGame()
  18.  
  19.  
  20. class var sharedInstance: OXGameController {
  21. struct Static {
  22. static var instance:OXGameController?
  23. static var token: dispatch_once_t = 0
  24. }
  25.  
  26. dispatch_once(&Static.token) {
  27. Static.instance = OXGameController()
  28. }
  29. return Static.instance!
  30.  
  31. }
  32.  
  33. // func getListOfGames() -> [OXGame]? {
  34. //// print("Getting list of games")
  35. //
  36. // if(gameList?.count == 0){
  37. //
  38. // let random: Int = Int(arc4random_uniform(UInt32(3)) + 2)
  39. // //Create games
  40. // for _ in 1...random {
  41. // self.gameList?.append(createGameWithHostUser("hostuser@gmail.com"))
  42. //
  43. // }
  44. //
  45. // }
  46. //
  47. // return gameList
  48. //
  49. // }
  50.  
  51. func gameList(presentingViewController:UIViewController? = nil, viewControllerCompletionFunction:([OXGame]?,String?) -> ()) {
  52.  
  53.  
  54.  
  55. }
  56.  
  57. private func setCurrentGame(game: OXGame){
  58. currentGame = game
  59. }
  60.  
  61. func getCurrentGame() -> OXGame? {
  62. // print("Getting current game")
  63.  
  64. return currentGame
  65. }
  66.  
  67. func createGameWithHostUser(hostEmail: String) -> OXGame {
  68.  
  69. let game = OXGame()
  70. game.gameId = getRandomID()
  71. game.hostUser = User(email:hostEmail,password: "",token:"",client:"")
  72. return game
  73.  
  74. }
  75.  
  76.  
  77. //Can only be called when there is an active game
  78. func playMove(index: Int) -> CellType{
  79. // print("PlayingMove on 'network'")
  80.  
  81. let cellType: CellType = currentGame.playMove(index)
  82. return cellType
  83. }
  84.  
  85. //Simple random move, it will always try to play the first indexes
  86. func playRandomMove() -> (CellType, Int)? {
  87. // print("Playing random move")
  88.  
  89. for i in 0...currentGame.board.count - 1 {
  90. if (currentGame.board[i] == CellType.EMPTY){
  91. let cellType: CellType = (currentGame.playMove(i))!
  92. // print(cellType)
  93. // print("Succesfully at: " + String(i))
  94. return (cellType, i)
  95. }
  96. }
  97. // print("Unsuccesfully")
  98. return nil
  99.  
  100. }
  101.  
  102. func createNewGame(host:User, presentingViewController:UIViewController? = nil, viewControllerCompletionFunction:(OXGame?,String?) -> ()) {
  103. print("Creating new network game")
  104.  
  105. }
  106.  
  107.  
  108. // func acceptGameWithId(gameId: String) -> OXGame? {
  109. //// print("Accepting network game")
  110. // for game in self.gameList! {
  111. // if (game.gameId == gameId) {
  112. // setCurrentGame(game)
  113. //// print("Succesfully")
  114. // return game
  115. // }
  116. //
  117. // }
  118. //// print("Not succesfully")
  119. // return nil
  120. // }
  121.  
  122.  
  123. func acceptGame(id:String, presentingViewController:UIViewController? = nil, viewControllerCompletionFunction:(OXGame?,String?) -> ()) {
  124.  
  125. }
  126.  
  127. func finishCurrentGame(){
  128. print("Finishing current game")
  129.  
  130. // if(gameList != nil && gameList?.count != 0){
  131. // var reducer = 0
  132. // for i in 0...(gameList?.count)! - 1{
  133. // if (getCurrentGame()?.gameId == gameList![i - reducer].gameId){
  134. // gameList?.removeAtIndex(i)
  135. // reducer += 1
  136. // }
  137. // }
  138. // }
  139. //
  140. currentGame.reset()
  141.  
  142. setCurrentGame(OXGame())
  143. }
  144.  
  145. //Helper functions
  146. private func getRandomID() -> String {
  147. let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  148. let len: Int = 10
  149. let randomString : NSMutableString = NSMutableString(capacity: len)
  150.  
  151. for _ in 1...len {
  152. let length = UInt32 (letters.length)
  153. let rand = arc4random_uniform(length)
  154. randomString.appendFormat("%C", letters.characterAtIndex(Int(rand)))
  155. }
  156.  
  157. return randomString as String
  158. }
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement