27 — Game: guessing a hidden set of colours
The computer hides three colours and you name three. It tells you only how many you got, never which ones, so finding them means changing one answer at a time and watching the number move.
Steps
Choose the hidden colours. Shuffle the list and take the first of it, three times over:
run it →set colors to list of red, blue, green, yellow set answer to an empty list repeat 3 times shuffle colors append the first of colors to answer end show answer joined by commaPrinting the answer is how you check the program while building it. Delete that last line once it works.
One try takes three colours, keeps them in a list, and counts how many of them are in the hidden set:
run it →set answer to list of red, blue, green set right to 0 set guess to an empty list repeat 3 times ask choice What colour? append choice to guess if answer contains choice add 1 to right end end show guess joined by comma show rightWinning is that one count:
run it →set right to 3 if right equals 3 show you found them all endCount the tries down, and show the answer when they run out:
run it →set lives to 5 subtract 1 from lives if lives equals 0 show out of tries endAll of it:
run it →set colors to list of red, blue, green, yellow set answer to an empty list repeat 3 times shuffle colors append the first of colors to answer end set lives to 5 repeat forever set right to 0 set guess to an empty list repeat 3 times ask choice What colour? append choice to guess if answer contains choice add 1 to right end end show guess joined by comma show how many of them are hidden show right if right equals 3 show you found them all stop end subtract 1 from lives if lives equals 0 show out of tries show answer joined by comma stop end endPosition is not checked. It only counts whether a colour is in there, so naming the same colour twice counts twice. Real Mastermind also says how many are in the right place, and that needs a loop that knows which position it is at — something sentence syntax does not have yet.
Try it yourself
Use five colours and six tries. Count for yourself which of the two changes makes it harder.
What you learned
- Shuffling and taking the first, several times over, makes a random set.
- A value taken out of one list goes straight into another.
- "How many did I get" is one counting name and one
contains. - The whole game has no quotes, no brackets and no equals sign.