Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. class SnakesAndLadders: DiceGame { // 프로토콜의 조건에 맞추기위해 dice라는 프로퍼티는 gettable하게 구현되어 있고, play()메소드가 구현되어 있다.
  2. let finalSquare = 25
  3. let dice = Dice(sides: 6, generator: LinearCongruentialGenerator())
  4. var square = 0
  5. var board: [Int]
  6. init() {
  7. board = Array(repeating: 0, count: finalSquare + 1)
  8. board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02
  9. board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08
  10. }
  11. // 강한 참조 순환을 막기위해 약한 참조를 함
  12. weak var delegate: DiceGameDelegate? // 게임 진행에 반드시 필요한 것은 아니기 때문에 옵셔널로 정의
  13. /// 게임의 전체 로직이 들어있는 메소드
  14. func play() { // 바로 위에 정의한 delegate가 DiceGameDelegation 옵셔널타입이므로 play() 메소드는 delegate의 메소드를 호출할때마다 옵셔널 체이닝을 한다.
  15. square = 0
  16. /// DiceGameDelegation의 게임 진행상황을 tracking하는 메소드(게임 시작)
  17. delegate?.gameDidStart(self)
  18. gameLoop: while square != finalSquare {
  19. let diceRoll = dice.roll()
  20. /// DiceGameDelegation의 게임 진행상황을 tracking하는 메소드(게임 진행)
  21. delegate?.game(self, didStartNewTurnWithDiceRoll: diceRoll)
  22. switch square + diceRoll {
  23. case finalSquare:
  24. break gameLoop
  25. case let newSquare where newSquare > finalSquare:
  26. continue gameLoop
  27. default:
  28. square += diceRoll
  29. square += board[square]
  30. }
  31. }
  32. /// DiceGameDelegation의 게임 진행상황을 tracking하는 메소드(게임 종료)
  33. delegate?.gameDidEnd(self)
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement