Year 7 OCR Computer Science: Case Study – Number Guessing Game | Year 7 OCR 计算机:案例分析——猜数字游戏

📚 Year 7 OCR Computer Science: Case Study – Number Guessing Game | Year 7 OCR 计算机:案例分析——猜数字游戏

In this article, we will work through a practical case study: designing a simple number guessing game. The computer picks a random number between 1 and 100, and the player tries to guess it. After each attempt, the program tells the player whether the guess is too high or too low. When the guess is correct, the program displays the number of attempts taken. This hands-on project will help you apply key computational thinking skills and programming concepts such as decomposition, pattern recognition, abstraction, algorithm design, variables, selection, and iteration.

在这篇文章中,我们将通过一个实际案例来学习:设计一个简单的猜数字游戏。计算机会随机选取一个 1 到 100 之间的数字,玩家进行猜测。每次尝试后,程序会告诉玩家猜测是太高还是太低。当猜中时,程序会显示所用的尝试次数。这个动手项目将帮助你运用计算思维的关键技能和编程概念,例如分解、模式识别、抽象、算法设计、变量、选择和循环。


1. Introduction to the Case Study | 案例介绍

Our case study is a number guessing game. It is a perfect project for Year 7 students studying the OCR Computer Science curriculum because it involves simple logic but covers fundamental programming structures. The game requires the program to generate a random number, accept repeated user input, make decisions based on comparisons, and keep track of how many guesses the user makes. By building this game, you will see how a computer processes a problem step by step from start to finish.

我们的案例是一个猜数字游戏。对于学习 OCR 计算机课程的七年级学生来说,这是一个完美的项目,因为它逻辑简单,却涵盖了基础编程结构。游戏要求程序生成一个随机数、接收重复的用户输入、根据比较结果做出决策,并记录用户猜测的次数。通过构建这个游戏,你将看到计算机是如何一步一步地从开始到结束处理问题的。


2. Decomposition: Breaking Down the Problem | 分解:拆解问题

Decomposition means taking a complex problem and splitting it into smaller, more manageable parts. Before writing any code, we can decompose the number guessing game into the following sub-problems:

分解是指将一个复杂的问题拆分成更小、更易处理的部分。在编写任何代码之前,我们可以将猜数字游戏分解为以下子问题:

1. Generate a random secret number between 1 and 100.

1. 生成一个 1 到 100 之间的随机秘密数字。

2. Create a variable to count the number of attempts and set it to zero.

2. 创建一个变量来记录尝试次数,并将其设为零。

3. Repeatedly ask the user for a guess and increase the attempt counter by one.

3. 反复要求用户输入猜测,并将尝试计数器加一。

4. Compare the guess with the secret number and give feedback: ‘Too high’, ‘Too low’, or ‘Correct!’.

4. 将猜测与秘密数字进行比较,并给出反馈:“太高了”、“太低了”或“正确!”。

5. If the guess is correct, stop the loop and display the total number of attempts.

5. 如果猜测正确,停止循环并显示总的尝试次数。

By decomposing the task, each piece becomes simple enough to solve individually, and you can check your understanding at every stage.

通过分解任务,每一部分都变得足够简单,可以单独解决,并且你可以在每个阶段检验自己的理解。


3. Pattern Recognition: Identifying Similarities | 模式识别:识别相似之处

Pattern recognition involves spotting similarities between problems or within parts of a problem. In the guessing game, we can identify several patterns. For example, after every guess, we perform the same sequence: get input, increment attempts, compare the guess, and output a message. This repeating pattern tells us that a loop or repetitive structure is needed. Additionally, the comparisons ‘is the guess too high?’ and ‘is the guess too low?’ are very similar; they both check a condition and direct the flow. Recognising these patterns helps us write more efficient and less repetitive code.

模式识别涉及发现不同问题之间或问题内部的相似之处。在猜数字游戏中,我们可以识别出几个模式。例如,每次猜测之后,我们执行相同的步骤:获取输入、增加尝试次数、比较猜测并输出消息。这种重复模式表明我们需要一个循环或重复结构。此外,“猜测是否太高?”和“猜测是否太低?”这两个比较非常相似;它们都检查一个条件并引导流程。识别这些模式有助于我们编写更高效、更少重复的代码。


4. Abstraction: Focusing on What Matters | 抽象:关注重点

Abstraction is about filtering out unnecessary details and concentrating on the essential features needed to solve a problem. For our game, we ignore aspects such as player names, colourful graphics, difficulty levels, or saving high scores. We focus only on the core mechanics: a random number, the player’s guess, a comparison, and a loop that continues until the correct guess is made. This simplified model makes the algorithm clear and easy to implement, whether we later build it in Python, Scratch, or pseudo-code.

抽象就是过滤掉不必要的细节,专注于解决问题所需的核心特征。对于我们的游戏,我们忽略玩家姓名、彩色图形、难度等级或保存最高分等方面。我们只关注核心机制:一个随机数、玩家的猜测、比较,以及一个循环,直到猜对为止。这个简化的模型使得算法清晰易懂,无论我们以后是用 Python、Scratch 还是伪代码来实现。


5. Algorithm Design: Step-by-Step Plan | 算法设计:逐步计划

An algorithm is a precise set of instructions to solve a problem. Here is the step-by-step algorithm for the number guessing game, written in plain English:

算法是精确的、用于解决问题的指令集。以下是猜数字游戏的逐步算法,用简单的英语描述:

Step 1: Generate a random integer between 1 and 100 and store it as number.

步骤 1:生成一个 1 到 100 之间的随机整数,并将其存储为 number。

Step 2: Set a variable attempts to 0.

步骤 2:将变量 attempts 设为 0。

Step 3: Ask the user to ‘Enter your guess:’ and save the input as guess.

步骤 3:要求用户“输入你的猜测:”,并将输入保存为 guess。

Step 4: Increase attempts by 1.

步骤 4:将 attempts 增加 1。

Step 5: If guess is equal to number, output ‘Correct! You took attempts attempts.’ and stop.

步骤 5:如果 guess 等于 number,输出“正确!你用了 attempts 次尝试。”并停止。

Step 6: Else if guess is greater than number, output ‘Too high. Try again.’

步骤 6:否则,如果 guess 大于 number,输出“太高了,再试一次。”

Step 7: Else, output ‘Too low. Try again.’

步骤 7:否则,输出“太低了,再试一次。”

Step 8: Go back to Step 3.

步骤 8:返回步骤 3。

This algorithm is clear, unambiguous, and uses sequence, selection, and iteration.

这个算法清晰、无歧义,并使用了顺序、选择和循环结构。


6. Flowchart Representation | 流程图表示

A flowchart uses symbols to visually represent an algorithm. The main symbols are: oval for Start/End, parallelogram for Input/Output, rectangle for Process, and diamond for Decision. Below is a description of the flowchart for our guessing game. You should be able to draw this using the standard symbols.

流程图使用符号来直观地表示算法。主要符号有:椭圆形表示开始/结束,平行四边形表示输入/输出,矩形表示处理,菱形表示判断。下面是猜数字游戏流程图的描述。你应该能使用标准符号将其画出。

Start → Process: Generate random number (1-100) → Process: Set attempts = 0 → Input: Ask for guess → Process: attempts = attempts + 1 → Decision: guess = number? → if Yes: Output: ‘Correct! Attempts: ‘ + attempts → End. If No: Decision: guess > number? → if Yes: Output: ‘Too high’ → go back to Input guess. If No: Output: ‘Too low’ → go back to Input guess.

开始 → 处理:生成随机数 (1-100) → 处理:设 attempts = 0 → 输入:要求猜测 → 处理:attempts = attempts + 1 → 判断:guess = number? → 如果是“是”:输出:“正确!尝试次数:” + attempts → 结束。如果是“否”:判断:guess > number? → 如果是“是”:输出:“太高了” → 返回输入猜测。如果是“否”:输出:“太低了” → 返回输入猜测。

Notice how the diamond decisions create two branches, and the loop is shown by arrows going back to the input stage.

注意,菱形判断创建了两个分支,而循环则通过返回输入阶段的箭头表示出来。


7. Pseudocode for the Guessing Game | 猜数字游戏的伪代码

Pseudocode is a readable description of an algorithm using a mix of natural language and simple programming structures. Here is the pseudocode for our game in OCR-style format:

伪代码是使用自然语言和简单编程结构混合而成的、可读的算法描述。以下是用 OCR 风格编写的游戏伪代码:

number ← RANDOM(1, 100)
attempts ← 0
REPEAT
    OUTPUT “Enter your guess: “
    guess ← USERINPUT
    attempts ← attempts + 1
    IF guess = number THEN
        OUTPUT “Correct! You took “, attempts, ” attempts.”
    ELSE IF guess > number THEN
        OUTPUT “Too high. Try again.”
    ELSE
        OUTPUT “Too low. Try again.”
    ENDIF
UNTIL guess = number

number ← 随机数(1, 100)
attempts ← 0
重复
    输出 “输入你的猜测:”
    guess ← 用户输入
    attempts ← attempts + 1
    如果 guess = number 那么
        输出 “正确!你用了 “, attempts, ” 次尝试。”
    否则如果 guess > number 那么
        输出 “太高了,再试一次。”
    否则
        输出 “太低了,再试一次。”
    结束条件
直到 guess = number

Note the use of REPEAT…UNTIL, which guarantees the loop runs at least once before checking the condition. This matches the game logic perfectly.

请注意 REPEAT…UNTIL 的使用,它保证循环在检查条件之前至少执行一次。这与游戏逻辑完全匹配。


8. Variables and Data Types | 变量与数据类型

Variables are named storage locations that hold data which can change during program execution. Let’s identify the variables we need and their data types:

变量是命名的存储位置,用于存放程序执行期间可能改变的数据。让我们确定所需的变量及其数据类型:

Variable Data Type Purpose
number Integer Stores the secret number to be guessed (1-100)
guess Integer Holds the player’s current guess
attempts Integer Counts the number of guesses made

中文翻译如下:

变量 数据类型 用途
number 整型 存储要猜的秘密数字 (1-100)
guess 整型 存放玩家的当前猜测
attempts 整型 统计已进行的猜测次数

All three variables are integers. We could also consider whether the guess input needs to be validated—if the user types a decimal or text, the program might crash. This is an important testing point.

这三个变量都是整型。我们还可以考虑是否需要验证猜测输入——如果用户输入小数或文本,程序可能会崩溃。这是一个重要的测试点。


9. Input, Output and User Interaction | 输入、输出与用户交互

Good user interaction makes a program straightforward to use. In our game, input and output must be clear. The program should prompt: “Enter your guess (1-100): ” so that the user knows what is expected. After each incorrect guess, the feedback “Too high” or “Too low” guides the next attempt. When the game ends, a congratulatory message displays the total number of attempts. It is also helpful to show the attempt number each time, for example, “Attempt 3: Too high.”

良好的用户交互使程序易于使用。在我们的游戏中,输入和输出必须清晰。程序应提示:“输入你的猜测(1-100):”,这样用户就知道需要做什么。每次猜错后,“太高了”或“太低了”的反馈会引导下一次尝试。游戏结束时,显示一条祝贺消息和总尝试次数。每次显示尝试次数也很有帮助,例如“第 3 次尝试:太高了。”

From a programming perspective, output is often done with PRINT or OUTPUT commands, while input uses INPUT or USERINPUT. Remember that whatever the user types is received as text, so we must convert it to an integer before comparison. In many languages this is done with functions like int().

从编程角度来看,输出通常使用 PRINT 或 OUTPUT 命令,而输入则使用 INPUT 或 USERINPUT。请记住,用户输入的内容是作为文本接收的,因此我们必须在比较之前将其转换为整数。在许多语言中,这通过诸如 int() 之类的

Published by TutorHao | Year 7 Computer Science Revision Series | aleveler.com

更多咨询请联系16621398022(同微信)

Comments

屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from aleveler.com

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version