Naeem Sarfraz

Blogging about Enterprise Architecture, ALM, DevOps & happy times coding in .Net

Random Puzzles in Scratch

Using the Super Scratch Programming Adventure examples I set our Code Club students recreating a memory game using the Mona Lisa puzzle game from the book. The completed version “hard codes” the sequence you need to memorise so one of the students set about creating a truly random sequence generator. I want to document one way of doing this in Scratch here for his benefit and anyone else who might find this useful.

Presenting the sequence to the game player is defined by a series of sequential blocks like so:

image

And judging the game players response is even more unwieldy as there is lots of duplication and is very difficult to modify if you want to extend the sequence. Go to the project hosted on the Scratch site for the full code and click “See Inside.”

image

Generating a Random Sequence

The numbers from which we want to generate our sequence represent the options available to the game player, e.g. 1 to 4. To avoid the “hard coding” we’re going to require a list variable to hold the random sequence. We can now fill this list using this block of code:

image

Sometimes this will generate a sequence where a number might repeat like “2 2 1 4”. For the game player this isn’t a nice experience so I’m going to add a check so that it will only add the random number to he list if it doesn’t match the last random number.

image

Judging the Last Move

Working out if the game player entered the correct move is really easy now that we have a list of random numbers. I chose to solve this by:

  1. Saving the game players chosen move to a lastkey variable
  2. Check that the value of the lastkey variable and the last number added to the list are the same
  3. If so then the game player got it right
    1. Let them know they got it right using the say block and
    2. Remove the last item from the list as we don’t need to check this number again

image

The Final Solution

Here’s is the new version.

 

A few things to note with this solution:

  • I haven’t implemented “lives” for the game player so that you can only get three attempts at getting the sequence correct
  • When you start you get the option of choosing a level. This simply extends the number of steps you need to memorise
blog comments powered by Disqus