Swift: create a board game – Part II

In the last tutorial (Swift – create a board game – Part 1) we have created a simple board game, in which we set up 5 different environments with some unique properties.

Let’s now spice things up and add up some traps and also some shortcuts. This will make the game a bit more challenging and less boring.

So, let’s for example add up 3 shortcuts: let’s put them at case 4, case 11 and case 18. When we reach this cases, we will advance 4 more cases. So when we reach:

  • the case 4, we will move directly to case 8
  • the case 11, we will move directly to case 15
  • the case 18, we will move directly to case 22

To do that, we will create a function, that will name it “shortcut” with the parameter “move” which will take the value of the variable total.

func shortcut(move : Int){

if total == 4 || total == 11 || total == 18 {

total = total + 4

print (“Congratulation, you found a shortcut! You reached case \(total)”)

}

}

We will also, add up some drawbacks. So when we will end up our turn on a certain number, we will go back. So let’s add the following drawbacks:

  • at case 7, we will go back to case 3
  • at case 19, we will go back to case 8
  • at case 24, we will go back to case 16
  • at case 29, we will go back to case 21

So we create a function named “drawback” with a parameter named move which will take the value of the variable total.

   func drawback(move : Int){

if total == 7  {

total = total – 4

print(“Damn! You have fallen from a clif! You are now at case \(total)”)

}

if total == 19 {

total = total – 11

print(“You have fallen in the Long River… The water brought you back to case \(total)”)

}

if total == 24 {

total = total – 8

print(“You have just encountered a ghost ! You run for your life! You reached case \(total) and you finally escaped from it”)

}

if total == 29 {

total = total – 8

print(“A zombie just appeared from a tombstone! You try to hide and go back to case \(total)”)

}

}

For all these drawbacks and shortcuts to apply, we will call these functions as follows:

   shortcut(move: total)

drawback(move: total)

We will copy – paste this code in all the environment functions. The final code, looks like this

Screen Shot 2017-04-17 at 15.51.12.png

Screen Shot 2017-04-17 at 15.51.32.png

Screen Shot 2017-04-17 at 15.51.45.png

Screen Shot 2017-04-17 at 15.51.57.png

Screen Shot 2017-04-17 at 15.52.15.png

 

 

One thought on “Swift: create a board game – Part II

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: