Andrew Sellergren
what ultimately matters in this course is not so much where you end up relative to your classmates but where you, in Week 11, end up relative to yourself in Week 0
Let's see if we can write an algorithm for putting on socks in the morning. David will follow our instructions exactly and Joseph will write them down:
Here we have a bit of ambiguity. Which sock? Before long, we'll need to express ourselves more precisely so the computer knows exactly what we're trying to accomplish.
Now, we want to find a matching sock. This is instinctive for a human (who can scan them all quickly with his eyes), but not so for a computer. In order to find a matching sock, we have to iterate or loop through all of the socks and compare them to the one we have. Something like this:
find matching pair
for each sock
pick it up...
if it's the same shape/size
take it
Notice that Joseph has already started following one programming convention: indentation. When you construct a loop, the chunk of code that gets executed with each iteration of the loop is usually indented. Likewise with the code that meets a certain condition (e.g. take it
in the example above).
find matching pair
for each sock
pick it up...
if it's the same shape/size
take it
Now that we've taken care of one foot, we need to make a design decision to take care of the second foot. Should we write another loop? Perhaps. Since we only have two cases to handle, we could simply copy and paste the steps above. However, anytime you find yourself copying and pasting code, you should probably consider using a loop instead.
Let's take a look at a short program in C:
#include <stdio.h>
int main(void)
{
printf("hello, world!");
}
After we open up a text editor, we insert the lines of code above and save the file as hello.c
. This file now represents our source code. As we've already learned, computers can't understand anything but binary, so we need to translate this source code into binary. This same code in binary is called object code. To do this translation, we run the following from the command line:
clang hello.c
a.out
has been created in our home directory. a.out
contains the 0's and 1's of our converted program. Now that it has been converted, we can execute it like so:
./a.outIt worked! Unfortunately, it's not very pretty. It seems to have printed hello, world!
followed by something like air:~ jharvard
. That last bit, however, is actually printed at the beginning of every line of our Terminal window. It designates the computer's name, the current directory, and the username. So it seems that our program worked, but it would have been prettier if we could have hit Enter after hello, world!
. We can do this by inserting what's called a linebreak. We represent a linebreak in code with a \n
like so:
#include <stdio.h>
int main(void)
{
printf("hello, world!\n");
}
a.out
actually contain? If we try to examine it using a text editor, we get junk. This is because the text editor is misinterpreting the 0's and 1's as ASCII characters. Somewhere amidst the junk, you can actually see the words "hello, world!"To look at the actual 0's and 1's that a.out
contains, we can execute the following command:
xxd -b a.out
Dancing cookies.sb
, we see that the cookies are sprites and in the scripts area, there are two long chains of puzzle pieces joined together, each with a "when green flag clicked" piece at the top. Recall that to start the animation, we clicked a green flag, which suggests that both of these scripts start running as soon as we do so.Hello1.sb
is equivalent to hello.c
, only a little more colorful.Hello2.sb
is slightly more complicated. The cat will say "O hai, world!" for 1 second, wait 1 second, say it again for 1 second, wait 1 second, and say it again for 1 second. This seems a little wasteful and, in fact, we could implement this more cleanly using the "repeat" block.Hello4.sb
and Hello5.sb
make use of conditions and boolean expressions. In the first, the condition 1 < 2 always evaluates to true, so the cat meows every time we click the green flag. In the second, however, the condition says, "pick a random number between 1 and 10 and if that number is less than 6, have the cat meow." Thus, the cat will meow approximately half the time we click the green flag.Hello6.sb
, we implement a loop which causes the cat to meow indefinitely. Infinite loops aren't necessarily bad: consider the case of the clock on your computer which is constantly updating the time.Hello7.sb
, we combine a loop and a condition so that the cat will meow only if the mouse pointer is touching it or, in other words, if we are petting it. In Hello8.sb
, we add an extra condition (using the else
keyword) so that the cat will meow indefinitely, but will roar if we touch it with the mouse pointer.Hello9.sb
executes two scripts in parallel (as Dancing cookies.sb
did). In the logic of one of these scripts, we see that if a variable named muted
is 0, we play the sound. Variables are another useful programming construct. They allow us to store information about the state of a program. Recall that 0 is equivalent to false, so this makes sense: if the script is not muted, it should play sound. The second script actually sets the variable muted
: to 1 if it's currently 0 or to 0 if it's currently 1. This variable setting occurs when we press the space bar. Note that variables in programs should have descriptive names, as with muted
here.FruitcraftRPG.sb
, for example, an array is used to store the different types of fruit which have been collected. An array is one of many different types of data structures which are essentially buckets in which we can store information of interest to our program.Scratch Scratch Revolution.sb
.sb) and btwalsh.sb
for more examples of what you can do with Scratch! Realize that they didn't implement these complex games all in one sitting. Rather, they broke them down into bitesize pieces. In the case of Scratch Scratch Revolution, maybe the student first implemented support for the left arrow key only. Then maybe the student implemented the green arrow that flies from the bottom of the screen. He or she might have then introduced some pseudorandomness, i.e. picking a number between 0 and 5 as we did earlier. We say pseudorandom instead of random because a computer can't actually be random.Threads.sb
, we achieve "multithreading," at least in appearance. In terms of programming, we have two different scripts associated with two different sprites, a cat and a bird. For the cat, we begin by placing him in a given spot on the stage and orienting him in a random direction. Then we begin a loop whereby if he touches the bird, then the game ends; otherwise, we orient him toward the bird and advance him one step. For the bird, we move him around the stage three steps at a time. Effectively, then, the cat is chasing the bird until he catches him. If we increase the number of steps that the cat takes compared to the bird, the cat will catch the bird all the more quickly.Events.sb
leverages events to play the game of Marco Polo. Thus far, our sprites haven't really been communicating with each other. But in this game of Marco Polo, one sprite is saying "Marco," and the other is listening for him to say it so that she can say "Polo" in response. The second sprite is listening for the event which the first sprite broadcasts.Raining Men.sb
. While you enjoy it, think about how you might implement it!