In class, we worked through this sequence of questions in random groups at the whiteboards. Each group picked one of five game cards, and each group also had a printed copy of the code.
If you were away
Pick one of the five games described below, and work through the questions in order, before looking at any answers. Questions 1 to 4 are about your game; questions 5 to 12 are about the Guessing Game’s code. Each question builds on the one before it. Surprised by what you discover in a given question? That is where the learning happens.
Once you have an answer, expand the answer box to check your work. If a question didn’t go the way you expected, try to explain why before moving on.
When you reach the end, read the summary of key ideas that we built together in class.
Your game
Each group picked one of these games. You only need one. Read its rules until you could play it in your head.
A. Tic-Tac-Toe, for two players. Draw a 3-by-3 grid. One player is X, the other is O. Take turns writing your mark in an empty square. Three of your marks in a row, across, down or corner to corner, wins. If all nine squares fill up first, it is a draw. One thing it remembers: whose turn it is.
B. Rock Paper Scissors, best of five. Count to three, and each player shows rock, paper or scissors. Rock beats scissors, scissors beats paper, paper beats rock. The same on both sides is a tie: play that round again. The first player to win three rounds wins the match. One thing it remembers: each player’s number of wins.
C. Snakes and Ladders, two or more players. The board has squares 1 to 100, and each player has a counter of their own colour. Roll a die and move your counter forward that many squares. Land at the foot of a ladder and you climb up it; land on a snake’s head and you slide down to its tail. The first to reach square 100 wins. One thing it remembers: the square each counter is on.
D. Pig, a dice game for two players. On your turn, roll a die as many times as you like, adding up the rolls. Roll a 1 and you lose this turn’s points, and your turn ends. Stop whenever you like to add this turn’s points to your score. The first player to reach 100 points wins. One thing it remembers: each player’s score.
E. Memory, two or more players. Lay 20 cards face down: 10 pairs of matching pictures. On your turn, turn over any two cards. If they match, keep them and go again. If not, turn them back face down, and the next player goes. When every card is taken, the player with the most pairs wins. One thing it remembers: how many pairs each player has.
Part 1: your game
Question 1
List everything your game has to remember while it is being played. Your game’s description above gives you one thing to start.
Answer
It depends on the game: anything the game would break without. Here is what I found for each.
- A. Tic-Tac-Toe: what is in each square, whose turn it is, how many moves so far, whether someone has won, the size of the grid, how many in a row it takes.
- B. Rock Paper Scissors: each player’s wins, which round it is, what each player showed, whether the match is over, how many wins it takes.
- C. Snakes and Ladders: the square each counter is on, whose turn it is, the last roll, whether someone has reached 100, each player’s colour, where each snake and ladder is.
- D. Pig: each player’s score, this turn’s points, the last roll, whose turn it is, whether someone has won, the 100 points it takes to win.
- E. Memory: how many pairs each player has, which cards are still face down, whose turn it is, how many pairs there are, the picture on each card, the colour of the card backs.
Question 2
Circle the things on your list that change during a game.
Answer
Scores and wins, whose turn it is, where things are (the squares, the counters, the cards), the last roll, and whether someone has won. Most of the list changes.
Question 3
Put a box around the things that never change during a game.
Answer
The numbers in the rules (the size of the board, the score that wins, how many wins it takes, how many pairs there are), and things fixed before anyone plays, such as each player’s colour or the pictures on the cards.
Question 4
Beside each thing, write what kind of value it is: a whole number, a number with a decimal point, some text, a colour, or a yes-or-no answer.
Answer
Scores, wins, rolls, squares and counts are whole numbers. Whose turn it is, and what is in a square or on a card, are text. “Has someone won?” is a yes-or-no answer. Each player’s colour is a colour. None of these games needs a number with a decimal point; a game with a timer would.
Part 2: the Guessing Game
Here is the top of the file that holds the Guessing Game. It is the same code as the printed code sheet.
// The game's settings. They sit up here, outside the game,
// so that every part of the game below can use them.
// TRY: Change highestNumber to 10 for an easy game, or to 1000 for a hard one.
let lowestNumber: Int = 1
let highestNumber: Int = 100
// The game itself: what it remembers, what it shows on the screen,
// and what it does when you click a button.
struct GuessingGameView: View {
// MARK: - Stored properties
// Each @State variable is something the game remembers.
// When one of them changes, the screen updates by itself.
// The number the player is trying to find. The app picks it at random.
@State var secretNumber: Int = Int.random(in: lowestNumber...highestNumber)
// The guess the player is about to make. It starts in the middle.
@State var guess: Int = (lowestNumber + highestNumber) / 2
// How many guesses the player has made in this game.
@State var guessCount: Int = 0
// The message shown to the player, with a symbol and a colour to match it.
@State var message: String = "I'm thinking of a number from \(lowestNumber) to \(highestNumber)."
@State var messageSymbol: String = "questionmark.circle.fill"
@State var messageColor: Color = Color.indigo
// Becomes true when the player finds the secret number.
@State var gameIsOver: Bool = falseQuestion 5
Find the two lines near the top that start with let. What do they hold? Could they change in the middle of a game?
Answer
lowestNumberis 1 andhighestNumberis 100: the range of numbers the secret number is chosen from. They never change while the game runs. They are the game’s settings.
Question 6
Find secretNumber. Does its line start with let or var? What is the word after the colon? What does it hold?
Answer
var. The word after the colon isInt. It holds the number the player is trying to find.
Question 7
Answer the same three questions for guess and for guessCount.
Answer
Both start with
var, and both areInt.guessholds the guess on the screen, before you click Guess.guessCountholds how many guesses you have made in this game.
Question 8
Answer the same three questions for message and for messageColor. What kind of value is each one?
Answer
Both start with
var.messageis aString, the text the card shows.messageColoris aColor, the card’s colour. One is some text; the other is a colour.
Question 9
Find gameIsOver. What are the only two values it could ever hold?
Answer
trueorfalse. It is aBool: a yes-or-no answer. It starts asfalseand becomestruewhen the player finds the secret number.
Question 10
Copy this table onto your board and fill it in for five names: lowestNumber, secretNumber, message, messageColor and gameIsOver.
| Name | Starts with let or var? | The word after the colon | What it holds |
|---|---|---|---|
lowestNumber |
Which of these change while you play? Which never change? What do the ones that start with let have in common?
Answer
Name letorvarAfter the colon What it holds lowestNumberletIntthe lowest number the secret can be secretNumbervarIntthe number to find messagevarStringwhat the card says messageColorvarColorthe card’s colour gameIsOvervarBoolhas the number been found? Every
varcan change while you play. The ones that start withletnever do: they are the game’s settings, written at the top, outside the game.
Question 11
In your own words, what do you think Int, String, Color and Bool mean?
Answer
Intis a whole number.Stringis text.Coloris a colour.Boolis a yes-or-no answer:trueorfalse.
Question 12
What would go wrong if the line for secretNumber started with let?
Need a hint?
A new game needs a new secret number.
Answer
A new game needs a new secret number, so
startNewGame()must changesecretNumber. Aletcan never change, so Xcode would refuse to run the game.
Bonus
The game is going to show a high score: the fewest guesses anyone has ever needed. Write the line it would need, in the same style as the others.
Answer
@State var bestGuessCount: Int = 0Any sensible name works. It must start with
var, because the record changes, and it is anInt, a whole number.
Summary of key ideas
Here is the summary of the concepts we consolidated together in class, after the whiteboard work:
Variables, Constants and Types
NOTE
If the whiteboard conversation didn’t quite click, or you were away, this is the document to read for a concrete summary of the key ideas: what a variable, a constant and a type are, the four types we have met so far, how to read a line such as
@State var guessCount: Int = 0word by word, and whysecretNumbermust be avar.