- Learnt28 min · three lists in step, and item # of
- Basic24 min · build the deck and find a card
- Challenge 1–327 min · the strongest, a hand, a total
- Extrabonus · two hands compared
Review — the Quiz Master arc 5 min
(item (3) of [questions v])reads row 3.(length of [questions v])drives a loop of any size.<[accepted v] contains (answer)?>searches the whole list at once.
Quick-fire
<[names v] contains [Bat]?> says true. Which row is the Bat in?
Reveal the answer
You cannot tell. Contains only says whether. Today you meet the block that says where — and then, next lesson, you write it yourself.
Today’s Topic 3 min
- Three lists kept in step — one card spread across three rows
(item # of [Bat] in [names v])— a position, not a value- 0 as the answer for “not in the list”
- Walking a deck to find the biggest, and to add it all up
Learning outcome
By the end of this lesson you will be able to:
- Hold several facts about one thing across lists that move together.
- Find which row something is in, and cope with it not being there.
- Walk a list keeping a “best so far” and a running total.
Learnt — a deck, and how to reach into it 28 min
1 · One card, three facts
A card in this game has a name, a power and a picture. Scratch has no way to put three things in one list row, so we use three lists and keep them in step:
| Row | names | powers | pics |
|---|---|---|---|
| 1 | Dragon | 9 | 1 |
| 2 | Shark | 7 | 2 |
| 3 | Dinosaur | 8 | 3 |
| 4 | Bat | 4 | 4 |
Row 4 of each list is one card. You met this in Lesson 3-7 with questions and answers; here it is three lists instead of two, and it matters more.
when flag clicked
delete all of [names v]
delete all of [powers v]
delete all of [pics v]
add [Dragon] to [names v]
add (9) to [powers v]
add (1) to [pics v]
add [Shark] to [names v]
add (7) to [powers v]
add (2) to [pics v]
How to use this: Click the green flag and watch two lists fill together, then the card change to match row 4.
2 · Dealing is one row number, used three times
To put a card on the table you need all three facts, and they all come from the same n:
switch costume to (item (n) of [pics v])
set [power v] to (item (n) of [powers v])
say (join (item (n) of [names v]) (join [ - power ] (power))) for (1.3) seconds
How to use this: Click the green flag and watch four cards glide from the deck to your slot, each with its own name and power.
3 · Where is it? — item # of
Every list block so far has answered “what is at row 4?”. This one answers the opposite question:
(item (4) of [names v])
(item # of [Bat] in [names v])
And if the thing is not in the list at all, it reports 0 — which is a useful answer, because there is no row 0.
How to use this: Click the green flag and watch three searches: the Bat, the Dragon, and a Unicorn that is not in the deck.
Basic — build the deck, find a card 24 min
Start a new project. Paint or pick a table backdrop, and add one card sprite.
Step 1 — six costumes
- Add six creature sprites from the library as costumes of ONE sprite.
- Note the order they are in — that is what the pics list will hold.
- Set the sprite’s size to about 55 so it fits a slot.
Step 2 — three lists
- Make
names,powersandpics. - Empty all three under the green flag.
- Add your six cards, three blocks each, always in the same order.
- Show the names and powers boxes and check they line up.
Step 3 — deal one
- Make a variable
nand set it to 1. switch costume to (item (n) of [pics v]).- Say the name and the power from the same row.
- Change n to 4 and run it again.
Step 4 — deal several
- Wrap steps 3 in
repeat (4)withchange [n v] by (1)at the top. - Add a glide from the deck to your slot so each card arrives.
Step 5 — find one
- Add
ask [Which card?] and wait. set [at v] to (item # of (answer) in [names v]).- If
atis 0, say it is not in the deck. Otherwise show it.
ask [Which card?] and wait
set [at v] to (item # of (answer) in [names v])
if <(at) = (0)> then
say (join (answer) [ is not in the deck.]) for (2) seconds
else
switch costume to (item (at) of [pics v])
say (join [Row ] (join (at) (join [, power ] (item (at) of [powers v])))) for (2) seconds
end
How to use this: Click the green flag and watch all three searches, including the one that finds nothing.
The picture never matches the name.
The pics list is out of step, or the costumes are in a different order from the one you wrote down. Show all three boxes and read row 3 across.
Searching for a card that exists says it does not.
Capitals are fine — Scratch’s comparison ignores them — but a trailing space is not. Check for one in the list row.
A card that is not in the deck shows the Dragon.
You are reading (item (at) of [pics v]) when at is 0. Test for 0 before using it.
The lists get longer every time I press the flag.
The three delete all of blocks are missing. Same trap as Lesson 3-5, three times over.
The strongest card
Walk the whole deck and find the card with the highest power.
- You may not look at the deck and type the answer in.
- It must still work after you change a power, or add a card.
- The winner’s name, power and picture all come out right.
How to use this: Click the green flag and watch it go through the deck, calling out each new strongest.
Teacher reveal — “best so far”
set [best v] to (0)
set [best row v] to (0)
repeat (length of [powers v])
change [n v] by (1)
if <(item (n) of [powers v]) > (best)> then
set [best v] to (item (n) of [powers v])
set [best row v] to (n)
end
end
Keeping the ROW as well as the value is the bit that matters — with just the power you know it is a 9 but not whose. Level 3-4’s “worst day” challenge was the same shape, which is worth pointing out.
Deal a hand
Take three cards out of the deck and into a hand.
- Each dealt card is added to a
handlist. - It is deleted from all three deck lists.
- At the end, the hand has 3 and the deck has 3.
How to use this: Click the green flag and watch three cards leave the deck for the hand. Read both boxes.
Teacher note
The trap is deleting from names only. Ask them to deal three and then show the powers box — if it still has six rows, the deck is broken and every card from row 1 down now has the wrong power.
What is the deck worth?
Add up every power in the deck.
- A running total that starts at 0.
- Each row is added as you pass it.
- It is right for a deck of any size.
How to use this: Click the green flag and follow the total climbing: 9, 16, 24, 28, 31, 36.
Teacher reveal
set [total v] to (0)
repeat (length of [powers v])
change [n v] by (1)
set [total v] to ((total) + (item (n) of [powers v]))
end
change [total v] by (item (n) of [powers v]) does the same job in one block, and is worth showing second — after they have seen the long way, because the long way is the one that generalises.
Whose hand is better?
Two hands, two totals, one winner.
- Two lists of powers: yours and theirs.
- One loop totals both at the same time.
- The bigger total wins, and the machine says the score.
How to use this: Click the green flag and watch both totals climb together, then the verdict.
Teacher reveal
repeat (length of [mine v])
change [n v] by (1)
set [my total v] to ((my total) + (item (n) of [mine v]))
set [their total v] to ((their total) + (item (n) of [theirs v]))
end
if <(my total) > (their total)> then
say (join [I win, ] (join (my total) (join [ to ] (their total)))) for (3) seconds
end
Ask what happens if the two hands are different lengths. The loop is driven by (length of [mine v]), so it quietly reads nothing off the end of the shorter one — a real bug, and a good one to find on purpose.
Summary 5 min
- Several facts about one thing go in several lists, kept in step by their row number.
(item (4) of [names v])is “what is at row 4”.(item # of [Bat] in [names v])is “which row is the Bat in” — and 0 when it is nowhere.- A row number found in one list reads the others.
- Best-so-far and running-total are both one walk down a list.
- In step
- Lists whose row numbers line up, one row per thing.
- Position
- Which row something is in.
- Best so far
- A variable holding the best you have seen while you keep looking.
- Running total
- A sum built up one row at a time.
Hand in
Save your deck. Next lesson you write item # of yourself — and find out how much work it has been doing for you.