Reader small image

You're reading from  Building Apple Watch Projects

Product typeBook
Published inFeb 2016
Reading LevelIntermediate
PublisherPackt
ISBN-139781785887369
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Stuart Grimshaw
Stuart Grimshaw
author image
Stuart Grimshaw

Stuart Grimshaw has programmed for Apple computers since the days before OS X and has been involved with developing for the Apple Watch since its release. Born in the UK and having lived in Germany and the Netherlands, he is currently a Senior iOS developer in London, England, United Kingdom. He has around 10 years of end-to-end development of projects experience in, iOS, iPadOS, watchOS (Apple Watch), tvOS (AppleTV), and macOS. He is passionate about the potential of the Apple Watch and Apple TV, as well as Apple's Swift programming language, and is a keen proponent of beach coding.
Read more about Stuart Grimshaw

Right arrow

Chapter 3. C-Quence – A Memory Game

We will be crafting an app that is a little more entertaining by using everything that we covered in Chapter 1, Exploring the New Platform, adding code which uses basic Swift features that most developers will find familiar and will address some of the topics that face the developer in creating software for a platform that presents some unique challenges.

C-Quence will be a game that challenges players' ability to memorize a sequence of colors generated by the app.

It is a game to be played in short bursts rather than prolonged activity, as one of the first things that becomes clear when using a physical device is that the watch is unsuited to tasks that take more than a short time to complete, which we will keep in mind as we look at the top-level design of the app.

Bear in mind that, although this is a very modest app in terms of the amount of coding it takes to bring it to completion, we still want to adhere to what some refer to as Best Practice (and...

Plan the app


Let's have a look at how the app will be developed.

Mission statement

Let us look at the app's requirements before beginning any work in Xcode.

We will write a game that presents a group of four color-coded buttons which flash in an order generated by the app, which is then to be repeated by the user by tapping the buttons. If the user does this successfully, the sequence length is extended by adding a randomly generated color, and is flashed again (the flash interval needs to be a suitable time, say, in the region of one second). The sequence gets longer and longer, each time requiring the user to repeat it. Eventually the user will fail, and will be presented with his 'score'—the length of the last correctly guessed sequence, and a prompt to start the next game.

So that's our game, time to get building that interface, right?

Well, no, not quite yet. To make the best use of our coding time, and to produce the best code possible, we will try to formalize our game requirements into...

Setting up the project


For this app, our setup process will closely resemble that of the previous chapter.

Create the Xcode project

To create the Xcode project, follow the given instructions:

  1. Select File | New | Project, and from the templates select iOS App With WatchKit App.

  2. Name the project C-Quence and deselect the Notification scene and any other scenes that may be preselected, we won't need them here.

The project window should now be looking similar to this:

Create Required Classes

Looking at the project navigator on the left, we see that inside the C-Quence project is a folder also named C-Quence, which contains files that are relevant to the iPhone companion app that we will not be needing to change in this chapter. The next two folders are all about the WatchKit Extension and WatchKit App. We can see here that Xcode's default project structure adheres very closely to the MVC pattern described above.

The MVC's View is the .storyboard file that the template creates for us. Similarly, the...

GameLogic


Although we have created the file GameLogic.swift file, we have not actually created the class yet (this is different to Objective C)

Create the GameLogic class

Below the import Foundation statement that is part of the template, add the following code:

import Foundation

class GameLogic {

}

Plan the class

We will create a class that will encapsulate the code that deals with the game itself in isolation from the user interface. The GameLogic class doesn't need to know anything about interactions with the user, that is something that will be taken care of by the InterfaceController class, so let's first think about what we will need it to do, so that we can start to plan which methods we will need to implement.

We need it to do the following:

  • Create and maintain a sequence of colors and add a random color to it when required

  • Evaluate whether a player's tap on a color is a correct answer

  • Provide information as to whether the game is still in play or finished

  • Clear the data that collects during...

Interface Controller


Next we go through the same process with the interface controller, namely planning out what we think it will need to do (remembering that an incomplete plan is good enough at this stage) and stub the methods that we expect to need.

Planning the interface

The InterfaceController class will act as the CONTROLLER of communication between the user interface—the VIEW—and the actual implementation of the game—the MODEL. It needs to inform the player what the game is doing and inform the GameLogic class what the player is doing.

So, very roughly, the controller will need to do the following:

  • Accept the player's instruction to start the game

  • Flash the colors in sequence

  • Accept player input

  • Enable and disable user input as required

  • Show results when a game is over

  • Start a new game

Define Outlets to the View

Add the following declarations to the top of the InterfaceController class (not the top of the InterfaceController.swift file):

class InterfaceController: WKInterfaceController {

  ...

Summary


In this chapter you have laid the groundwork for the rest of the app and done so in a way that gives you the best possible chance of producing code that is robust, easy to maintain, and easy to comprehend when returning to it after six months on a surfing holiday.

Specifically, you have learned to plan the app according to a projected user story, applying the Model-View-Controller design pattern, and then set up the project according to that pattern; you have used Group objects to layout the user interface in Interface Builder and you have prepared the Interface Controller class's methods. You have also implemented some best programming practices by encapsulating the game logic into a class of its own and using enums to make your code easier to write and clearer to read.

In the following chapter we will flesh out the methods we have already declared, and begin to run, evaluate, and improve the code; we will add the ability to navigate to other screens, and enable the user to customize...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Building Apple Watch Projects
Published in: Feb 2016Publisher: PacktISBN-13: 9781785887369
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Stuart Grimshaw

Stuart Grimshaw has programmed for Apple computers since the days before OS X and has been involved with developing for the Apple Watch since its release. Born in the UK and having lived in Germany and the Netherlands, he is currently a Senior iOS developer in London, England, United Kingdom. He has around 10 years of end-to-end development of projects experience in, iOS, iPadOS, watchOS (Apple Watch), tvOS (AppleTV), and macOS. He is passionate about the potential of the Apple Watch and Apple TV, as well as Apple's Swift programming language, and is a keen proponent of beach coding.
Read more about Stuart Grimshaw