复习:我们学到哪里了 5 分钟
- 交换需要第三个框;三个一起走的列表要一起交换。
- 一轮会让最大的牌浮到最上面。
- 冒泡排序就是一轮一轮地做,直到有一轮没有交换。
快问快答
你的排序不比较任何东西,而是用随机的行号交换十二次。你做出了什么?
看答案
一个洗牌。同样的九块积木,不比较,行号随机。今天的游戏第一个就需要它。
今天的主题 3 分钟
- 一个回合:一次比较,一分
- 洗牌:用随机行号来交换
- 出过的牌离开牌组
- 用第 3-11 课的排序排好排行榜,再用
item # of得出名次
学习目标
上完这节课,你能够:
- 把一个列表洗乱。
- 进行一场有好几个回合的比赛,出过的牌就从牌组里拿走。
- 给排行榜排好顺序,并从上面读出名次。
学一学:用牌组来玩 28 分钟
1 · 一个回合就是一次比较
你有一张牌。对手有一张牌。谁的力量大,谁就赢这个回合。整个游戏就是这样,它是一个 if / else:
set [my power v] to (item (1) of [powers v])
set [their power v] to (item (2) of [powers v])
if <(my power) > (their power)> then
change [my score v] by (1)
say [Mine!] for (1) seconds
else
change [their score v] by (1)
say [Theirs.] for (1) seconds
end
怎么用: 点绿旗,看三个回合,每个回合比较一次。
2 · 洗牌就是一个“没有意见”的交换
你的排序在两行顺序反了的时候交换它们。把比较拿掉,随机选两行,做十几次:
repeat (12)
set [a v] to (pick random (1) to (length of [names v]))
set [b v] to (pick random (1) to (length of [names v]))
set [hold name v] to (item (a) of [names v])
set [hold power v] to (item (a) of [powers v])
set [hold pic v] to (item (a) of [pics v])
replace item (a) of [names v] with (item (b) of [names v])
replace item (a) of [powers v] with (item (b) of [powers v])
replace item (a) of [pics v] with (item (b) of [pics v])
replace item (b) of [names v] with (hold name)
replace item (b) of [powers v] with (hold power)
replace item (b) of [pics v] with (hold pic)
end
a 和 b 代替了 i 和 i + 1。怎么用: 点绿旗,看十二次随机交换把牌组搅乱。再按一次,顺序又不一样。
3 · 牌离开牌组
每个回合出两张牌,所以有两张牌要离开。它们要从三个列表里都离开,从第 1 行删,删两次:
delete (1) of [names v]
delete (1) of [powers v]
delete (1) of [pics v]
delete (1) of [names v]
delete (1) of [powers v]
delete (1) of [pics v]
基础任务:完整的比赛 24 分钟
打开你的牌组。这次有两个卡牌角色:你的和对手的。
第 1 步:两个位置
- 复制你的卡牌角色,把复制出来的那个叫 Theirs。
- 在桌上的每个位置各放一个。
第 2 步:洗牌
- 建好
a和b。 - 在绿旗下面、牌组放好牌以后,搭好交换十二次的洗牌。
- 运行几次,看列表框。
第 3 步:一个回合
- 建好
my power、their power、my score和their score。 - 把你的牌显示成第 1 行,再广播一下,让对手的牌显示第 2 行。
- 比较两个力量,给赢的一方加分。
第 4 步:三个回合
- 用
repeat (3)把回合包起来。 - 在回合的最下面,从三个列表里都删掉第 1 行和第 2 行。
- 在舞台上显示一个回合计数器。
第 5 步:赢家
- 循环结束后,比较两个得分。
- 说出谁赢了,赢了几分。
repeat (3)
change [round v] by (1)
switch costume to (item (1) of [pics v])
set [my power v] to (item (1) of [powers v])
set [their power v] to (item (2) of [powers v])
broadcast [their card v]
say (join (item (1) of [names v]) (join [ ] (join (my power) (join [ vs ] (their power))))) for (1.5) seconds
if <(my power) > (their power)> then
change [my score v] by (1)
else
change [their score v] by (1)
end
delete (1) of [names v] // and the same for powers and pics
delete (1) of [names v] // twice over - two cards were played
end
怎么用: 点绿旗,看一副洗好的牌打三个回合。再按一次,就是另一场比赛。
第 4 回合拿“什么都没有”和“什么都没有”比。
六张牌的牌组是三个回合。要么用 repeat 3,要么用 repeat until <(length of [names v]) < (2)>。
对手的牌总是同一个动物。
Theirs 角色没有去读列表。收到广播时,它需要 switch costume to (item (2) of [pics v])。
两个得分加起来不是三。
平局掉进了 else,把分给了对手。想好平局要怎么办,并把它说清楚:<() = ()> 值得有自己的一条路。
同一张牌出了两次。
每个回合只删了一张牌,而不是两张;或者删除的积木放在了比较的上面。
排行榜
四个玩家,四个得分,把排行榜排好顺序。
- 两个一起走的列表:
players和scores。 - 用第 3-11 课的冒泡排序,指向 scores。
- 排完以后,赢家在第 1 行。
怎么用: 点绿旗,看排行榜一次交换一下,自己重新排好。
Teacher note
This is last lesson’s sort with two lists instead of three and different names on them. Pupils who rewrite it from scratch have missed the point; pupils who copy it across and change four words have got it.
我第几名?
排行榜排好以后,行号就有了新的意思。
- 问一个玩家的名字。
- 说出他是第几名,一共有几个人。
- 没有参加的人,也要告诉他。
怎么用: 点绿旗,看三次查找,其中一次找的是不在排行榜上的玩家。
Teacher reveal — why the sort had to happen first
set [place v] to (item # of (who) in [players v])
On an unsorted board this number is meaningless — it is just where the name happens to sit. It only becomes a place because the list is in order. That sentence is the reason this lesson comes after Lesson 3-11 rather than before it.
领奖台
显示前三名,完全不用查找。
- 第一、第二、第三,按这个顺序。
- 每个人都配上得分。
- 不要用循环去找最大的,你已经排好了。
怎么用: 点绿旗,看领奖台直接从第 1、2、3 行读出来。
Teacher note
Worth drawing out: sorting is expensive, but it is paid once and then a dozen questions become free — top three, last place, who came third, is this in order. That trade is why real programs sort things they do not obviously need sorted.
发布它
把整个项目组合成一个别人能玩的游戏,然后分享出去。这是这一段的结尾,把它做成你愿意拿给别人看的作品。
- 一个标题、一副洗好的牌,还有一场三个回合的比赛。
- 你的得分加进排行榜。
- 排行榜排好顺序,并从上面读出你的名次。
- 一个再玩一次的按键。
怎么用: 点绿旗,看完整个游戏:标题、三个回合、记下得分、排好排行榜、说出名次。
Teacher reveal — the running order
add [Mia] to [players v]
add (my score) to [scores v]
broadcast [sort the board v] // the bubble sort from Lesson 3-11
set [place v] to (item # of [Mia] in [players v])
say (join [Mia came ] (join (place) (join [ of ] (length of [players v])))) for (3) seconds
Add first, sort second, look up third. Any other order gives a wrong answer, and the wrongness is quiet — a place that looks plausible. Ask the class to predict what happens if you sort before adding.
总结 5 分钟
- 一个回合是一次比较;一场比赛是回合的循环。
- 洗牌就是用随机行号、不做比较的交换。
- 出过的牌要从它所在的所有列表里删掉,从第 1 行删。
- 排好顺序的排行榜,能把
(item # of () in [players v])变成名次。 - 排序只要花一次功夫,很多问题就不费事了:前三名、最后一名、谁是第三名。
- 洗牌
- 把列表变成随机的顺序。
- 回合
- 游戏里的一轮,由一次比较决定输赢。
- 排行榜
- 一份玩家和得分的列表,一直保持排好的顺序。
- 名次
- 排好顺序的列表上的行号。
交作业
分享你做完的游戏,把链接发给老师。它应该能洗牌、发牌、打一场比赛、记下得分,并给排行榜排名。
这是十二节课、三个游戏。你写了两个有名字的算法:线性查找和冒泡排序。再过几年,电脑科学考试问的就是这两个。