17 — Word guess: a hidden-word game
This is where Part 2 begins. Everything from Part 1 comes together into a game of guessing a hidden word in six tries. Lists, chance, loops and conditions all appear, and there is not one quote, bracket or equals sign in it.
Steps
Pick the hidden word at random. Shuffle a list and take the first of it:
run it →set words to list of apple, grape, melon shuffle words set secret to the first of wordsA word can be walked through letter by letter.
for each letter inhands them over one at a time:
run it →set secret to apple for each letter in secret show letter endBuild the masked word. A letter already guessed stays; one still hidden becomes
_. Collect them in a list and join it with spaces:
run it →set secret to apple set found to list of a set shown to an empty list for each letter in secret if found contains letter append letter to shown else append _ to shown end end show shown joined by spaceappleis a, p, p, l, e, and onlyahas been found, so it printsa _ _ _ _. Change the word and watch the mask follow it.Winning is one question. If nothing is masked any more, it is all found:
run it →set shown to list of a, p if shown does not contain _ show you got it endHere is the whole game. Put it in
word-guess.nme:
run it →set words to list of apple, grape, melon shuffle words set secret to the first of words set found to an empty list set lives to 6 repeat forever set shown to an empty list for each letter in secret if found contains letter append letter to shown else append _ to shown end end show shown joined by space if shown does not contain _ show you got it stop end if lives equals 0 show out of guesses show secret stop end show lives ask letter Guess a letter if secret contains letter show yes else show no subtract 1 from lives end append letter to found end
Try it yourself
Add more words to the list. Then stop a repeated guess from costing a life —
one if found contains letter does it.
What you learned
- Shuffling a list and taking the first of it is a random pick.
for each letter inwalks a word letter by letter.- The masked word is a list you build and then join.
does not containasks whether you have won, in one line.- The whole game has no quotes, no brackets and no equals sign.