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.

Question 2

Circle the things on your list that change during a game.

Question 3

Put a box around the things that never change during a game.

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.

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 = false

Question 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?

Question 6

Find secretNumber. Does its line start with let or var? What is the word after the colon? What does it hold?

Question 7

Answer the same three questions for guess and for guessCount.

Question 8

Answer the same three questions for message and for messageColor. What kind of value is each one?

Question 9

Find gameIsOver. What are the only two values it could ever hold?

Question 10

Copy this table onto your board and fill it in for five names: lowestNumber, secretNumber, message, messageColor and gameIsOver.

NameStarts with let or var?The word after the colonWhat it holds
lowestNumber

Which of these change while you play? Which never change? What do the ones that start with let have in common?

Question 11

In your own words, what do you think Int, String, Color and Bool mean?

Question 12

What would go wrong if the line for secretNumber started with let?

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.

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 = 0 word by word, and why secretNumber must be a var.