Lab 1: Games
Students build a number-guessing game in which the player tries to guess the computer's secret number, and the computer responds with as human-like dialog as the programmer can build.
Students gain more experience with conditionals and predicates, preparing them to refine and deepen their understanding in lab 3.
This game comes back more than once in later units, because it's the canonical example of a binary search strategy. If you notice that some students always guess 5 first, while other kids start with 1 or 10, and if there's time at the end of the week, consider a class discussion of strategies.
This lab also expands students' understanding of variables. Students have encountered variables in two contexts: they have created blocks that have input variables, usable only inside the block for which they were created; and they have used the for
block, which provides an index variable (i.e., a counter), usable only in the script that contains the for block. This lab introduces script variables (a kind of local variable), created by the
block, which lets programmers create their own temporary variables in which to store information that a script needs while it is running. A script variable is called "local" because it exists only while that script is active and is accessible only by blocks inside that script. Blocks inside that script can set, use, or change the value of that script's variables; blocks outside that script cannot. Index and input variables are also local. By contrast, a global variable is accessible by any block in any script belonging to any sprite: it is global in scope.
It's traditional in teaching programming to start with global variables, on the theory that they're easier to understand. We take pains to teach global variables last, so that students get practice dealing with limited-scope variables early. This lets us teach good programming style without hectoring students about it.
Students take time to debug each portion of their project. They get suggestions for features to add but are on their own to think about how to structure these extensions and modifications so that the code works and remains clear.
Pacing
The 5 lab pages could be split across 2–4 days (
75–150 minutes). Expected times to complete follow:
Lab Pages
-
Page 1: Starting a Number Guessing Game.
-
Page 2: Checking the Player's Guess.
-
Learning Goals:
- Use the
repeat until
conditional to control program flow.
- Use the
ask
and answer
blocks to manage text input.
-
Page 3: Debugging and Extending Your Number Guessing Game.
-
Learning Goals:
- Analyze and debug existing code.
- Implement additional conditions using predicates.
-
Tips:
- The bug in the
Number guessing game
block is in the predicate of the repeat until
, that is, in answer =
secret number. If the same number is randomly chosen twice in a row, the player's last guess will be saved in answer
, and therefore without asking the player to guess, the program will match the secret number and the player will be congratulated. A good strategy to avoid this is to save answer
to a variable and make sure to clear that variable's value every time the game starts again or to reset it to something that is definitely not the right answer (such as a word or a negative number).
- Debugging is a skill to refine over time. To support its development, analyze and debug projects together as a class. You may also have students share bugs they have encountered either to solicit suggestions from the class or to share a successful debugging approach. Make "Cherish your bugs" a classroom slogan. If students keep journals, ask them to record every bug: the symptom, the error in the code, how they found the error, and how they fixed it.
-
Page 4: Keeping Score with Global Variables.
-
Learning Goals:
- Create and use a global variable to keep track of a game score.
- Use conditionals to end the game when the player reaches a target score.
- Use
stop
blocks to manage program flow.
-
Tips:
- Note that this page is about the Click Alonzo game from U1L1, not about the number guessing game!
- Be sure that students rename their project with a Unit 2 filename: U2L1-ClickAlonzo as Snap! will overwrite their Unit 1 project if students save their work under the U1L1-ClickAlonzo filename.
- The student page urges students to get in the habit of saving an old project under a new name before they change anything in the project.
-
Page 5: Choosing a Costume.
-
Learning Goals:
- Tips:
- This page is only superficially about costumes. It's about assigning a list value to a variable, about selecting an item from a list, and about the syntax of dropping an expression over a menu-type input slot.
Do we need this commented out HTML? --MF, 5/27/20
Solutions
Correlation with 2020 AP CS Principles Framework
Computational Thinking Practices: Skills
- 2.B: Implement an algorithm in a program.
- 3.A: Generalize data sources through variables.
- 4.B: Determine the result of code segments.
Learning Objectives:
- AAP-1.A: Represent a value with a variable. (3.A)
- AAP-1.B: Determine the value of a variable as a result of an assignment. (4.B)
- AAP-2.B: Represent a step-by-step algorithmic process using sequential code statements. (2.B)
-
AAP-2.N: For list operations:
- Write expressions that use list indexing and list procedures. (2.B)
- Evaluate expressions that use list indexing and list procedures. (4.B)
- AAP-3.E: For generating random values:
- Write expressions to generate possible values. (2.B)
Essential Knowledge:
- AAP-1.A.1: A variable is an abstraction inside a program that can hold a value. Each variable has associated data storage that represents one value at a time, but that value can be a list or other collection that in turn contains multiple values.
- AAP-1.B.1: The assignment operator allows a program to change the value represented by a variable.
- AAP-1.B.2: The exam reference sheet provides the "
←
" operator to use for assignment. For example, a ← expression
evaluates expression
and then assigns a copy of the result to the variable a
.
-
AAP-1.B.3: The value stored in a variable will be the most recent value assigned. For example:
a ← 1
b ← a
a ← 2
display(b)
still displays 1
.
- AAP-1.C.3: An index is a common method for referencing the elements in a list or string using natural numbers.
- AAP-1.D.8: The exam reference sheet describes a list structure whose index values are 1 through the number of elements in the list, inclusive. For all list operations, if a list index is less than 1 or greater than the length of the list, an error message is produced and the program will terminate.
- AAP-2.E.1: A Boolean value is either true or false.
-
AAP-2.N.1: The exam reference sheet provides basic operations on lists, including:
- accessing an element by index:
aList[i]
accesses the element of aList at index i
. The first element of aList
is at index 1
and accessed using the notation aList[1]
.
- determining the length of a list:
LENGTH(aList)
evaluates to the number of elements currently in aList
.
-
AAP-3.A.8: The exam reference sheet provides
result ← procName(arg1, arg2, …)
to assign to result the “value of the procedure” being returned by calling
PROCEDURE procName(parameter1, parameter2, …)
{
<block of statements>
RETURN(expression)
}
- AAP-3.A.9: The exam reference sheet provides procedure
INPUT()
, which accepts a value from the user and returns the input value.