Guess the Number game: First play guess the number with a friend. Think of a number between 1 and 10. The friend guesses a number, you respond: too high, too low or that's it! In this lesson we learn about binary search (the strategy that allows you to always win the game in 5 or less guesses).
Example:
- version1 https://scratch.mit.edu/projects/91603538
- version2 https://scratch.mit.edu/projects/93033774/
Activity: Create a guess the number game in which the computer picks a random number between 1 and 10, and the user of the game keeps guessing the number. If the user guesses the number in 5 guesses or less, he wins. Otherwise he looses.
Steps:
- Display a title and simple instructions on the screen
- In the first version of your program, let the user guess as many times as he needs to.
- Create a variable named The Number and assign it a random number between 1 and 10. This number will be hidden from the user of the game, but for now, for debugging purposes, display it on your screen by marking the checkbox next to the variable name in the Data blocks.
- Use the Sensing / Ask block to prompt the user to guess the number and take his input. When the user types in his guess, it gets saved in a variable named answer. Wrap this into a Forever block, since we want the user to be able to guess more than once.
- Compare the value stored in 'answer' to the value stored in the variable called 'The Number'. Check for 3 possible conditions: correct, too high, too low (using if/else blocks). Display different messages based on which condition is met.
- Test your program. Once it works correctly, continue.
- Binary Search: In computer science, a binary-search or half-interval search finds the position of an item within a sorted list. In our game, the sorted list is: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
Definition: "The binary search algorithm begins by comparing the target value to the value of the middle element of the sorted array. If the target value is equal to the middle element's value, then the position is returned and the search is finished. If the target value is less than the middle element's value, then the search continues on the lower half of the array; or if the target value is greater than the middle element's value, then the search continues on the upper half of the array. This process continues, eliminating half of the elements, and comparing the target value to the value of the middle element of the remaining elements - until the target value is either found"
In our game, the user can always win in 5 steps or less if he uses the binary search strategy.
Let's say the computer generated number is 4.
The user guesses 5, because that's halfway between 1 and 10
Computer responds: To high. Guess again!
Then the user guesses 3, because that's halfway between 1 and 5
Computer responds: Too low. Guess again!
Now the user knows that the number is between 3 and 5, so he guesses 4 and he wins.
- In the second version of your program, add a limit of 5 guesses. Add a new variable for maximum number of allowed guesses. When the guesses run out, display a message: "You lost. The number was _ (display here the random number the computer picked at the beginning of the game). Click the green flag to play again."
Tip: Concatenate (glue together) the string "You lost. The number was " and the value of The Number. Use the Join block.
Tip: While debugging, display the value of all your variables on your screen, including The Number (the computer generated number). Just don't forget to hide the variables after you are done testing and debugging!
Test your program. Make sure it works for the following test cases:
Test 1 When the user guesses the number in 5 or less tries, the user wins.
Test 1.1 A message appears that informs the user that he won.
Test 1.2 A message appears that informs the user of how many guesses it took for him to guess correctly
Test 2 When the user tried to guess 5 times, but he did not get the number, the program must NOT allow the user to input any more guesses
Test 2.1 The program should also display a message to inform the user that he lost and what was the number the computer picked.