Swift: create a board game – Part I

In this tutorial, we will learn how to create a simple board game using only what we have learned so far.

The board game that we will create, will be a 5 zones running scenario. The goal is to reach as fast as possible the case 30. We will create 5 areas, each with its personalised properties. The 5 areas are:

  • Grassland – Here the player just starts running, so he is very fresh. We will set that all dices are between 3 and 6
  • Mountain – It’s very difficult to climb a mountain and it’s slow… all dices will be between 1 and 4
  • Forest – It’s quite difficult to see in a dense forest, so we can’t run like in a grassland… all dices will be between 1 and 5
  • Swampland – We all can imagine that going through a swamp can be very very slow… all dices are between 1 and 3
  • The Cemetery – Here all is quite normal, all dices are between 1 and 6 but we will set several traps

So what will we need ?

  • A dice – variable that will give us a random between 1 and 6
  • A total – variable that will add up all the dices
  • Different conditions that we will apply to each environment

So the first thing is to declare the total variable and set it initially to zero.

var total : Int = 0

For simplicity, we will create for the moment a one player game that will play alone. Afterwards, we can add players and set up a button which will roll the dice. So we will create the main loop, which will run as long as the total is different than zero. For that we will use the while function (Swift: While and Repeat-While).

while total != 0 {

}

print(“Congratulation! You won the game!”)

All the code will be set inside this loop. We exit the loop only when we reached the case 30 and in that case, we have won the game.

The next step is to create the dice which will roll a random number between 1 and 6. To do that, we will use the function “arc4random_uniform()” seen before (Swift: First application).

var dice = Int(arc4random_uniform(6)+1)

Now, we will set up the rules how we add up dices to form the total. Let’s set it up in a function so we can call it more easily later, as we have seen it before (Swift: Functions).

   func totalz(tot : Int) {

if total + tot <= 30 {

total = total + tot

} else {

left = 30 – total

print(“You rolled a \(dice) but you need \(left) to reach the end! You won’t move this time “)

}

So we create a function called “totalz” (name it how you want). Inside the function we will set up an if…else function which will add up the dices only if they are below or equal to 30. To do so, we set the variable total as the sum of the total before and we add up the dice which was last rolled.

If the total goes over 30, then we will not add up the last dice, we will only print up a message and we will not move. For a more personalised message, we will create a variable named “left” which will tell us how much we need to roll the last dice when we are near 30. Do not forget to declare the variable let at the beginning of the code as follows:

var left : Int = 0

Let’s now create the different functions for each environment.

The Grassland

Let’s create a function named “Grass” with one parameter named “roll” which will be the dice we roll. Recall that we said that the dice in the Grassland is always between 3 and 6.

To respect that condition we will set the dice equal to the number we roll and we add 2. However, if we roll a 5 and we add 2, then we will have a 7 (or if we roll a 6 and we add 2, then we will have 8). So to avoid this, we will need to set up an if…else condition which will state that if the dice is higher than 6, then the dice is 6 else, we take the dice we rolled and we add 2.

We then afterwards call up the function total, that we have seen before in order to add up the dices –> totalz (tot:dice). This basically, calls up the function “totalz” seen before and gives the value of the “dice” to the variable “tot” in the “totalz” function.

func Grass(roll : Int) {

dice = roll + 2

if  dice > 6 { dice = 6 }

else { dice = roll + 2 }

totalz(tot : dice)

countgrass += 1

 

if countgrass == 1 { print(“You entered Greenland, here you move fast… All dices are between 3 and 6”) }

 

print (“You rolled a \(dice)”)

print(“You arrived at case \(total)”)

}

We also create an additional variable, named “countgrass” that we increment each time that this functions is executed by 1. Once more do not forget to declare the variable at the beginning of your code as follows:

var countgrass : Int = 0

We do so, in order to show up a text to the player, the first time he enters a new area which will state the dice rules. To do so, we created an if…else function which will show up the message only when the variable countgrass is 1.

We will do the same for the other areas. The only thing that changes is the dices which are rolled.

So for the mountains we will have:

func Mountain(roll : Int) {

dice = roll – 2

if dice < 0 { dice = 1 }

else { dice = roll – 2 }

totalz(tot : dice)

countmountain += 1

if countmountain == 1 { print(“You arrived in the Mountains, here you move slow… All dices are between 1 and 4”) }

print (“You rolled a \(dice)”)

print(“You arrived at case \(total)”)

}

The Forest

func Forest(roll : Int) {

dice = roll – 1

if dice < 0 { dice = 1 }

else { dice = roll – 1 }

totalz(tot : dice)

countForest += 1

if countForest == 1 { print(“You entered Forestland, here you need to be careful… All dices are between 1 and 5”) }

print (“You rolled a \(dice)”)

print(“You arrived at case \(total)”)

}

The Swamp

func Swamp(roll : Int) {

dice = roll – 3

if dice < 0 { dice = 1 }

else { dice = roll – 3 }

totalz(tot : dice)

countSwamp += 1

if countSwamp == 1 { print(“You arrived in Swampland, here you move very slow… All dices are between 1 and 3”) }

print (“You rolled a \(dice)”)

print(“You arrived at case \(total)”)

}

The Cemetery

func Cemetery(roll : Int) {

dice = roll

totalz(tot : dice)

countCemetery += 1

if countCemetery == 1 { print(“You entered the Cemetery… All dices are between 1 and 6 but be careful! You never know what you may encounter”) }

print (“You rolled a \(dice)”)

print(“You arrived at case \(total)”)

}

Now, that we have setup the environment rules, we need to setup where each zone is.. So let’s divide our board game as follows:

  • Case 0 – 5 : Grassland
  • Case 6 – 10 : Mountain
  • Case 11 – 16 : Forest
  • Case 17 – 21 : Swampland
  • Case 22 – 29 : Cemetery
  • Case 30 : Finish

Now, we need to tell our small game, which function to call depending on where we are. We know where we are based on the “total” variable. So the best way to do it, is with the switch function (Swift: Switch). Basically, what we will do, is compare if our variable total is part of a certain range: for example if our total is between 0 and 5, then we are in the Grassland and we will call the function Grassland. As we move along, we will enter new zones and other functions, which we have declared before are called.

We create a switch function for variable total and we create different cases based on ranges for each zone. If the total is in a certain range, we call up the function of the specific zone.

switch total {

case 0…5 : Grass(roll : dice)

case 6…10 : Mountain(roll : dice)

case 11…16 : Forest(roll : dice)

case 17…21 : Swamp(roll : dice)

case 22…29 : Cemetery(roll : dice)

default: print(“Error!”)

}

In part II (Swift: create a board game – Part II), we will add up some challenges and also helpers for the player in order to make the game a bit more interesting.

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

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: