# Module 6 - The “Guess the NumberLogic Game” Project

Skocz do: nawigacji, wyszukiwania

These materials have been developed by OśrodekEdukacjiInformatycznejiZastosowańKomputerów (Centre for Information Technology Education and Application of Computers) in Warsaw as part of the “MistrzowieKodowania” (“Coding Masters”) Programme funded by Samsung Electronics Polska, licensed under CC-BY-SA “Attribution – Share Alike 3.0 Poland”

## Information

### Objectives

#### General objectives:

• Review and revision of previously presented Scratch commands and language structures;
• Introduction of a binary search algorithm;
• Development of a simple logic game;
• Implementation of the game in the Scratch environment.

#### Specific objectives:

• The student will understand and be able to use appropriate structural instructions, including a conditional iteration.
• The student will understand the concept of variable and will be able to use it in the programme.
• The student will understand and be able to use messages and programme reactions to a received message.
• The student will be able to implement a simple logic game in the Scratch environment.
• The student will be able to use a binary search algorithm.

## Stages

Time to complete this part: about 10 minutes

At the beginning of the lesson we remind students what they were supposed to prepare between classes. We invite a volunteer to present his/her solution. We ask if anyone had a different way of finding a solution. We discuss the solutions (praising correct solutions to individual tasks).

If there was no homework, we start by repeating key issues from the previous module. We also ask if students have any doubts regarding their own projects. If anyone wants to show something he/she is proud of, we let him/her do it.

### Designing a game involving guessing a number randomly selected by the computer

Time to complete this part: about 20 minutes

At the beginning of the lesson we present the design concept which we want to develop in class. Together with the students we consider how a game involving guessing a number randomly selected by the computer could work. We considering how the programme should react to correct and incorrect answers. How can a response given by the programme prompt the player to find the correct result? We reach the conclusion that the programme should inform the player whether the number provided is smaller or larger than the randomly selected one. Together with the students we discuss this and write down the proposed algorithm on the board.

The first algorithm version may look like this:

After the programme has been started:

• it displays a message telling you how to play, e.g. “I thought of a number between 1 and 100. Guess what number it is!”
• it draws a number (in the range from 1 to 100) and stores in a variable
• it displays the words “Give a number” and waits for the player’s answer
• it compares the player’s answer with the drawn number:
• if the number does not match
• it checks if the number given by the player is higher than the randomly selected number
• if it is
• it displays the message: “Too high!”
• otherwise
• it displays the message “Too low!”
• once again it prompts the player, waits for an answer and compares it with the drawn number
• if the number matches
• it displays “Well done!”
• it ends the script execution

After the first algorithm version has been written down we point out that we are dealing with certain actions which will be repeated.

We ask the following question: Which actions are repeated? Do we know how many times? Can we formulate a condition that must be met to terminate the repetition?

If necessary, we guide students to realize that from the step “the programme compares the player’s answer” actions are repeated until the answer provided is correct (the provided number matches the number drawn by the programme). We point out it will be appropriate to use a relevant loop.

We ask the following question: Which loop will work here and what should it comprise? We ask the students to find a loop block that they think fits best.

In this case, we help students find the loop repeat until.

We modify the algorithm written down on the boardaccordingly and include the loop repeat untilin it. The modified algorithm may look like this:

After the programme is started:

• it displays a message telling you how to play, e.g. “I thought of a number between 1 and 100. Guess what number it is!”
• it draws a number (in the range from 1 to 100) and stores it in a variable
• it displays the words “Give a number” and waits for the player’s answer
• it repeats the following instructions until the player’s answer matches the randomly selected number stored in the variable:
• it checks if the number given by the player is greater than the randomly selected one
• if it is
• it displays the message: “Too high!”
• otherwise
• it displays the message “Too low!”
• it displays the words “Give a number” and waits for the player’s answer
• it displays “Well done!”
• it ends the script execution

### Implementation of the “Guess the Number” logic game

Time to complete this part: about 25–30 minutes

We ask students the following question: What block can we use to ask a question? Where will the answer be stored? We ask them to find such a block. Students should find two blocks which are among sensors and are placed one above the other.

We ask students the following question: What blocks can we use to compare two number values? We ask them to find such a block. Then we ask what exactly we will be comparing.

Students should find a comparison block and come up with the idea that we will compare the drawn value stored in a variable with a variable named answer where the value entered by the player will be stored. We point out the need to create a variable (e.g. named number) in which we will save the drawn number.

Additionally, we tell students that they can use blocks which let them verify which number is higher, which will be useful when creating the game script.

Task: We ask the students to create a script in accordance with the developed algorithm, as well as to choose a suitable game background and a sprite which will communicate with the player.

Sample script may look as follows:

It is a good idea to point out (unless the students notice this themselves) that the commands which display messages should stay visible for a specified time (e.g. 2 or 3 seconds) rather than disappear immediately.

Sample stage and sprite for the game:

After the students have written the game script we ask them to play the game for a while. Then we ask them to propose a method of asking questions (i.e. a number guessing algorithm) to guess the number drawn by the computer as quickly as possible (asking as few questions as possible).

### Introduction of a second sprite and communication between their scripts

Time to complete this part: about 25–30 minutes

We propose to make the game more attractive by introducing another sprite character. Together with the students we consider how to modify our script so that one sprite asks questions, and the other answers them. We write down our findings on the board, for example:

Ala’s and Jaś’s scripts may look like this:

Jaś’s script:

Sample stage for the modified project:

### Introducing a question counter

Time to complete this part: about 10 minutes

Together with the students we reflect that our game lacks a scoring system. We propose that students introduce a countervariable to calculate how many questions Jaśbefore he guesses the drawn number.

Task:We ask the students to individually introduce necessary modifications to the programme and then show their solutions.

A possible solution