setting maximum guesses
Learning Goals
By the end of the lesson, students will be able to:
- keep track of the state of the game
- increment a variable
Overview
Students use their knowledge of defining functions, creating variables, and conditionals to add a maximum number of guesses to their game. If the maximum number of guesses has been exceeded, then the game returns a game over message.
Suggested stations
- variable values
- conditional syntax
plan (5 minutes)
- Lead a discussion with students about how the guessing part of the game would work in the human context. The final solution should look similar to:
- Person 1 thinks of a number. (variables)
- Person 1 decides the maximum number of guesses. (variables)
- If the person has guesses left. (conditionals)
- Person 2 guesses a number.
- Person 1 (game) checks if the guess is correct. (conditionals)
- If the guess is correct person 2 wins (return, strings)
- Otherwise person 1 give person 2 a hint: higher or lower. (function, return, strings, conditional)
- Otherwise game is over. (conditionals)
- Label each of the steps with a javascript concept (in the parentheses above)
- Discuss which concepts are new.
produce (20 minutes)
Remind students that their goals are:
- Create a variable to store the maximum guesses
Create a function called
checkDonethat checks if the current number of guesses is greater than or equal to the maximum guesses.- If it is tell the player the game is over.
- Otherwise check the guess.
pseudocode:
SET answer to number SET guesses to 0 SET maxGuesses to number FUNCTION main (guess) RETURN guessNumber(guess) FUNCTION guessNumber (guess) INCREMENT guesses IF checkDone() is true RETURN lose message ELSE IF checkGuess(guess) is true RETURN win message ELSE RETURN renderHint(guess) FUNCTION checkDone (guess) IF guesses is greater than or equal to maxGuesses RETURN true ELSE RETURN false FUNCTION checkGuess (guess) IF guess is equal to answer RETURN true ELSE RETURN false FUNCTION renderHint (guess) IF guess is greater than answer RETURN too big hint ELSE RETURN too small hint
With their partner, students plan and attempt to implement a solution for creating a maximum number of guesses. If the maximum number is reached, the game should output 'Game over'
- Fill in handout
- Discuss plans
- Attempt a solution
- Justify reasoning
implement (15 minutes)
- Discuss with students their ideas. Make sure to have students from each group explain what they attempted or any ideas they had on how they could use their knowledge to implement a solution.
After hearing their solutions, introduce students to the teacher solution.
var MAX_TRIES = 10 var guesses = 0 var answer = 6 function main (guess) { return guessNumber(guess) } function guessNumber (guess) { if (checkDone()) { return 'Game over' } else { if (checkGuess()) { return `Correct!` } else { return renderHint(guess) } } } function checkDone (guess) { if (guesses >= MAX_TRIES) { return true } else { return false } } function checkGuess (guess) { if (guess === answer) { return true } else { return false } } function renderHint (guess) { if (guess > answer) { return `${guess} is too high!` } else { return `${guess} is too low!` } }- Students contrast the ideal solutions with their own ideas.
- Student implement a working solution in their project.
justify (5 minutes)
- As a group, explain the code that they have written. Make sure to decompose each block and expression that was added to the code.
- Tell students that in the future, they are expected to justify their code in this way after each feature that they add.
- Students practice by explaining their code to a partner.
test
- Students play the game of their partner.
- When students are testing a game, they are looking for:
- bugs
- feature improvements
- When students find a bug they fill out a bug report:
- What were you doing in the game?
- What did you type into the input box?
- What did you expect to happen?
- What actually happened?