Day 5: Program State
Today we will practice using global variables to store and change the state of the program..
Please switch driver and navigator each exercise, and share your solutions with your partner.
Before you begin, check that you and your partner can answer the following:
- Why use global variables?
- What is the difference between global and local variables?
- Can a global variable be accessed or manipulated within a function?
- Can a variable declared in
mainbe accessed or manipulated in a different helper function?
Create a game where a player must correctly guess secret words to win.
The computer chooses a random word from a set of three words. Feel free to use whatever words you'd like, but we will be using the words "banana", "chisel", and "faucet".
To win the game the player must guess correctly twice in total; but is allowed to guess wrongly in between their two correct guesses.
For each guess, output all information such as the guessed word, the secret word, and how many correct guesses the player still needs until she wins.
Update Secret Word with an additional rule: The player must guess correctly twice in a row to win.
Update Secret Word game such that the number of times the player needs to guess correctly in a row changes between rounds.
We define "round" as all gameplay until a player wins. The number of times the player needs to guess correctly is a number from 2 to 4, randomly chosen at the start of the round.
When the player wins, the computer updates the number of times the player needs to guess correctly in a row for the next round.
Reminder: There is no need to complete all exercises on this page. Once comfortable with the concepts, feel free to move on to Scissors Paper Stone: Part 2 You can always come back and attempt the additional problems when you are feeling more comfortable.
Create a new version of our Coding Fundamentals dice game (where the player guesses the dice roll) that randomly becomes easier or harder. At the beginning of the game and each time the player wins, the computer selects a random number from 1 to 3, which we call the "within number". If any of the player's guesses fall within the dice roll plus or minus the within number, the player wins.
For example: the app randomly chooses 3 as the within number. The player can guess within 3 of the dice roll to win the game. The player guesses 2 and the dice roll is 5. The guess is within 3 of the dice roll so the player wins the game. At this point, the computer adjusts the difficulty of the game by choosing a new within number.
Update our dice game to include an additional dice. With each player guess, the computer rolls 2 dice. If the player guess is within either of the 2 dice rolls plus or minus the within number, the player wins.
Update our dice game to allow the player to play a round of 4D every 2 times the player wins the dice roll guess. Use input control from Module 4.3 to verify our 4D game logic, that it is possible to win this 4D game.
Update Secret Word with the following additional rules.
- 1.The player must guess correctly twice in a row to win
- 2.Secret words cannot be repeated 2 guesses in a row. E.g. the secret word cannot be "banana" for 2 player guesses in a row. If the secret word for the 1st guess were "banana", the secret word for the 2nd guess would have to be either "chisel" or "faucet".
Here is a reference solution for Day 4 exercises. Please only view the reference solution for each exercise after you have attempted the exercise yourself. Note that there are many ways to implement these solutions and the reference solution is only 1 way.