is it in there?

Sometimes you want to know if something is in a list. If the list isn’t sorted, the only way you can know for sure is to look at every item in the list and see if it is what you’re looking for. That is called a sequential search and it’s pretty easy to do. Since we’re using Snap, I’ll draw you a picture.

This script reports whether the item was found in the search or not, but it could be modified to do other things. Suppose for example, you have a list of students and you’re looking for all students named Bob. You don’t want it to simply stop with the first student. So you’d need to modify the search to change the “found” half of the or on the repeat until … and instead have found be a variable that you set to an empty list before the loop and then add the index positions as list items to it when you find a Bob in the class. If there is one. If there are no students named Bob, you would have an empty list, same as what you started with. If there were two students named Bob, you would have a list with two index positions in it, telling you where the name Bob appears in the list.

Of course there are other ways. This is programming and, generally speaking, you can find several valid solutions to any problem.

If the list is sorted, there are shortcuts. You can split the list and look in the part of the list where you know something is. You do this when you look through any alphabetized anything. You know not to look for something that starts with a  in the r section.

i’ve got a secret

I said before Hangman is about lists. One of those lists is a list of blank letters that represent the word the player is trying to guess. The easiest way to deal with this is to write a custom block that generates a list of as many blanks as there are letters in the word.

In programming, there are always several right ways, however, there are usually many more wrong ones. Here is one idea of what that custom block to make a list of blanks might look like.

Someone could also build a word of blanks and then use this block from the Import Tools to turn it into a list.

However you build the custom block to make your list of blanks, to say three blanks the script would look like this:

In Hangman, the list of <length> blanks block would the beginning of my display word, before any letters were guessed. If the word were cat it would be three letters. Or run.

To code it, I’d make a variable to store that list. and then replace the blanks with letters as they were guessed correctly.The list variable would belong to all sprites so the host could do the work of checking it and the assistant could do the work of saying it.

it’s all about the lists

Fundamentally, Hangman is a game of lists.  You have the alphabet, the list of letters that make up a secret word and a list of letters that have been guessed.

This is why it was chosen as the project for the list unit in our curriculum. Snap has a lot of blocks to deal with lists but if you use the import Tools, you get access to even more.  My favorites are andThe first one turns a word into a list and the second one turns a list into a sentence.  Both are useful for Hangman.

making a list, checking it once

Checking a list is called traversal (actually, going through the list and doing anything to it is).  Here’s an example of a list and a traversal of it.  These are the Great Lakes of North America.

The script variable is a variable that does not belong to any sprite and is used only in the script.  This code only says the names of the Great Lakes, but if you take out that purple block and put something else in there, you could do other things.  For example, if you wanted to see which lake had the longest name, you could have a script variable called longest and store the first name there. Then, instead of saying the name, you could have an if block that checks if the current name is longer than the longest  name and if it is, make that name the longest name..  Then put your purple say block at the end and say longest for 2 secs.