September Valentine

Hearts, particularly valentine hearts, tend to be a feature of February rather than September. Fortunately, my valentine isn’t crammed full of them. I’ll share it with you, below, as it’s a Snap project.  This school year, I’m providing what TEALS refers to as “lab support” for students in the morning class at DeKalb High School of Technology—South (DHSTS) in Decatur, Georgia.  And, as part of that, I’m working the projects, same as last year, although now, Snap, is not a stranger to me.

DHSTS is a magnet school.  Students are bused in from other high schools to spend half a school day (four periods) in my class, five days a week. This means we cover a lot of material in a short time. In fact, they’re doing an intro to tech first quarter,‘s Computer Science Principles during the second quarter and AP Computer Science (with Java and a test), third and fourth quarters.

Which brings me back to the valentine.  As part of the intro, we’re doing a little Snap and we’ve chosen what TEALS calls the Animated Storytelling Project.  Last year, I did a nursery rhyme, “Itsy Bitsy Spider” and all of my students did nursery rhymes as well. This year, it’s expanded. It does have to (loosely) tell a story/narrative and fulfill the rubric.

And so, I present to you.  Carol Ann Duffy’s poem “Valentine” in Snap.

reading code to write code better

Programmers look at other programmers’ code. That’s one of the points behind having the blog. This is a Good Thing because as someone who writes code, I learn from reading it. I am not alone.

What sometimes happens though is a coder will lift a snippet out. In most programming languages, with limited syntax, once someone has the best algorithm coded, there’s not a lot of room for improvement. While there may always be more than one Right Way to accomplish a task, there may only be one Best Way and once someone’s found it, it’s found forever (until someone finds An Even Better Way—which does sometimes happen, but very rarely).

So it makes sense to use a standard implementation of a known algorithm. It will look exactly like everyone else’s. These algorithms all have names though. Swap. Binary Search. Merge Sort. Whether you lift a merge sort from Joe Schmoe’s code or roll your own, it’ll be a merge sort. It would be 100% possible for you to create the exact same merge sort as Joe’s without ever seeing his.

On the other paw, if you have code that is the exact same as Joe’s for something else, then chances are it is Joe’s code and that is not only illegal, it is wrong.

There are all kinds of ethical reasons why this is a bad idea, but I’m not here to be your conscience. The number one practical reason it’s wrong is that if you didn’t write it, you do not deeply understand it, and if you don’t understand it, you cannot modify it or fix it if it breaks (or if it is already broken). Heck, if it’s broken and you don’t understand it, you won’t even know.

The number two practical reason is if you get caught, you could lose your job, or worse, your stuff (your bank account(s) are first, and then they sell your assets). You could go to jail. It has happened to other coders.

Now some code is okay to use, but it’s clearly marked with licenses (such as the GNU Public License) that specifically say so. If you’re in school, though, your class and school rules override any licenses. When in doubt, check with your instructor. If you’re working for someone else, your employer’s rules apply. Check with your boss if you have a question.

Study code. Read it. Learn to spot the good and the bad in it. Then use that knowledge to build your own solutions.

the clock strikes again

My students will be doing final Snap projects soon and that means I will be guiding them. Part of that process is showing them other Snap projects written by other students. And wouldn’t you know it? This is the weekend the Snap folks are modding the cloud and it is unavailable. That means there is no access to the projects stored on the Snap cloud and no saving new projects to the Snap could, either.

What’s a woman to do? Write a new project and save it locally, that’s what. Here is the new Pomodoro Clock, Snap edition:

No link because … no Snap cloud. I’ll be GitHubbing it soon, but I want to beta it first. This has no custom blocks, no lists, many costumes, and many broadcast messages.

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.

every rose has a thorn … every script has a sprite

Cliches get that way by being true. People remember them for the next situation because they have an established track record.

Every script has a sprite that it’s attached to.  If you want to do something in Snap, you pretty much need to hang it on a sprite (or the stage, but that’s a special case). It’s a feature of Snap.   Sprites don’t need scripts.  You can have a sprite sit there on the stage and look pretty and do nothing but you can’t have a script run without there being a sprite for it.

This is important because if you want to do something in a Snap project, you have to decide which sprite is going to be in charge of it.  Suppose there is a game where the player tries to catch falling raindrops in a cup.  If a raindrop lands in the cup, a message is broadcast to add a point to the score. Which sprite sends that broadcast?  The landing raindrop or the receiving cup? There are good arguments either way. It’s up to the coder to decide.  But the coder has to attach it to some sprite.

That’s why I say every script has a sprite.

planning and implementation

One of the hardest lessons to learn is that it’s easier to work from a plan than to wing it. Most things that most people do most of the time don’t require a lot of planning or forethought because they’re repetitive tasks that have been done before. For example, I don’t make a plan to drive to my grocery store because I know the route, I know what I’m likely to encounter, etc.

The truth is, the first time, I didn’t know.  And when you’re creating a project, it’s the first time. When I go to a new place for the first time, I use Google to map it, I figure out how long it will take to get there so I can leave at the right time, I maybe even use the street view to see what the landmarks might be.  A planning worksheet is an equivalent for a software project.  It’s a way to break down the problem of how do I get from nothing to a solution in steps.

Depending on the complexity, a planning sheet will have different kinds of information but since Hangman is my current project, I’m going to frame this topic from that perspective.  There is a worksheet that was thoughtfully provided, but what if there hadn’t been?  What kinds of things would need to be on it?

Well, looking at the rubric, Hangman has to be able to do a lot of things. 

So any planning sheet has to account for that. Since this is Snap, there will be sprites and the stage.  Every script will belong to either a sprite or the stage so sprite ownership is something that needs to be part of the planning sheet.  Also, information will need to be stored. That means variables will be there, too.  I’ll need a column for pseudocode and a column for issues or pitfalls or whatever I want to call the potential snags I’ll run into.  This is not what the planning sheet looks like that was handed out.  
This is a matrix with two examples filled in. Kinda like a spreadsheet.  The first column is the rubric item and the rest are the columns I identified that I, as the project coder, would need to cut the project down to size.  Other people might see the project with different frameworks and have different columns.

The important thing is to have a plan.  And a planning sheet gives you a good place to create one. The better you plan, the less actual work you have to do (at least that’s the theory).  And in my experience, I’ve found it true often enough that I make it a habit to plan many facets of my life, not only my programming projects.

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.

back in business

I am back,  I’ve hiatused long enough and I have fixed the images and code snippets.  What you’ve missed is lists.  Traversals and sequential searches of same.  I’ll cover them as part of Hangman, which I wrote in Snap, but as my students are writing it now, I’ll not be posting mine quite yet.

I’ll post my platform game though. CatStuff.  Here’s a screenshot.Basically, the cat can jump over the saucerbot but if they collide, the game is over.  That’s it.  It wraps. It’s boring.