Swift: First application

After learning some basics in Swift, it is now time to apply what we have learned and create a little fun game.

So, let’s create a simple dice game between 2 friends, let’s call them Zack and Andrew. The goal is to reach exactly 21. So the first one that will reach 21 will win the game. If, however one goes over 21, he will need to start again from 0.

First we will need to create constants for our friends: Zack and Andrew.

let Zack : String

let Andrew : String

We will also need a variable which will count the number of times, each of them will roll the dice to reach exactly 21: let’s create a count variable that we set at first to zero for each player

var countZack : Int = 0

var countAndrew : Int = 0

We need also a variable that will add up the dices to form the total for each player… because you know, a dice goes from 1 to 6 and we want to reach 21… Once more, we set them both to 0, because we don’t want to give any advantage to any of them…

var totalZack : Int = 0

var totalAndrew : Int = 0

Now that we have our environment set up, we will still need a dice that gives a random number between 1 and 6 and then to set the rules of the game. Recall from what we have seen before (Swift: While and Repeat-While) that the best function in this case to use is “while”. Indeed, we want a loop that will run until the player reaches exactly 21. Let’s create the rules for Zack…

So we want the game to go on as long as the total is different from 21.

while totalZack != 21 {


We now need to create the dice… a variable that will give a random number between 1 and 6. To do that, we will use the Swift function arc4random_uniform()

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

The function creates a random number between 0 and 5 and we add “+1” in order to have a number between 1 and 6.

Now we need to set a rule that  will reset his total to zero if his total goes above 21, however the count number of dices rolled will still increase.

if totalZack > 21 {

totalZack = 0

countZack +=1


Now we put everything inside the while loop and we add each dice to create the total and we increment the count.

while totalZack != 21 {

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

if totalZack > 21 {

totalZack = 0

countZack +=1


totalZack = totalZack + dice

countZack += 1


print(“Andrew needs to play \(countZack) times in order to reach exactly 21!”)

Finally, when Andrew reaches 21, the while loop will stop and we will output our message, which will tell us the number of dices he rolled (the variable countZack) in order to reach exactly 21.

The final code for both players in Xcode is the following:

Screen Shot 2017-04-16 at 15.31.04.png

We can also add a final piece of code that will tell us who won.

Screen Shot 2017-04-16 at 15.32.54.png


One thought on “Swift: First application

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: