Wednesday, December 16, 2015

Swift: board game

I found a very interesting board game in Apple's Mac Developer Library. It is a simple playground of codes. Based on those codes, I added random dice function to make the game more fun.

The updated codes work very well in Xcode playground, however, the same one does not work in IBM Swift Sandbox. The random function arc4random_uniform is not supported there. Soon I found another Swift on-line complier so that I can share my codes with people who do not have Xcode nor Mac.



Here are the rules of the game:

  1. Start from square 1
  2. Get dice number: 1-6
  3. Move to next square based on dice number, for example, 3, moving 3 steps forward.
  4. Move to the end of ladder if ladder, up to 11 on square 3, or
  5. slip down to snake's tail if snake there,for example slip to 8 on square 19
  6. Stay if steps are more than 25.
  7. Game over when moving to exactly square 25.
Here are my codes modified from Mac Developer Library example:


  1. // function to get dice value
  2. func dice(diceValue : Int) -> Int {
  3.    var value = diceValue
  4.    if value >= 7 {
  5.        // generate random number
  6.        drand48()
  7.        value = Int(arc4random_uniform(6) + 1)
  8.    } else {
  9.        // increment number in sequence till 7
  10.        ++value
  11.        value = value == 7 ? 1 : value
  12.    }
  13.    return value
  14. }
  15. let finalSquare = 25
  16. // Initialize array with 0s
  17. var board = [Int](count: finalSquare + 1, repeatedValue: 0)
  18. // Set jump and slip values for laders and snakes
  19. board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02
  20. board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08
  21. var square = 0
  22. var diceRoll = 0 // 0..6 for increment sequnce or > 6 for random number
  23. var step = 0
  24. print("你好,我是「茶樹油客」改寫的程序,讓我來玩遊戲吧!")
  25. gameLoop: while square != finalSquare {
  26.    step++
  27.    //diceRoll = dice(7) // random number
  28.    diceRoll = dice(diceRoll) // sequence 1-6 number
  29.    print("第\(step)步: 骰子: \(diceRoll), ", terminator: "")
  30.    switch square + diceRoll {
  31.    case finalSquare:
  32.        // diceRoll will move us to the final square, so the game is over
  33.        print("走到 \(finalSquare). \n祝賀,遊戲完成了!")
  34.        break gameLoop
  35.    case let newSquare where newSquare > finalSquare:
  36.        // diceRoll will move us beyond the final square, so roll again
  37.        print("太大, 不走")
  38.        continue gameLoop
  39.    default:
  40.        // this is a valid move, so find out its effect
  41.        square += diceRoll
  42.        square += board[square]
  43.        print("走到 \(square)")
  44.    }
  45. }

 The result is as follows:


你好,我是「茶樹油客」改寫的程序,讓我來玩遊戲吧!
第1步: 骰子: 1, 走到 1
第2步: 骰子: 2, 走到 11
第3步: 骰子: 3, 走到 4
第4步: 骰子: 4, 走到 8
第5步: 骰子: 5, 走到 13
第6步: 骰子: 6, 走到 8
第7步: 骰子: 1, 走到 18
第8步: 骰子: 2, 走到 20
第9步: 骰子: 3, 走到 23
第10步: 骰子: 4, 太大, 不走
第11步: 骰子: 5, 太大, 不走
第12步: 骰子: 6, 太大, 不走
第13步: 骰子: 1, 走到 16
第14步: 骰子: 2, 走到 18
第15步: 骰子: 3, 走到 21
第16步: 骰子: 4, 走到 25.
祝賀,遊戲完成了!


The original codes for the game are near the end in the section of Control Flow.

References


0 comments: