[ Silence ]
>>[background noise] Alright, well thank you.
Alright, welcome back.
My name is David Malan,
for those of you joining us for the first time.
And this is CS50 and this is the continuation of week zero.
For those of you who may have missed Wednesday,
notice that we still have some syllabi.
No more cake but if you did in fact miss Wednesday,
do notice that on the courses website is as will always be
>>This is not your typical high school computer science course
>> So that there is what you missed on Wednesday.
So in the future you will find
that the course makes available its videos in flash formats,
quick time formats as well as in MP3 formats
on the courses website so do take a look at that.
So we've been hard at work over the past couple of days,
particularly one of our CA's who actually is an alumnus,
he lives in New York, his name is Welly [phonetic].
You may meet him in the virtual terminal room over time
and we've implemented what we're now calling Shuttleboy voice
so you may recall and you may in fact
as an upper classman have used before,
this program called Shuttleboy.
It's a good 12 plus years old now.
It lives via this link on CS50 zone websites here
and let me actually grind to a halt here.
Alright, let's change to this, oh, so what you will see
in a moment, oh, alright [laughter] my name is David
Malan and this is CS50.
Welcome back to week zero continued.
We will crop off of the video everything that just happened.
If you missed Wednesday's lecture video,
know that it is available online, I won't belabor
that particular point but I will draw your attention
to a little application that we've been working on so
for sometime there's existed this program called Shuttleboy.
I'm, say, at Memorial Hall.
I need to get back after class to the quad so I click two links
and wala, [phonetic] there are the next several shuttles I can
get to.
Well we thought it would be fun to take things
to the next level this year and to implement Shuttleboy voice,
the new and improved version of the same idea.
As you may recall from Wednesday we were lucky enough
to share a secure C17 bug CS50.
It's not quite yet in production.
This is really just a sneak preview
but in a moment I'm going to turn this microphone on
and we're going to make a phone call together
and see who here picks up.
So I'm going to go ahead and turn on my speaker phone.
We're going to dial and see what happens here.
>>This is CS50 for Shuttleboy.
Press 1 for Quinton [phonetic] dining.
Press 2 to start over.
Press zero
>>I'll press 1 for Shuttleboy.
[noise]
>>What is your origin for quad?
Press 1, Mather.
Press 2, Boylston [phonetic] Press 3, Lamont.
Press 4, Mem Hall.
Press 5 to start over.
[noise] What is your destination for quad?
Press 1, the next shuttle leaves in 5 minutes at 11:00 pm
and then at 11:10 pm, 11:20 pm and 11:30 pm.
[laughter]
>>So you're going to be here a while today.
It's perhaps the media takeaway
but [applause] so that's not a bug.
That's a feature.
So we just dummied it up with some data for demonstration sake
but it should debut for all of you this coming week.
And notice we've optimized for the most popular stop
so if you want to get on, for instance, at Pound Hall,
it's not gonna happen with Shuttleboy.
Boys Quad, Mather, Mem Hall, Lamont,
all the most popular stops will be optimized in that interface.
So, more to come.
Why do something like that?
Well frankly sometimes, as you'll see perhaps
in this course or beyond, you do things just because you can.
And by courses end will you be able to do that and much more?
So today is ultimately
about laying the foundation for the semester.
And this semester is about Computer Science,
it's about Programming but that sort
of begs the question perhaps on day 1,
what is the latter, what is Programming?
Can someone in laymen's terms give us an answer
to this question?
What is Programming?
You don't want to be down here
because I can see you much more easily, yes?
>>Writing a set of instructions to a computer
>>Okay so writing a set of instructions to a computer.
So a computer is just this fairly dumb device.
It's a mechanical device
that literally can only do what it is told
so programming is ultimately
about telling a computer what to do.
Now how do you tell a computer what to do?
Well in this course and in programming in computer science,
we don't mean you just point and click and choose this
from the menu and at that high user level
but rather how do you actually go in underneath the hood
and make all of that possible?
Well you do this with programming languages.
C is the one we'll focus on in this course and PHP
and JavaScript and C Sharp and a whole bunch of others.
History is littered with dozens,
hundreds of programming languages,
some of which are very much still in vogue,
some of which have partly died off,
some of which really never saw the light of day.
So today we're going to focus on programming ultimately but not
by way of C, not by way of Java,
not by way of these textual languages
that while powerful ultimately and the languages
that we'll use throughout the semester,
they're not quite the easiest thing
to get excited about at term start.
In fact what I'm going to do here in just a moment
on my machine, is pull up a little program that comes
with Mac OS, you can do something similar in Windows
and you'll see how to do this on your own computer before long
but this is a terminal and what it ultimately gives me access
to is the files, the programs, underneath the hood on a Mac
which is essentially a unit spaced computer.
So what I'm going to do just to underwhelm you here on day 1
of 2 so far, I'm going to go ahead and write hello dot C.
So this is a text editor.
This is like a geeks version of Notepad or Microsoft Word
but much simpler than the latter certainly.
And what I'm going to do is something like this
so include standard IO.H, whatever that means.
I'm gonna say [inaudible].
I'm gonna say main.
I'm gonna say [inaudible] a couple
of brackets and a parenthesis.
And then in here I'm gonna say print F. Let's say hello,
world and then whatever this backslash N thing denotes
and then a semicolon.
So it's really hard to get excited
if this is what you've got in store
for you throughout the semester and plus what is [inaudible],
what is [inaudible], what is char, why all this syntax?
Well it's a very fair question.
It's really not intellectually all that interesting.
We do ultimately need conventions.
We need a language.
We need some basic definitions of how you express yourself
to a computer that's really not interesting here
in week zero thus far.
And so we'll quickly move away from this but just to again,
as promised, underwhelm you, now I have a file called hello .C,
just like .text or .dock.
The .C is just a convention.
I'm gonna run a program called GCC which is a compiler.
If you're familiar with Visual Studio, something like that,
it's the same type of program
but in a Unix or Lynx environment.
And I'm gonna say
to the computer compile the stuff I just wrote.
In other words, turn it into zeros and ones.
Now the fact that no messages got printed is actually a
good thing.
So weekends when you're actually playing with C for yourself
for the first time, if you get a message
at that very first point, damn, means you've messed something
up already but that's okay because much of the process
of learning and in doing interesting things, frankly,
will be tripping over some basics over time.
Fortunately I have not put my own foot in my mouth here yet.
But there's now this file.
It's cryptically named a.out,
just a stupid convention someone came up with years ago.
We can change this ultimately and we will but inside
of this file are a bunch of zeros and ones
that ultimately this computer, this Mac,
this Intel Mac can understand.
So I'm gonna go ahead and hit enter and wala,
you're very first computer science program.
Thank you.
[laughter and applause] Alright so not terribly exciting
and the thing is we don't need to focus so much
on just these basic things like parenthesis and semicolons.
All of these are completely uninteresting
and we'll get there but you'll find that it's really not
such a big deal and it begins to come more naturally.
But today we focus, for just this week zero,
on programming itself and what it means
to make computers do what you want but doing it
in a much more intuitive way.
And we do this, as promised on Wednesday,
by way of this graphical language developed
by MIT's media lab a couple of years ago.
It happens to be called Scratch
and what we'll see here is another demo
of what you saw briefly on Wednesday
and this one I will give you not one that I myself have made
but a former teaching fellow of the course.
I give you by way of this demo, raining men.
[laughter]
[music and singing]
So hopefully much more interesting than hello world
at the command prompt.
Now grant it that was made by a former teaching fellow,
in fact one of the stars of the football team.
So realize we do get all types here in CS50,
not those that I might have recalled from my own high school
but this one is clearly more presentation oriented
than it is interactive and a lot of what using computers is
about is taking input and then producing output,
actually interacting with the human.
So why don't we do just that.
And in fact, lest we send the message
that only teaching fellows can make things
that are this much more interesting than hello world,
let me show you something that was made
by a former CS50 student but for this we need one volunteer
to come on up here and stand before my keyboard.
Yep, in the back on the edge there, come on down.
And you do have to be comfortable, again,
being on camera since what you don't see happen is the signing
of legal forms off to the stage once we're done
with demonstrations like this.
So what I'm going to do is bring up a demonstration.
This one was by Andrew Berry.
This next one is by Ann Shea [phonetic]
and this one, no, come on.
No turning back now.
[laughter] May look familiar
if you've played a little something like this before.
[laughter] So, some of you might have played DDR or these kinds
of coordination games, maybe not,
if not it's better for the demo.
So in a moment, and what is your name?
>>Yonason [phonetic]
>>Yonason, David, nice to meet you.
So we're running on hit space to begin
and what's gonna happen is music is gonna start playing
and as the instructions here say, you're gonna have
to hit the arrows in lock step with stuff that's flowing
up the screen so that it overlaps the shapes
and then we'll keep track of your score and just how awesome,
cool, good or boo you are.
Ready to go?
Arrow keys down here, here we are.
[music and noise]
[background music] Alright, how a big round of applause?
That was pretty damn good.
So how does one go about writing this stuff?
Well the interesting question today, legal form, there we go.
[laughter] So the interesting thing today is going to be
to try and dissect, intellectually,
what is going on, right.
These things might be fun and they might look silly
but there's really some interesting stuff going
on underneath the hood.
There's some randomness going on there.
The game is not always the same in this particular case.
In the previous case
with Raining Men there was clearly synchronization,
there was lots of stuff going on at the same time
and if you think back just a moment ago at what I did
at the command prompt, the textual interface,
it's hard to imagine how you could possibly coordinate all
of those several things going
on when I've completely underwhelmed you thus far
by just printing a simple instruction to the screen.
And I'll give you a quick teaser too, in a little bit,
of one that I wrote that we'll use for the sake of discussion
if only cause I understand what's underneath the hood
of that one best and that one, we'll see, is called Oscar Time.
But first let's consider outside the context
of this thing called Scratch,
what really programming is all about.
Well let's pick something completely silly.
So waking up in the morning and putting on one's socks, right,
you've probably, hopefully, never really thought hard
about this particular problem.
You just do it.
But suppose you were trying to instruct a computer to do it
or maybe more concretely a robot to do it, you kind of have
to think a little harder.
Now perhaps I've raised the bar to volunteers a bit higher
but I need one volunteer for this thing as well.
Yes, come on down and you too will be on camera and shoeless.
[laughter] So I went shopping beforehand,
turns out you can buy some amazing propaganda at the Coupe.
This is ultimately going to be about this algorithm here,
oh come prepared, what is your name?
>>Josh
>>Josh, alright, come on over, come on over to your chair here.
We'll need you to take off your socks.
I kind of splurged here.
We went with a few different styles here today
so they're all clean though.
I thought for a moment maybe I'd just go home for this
but I thought that might not go over so well.
So we have some H branded socks here,
which we'll put, say in the sock pile.
I also got some more sporty socks,
if this is what your preference so we have these sort
of low rise type socks stained crimson.
And then these were actually my favorite so [laughter]
because you know there's some tourists out there walking
around in these somewhere so we also have a pair of these.
So an algorithm ultimately, as we'll see in just a moment,
is a procedure, it's a program but it's not necessarily written
in a specific language and in fact what we see here
on the board is what we'll call sudo code.
It's not quite code.
It's not programming code.
It's certainly not zeros and ones but it's kind of a cross
between something that's very succinct and logical
and it's also nonetheless very human readable.
And so throughout the semester before we sit down
and start typing things away at a command prompt like I did
or even dragging and dropping, it's often worthwhile thinking
about how could you express this program in just English
or English like syntax because then you could work
through the logic, then you could think
through hopefully some of the bugs, some of the corner cases
and then proceed to implement that algorithm.
So we're gonna go ahead and use Josh here for the sake
of execution and we'll see
if we can't tease apart a few programmatic fundamentals
that will then recur in much more traditional contexts
than this one.
So Josh if you would, perhaps, think to yourself the following,
let socks on feet, all one word.
I seemed to have used this underscore notation just
to tie it all together as one string,
let's socks on feet equal zero.
So this is easy.
This is a variable, right, just like X and Y and Z
in high school were variables that stored value,
so is socks on feet a little more interestingly named a
variable that is in fact consistent with reality.
So we're initializing our program here.
So two, line two induces what we'll call,
and this too might be self explanatory, a loop.
So something that repeats zero or more times.
Now in this case I've used the key word while
which does imply doing something while something is the case.
We'll see terms like for and do while
and similar synonyms essentially but for this we chose this
so while socks on feet exclamation point equals two.
Now if you don't know what that means,
take a stab at what it might mean.
So not equal to, so bang equal sign
so computer scientist geeks say bang not exclamation point
so bang equals means not equal to two.
So we're going to do the following while Josh presumably
has a zero or one or you know maybe even three socks
on his feet cause we're going
to do this while it's not equal to two.
So, here's where your awkward role play comes in.
Open the sock drawer, excellent.
Look for sock.
Okay if you find sock, so this too is bold faced for a reason,
this is a condition, a branch,
something that happens optionally if something is true
or false so if you find a sock, have you?
Okay then and notice the indentation here too
so this is something we'll use consistently.
This is a matter of style.
The computer often does not care if you have spaces,
tab characters, indentation, no indentation and those
of you have written HTML or web pages before probably know
that it might look pretty to make everything look nice
and hierarchical and pretty printed as they say
but the computer certainly doesn't care.
But here we're humans, we do care so it's indented to imply
that line 6 on down are now encapsulated inside
of what line 5 is telling us to do.
So if line 5 is true, if find sock, which you did,
then we proceed to the indentation.
Put on sock, excellent.
Socks on feet plus plus, this is shorthand notation
and we'll see this in C, JavaScript, PHP,
plus plus as you might infer, just means incremented by one.
So socks on feet now equals one.
Look for matching sock.
And this we might call a statement.
Anytime it's just sort of matter of fact here,
we'll call that a statement.
Okay you've looked.
If you find a matching sock then indentation,
put on matching sock.
So far so good.
Socks on feet plus plus.
Close sock drawer and now notice I'm not going to proceed
to do anything with line 13 cause line 13 is exclusive
with respect to 9 so if I did 9 I don't do 13.
Similarly if I did 5, I don't do 16
and now what do I go back up to?
Well following the logic of the indentation I now go back
to line 2 and I check this condition,
while socks on feet not equal to 2, what do I do now?
[inaudible audience comment] Yeah, I seem to move on.
So what's next?
Well line 18 which isn't even present so we implicitly drop
out of the program and we're done.
So that was actually very good.
And now if you wouldn't mind, let's restart this.
Take off this socks if you would and that will get awkward for me
so I'm gonna take, say this, this and this.
Did we screw up?
Is this program actually correct?
Well let's see.
Let's socks on feet equal zero.
Good, while socks on feet not equal to 2.
Good, that is true.
Indentation, open sock drawer, look for sock.
If you find sock then, [laughter] nice, put on sock.
You will always remember this moment in college.
[laughter] Excellent, socks on feet plus plus.
Look for matching sock.
Problem, if you find matching sock then line 9 doesn't seem
to apply so we go to line 13 followed
by 14 else remove first sock from foot.
Socks on feet, minus minus
so now we're back to zero presumably.
What line do I go back to next?
[inaudible audience comment] So not 5, I heard but rather,
so I'm back up at 2 while socks on feet not equal to 2, okay,
true so we proceed to open sock drawer, look for sock,
if you find sock then put on sock.
So at this point you're probably getting a little bored, right?
Because what's gonna happen here?
So this is one of these things called an infinite loop
or if you've never heard of that expression, that's what it is
and frankly you've probably experienced this before,
whether you have a Mac, a PC or whatever, you've probably
at least once in life gotten really ticked off
at your machine because it's kind of just hanging and hanging
and hanging or the stupid hourglass is just spinning
and spinning and spinning and it never stops for some reason.
Well what is that?
Well technically it could be any number of things
but it's probably a mistake that some programmer made,
didn't anticipate the fact
that maybe I haven't done the laundry all that often
and therefore I might not have sufficiently many socks.
Maybe I just don't have certain socks anymore cause they got
torn and I threw them out.
These are what we'll call corner cases and clearly bad,
awkward things arise if you don't handle these cases.
So in those cases with your own machines you probably rebooted
or you force quit, you pulled up the task manager,
all these stupid real world issues that boil
down to stopping a program that's caught
in an infinite loop.
Are there other bugs here that we might trip over?
[inaudible audience comment] Sorry?
[inaudible audience comment] Yeah so I don't,
so there's also some real world problems here perhaps,
especially if this is again not Josh the human
but Josh the robot where it keeps saying open, what is,
it said open sock drawer, open sock drawer.
Like, what does that even mean?
I mean either the robot is gonna start ripping the thing
out of the wall or it's just gonna get confused
because it's ambiguous.
And ambiguity is often a problem too because what is going
to happen in cases that are not so explicitly specified by you.
So why don't we put Josh out of his awkwardness here.
You get about 6 souvenirs today.
They are clean.
I can give you a little shopping bag,
if not the receipts if you want.
But here we have some nice little Harvard souvenirs
and perhaps a quick round of applause for Josh.
Thank you very much.
[applause] Alright so this is what we did
at the command line quickly
and this is what we'll do a week plus from now.
This week we focus on things more intuitive
but this is a basic C program, a canonical program
and in fact it will get much more interesting,
much more powerful than this
but what really happened underneath the hood
when I wrote this little program?
Well if that's my program at top left and I ran
that program called GCC, the compiler,
well what that compiler did, it was written
by some smart people a while ago, it converts what kind
of looks like English but is definitely not English I'm used
to writing in, it converts it to zeros and ones
that my particular computer understands
and this process is called compiling.
And this is partly the reason why
if you've ever bought a program, maybe Microsoft Office,
you either buy the shrink wrapped box that says
for Windows or you buy the shrink wrapped box that says
for Mac OS because even companies
like Microsoft very often have
to essentially write two versions of programs
because a compiler, as I hinted at with my own Mac,
is kind of machine specific
or at least it's operating system specific.
So unfortunately we don't really live in a world that's
as perfect enough whereby Microsoft writes their program
once and they run GCC on a Mac, they run GCC on a PC.
They copy the resulting A dot out file or MS Word.
Exe file to a CD and ship it.
There's a lot more to it than that.
So one of the things we'll talk about closer
to terms end is the notion of cross platform software.
Java, one of the appeals of Java, if I'm familiar,
is that in theory you're supposed to be able
to write a Java program once and then run it
on any computer you want, a Mac, a PC, a lynx box,
even a cell phone these days.
And you'll use a program like this, the virtual terminal room
to which I referred on Wednesday for virtual office hours,
if you decide to log on,
that's actually you'll see a program written in Java
because that particular company, called Illuminate,
decided it's really expensive and it's really uninteresting
for us to have to write our software multiple times just
so that we can have Mac customers, PC customers
and so forth so they went with Java.
But there's a downside even to that.
So, what then are we going to do this week?
It is Scratch.
And today what we'll do is just lay the foundation
for just these basic programmatic constructs
but by lectures and today we'll be already begin our transition
to C. Your first problem set, which will be posted,
as will be typically the case, by 7:00 pm tonight
on the courses website.
If you're like me you're welcome to dive in at 7:01.
If you have more going
on you can wait a few days cause you have ample time for this one
but they'll be posted on the courses website at CS50.net
but let me encourage you to take note of this too.
So especially for this first problem set you'll find
that it's rather fun and in fact you'll probably get a kick
out of sharing or making your roommate take a look
at what you've actually been working on.
But so that you don't feel completely thrown
into the lion's den, so to speak,
especially if you're feeling a bit among those less
comfortable, we will start office hours this coming week.
So no sections just yet.
We will start the process
of sectioning next Wednesday onward,
some more on that next week but office hours,
this list will grow as our 50 plus person team begins
to figure out their own schedules, we're gonna hang out.
Next week on Monday, Tuesday, Wednesday, Thursday evenings,
7:00 pm onward in Cavet [phonetic] Houses dining hall
and in Quincy Houses dining hall where you can bring your laptop,
you can hang out for brain break and a number
of the teaching fellows and CA's will be there to help you,
to chat with you and just generally tackle this as a team.
So what are you going to be tackling?
Well this year it's Scratch.
I went to a website called scratch.mit.edu.
It works for Mac's, it works for PC's.
I downloaded this program and wala, this is what I get.
But what is this interface?
Well on the left hand side here we have these puzzle pieces
and they literally look like puzzle pieces
that in a moment I'll be able to drag into this scripts area here
and it's by dragging and dropping these pieces
that I will be programming, so to speak.
Well in the top right we have what we're going
to call the stage.
This is where stuff happens, as we've already seen.
This little pat by default, this is Scratch,
sort of the mascot they went with
but we can change his appearance at will.
This is what we'll call a Sprite
or really it's what we'll call more technically a thread,
a miniature program that you can instruct
to do things independently of anything else.
And at bottom right here we'll see all
of the many different Sprites that I might possibly create
so let me go and do this.
So control, when green flag clicked.
So when I a moment ago typed that cryptic sequence
of instructions into main,
main kind of has some implied meaning,
this is the main guts of my program?
Well this is sort of the equivalent so those
of you coming to the course from like a Java background,
it's like writing your main method.
So this block, just to be clear, has this icon on it that says
when green flag clicked and that's why,
if you've been paying close attention,
every time I start these demos I click that green flag.
So what does that mean?
That means when we click that green flag this cat,
this Sprite, because he's currently selected,
is going to do whatever the blocks tell him to do
that are attached to this particular starting point.
So what do we want him to do?
Well let's do something simple.
I'm gonna click the little pallet here for looks.
So MIT has nicely color coded things just
so that all related blocks are similarly colored.
I'm gonna flip through this and okay,
this is interesting, say hello.
So say hello for 2 seconds and just
because it's a little small that's what
that puzzle piece looks like and notice too,
and this is the magic of it,
you bring it close enough, it snaps together.
Right, it's a lot easier
than remembering alright I put the parenthesis
and then the semicolon and then hit enter.
Well you just drag and drop
and these things will fit together logically
if that makes since.
Programmatically I'm gonna click the green flag, wow,
alright so I just wrote what was this a moment ago.
Not so bad but not that interesting, in fairness.
So let's do something a little more interesting.
So let me say something like this, what is your name?
So now I need a way of actually getting input
so how can I do this?
Well under sensing here there's this block,
now ask what's your name and weight?
Well let me try and do this, what's your name, oh wait,
this is already a book, right?
So I've kind of gotten ahead of myself so let's do this, hello,
we'll say that for 2 seconds, then ask this and then wait.
Well let's see, hello, what's your name?
Alright, David.
[laughter] Kind of stupid program, right?
I've asked for my name but I'm not doing anything yet.
So let's do something a little cute perhaps so looks,
how about, hmm, so there's the saved block,
there's this think block.
Let me plug this in.
Now I don't want it to say, well okay we'll let it say hmm
but then I'm going to be a little cute
and say something like, let's say doesn't ring a bell
and actually hmm is a little generic
so let's do something even more interesting still.
Under sensing, notice that there was this answer here
and if I had actually gotten some help
about this block I wouldn't have gotten a little cheat sheet.
What does this do?
So this programming environment, it's actually very easy
to bootstrap yourself cause all
of the help is right there for you.
I'm actually gonna drag this and then notice
that the hmm doesn't have to be me manually type.
I can actually plug in this variable so now I go ahead
and say this, hello, what's your name?
Alright, David, hmm, and that's a little cartoon bubble there,
doesn't ring a bell.
Alright so baby steps to be sure
and still not all that glamorous.
Probably you're not tapping your roommate
on the shoulder just yet.
So what more can we do with this?
Well let's see if we can't take things progressively higher
and higher.
And what you'll see is even though a lot
of these initial demonstrations are terribly simply,
that's what programming ultimately is about.
It's taking some very basic building blocks and building
on top, on top, on top of them, kind of combining them
into things we'll start calling functions and libraries
so that you don't have
to reinvent these uninteresting wheels all the time
until finally do you get much more interesting software.
Case in point, things like Shuttleboy that we don't have
to worry so much about these minutia before long.
So there are these things called statements
in programming and in Scratch.
Anything that is a block that's sort of imperative, say this,
wait for this amount of time, play sound,
we'll call these statements and these are just a few examples.
So here are two programs.
The left one we can figure out intuitively
so one green flag clicked,
that example is gonna say oh hi world, for one second,
then it's gonna pause.
It's gonna say it again, then it's gonna pause.
And then it's gonna say it again,
right, rather underwhelming.
Let's go ahead and pull up the third version of this though
and all of these examples you can download
from the courses website, let me go ahead and pull up hi3.sb,
double clicking suffices, that's the program.
Notice there's really not much complexity here.
I still have just one cat
but let me go ahead and click play now.
[noise] Okay still maybe not roommate worthy but let's see
where else we can go with this.
Alright so there's Boolean expressions
in program languages.
A Boolean expression, named after a Mr. Bool,
is an expression that in the end is either true or it's false.
It's either 1 or it's zero.
It's yes. It's no.
These two dichotomies.
It doesn't matter what label you slap on them
but it's this notion of true and false.
So what kinds of questions might return yes no answers,
true false answers?
Well is the cat touching the mouse pointer?
In other words is my mouse hovering over him
because if he is that could be an interesting mode
of interaction.
Is the mouse button down?
That too is kind of an interesting question to ask
because if so maybe I'll do something interesting.
Is something less than something?
Well sometimes you're supposed to do numeric manipulations,
even in a context like this.
And then there's this, this Boolean and operator.
If any of you are particularly savvy with Google
or search engines, sometimes you might realize that you can type,
say, David and Harvard into Google search box or something
like that, to get results that actually relate not to David
or Harvard but both of them, in fact.
And so we can merge Boolean expressions into singular units.
Why is that interesting?
Well if we can ask these yes no questions we can do things
or not conditionally.
Just like Josh put on the sock only if he found a match,
can we similarly distinguish
between different states of our world here?
So there's this if block in Scratch,
at the far left here so if something.
And notice there's this funky triangular shape here too
so you'll find that there are similarities,
not just with color in this program but also
in the shape of the block.
So just as a visual queue, what can you put
into that place holder?
But it's not just interesting to say,
if something's true do this, sometimes you want it
to be exclusive, do this, else do that
and so there's this if else construct.
Now sometimes you might want to do this,
that or the other thing.
Well these things can nest so even though it doesn't look
like you can cram another block inside that middle one, else,
I mean it's only this big.
How in the world do we fit anything in there?
Well what you'll find is
that Scratch automatically adapts itself.
It just grows the size of the block if you want
to squeeze more blocks into it.
So now we have the ability to do things conditionally.
Well why is that helpful?
Well let's take a look at iteration 4 of this.
So I'm gonna go open hi4.sb.
It's not a very interesting program yet so let me hit play.
[noise] I'm gonna hit it again.
[noise] This is sort of vacuous truth, right?
This is not [background noise] a very interesting use
of a condition because one's value is presumably never
changing so let's hope I did something a little smarter
with version 5 here.
This is version 5 of this same idea.
Huh, so now the fact that we can plug in values from elsewhere,
this gets a little more interesting.
So this block here let me zoom in,
is what we'll call a sudo random number generator.
Now to the target audience of Scratch, which is usually people
about yay high so 10 year olds, 12 year olds,
MIT teachers don't whip out terms like,
now use a sudo random number generator to make your cat move
but that's what we're gonna call it, right?
We're [laughter] in this case going to pick a random number.
How that gets picked is actually an interesting question
unto itself.
And those of you who like mathematics, like theory,
I mean there's a whole world of discovery and a whole world
of research that asks the question,
how do you make machines, which are by nature deterministic?
They only do what you tell them.
How do you make a machine like that pick a random number?
It would seem to be the case that you have
to tell the machine what number to pick but that would sort
of defeat the point of having randomness at all.
But in fact there are ways of doing this or there are ways
of simulating this and as an aside you can kind of cheat.
For instance, if your computer has a microphone,
well odds are you're not going to be
in a perfectly quiet room all the time
so maybe you get some randomness, some random noise,
by way of turning on your microphone
so there's some juicy areas of discovery there.
But for us, we're just gonna use it mechanically.
Pick a random number from 1 to 10, this has the effect,
if all goes according to plan, of the following, is that a bug?
I click, nothing happens.
It's kind of to be expected I think, right?
[noise] Okay that time it worked.
It didn't.
[noise] Did.
Didn't, [noise] did, now as you may recall,
if we do this an infinite number of times,
50 percent of them will in fact meow, 50 percent of them won't.
It's actually a good statistic
so last year I kept hitting the button
and the damn thing kept meowing, meowing, meowing
but that actually speaks to the fact that it is in fact random,
at least in some part.
So who cares?
What can we do with this?
Well let's see what else we can build on.
So there's a way of doing things multiple times.
Now in one of my first programs I had the cat meow 3 times.
It said play sound, meow, wait a second, play it, wait a second,
play it, wait a second.
Well that's kind of a nice capability if you can tell it
to do something multiple times.
Let me go ahead and open up this thing here.
And again this is what we just glanced at at the slide.
It's rather underwhelming and in fact,
it's perhaps more interesting if we
at least make it slightly annoying
by doing it this way with the sound.
[noise] And in fact I'll indulge
in what you inevitably will find a fun way of procrastinating,
it turns out we don't have to use just the meow.
We can click things like import.
You'll find that this environment comes
with all these various effects
so maybe it would be more fun to do something like.
[noise and laughter] Okay and there's the cat.
There's other things too.
I think if we go into vocals we've got this one.
[noise] So that's not that bad in terms of annoying.
[laughter] So let me do this again, and please stop me,
we can't go on this tangent too long.
So there's three tabs up there and again,
this is not interesting stuff intellectually
so we'll wave our hands at it largely today but I'm going
to go back to my scripts now and now notice, to change the sound,
alright so I'll do this, this, this.
But the point I wanted to make is what do I do if I want
to do not 3 times but 6 times?
Well you can duplicate things.
So actually let me go ahead and duplicate it from here
and I'm just right clicking or control clicking.
Let me get this stuff out of the way there.
And yep, so now I have it going 1, 2, 3, 4, 5 times
and actually let me detach this, 6, 7 times.
I'm gonna drop this over there
and let's just see how annoying this gets.
[noise]
Okay so the point is now not to annoy but to demonstrate
that this is not the smartest approach to telling the computer
to do something again and again.
If you find yourself, whether in C, Scratch, Java Script, PHP,
just kind of copying and pasting to get your program
to do something multiple times, multiple times,
which may be compelling, there's got to be another way
and in fact there are.
There are these constructs, as we saw with Josh's example,
of looping in Scratch and in C and in Java Script
and in a lot of modern languages.
These things are called wild loops or for a loop.
In Scratch they're called forever loops
or repeat some number of times.
So already can you probably imagine how you might
integrate these.
Well what about something like this?
If we wanted to create that very simple effect
of doing something ad nauseum, if we pull up version 6 of this,
notice that the blocks I've chosen are one green flag click.
I then grab that forever block.
And to be clear, the forever block is not normally that tall
so notice the mere act of dragging something
like this inside the forever loop makes it bigger.
And then if I want to squeeze this in it may trune [phonetic]
but again everything is similarly shaped.
Now I'm gonna have the same effect,
slightly less annoying [noise]
and now [background noise] I've arguably implemented this
with a much better design.
There was no reason for the copy paste and in fact if you sort
of think about where that bad habit leads you,
if you're copying and pasting constantly,
imagine how much work it would be for me to go back
into the previous version of that program
and actually change something
like the sound that's being played.
I mean my God I have to like change it in 6 or 7 drop downs,
here I just change it in 1 place.
So we already have this ability to do things
that are a little better designed.
Well what about this thing at top right?
So top right is saying when green flag is clicked,
do the following forever.
Well what?
If touching mouse pointer, play sound meow
and wait two seconds and then repeat.
Alright so let's open this.
This is version 7.
I'm gonna go ahead and open this.
If you wondered how I was doing it before,
at the top right there's presentation mode
where you can zoom in and make it even bigger.
So now I hit play.
Nothing seems to be happening but that's consistent
with the previous slide because I didn't use the save block,
I didn't use the think block but notice my mouse,
if I move it closer and closer to the cat [noise],
it's kind of like petting the cat, if you will.
Clever? I thought so.
Alright so kind of interesting.
Let's take things slightly higher.
So that's version 7.
Let me go to version 8 here.
Version 8 is not that much bigger
but now notice, what's gonna happen?
You can kind of see this one coming.
[inaudible audience comment] Alright
so this was me thinking this was really clever.
Alright so let me go ahead and full screen it,
click the green flag and now notice [background noise] he's
just going about his business meowing, okay that's all good.
If I now try to touch him, [noise] bad things happen
and then he'll go back ultimately to meowing.
Okay so now we have this ability
to do things conditionally inside of loops.
So already we seem to have a fairly rich amount
of expressiveness.
We can kind of think about doing something, like I did last night
when putting that together.
I wanted the cat to just meow, meow, meow and then I wanted
to do something optionally so there you have loop.
You have condition sandwiched in between.
Think about what statements you want.
And wala, we have that effect.
Well what more can we do?
Well we have variables.
Now variables are useful, certainly in the context of Josh
where he had to maintain state.
He had to remember how many socks he had on.
Otherwise he would just keep putting on socks,
putting on socks, putting on socks.
The use of that variable allowed him to short circuit that loop.
And terminate as soon as he reached some happy place.
Well what about these two programs here?
Count 1.sb is at left so when green flag is clicked set a
counter, that's apparently a variable that I chose.
Better named than X. Better named than Y,
which though you might have used in algebra, really isn't
that useful when you're trying
to remember what your own program does
so again a good matter of style is pick variable names
that say something interesting.
So say counter to zero, forever, say counter for 2 seconds.
Then wait a second, change counter by 1.
So this is Scratch's approach to plus plus.
Alright so in count 1.sb we have the following,
count 1 [silence] no sound but I thought it was very clever too
that the sheep was doing the counting.
[laughter] Ha, ha, ha, see, geek humor.
Alright so things are more fun with sound so we'll kill
that thing but it's going to go on up at least
until we hit some upper bounds.
Can you anticipate maybe if you're coming at this
with a background in some programming,
what could still happen that's bad here?
[inaudible audience comment] Sorry,
might only be using 8 bits.
So actually roll back mentally to Wednesday when we talked
about representing numbers
and by default I kept using 8 bits to represent numbers.
Well if you have 8 bits, each one can be a zero or a 1,
that means you have two possible values times 2 here, 2, 2, 2, 2,
2, so that's 2 to the eighth,
right so that meant we had 256 possible values you can
represent with 8 bits.
The number zero then 1 then 2 then 3 then 4 then 5,
all the way up to 255.
And again, why 255, not 256?
Computer scientists tend to start counting at zero
so we spend one of our encodings that way.
So a problem that might arise here is MIT probably made a
judgment call somewhere underneath the hood
when they implemented Scratch itself where they decided,
you know what, no 12 year old is gonna count up to 4 billion.
We don't need 32 bit integers.
We don't need integers that are represented
by 32 individual bits, maybe just 8 or maybe 16 but they had
to make a judgment call.
And just as, think back to the Y2K problem,
what was that problem?
Well the world had decided, in the 1970's give or take,
you know this codes never going to be used 30 years from now,
let's just use 2 digits to represent the year
and then we all, you may recall, bad things happened
or bad things were predicted to happen.
Certainly expensive things happen
when we roll back from that mistake.
So same thing here, let's not spend the time figuring
out just how high this sheep can count but that too is going
to be an issue we trip over when you start coding in C and in PHP
and in Java Script where you have much more control
over the representation of numbers and of data.
What about count 2.sb here?
So when green flag clicked, set number to a random number,
say the number and then if that number is less
than 6, play sheep.
So in English, in laymen's terms, this thing is going
to something 50 percent of the time
but at least now we're going to have some introspection,
we're gonna see why it's actually playing a sound or not
because it's going to tell me what the number is,
whereas before I simply had to guess.
So this is count 2, still involving our little sheep here.
If I hit play 6, nothing happens because 6 is not less than 6 .
Play 10, still nothing, 7, still nothing,
see this is where we could be tossing a coin
and it might be coming up heads that many times,
hopefully it will go [noise] the other direction 50 percent
of the time.
So if do that an infinite number
of times hopefully the math would in fact work out.
Alright so those are variables.
Well variables are useful but only when you want
to store one piece of information.
But you could just use then multiple variables
to store multiple pieces of information.
But if you think now even in terms of Scratch
that each variable right now is its own block.
Imagine how you might implement, maybe a game like some kind
of RPG game where you're a character,
ala Super Mario Brothers and you have to go
around collecting things.
Any game like Zelda.
Any game where you go, or even Halo.
Anytime you pick up something and have to add it
to your inventory, it would probably be a fairly
unmanageable program if you had to have a separate variable,
a separately named variable for every possible possession
that someone in a game might need to carry around.
It would be a lot easier if you could sort of have just a bucket
or a satchel, whatever the analog is in the actual game
that can contain zero or more items.
And that data structure, as we'll start calling it
in programming, is what's called an array,
sometimes called a vector, also we'll see fancier versions
of this ultimately called lists and trees but more
on that in the time to come.
So an array in Scratch gives us this capability.
We give the array a name.
It's called a list in the context of Scratch.
And then we can use this block at top.
Add something to inventory.
Delete 1 from inventory.
Insert thing, a string, at location 1 of inventory.
In other words, and we won't spend too much time
on using these now because you might not need them
but if you find ultimately that you kind of need to keep
to keep an arbitrary amount of information around,
well the data structure you use to make that happen
in general is this thing called an array.
Well let's see this for real.
So this is actually a program
that is a role playing game of sorts.
It's called Fruit Craft,
a play on War Craft, if you're familiar.
Let me go ahead and full screen this one here, click play,
and we have a little animation it seems.
Alright you can move me with the arrow keys.
So just as our little DDR game let me move,
notice and actually the goal is to look for fruits and so
like an idiot I kind of did this the first time.
Apparently this is a picture [laughter] of fruit.
So there's clearly some bugs if you will
or at least some disregard for our 3-dimensional world here.
[laughter] So I'm gonna go around
or over or through the table.
I'm gonna follow this arrow.
I'm just using my arrow keys right now.
Alright so okay, there we go.
So now notice the top left.
Scratch is showing me what is inside my list, aka array,
now I have cherries and orange, this looks interesting.
Let me go here.
Oh okay so we have, remember some sensing.
So remember just as I'm able
to sense is the Sprite touching my mouse pointer,
I can also sense is a Sprite touching another Sprite?
Is this person touching the sign, if so do something.
And even though we might not know all the blocks that we need
to make that happen, it's these basic fundamentals,
right an if condition that says if touching then say and then
when not touching don't say.
So hide that text.
Alright so let me go and pick up these things, pineapple,
bananas, another apple,
hopefully this is the right place, okay, interesting.
Great job.
You brought, cherries, orange, bananas, pineapple, apple,
here's your fruit platter.
[laughter] Okay so simple but again demonstrates
where you might, in a fairly compelling context,
actually need some kind of variable amount of storage.
Well let's make this more sophisticated still.
When this is actually a topic that you can't traditionally get
to in the first week of a course let alone an entire semester
sometimes, this notion of threading because a lot
of languages, whether it's C or Java, really don't make it easy
of what we're about to do and frankly what we've been doing,
which is having multiple things happen at once.
So the program I wrote earlier that was very underwhelming,
hello world, just printing it to the screen?
That's kind of the only thing it did.
I could add another print F line, another print F line,
another print F line but those are all gonna happen in series,
not in parallel at the same time.
I'm gonna get one output, one output,
one output so what's nice about Scratch is
that you can have the cat moving,
you can have a sheep doing something,
you can have this character interacting
with the guy behind the counter,
you have a multithreaded environment.
Well what is a thread?
A thread is like a miniature program that's running inside
the context of a larger program
and they can operate what essentially looks
like simultaneously.
Now this is a bit of a white lie because ultimately,
and we'll talk about this towards the end of the course,
many computers have just one CPU, central processing unit,
the brains of the computer.
When it says Intel inside it means there's an Intel
CPU inside.
Well if the computer only has one brain or one CPU,
the reality is that computer can only do one thing at a time.
But the neat thing is when you have a gigahertz,
so in other words a hertz,
something happening once per second and you can think
of there being a clock inside of your own CPU
so if you have a one gigahertz CPU there's a little clock
or a little crystal that's oscillating a billion times,
giga meaning billion, a billion times per second.
Well my God, if you can do something that fast,
clicking your fingers a billion times per second,
surely we can dupe humans into thinking
that two things are happening simultaneously.
So if you've ever run AOL instant messenger in a window
and also Microsoft Word and a browser window and any number
of other programs, you are multitasking.
But realize that you don't have to quit one
of those programs to load the other.
You're running them all simultaneously.
But what Windows is doing,
what Mac OS is doing is just giving everyone
of those programs only a split second of computation
in the CPU, a split second of attention
but because it does it here, here, here, here,
here then here, here, here, here, here where each
of these things has a different program running
in your task bar, you the human, being the slowest piece
of this puzzle, have the illusion of oh,
I am running multiple programs at once.
Those of you with iPhones might be somewhat annoyed these days
that Apple doesn't support background applications,
at least written by other people besides Apple.
In other words you can't run one program and then use another
at the same time, if they're written by people
in the app store and that's
because Apple has essentially said users may not run multiple
threads simultaneously unless we, Apple,
implemented those threads ourselves,
unless we wrote the code.
Now this is partly because the battery life of this thing kind
of sucks right now and if you allow people to write programs
that run in the background,
probably the concern they have is
that you're gonna drain people's batteries and most people
out there don't understand what a thread is and they don't care
and they shouldn't have to care but that's Apple's way,
probably, at least in part, of dealing with that issue
of multithreading, being expensive in terms of battery.
So here is a thread.
So move 1.sb tells us to do the following,
when the green flag is clicked, go to xy.
So it turns out this whole time that stage can be modeled
and in fancy terms is like a Cartesian plane
where you have X coordinates and Y, right,
just like in mathematics.
So zero zero is pixel location zero zero,
the little dot somewhere on that stage points
in the direction 90 degrees so that's either here, here,
here or here, we'll see once we orient ourselves
and then forever do the following.
If touching the edge, play sound,
this could be interesting, until done.
Turn 180 degrees, move 5 steps and then do it again and again
because it's inside the forever loop so this is move 1.sb.
So let me go down to this, move 1.sb, I'll go ahead
and full screen it again, hit play.
[noise]
[background noise] I don't know if this is
like a pedagogical technique or just a bad habit but it seems
if you right program loud and annoying,
people remember them so be it.
So this thing, this is the Sprite.
Notice I've changed his costume, how?
Well I probably went up here, I clicked costumes,
I fussed around with paint or importing a JPEG or whatever,
some uninteresting details intellectually
but you're nonetheless welcome to play with and procrastinate
with for project zero but I've made a duck
that orients himself 90 degrees and he starts moving
so this notion of walking is really about moving some number
of steps, some number of pixels and then notice
because I'm doing this again and again and again forever,
I'm constantly checking, am I touching the edge,
am I touching the edge?
If so, yell, then turn yourself around and do it again.
So again, not a very interactive program
but all these simple puzzle pieces,
all these simple building blocks we've been looking
at are really what comprised Ann's DDR like game and Andrews,
Raining Men and some
of the other more sophisticated programs that you'll write.
So here's one that actually is multithreaded.
That was just one duck doing his own thing.
Now we'll see a bird and a cat, each of which is a Sprite
in Scratch speak and each of which has its own script.
So notice in this program move 2.sp, I'm actually going
to have 2 different scripts, each associated
with a different Sprite.
So I've highlighted cat at the moment, here is the cat script.
If I click bird, here is the bird script
and they are clearly different.
Well how are they different?
Well let's look at the cat first.
The cat is doing this, go to negative 160, negative 160.
Why? Well again I was kind of fussing around one day
and I decided let's have the cat start there specifically,
trial and error in this case.
Point in direction, well I wanted this demo
to be a little different each time,
otherwise it's really uninteresting
so pick a random degree between 91 and 179,
then forever do the following, if touching bird,
here's where it gets fun, play sound, that particular sound,
line 4 and then stop the script.
In other words, game over is the implication there, exit quit.
Otherwise, actually I misspoke, it's not an else,
it's not an otherwise but no matter what,
point yourself toward the bird and move one step.
So in other words, if you're touching the bird,
play the sound and quit, game over.
No matter what though, proceed
to point toward the bird and move one step.
And actually it is implicitly an if else
because of the termination so I retract my retraction.
So, let's take a look at the bird.
What is the bird doing?
The bird's not doing all that much.
The bird starts at a hard coded place and I only did this
because it would be really stupid
if the bird might sudo randomly start on top of the cat and then
if the game has no point.
So I go to this location, which I know is different
from the cat, point in 45 degree direction and then forever.
Now notice what I did.
It turns out with Boolean Expressions not only can you
check for true or false, you can flip the meaning
so there's a block called not which inverts meaning,
which is good because I want to do forever,
the following forever if I'm not touching the cat, move 3 steps
but if I'm on the edge, bounce.
So you'll also see in Scratch, for user friendliness,
sometimes they merge the notion of a condition and a statement
like this so if on edge bounce, just happens
to be consolidated into 2.
So let's play.
I'll go ahead and full screen it.
I'll go ahead and click the green flag.
And notice this isn't random.
The cat is again point toward bird.
The bird is moving around somewhat randomly or [noise]
at least, alright so kind of sad but kind of clever nonetheless,
at least programmatically.
So what more is there?
Well what about if we do something like this interesting?
So this example, as we'll see, not only uses multiple threads,
multiple scripts happening in lock step
but I'm also using a variable to maintain state and essentially
to communicate across the two Sprites.
And this is useful if you effectively want
to pass information between the two.
So let me go to version 10 of my hi program,
that's not in this folder, interesting.
Alright well rather than drag our feet, well let's go ahead
and do, well you know what, we're out of tape anyway.
Let's go ahead and take a 5 minute break, we'll change tapes
and then resume once I've found this file.
[background noise] Alright so version 10 has just gone missing
so fortunately we have the source code here.
On the left hand side we have a script,
a Sprite that just says forever do the following,
if muted equals zero, play sound sea lion,
think oh hi for 2 seconds, then wait, then repeat
and then notice what the right hand side is doing
and this is really the only take away here.
Giving ourselves a mean of communication between Sprites.
One the right hand side I have this variable called muted.
I set muted to zero.
That is false.
It is not muted, is the implication
and then forever do the following.
If the key space pressed, then check the following.
If it's muted, set muted to 1, else set muted to zero.
So the right hand side,
even though there's some new blocks there, what is that doing
in laymen's terms, the right hand side script?
It's just [inaudible audience comment],
yeah it's just toggling the state of this variable, right?
We have a forever loop
which just means forever check the following.
Is the space bar pressed?
If so flip the value of muted.
So the goal of this exercise here was again
to have a little fun playing sound
but when I hit the space bar it either mutes
or effectively unmutes, what, unmutes the other guy
because the other guy, because he's also happening
in lock step.
Whether he's a cat or a bird, it really doesn't matter
in this case what the Sprite is, who's using this script,
the left hand side guy is only going to play the sound sea lion
if that value of muted is zero.
So in other words, if I hit the space, I make him bark.
If I hit it again, it makes him stop
and this just goes on in perpetuity.
So it's not such a loss if we have that one here.
This one's a little more interesting anyway.
So I'll go ahead and beat myself up here, if you will.
This is actually borrowed from a version
of this program someone else wrote,
it wasn't called David at the time.
I took some liberties since I didn't think it would be the
friendliest thing to have our student punch him in the face
so I swapped in my own face here and now think about it
for just a moment, what's probably going
on underneath the hood here?
So I'm apparently moving back and forth, as any boxer might
and if I use my arrow keys here I can start punching
and missing.
[laughter] But there we have here and actually
if we turn this feature here on, notice too,
[music] notice my face is clearly changing color.
[music and laughter] So what's going on?
Well we don't have to even dissect all the pieces cause
for the most part we presented all the fundamentals.
There's more than just 1 script now, more than just 2.
There's actually 4 that belong to the left glove.
It looks like there are as many that belong to the right.
My own self has this slightly larger one but it all boils
down if you start to focus on the specifics,
and again all this code will be online, it just boils
down to making the computer do what you want it to do and each
of these things are Sprites therefore they can detect
themselves, if touching one Sprite, do this and so
with these very simple building blocks,
almost all of which we've rattled off already,
can you do things that are increasingly compelling
and much more interesting than just hello world.
Well this is one final functionality that's provided
by Scratch.
It is not often found in at least introductory courses
and this notion of events, we'll actually see this in the context
of web programming later in the semester with Java Script.
For C we won't really play around with this idea
but this example here simply has 2 Sprite's,
each of which has one script,
but these guys too can communicate,
not by using a single variable,
so when I discussed a moment ago the notion of muting,
that's what we'll call a global variable, global in the sense
that anyone can use or manipulate it.
We'll see that that's not often the best practice
but with events can you do the same thing.
You can broadcast an event and the idea is
that any other Sprite that wants to listen
for that event can somehow respond to it.
So in this case here, Marko.sb we have essentially 2 Sprite's
playing a game with each other.
If I go ahead and play this, we'll see on the left hand side,
Marko is waiting forever for me to do what?
Hit the space so I'm gonna hit the space.
Marko and now the female says polo so why did she say that?
Well let's take a look at her.
Oh, when I receive event, say polo for 2 seconds.
So we have cross thread communication,
inter-thread communication, which is useful, you know yes
to make this simple example happen but we offer it
for today's purposes as a little tool for ones tool kit,
so to speak, that if you need 2 Sprites
to somehow communicate you've got a couple of options.
One, share a variable that both of them check the value of
and maybe change or you have events
which might be a little cleaner than that,
if you want to send something arbitrary.
Well it turns out that Scratch also supports these things
and in problem said zero, it already is coming
in 2 different editions, standard and hacker.
So the standard edition is, you'll see when it goes online
in PDF form tonight, invites you to pretty much make anything
of interest to you so long as it checks off a few boxes.
You need to use at least a variable.
You need to have at least a few different Sprites.
You need to use a sound.
So we lay some basic ground rules just to kind of push you
to do more than just scratch the surface.
But if you're really the type who wants to get his
or her hands dirty very early and maybe do have more of it,
a more comfortable background, there's these things here.
So these are sensor boards that we bought a whole bunch
of for students in the course to use and they have buttons,
they have little sliders that move left to right.
They have little inputs that you can connect alligator clips
to so that you can check resistance,
whether through some physical medium like yourself, left hand
and right hand, water, metal, anything like that.
There's a light sensor.
There's an audio sensor.
And so these things too, very simple ideas, taking sounds,
do something with it, you can actually detect it
programmatically within the program.
So this one is actually a simple example of precisely this,
singer.sb, let me go ahead
and open this one here, I'll full screen him.
He doesn't do all that much and here we go,
[noise] he responds to sounds.
Now he also responds to this and now it just looks
like I'm talking so it's not terribly sensitive
to different types of speech but we're clearly still responding
to something a little binary in nature.
I wonder how long we could give lecture like,
and see how distracting that is.
[laughter] Alright so this one, let me go ahead
and full screen this one.
[noise]
We need to raise the house lights again.
[background music] Can we pull them down again?
So it is in fact a light sensor
and the game gets a little freaky when the lights go down.
That was actually unintentional the first time.
Normally I use my hand but taking
down Sanders Theatre lights also works as well there.
Well we have one other here
so this one was particularly sophisticated but again speaks
to the relative complexity you can create
with what is ultimately quite simple building blocks.
This is one by David Woo, now a junior.
I'm gonna click green flag here.
[background music] This one uses the little sensor bar
to move this guy back and forth and the goal is I think
to get the bananas and not get hit by these,
the monkey's thumb counter has just gone up which means we're
like 174 milliseconds or whatever, I'm stunned
and he's not responding now, notice his eyes.
So that's bad.
[noise and laughter] So that actually use the slider back
and forth but this one actually gets some notion
of some friction coefficient.
So it's not just going left and right in response to me.
He kind of skids out on what is ultimately supposed to be ice.
So again, very simple primitives, right?
Light on, light off, sound on, sound off, slider left,
slider right but even Scratch can interface with this.
So realize that for problems set zero the hacker edition is
by nature meant to be very, very accessible and we have over 100
or 200 of these things out in the hall that you'll be able
to collect today and at any office hours next week
for problem set zero.
So this is this program now that I promised.
This is the first thing that I wrote and this is one
of those either sad or really impressive stories
of late Friday night 8 hours straight playing this.
You'll hear in a moment that I chose a song
that is perhaps the most annoying song in the world
to play ad nauseum for 8 hours while you debug your own program
but this is a little something called Oscar time
and its source code too will be distributed
on the courses website just so that you can see a lot
of different interactions going on.
So I stole this actually from an old piece
of software from Mac OS.
So back when I was a kid growing up with like a Macintosh SC,
there was this plug in called Oscar and you could install it
and then anytime you dragged trash to your trash can,
Oscar would pop out, sing a little song,
and then go back in, right?
[laughter] Completely amusing for like the first few weeks,
first few months and then you realize this is just,
you get, the slippery slope.
So let me go ahead and play this,
perhaps not in its entirety less you never again be able
to watch Sesame Street but as the instructions say,
drag as much falling trash as you can to Oscar's trashcan.
[background music] So notice this game too is going
to interact with my keyboard.
This is trash, click, drag and I won't go too far but notice
if I pull it into the trash quickly, the trash reappears
and now notice it's appearing in a different place each time
so even this game is using some sudo randomness.
And what we'll see is that this gets progressively faster
and progressively faster, much like the DDR game before.
So I thought a fun note, perhaps to conclude on, is this,
throughout today we've been dragging
and dropping puzzle pieces, it's a very intuitive language,
problem set zero will task you with precisely that.
Implementing the program that most amuses you.
We spoke of cell phones earlier today,
albeit in the context of iPhones.
Yes it is the case, if you read this morning's Crimson,
that Google very kindly donated some 20 telephones to CS50
and CS1 so that we could empower some of you, if you so desire
at terms end, to implement any final project that you want
but also one if you so choose,
on what's called an android cell phone, marketed by T-Mobile
and some other folks throughout the world.
Well we read this article.
The Crimson has always been very good to us
in recent years, press wise.
We were quite flattered that they took an interest
in this project but we were also quite amused.
And within minutes of this article going online,
on the front page of today's Crimson, I got this note here,
whose names and such have been redacted.
[laughter] So let's be clear, this is the email I got.
So then I got a little curious, right?
So I'm like okay, interesting, there's an article here
about CS50 so thecrimson.com.
Okay, yep we are in fact on the front page there as well
as in the print version.
So what you hopefully realize today, paragraph 2, look for it.
[laughter] So bless their hearts, right?
This is in fact drag and drop programming
that you will be focusing on for problem set zero
but I did think, in fairness cause again we're big fans
and frankly a lot of our team members are
on the Crimson so it's all good.
I did think that I could at least make this so.
So instead of Oscar Time which drops this trash, I went ahead
and decided we could do this.
You know so I'm gonna go to the trash here,
some little on the fly programming.
You know what?
I need a different costume here.
I'm gonna click import.
I kind of prepared in advance here.
I'm going to go to my desktop folder.
I'm going to go into my zero folder where I hid it.
I've got a little costume here.
[laughter] So if you will, let me go to this thing here.
I'm gonna go ahead and click this button
to shrink him down a little bit.
You have the ability to rotate Sprites as you'll see.
I'm gonna tell it not to rotate anymore and now
if we could crank the volume up just a little bit we have
for you, in conclusion today, drag and drop programming.
[laughter and music]
We'll see you on Wednesday.
[applause and music]