1 00:00:00,506 --> 00:00:08,476 [ Silence ] 2 00:00:08,976 --> 00:00:12,906 >>[background noise] Alright, well thank you. 3 00:00:14,706 --> 00:00:15,676 Alright, welcome back. 4 00:00:15,676 --> 00:00:16,856 My name is David Malan, 5 00:00:16,856 --> 00:00:18,796 for those of you joining us for the first time. 6 00:00:18,796 --> 00:00:22,566 And this is CS50 and this is the continuation of week zero. 7 00:00:22,566 --> 00:00:24,276 For those of you who may have missed Wednesday, 8 00:00:24,276 --> 00:00:26,656 notice that we still have some syllabi. 9 00:00:26,656 --> 00:00:29,396 No more cake but if you did in fact miss Wednesday, 10 00:00:29,396 --> 00:00:32,606 do notice that on the courses website is as will always be 11 00:00:32,606 --> 00:00:36,806 >>This is not your typical high school computer science course 12 00:00:36,806 --> 00:00:39,476 >> So that there is what you missed on Wednesday. 13 00:00:39,476 --> 00:00:40,836 So in the future you will find 14 00:00:40,836 --> 00:00:44,376 that the course makes available its videos in flash formats, 15 00:00:44,376 --> 00:00:47,876 quick time formats as well as in MP3 formats 16 00:00:48,296 --> 00:00:50,556 on the courses website so do take a look at that. 17 00:00:50,766 --> 00:00:53,606 So we've been hard at work over the past couple of days, 18 00:00:53,706 --> 00:00:56,726 particularly one of our CA's who actually is an alumnus, 19 00:00:56,726 --> 00:00:58,256 he lives in New York, his name is Welly [phonetic]. 20 00:00:58,256 --> 00:01:00,996 You may meet him in the virtual terminal room over time 21 00:01:01,236 --> 00:01:04,956 and we've implemented what we're now calling Shuttleboy voice 22 00:01:05,496 --> 00:01:07,816 so you may recall and you may in fact 23 00:01:07,816 --> 00:01:09,716 as an upper classman have used before, 24 00:01:10,216 --> 00:01:11,596 this program called Shuttleboy. 25 00:01:11,596 --> 00:01:13,956 It's a good 12 plus years old now. 26 00:01:14,256 --> 00:01:19,196 It lives via this link on CS50 zone websites here 27 00:01:19,196 --> 00:01:23,816 and let me actually grind to a halt here. 28 00:01:24,436 --> 00:01:29,536 Alright, let's change to this, oh, so what you will see 29 00:01:29,536 --> 00:01:37,566 in a moment, oh, alright [laughter] my name is David 30 00:01:37,756 --> 00:01:39,776 Malan and this is CS50. 31 00:01:39,856 --> 00:01:41,916 Welcome back to week zero continued. 32 00:01:41,916 --> 00:01:44,936 We will crop off of the video everything that just happened. 33 00:01:45,026 --> 00:01:46,726 If you missed Wednesday's lecture video, 34 00:01:46,726 --> 00:01:49,826 know that it is available online, I won't belabor 35 00:01:49,826 --> 00:01:52,376 that particular point but I will draw your attention 36 00:01:52,376 --> 00:01:54,486 to a little application that we've been working on so 37 00:01:54,486 --> 00:01:57,156 for sometime there's existed this program called Shuttleboy. 38 00:01:57,386 --> 00:01:59,106 I'm, say, at Memorial Hall. 39 00:01:59,106 --> 00:02:02,046 I need to get back after class to the quad so I click two links 40 00:02:02,286 --> 00:02:04,476 and wala, [phonetic] there are the next several shuttles I can 41 00:02:04,476 --> 00:02:04,886 get to. 42 00:02:05,056 --> 00:02:07,326 Well we thought it would be fun to take things 43 00:02:07,326 --> 00:02:10,826 to the next level this year and to implement Shuttleboy voice, 44 00:02:10,826 --> 00:02:13,186 the new and improved version of the same idea. 45 00:02:13,186 --> 00:02:16,506 As you may recall from Wednesday we were lucky enough 46 00:02:16,506 --> 00:02:19,646 to share a secure C17 bug CS50. 47 00:02:19,646 --> 00:02:22,166 It's not quite yet in production. 48 00:02:22,166 --> 00:02:24,196 This is really just a sneak preview 49 00:02:24,406 --> 00:02:26,256 but in a moment I'm going to turn this microphone on 50 00:02:26,256 --> 00:02:27,786 and we're going to make a phone call together 51 00:02:27,786 --> 00:02:30,236 and see who here picks up. 52 00:02:30,236 --> 00:02:32,466 So I'm going to go ahead and turn on my speaker phone. 53 00:02:32,786 --> 00:02:34,716 We're going to dial and see what happens here. 54 00:02:36,556 --> 00:02:39,456 >>This is CS50 for Shuttleboy. 55 00:02:39,826 --> 00:02:41,836 Press 1 for Quinton [phonetic] dining. 56 00:02:42,256 --> 00:02:44,696 Press 2 to start over. 57 00:02:44,696 --> 00:02:45,556 Press zero 58 00:02:45,596 --> 00:02:47,156 >>I'll press 1 for Shuttleboy. 59 00:02:47,156 --> 00:02:47,223 [noise] 60 00:02:47,223 --> 00:02:53,616 >>What is your origin for quad? 61 00:02:53,616 --> 00:02:55,426 Press 1, Mather. 62 00:02:55,456 --> 00:02:58,976 Press 2, Boylston [phonetic] Press 3, Lamont. 63 00:02:59,306 --> 00:03:01,356 Press 4, Mem Hall. 64 00:03:01,356 --> 00:03:03,246 Press 5 to start over. 65 00:03:03,246 --> 00:03:06,916 [noise] What is your destination for quad? 66 00:03:06,916 --> 00:03:11,586 Press 1, the next shuttle leaves in 5 minutes at 11:00 pm 67 00:03:11,586 --> 00:03:17,476 and then at 11:10 pm, 11:20 pm and 11:30 pm. 68 00:03:17,476 --> 00:03:18,346 [laughter] 69 00:03:18,346 --> 00:03:20,366 >>So you're going to be here a while today. 70 00:03:20,366 --> 00:03:21,786 It's perhaps the media takeaway 71 00:03:22,436 --> 00:03:25,366 but [applause] so that's not a bug. 72 00:03:25,406 --> 00:03:26,146 That's a feature. 73 00:03:26,146 --> 00:03:28,746 So we just dummied it up with some data for demonstration sake 74 00:03:28,746 --> 00:03:31,546 but it should debut for all of you this coming week. 75 00:03:31,546 --> 00:03:33,936 And notice we've optimized for the most popular stop 76 00:03:33,936 --> 00:03:37,766 so if you want to get on, for instance, at Pound Hall, 77 00:03:37,766 --> 00:03:39,546 it's not gonna happen with Shuttleboy. 78 00:03:39,546 --> 00:03:42,186 Boys Quad, Mather, Mem Hall, Lamont, 79 00:03:42,186 --> 00:03:45,416 all the most popular stops will be optimized in that interface. 80 00:03:46,546 --> 00:03:48,456 So, more to come. 81 00:03:48,536 --> 00:03:49,696 Why do something like that? 82 00:03:49,906 --> 00:03:52,046 Well frankly sometimes, as you'll see perhaps 83 00:03:52,046 --> 00:03:54,676 in this course or beyond, you do things just because you can. 84 00:03:54,856 --> 00:03:58,486 And by courses end will you be able to do that and much more? 85 00:03:58,746 --> 00:04:00,216 So today is ultimately 86 00:04:00,216 --> 00:04:03,166 about laying the foundation for the semester. 87 00:04:03,526 --> 00:04:05,626 And this semester is about Computer Science, 88 00:04:05,626 --> 00:04:07,346 it's about Programming but that sort 89 00:04:07,346 --> 00:04:09,096 of begs the question perhaps on day 1, 90 00:04:09,356 --> 00:04:11,866 what is the latter, what is Programming? 91 00:04:12,076 --> 00:04:15,186 Can someone in laymen's terms give us an answer 92 00:04:15,186 --> 00:04:15,916 to this question? 93 00:04:15,986 --> 00:04:16,836 What is Programming? 94 00:04:20,246 --> 00:04:21,276 You don't want to be down here 95 00:04:21,276 --> 00:04:22,606 because I can see you much more easily, yes? 96 00:04:23,386 --> 00:04:25,336 >>Writing a set of instructions to a computer 97 00:04:25,336 --> 00:04:27,546 >>Okay so writing a set of instructions to a computer. 98 00:04:27,546 --> 00:04:30,296 So a computer is just this fairly dumb device. 99 00:04:30,296 --> 00:04:31,306 It's a mechanical device 100 00:04:31,526 --> 00:04:34,096 that literally can only do what it is told 101 00:04:34,096 --> 00:04:35,816 so programming is ultimately 102 00:04:35,816 --> 00:04:37,556 about telling a computer what to do. 103 00:04:37,556 --> 00:04:39,246 Now how do you tell a computer what to do? 104 00:04:39,426 --> 00:04:42,286 Well in this course and in programming in computer science, 105 00:04:42,486 --> 00:04:45,066 we don't mean you just point and click and choose this 106 00:04:45,066 --> 00:04:47,946 from the menu and at that high user level 107 00:04:47,946 --> 00:04:50,476 but rather how do you actually go in underneath the hood 108 00:04:50,476 --> 00:04:52,446 and make all of that possible? 109 00:04:52,646 --> 00:04:54,526 Well you do this with programming languages. 110 00:04:54,826 --> 00:04:57,046 C is the one we'll focus on in this course and PHP 111 00:04:57,046 --> 00:05:01,146 and JavaScript and C Sharp and a whole bunch of others. 112 00:05:01,506 --> 00:05:03,356 History is littered with dozens, 113 00:05:03,546 --> 00:05:05,326 hundreds of programming languages, 114 00:05:05,586 --> 00:05:07,256 some of which are very much still in vogue, 115 00:05:07,256 --> 00:05:08,976 some of which have partly died off, 116 00:05:09,046 --> 00:05:11,506 some of which really never saw the light of day. 117 00:05:11,506 --> 00:05:15,866 So today we're going to focus on programming ultimately but not 118 00:05:15,866 --> 00:05:17,686 by way of C, not by way of Java, 119 00:05:17,686 --> 00:05:19,866 not by way of these textual languages 120 00:05:19,866 --> 00:05:23,066 that while powerful ultimately and the languages 121 00:05:23,066 --> 00:05:24,536 that we'll use throughout the semester, 122 00:05:24,876 --> 00:05:27,296 they're not quite the easiest thing 123 00:05:27,296 --> 00:05:29,356 to get excited about at term start. 124 00:05:29,356 --> 00:05:31,906 In fact what I'm going to do here in just a moment 125 00:05:31,906 --> 00:05:34,916 on my machine, is pull up a little program that comes 126 00:05:34,916 --> 00:05:37,196 with Mac OS, you can do something similar in Windows 127 00:05:37,196 --> 00:05:39,546 and you'll see how to do this on your own computer before long 128 00:05:39,776 --> 00:05:42,856 but this is a terminal and what it ultimately gives me access 129 00:05:42,856 --> 00:05:46,426 to is the files, the programs, underneath the hood on a Mac 130 00:05:46,596 --> 00:05:48,946 which is essentially a unit spaced computer. 131 00:05:49,206 --> 00:05:53,116 So what I'm going to do just to underwhelm you here on day 1 132 00:05:53,266 --> 00:05:56,726 of 2 so far, I'm going to go ahead and write hello dot C. 133 00:05:57,026 --> 00:05:58,326 So this is a text editor. 134 00:05:58,326 --> 00:06:01,636 This is like a geeks version of Notepad or Microsoft Word 135 00:06:01,636 --> 00:06:03,886 but much simpler than the latter certainly. 136 00:06:03,886 --> 00:06:05,946 And what I'm going to do is something like this 137 00:06:06,096 --> 00:06:11,446 so include standard IO.H, whatever that means. 138 00:06:11,616 --> 00:06:12,296 I'm gonna say [inaudible]. 139 00:06:12,296 --> 00:06:13,206 I'm gonna say main. 140 00:06:13,416 --> 00:06:15,346 I'm gonna say [inaudible] a couple 141 00:06:15,346 --> 00:06:17,666 of brackets and a parenthesis. 142 00:06:17,666 --> 00:06:22,096 And then in here I'm gonna say print F. Let's say hello, 143 00:06:22,096 --> 00:06:25,676 world and then whatever this backslash N thing denotes 144 00:06:25,676 --> 00:06:27,246 and then a semicolon. 145 00:06:27,356 --> 00:06:29,306 So it's really hard to get excited 146 00:06:29,306 --> 00:06:30,796 if this is what you've got in store 147 00:06:30,796 --> 00:06:33,336 for you throughout the semester and plus what is [inaudible], 148 00:06:33,336 --> 00:06:36,226 what is [inaudible], what is char, why all this syntax? 149 00:06:36,526 --> 00:06:38,056 Well it's a very fair question. 150 00:06:38,056 --> 00:06:40,426 It's really not intellectually all that interesting. 151 00:06:40,426 --> 00:06:42,036 We do ultimately need conventions. 152 00:06:42,206 --> 00:06:43,856 We need a language. 153 00:06:43,886 --> 00:06:46,796 We need some basic definitions of how you express yourself 154 00:06:46,796 --> 00:06:49,546 to a computer that's really not interesting here 155 00:06:49,546 --> 00:06:50,716 in week zero thus far. 156 00:06:50,716 --> 00:06:54,226 And so we'll quickly move away from this but just to again, 157 00:06:54,226 --> 00:06:58,286 as promised, underwhelm you, now I have a file called hello .C, 158 00:06:58,536 --> 00:07:00,326 just like .text or .dock. 159 00:07:00,326 --> 00:07:01,746 The .C is just a convention. 160 00:07:01,926 --> 00:07:06,146 I'm gonna run a program called GCC which is a compiler. 161 00:07:06,146 --> 00:07:08,456 If you're familiar with Visual Studio, something like that, 162 00:07:08,456 --> 00:07:09,616 it's the same type of program 163 00:07:09,616 --> 00:07:11,206 but in a Unix or Lynx environment. 164 00:07:11,496 --> 00:07:12,156 And I'm gonna say 165 00:07:12,156 --> 00:07:15,076 to the computer compile the stuff I just wrote. 166 00:07:15,256 --> 00:07:17,766 In other words, turn it into zeros and ones. 167 00:07:17,986 --> 00:07:20,776 Now the fact that no messages got printed is actually a 168 00:07:20,776 --> 00:07:21,326 good thing. 169 00:07:21,326 --> 00:07:23,866 So weekends when you're actually playing with C for yourself 170 00:07:23,866 --> 00:07:26,376 for the first time, if you get a message 171 00:07:26,376 --> 00:07:29,616 at that very first point, damn, means you've messed something 172 00:07:29,616 --> 00:07:32,176 up already but that's okay because much of the process 173 00:07:32,176 --> 00:07:34,746 of learning and in doing interesting things, frankly, 174 00:07:34,746 --> 00:07:37,366 will be tripping over some basics over time. 175 00:07:37,426 --> 00:07:40,566 Fortunately I have not put my own foot in my mouth here yet. 176 00:07:41,006 --> 00:07:42,466 But there's now this file. 177 00:07:42,466 --> 00:07:44,386 It's cryptically named a.out, 178 00:07:44,496 --> 00:07:46,896 just a stupid convention someone came up with years ago. 179 00:07:47,076 --> 00:07:49,716 We can change this ultimately and we will but inside 180 00:07:49,716 --> 00:07:52,596 of this file are a bunch of zeros and ones 181 00:07:52,816 --> 00:07:55,926 that ultimately this computer, this Mac, 182 00:07:56,156 --> 00:07:58,016 this Intel Mac can understand. 183 00:07:58,016 --> 00:08:01,456 So I'm gonna go ahead and hit enter and wala, 184 00:08:01,816 --> 00:08:04,336 you're very first computer science program. 185 00:08:05,216 --> 00:08:05,626 Thank you. 186 00:08:08,856 --> 00:08:11,446 [laughter and applause] Alright so not terribly exciting 187 00:08:11,446 --> 00:08:13,956 and the thing is we don't need to focus so much 188 00:08:13,956 --> 00:08:18,166 on just these basic things like parenthesis and semicolons. 189 00:08:18,166 --> 00:08:19,676 All of these are completely uninteresting 190 00:08:19,676 --> 00:08:21,896 and we'll get there but you'll find that it's really not 191 00:08:21,956 --> 00:08:24,376 such a big deal and it begins to come more naturally. 192 00:08:24,586 --> 00:08:27,026 But today we focus, for just this week zero, 193 00:08:27,116 --> 00:08:29,246 on programming itself and what it means 194 00:08:29,246 --> 00:08:31,936 to make computers do what you want but doing it 195 00:08:31,936 --> 00:08:33,716 in a much more intuitive way. 196 00:08:33,716 --> 00:08:35,366 And we do this, as promised on Wednesday, 197 00:08:35,596 --> 00:08:37,666 by way of this graphical language developed 198 00:08:37,666 --> 00:08:39,526 by MIT's media lab a couple of years ago. 199 00:08:39,766 --> 00:08:41,296 It happens to be called Scratch 200 00:08:41,516 --> 00:08:43,166 and what we'll see here is another demo 201 00:08:43,166 --> 00:08:45,236 of what you saw briefly on Wednesday 202 00:08:45,236 --> 00:08:51,296 and this one I will give you not one that I myself have made 203 00:08:51,296 --> 00:08:53,106 but a former teaching fellow of the course. 204 00:08:53,446 --> 00:08:57,516 I give you by way of this demo, raining men. 205 00:08:57,516 --> 00:08:58,016 [laughter] 206 00:08:58,016 --> 00:10:29,000 [music and singing] 207 00:10:30,996 --> 00:10:34,026 So hopefully much more interesting than hello world 208 00:10:34,026 --> 00:10:34,746 at the command prompt. 209 00:10:34,746 --> 00:10:37,186 Now grant it that was made by a former teaching fellow, 210 00:10:37,376 --> 00:10:39,336 in fact one of the stars of the football team. 211 00:10:39,336 --> 00:10:41,926 So realize we do get all types here in CS50, 212 00:10:41,926 --> 00:10:44,726 not those that I might have recalled from my own high school 213 00:10:44,886 --> 00:10:47,696 but this one is clearly more presentation oriented 214 00:10:47,696 --> 00:10:50,546 than it is interactive and a lot of what using computers is 215 00:10:50,546 --> 00:10:53,136 about is taking input and then producing output, 216 00:10:53,136 --> 00:10:54,666 actually interacting with the human. 217 00:10:54,916 --> 00:10:55,966 So why don't we do just that. 218 00:10:55,966 --> 00:10:57,816 And in fact, lest we send the message 219 00:10:57,816 --> 00:10:59,676 that only teaching fellows can make things 220 00:10:59,676 --> 00:11:01,936 that are this much more interesting than hello world, 221 00:11:02,066 --> 00:11:03,226 let me show you something that was made 222 00:11:03,226 --> 00:11:07,466 by a former CS50 student but for this we need one volunteer 223 00:11:07,466 --> 00:11:10,726 to come on up here and stand before my keyboard. 224 00:11:11,796 --> 00:11:14,016 Yep, in the back on the edge there, come on down. 225 00:11:15,306 --> 00:11:16,756 And you do have to be comfortable, again, 226 00:11:16,756 --> 00:11:20,206 being on camera since what you don't see happen is the signing 227 00:11:20,206 --> 00:11:22,686 of legal forms off to the stage once we're done 228 00:11:23,266 --> 00:11:24,716 with demonstrations like this. 229 00:11:24,716 --> 00:11:26,946 So what I'm going to do is bring up a demonstration. 230 00:11:26,946 --> 00:11:28,506 This one was by Andrew Berry. 231 00:11:28,506 --> 00:11:30,286 This next one is by Ann Shea [phonetic] 232 00:11:30,466 --> 00:11:33,386 and this one, no, come on. 233 00:11:33,676 --> 00:11:34,616 No turning back now. 234 00:11:35,866 --> 00:11:37,186 [laughter] May look familiar 235 00:11:37,186 --> 00:11:42,026 if you've played a little something like this before. 236 00:11:42,076 --> 00:11:46,576 [laughter] So, some of you might have played DDR or these kinds 237 00:11:46,576 --> 00:11:48,236 of coordination games, maybe not, 238 00:11:48,236 --> 00:11:49,746 if not it's better for the demo. 239 00:11:50,376 --> 00:11:52,266 So in a moment, and what is your name? 240 00:11:52,586 --> 00:11:53,196 >>Yonason [phonetic] 241 00:11:53,196 --> 00:11:55,296 >>Yonason, David, nice to meet you. 242 00:11:55,296 --> 00:11:58,316 So we're running on hit space to begin 243 00:11:58,316 --> 00:12:00,736 and what's gonna happen is music is gonna start playing 244 00:12:00,736 --> 00:12:02,666 and as the instructions here say, you're gonna have 245 00:12:02,666 --> 00:12:05,346 to hit the arrows in lock step with stuff that's flowing 246 00:12:05,346 --> 00:12:07,406 up the screen so that it overlaps the shapes 247 00:12:07,626 --> 00:12:10,066 and then we'll keep track of your score and just how awesome, 248 00:12:10,066 --> 00:12:12,506 cool, good or boo you are. 249 00:12:13,006 --> 00:12:13,426 Ready to go? 250 00:12:13,486 --> 00:12:14,966 Arrow keys down here, here we are. 251 00:12:15,516 --> 00:13:22,916 [music and noise] 252 00:13:23,416 --> 00:13:25,916 [background music] Alright, how a big round of applause? 253 00:13:26,896 --> 00:13:31,906 That was pretty damn good. 254 00:13:32,216 --> 00:13:37,326 So how does one go about writing this stuff? 255 00:13:37,326 --> 00:13:40,776 Well the interesting question today, legal form, there we go. 256 00:13:40,946 --> 00:13:43,026 [laughter] So the interesting thing today is going to be 257 00:13:43,026 --> 00:13:45,116 to try and dissect, intellectually, 258 00:13:45,116 --> 00:13:45,976 what is going on, right. 259 00:13:45,976 --> 00:13:48,046 These things might be fun and they might look silly 260 00:13:48,326 --> 00:13:50,116 but there's really some interesting stuff going 261 00:13:50,116 --> 00:13:51,436 on underneath the hood. 262 00:13:51,626 --> 00:13:53,296 There's some randomness going on there. 263 00:13:53,296 --> 00:13:56,106 The game is not always the same in this particular case. 264 00:13:56,366 --> 00:13:57,326 In the previous case 265 00:13:57,326 --> 00:13:59,426 with Raining Men there was clearly synchronization, 266 00:13:59,426 --> 00:14:01,756 there was lots of stuff going on at the same time 267 00:14:01,986 --> 00:14:03,986 and if you think back just a moment ago at what I did 268 00:14:03,986 --> 00:14:06,236 at the command prompt, the textual interface, 269 00:14:06,536 --> 00:14:09,386 it's hard to imagine how you could possibly coordinate all 270 00:14:09,386 --> 00:14:10,736 of those several things going 271 00:14:10,736 --> 00:14:13,086 on when I've completely underwhelmed you thus far 272 00:14:13,086 --> 00:14:16,286 by just printing a simple instruction to the screen. 273 00:14:16,516 --> 00:14:20,266 And I'll give you a quick teaser too, in a little bit, 274 00:14:20,536 --> 00:14:23,836 of one that I wrote that we'll use for the sake of discussion 275 00:14:23,836 --> 00:14:26,576 if only cause I understand what's underneath the hood 276 00:14:26,576 --> 00:14:29,906 of that one best and that one, we'll see, is called Oscar Time. 277 00:14:29,906 --> 00:14:33,286 But first let's consider outside the context 278 00:14:33,286 --> 00:14:34,456 of this thing called Scratch, 279 00:14:34,456 --> 00:14:36,896 what really programming is all about. 280 00:14:36,896 --> 00:14:38,506 Well let's pick something completely silly. 281 00:14:38,796 --> 00:14:41,696 So waking up in the morning and putting on one's socks, right, 282 00:14:41,696 --> 00:14:44,176 you've probably, hopefully, never really thought hard 283 00:14:44,176 --> 00:14:45,326 about this particular problem. 284 00:14:45,326 --> 00:14:46,016 You just do it. 285 00:14:46,246 --> 00:14:48,706 But suppose you were trying to instruct a computer to do it 286 00:14:48,706 --> 00:14:51,776 or maybe more concretely a robot to do it, you kind of have 287 00:14:51,966 --> 00:14:53,526 to think a little harder. 288 00:14:53,896 --> 00:14:56,796 Now perhaps I've raised the bar to volunteers a bit higher 289 00:14:56,796 --> 00:15:00,186 but I need one volunteer for this thing as well. 290 00:15:02,286 --> 00:15:07,476 Yes, come on down and you too will be on camera and shoeless. 291 00:15:09,016 --> 00:15:10,826 [laughter] So I went shopping beforehand, 292 00:15:11,886 --> 00:15:14,586 turns out you can buy some amazing propaganda at the Coupe. 293 00:15:14,586 --> 00:15:19,686 This is ultimately going to be about this algorithm here, 294 00:15:19,686 --> 00:15:21,366 oh come prepared, what is your name? 295 00:15:21,616 --> 00:15:21,796 >>Josh 296 00:15:22,006 --> 00:15:25,156 >>Josh, alright, come on over, come on over to your chair here. 297 00:15:25,156 --> 00:15:26,696 We'll need you to take off your socks. 298 00:15:27,196 --> 00:15:29,046 I kind of splurged here. 299 00:15:29,046 --> 00:15:31,316 We went with a few different styles here today 300 00:15:31,396 --> 00:15:33,876 so they're all clean though. 301 00:15:33,876 --> 00:15:35,916 I thought for a moment maybe I'd just go home for this 302 00:15:35,966 --> 00:15:37,676 but I thought that might not go over so well. 303 00:15:37,876 --> 00:15:40,206 So we have some H branded socks here, 304 00:15:40,206 --> 00:15:42,496 which we'll put, say in the sock pile. 305 00:15:42,496 --> 00:15:44,726 I also got some more sporty socks, 306 00:15:44,726 --> 00:15:47,866 if this is what your preference so we have these sort 307 00:15:48,086 --> 00:15:50,306 of low rise type socks stained crimson. 308 00:15:50,306 --> 00:15:53,476 And then these were actually my favorite so [laughter] 309 00:15:54,206 --> 00:15:58,406 because you know there's some tourists out there walking 310 00:15:58,406 --> 00:16:01,886 around in these somewhere so we also have a pair of these. 311 00:16:01,886 --> 00:16:05,046 So an algorithm ultimately, as we'll see in just a moment, 312 00:16:05,046 --> 00:16:08,346 is a procedure, it's a program but it's not necessarily written 313 00:16:08,346 --> 00:16:10,536 in a specific language and in fact what we see here 314 00:16:10,536 --> 00:16:13,036 on the board is what we'll call sudo code. 315 00:16:13,036 --> 00:16:14,096 It's not quite code. 316 00:16:14,096 --> 00:16:15,176 It's not programming code. 317 00:16:15,176 --> 00:16:17,926 It's certainly not zeros and ones but it's kind of a cross 318 00:16:17,986 --> 00:16:20,066 between something that's very succinct and logical 319 00:16:20,306 --> 00:16:22,506 and it's also nonetheless very human readable. 320 00:16:22,506 --> 00:16:25,246 And so throughout the semester before we sit down 321 00:16:25,246 --> 00:16:27,976 and start typing things away at a command prompt like I did 322 00:16:28,206 --> 00:16:31,126 or even dragging and dropping, it's often worthwhile thinking 323 00:16:31,306 --> 00:16:34,366 about how could you express this program in just English 324 00:16:34,366 --> 00:16:37,056 or English like syntax because then you could work 325 00:16:37,056 --> 00:16:38,486 through the logic, then you could think 326 00:16:38,486 --> 00:16:40,696 through hopefully some of the bugs, some of the corner cases 327 00:16:40,966 --> 00:16:43,206 and then proceed to implement that algorithm. 328 00:16:43,476 --> 00:16:45,526 So we're gonna go ahead and use Josh here for the sake 329 00:16:45,526 --> 00:16:47,326 of execution and we'll see 330 00:16:47,326 --> 00:16:50,356 if we can't tease apart a few programmatic fundamentals 331 00:16:50,356 --> 00:16:53,056 that will then recur in much more traditional contexts 332 00:16:53,216 --> 00:16:53,936 than this one. 333 00:16:54,486 --> 00:16:57,966 So Josh if you would, perhaps, think to yourself the following, 334 00:16:57,966 --> 00:17:00,286 let socks on feet, all one word. 335 00:17:00,286 --> 00:17:02,316 I seemed to have used this underscore notation just 336 00:17:02,316 --> 00:17:04,246 to tie it all together as one string, 337 00:17:04,586 --> 00:17:06,586 let's socks on feet equal zero. 338 00:17:06,846 --> 00:17:07,566 So this is easy. 339 00:17:07,566 --> 00:17:09,976 This is a variable, right, just like X and Y and Z 340 00:17:09,976 --> 00:17:11,816 in high school were variables that stored value, 341 00:17:11,816 --> 00:17:14,556 so is socks on feet a little more interestingly named a 342 00:17:14,556 --> 00:17:17,196 variable that is in fact consistent with reality. 343 00:17:17,196 --> 00:17:18,986 So we're initializing our program here. 344 00:17:19,286 --> 00:17:21,716 So two, line two induces what we'll call, 345 00:17:21,716 --> 00:17:23,966 and this too might be self explanatory, a loop. 346 00:17:24,316 --> 00:17:27,036 So something that repeats zero or more times. 347 00:17:27,036 --> 00:17:29,146 Now in this case I've used the key word while 348 00:17:29,396 --> 00:17:32,706 which does imply doing something while something is the case. 349 00:17:32,926 --> 00:17:35,546 We'll see terms like for and do while 350 00:17:35,936 --> 00:17:39,226 and similar synonyms essentially but for this we chose this 351 00:17:39,356 --> 00:17:43,826 so while socks on feet exclamation point equals two. 352 00:17:44,366 --> 00:17:45,966 Now if you don't know what that means, 353 00:17:46,326 --> 00:17:49,496 take a stab at what it might mean. 354 00:17:49,716 --> 00:17:52,526 So not equal to, so bang equal sign 355 00:17:52,526 --> 00:17:55,696 so computer scientist geeks say bang not exclamation point 356 00:17:55,906 --> 00:17:58,046 so bang equals means not equal to two. 357 00:17:58,046 --> 00:18:01,436 So we're going to do the following while Josh presumably 358 00:18:01,436 --> 00:18:05,766 has a zero or one or you know maybe even three socks 359 00:18:05,766 --> 00:18:06,786 on his feet cause we're going 360 00:18:06,786 --> 00:18:08,216 to do this while it's not equal to two. 361 00:18:08,486 --> 00:18:11,106 So, here's where your awkward role play comes in. 362 00:18:11,306 --> 00:18:14,096 Open the sock drawer, excellent. 363 00:18:14,346 --> 00:18:15,906 Look for sock. 364 00:18:16,986 --> 00:18:21,106 Okay if you find sock, so this too is bold faced for a reason, 365 00:18:21,106 --> 00:18:22,466 this is a condition, a branch, 366 00:18:22,546 --> 00:18:25,336 something that happens optionally if something is true 367 00:18:25,336 --> 00:18:27,656 or false so if you find a sock, have you? 368 00:18:28,256 --> 00:18:31,266 Okay then and notice the indentation here too 369 00:18:31,266 --> 00:18:33,526 so this is something we'll use consistently. 370 00:18:33,526 --> 00:18:34,816 This is a matter of style. 371 00:18:34,816 --> 00:18:37,796 The computer often does not care if you have spaces, 372 00:18:37,796 --> 00:18:40,586 tab characters, indentation, no indentation and those 373 00:18:40,586 --> 00:18:43,516 of you have written HTML or web pages before probably know 374 00:18:43,516 --> 00:18:45,566 that it might look pretty to make everything look nice 375 00:18:45,566 --> 00:18:47,996 and hierarchical and pretty printed as they say 376 00:18:48,126 --> 00:18:49,756 but the computer certainly doesn't care. 377 00:18:49,976 --> 00:18:52,916 But here we're humans, we do care so it's indented to imply 378 00:18:53,176 --> 00:18:57,156 that line 6 on down are now encapsulated inside 379 00:18:57,156 --> 00:19:00,006 of what line 5 is telling us to do. 380 00:19:00,136 --> 00:19:03,046 So if line 5 is true, if find sock, which you did, 381 00:19:03,046 --> 00:19:04,936 then we proceed to the indentation. 382 00:19:04,936 --> 00:19:09,846 Put on sock, excellent. 383 00:19:10,276 --> 00:19:12,826 Socks on feet plus plus, this is shorthand notation 384 00:19:12,826 --> 00:19:15,106 and we'll see this in C, JavaScript, PHP, 385 00:19:15,156 --> 00:19:18,366 plus plus as you might infer, just means incremented by one. 386 00:19:18,366 --> 00:19:20,636 So socks on feet now equals one. 387 00:19:20,866 --> 00:19:21,836 Look for matching sock. 388 00:19:22,256 --> 00:19:23,796 And this we might call a statement. 389 00:19:23,796 --> 00:19:25,696 Anytime it's just sort of matter of fact here, 390 00:19:25,696 --> 00:19:26,646 we'll call that a statement. 391 00:19:26,646 --> 00:19:27,746 Okay you've looked. 392 00:19:27,946 --> 00:19:33,056 If you find a matching sock then indentation, 393 00:19:33,056 --> 00:19:34,826 put on matching sock. 394 00:19:36,216 --> 00:19:37,336 So far so good. 395 00:19:37,516 --> 00:19:39,056 Socks on feet plus plus. 396 00:19:39,276 --> 00:19:42,736 Close sock drawer and now notice I'm not going to proceed 397 00:19:42,736 --> 00:19:46,686 to do anything with line 13 cause line 13 is exclusive 398 00:19:46,686 --> 00:19:49,706 with respect to 9 so if I did 9 I don't do 13. 399 00:19:49,936 --> 00:19:53,546 Similarly if I did 5, I don't do 16 400 00:19:53,736 --> 00:19:55,446 and now what do I go back up to? 401 00:19:55,446 --> 00:19:58,676 Well following the logic of the indentation I now go back 402 00:19:58,706 --> 00:20:01,076 to line 2 and I check this condition, 403 00:20:01,076 --> 00:20:05,746 while socks on feet not equal to 2, what do I do now? 404 00:20:06,396 --> 00:20:08,846 [inaudible audience comment] Yeah, I seem to move on. 405 00:20:08,846 --> 00:20:09,606 So what's next? 406 00:20:09,606 --> 00:20:13,686 Well line 18 which isn't even present so we implicitly drop 407 00:20:13,686 --> 00:20:14,916 out of the program and we're done. 408 00:20:15,266 --> 00:20:16,296 So that was actually very good. 409 00:20:16,296 --> 00:20:18,226 And now if you wouldn't mind, let's restart this. 410 00:20:18,336 --> 00:20:22,416 Take off this socks if you would and that will get awkward for me 411 00:20:22,456 --> 00:20:26,316 so I'm gonna take, say this, this and this. 412 00:20:26,316 --> 00:20:27,216 Did we screw up? 413 00:20:27,216 --> 00:20:28,866 Is this program actually correct? 414 00:20:29,026 --> 00:20:29,446 Well let's see. 415 00:20:29,446 --> 00:20:30,746 Let's socks on feet equal zero. 416 00:20:30,746 --> 00:20:32,946 Good, while socks on feet not equal to 2. 417 00:20:32,946 --> 00:20:33,846 Good, that is true. 418 00:20:33,846 --> 00:20:36,636 Indentation, open sock drawer, look for sock. 419 00:20:37,496 --> 00:20:40,996 If you find sock then, [laughter] nice, put on sock. 420 00:20:46,936 --> 00:20:49,356 You will always remember this moment in college. 421 00:20:50,076 --> 00:20:56,396 [laughter] Excellent, socks on feet plus plus. 422 00:20:56,396 --> 00:20:58,466 Look for matching sock. 423 00:20:59,036 --> 00:21:03,146 Problem, if you find matching sock then line 9 doesn't seem 424 00:21:03,146 --> 00:21:06,776 to apply so we go to line 13 followed 425 00:21:06,776 --> 00:21:11,306 by 14 else remove first sock from foot. 426 00:21:11,556 --> 00:21:15,366 Socks on feet, minus minus 427 00:21:15,706 --> 00:21:17,196 so now we're back to zero presumably. 428 00:21:17,196 --> 00:21:22,336 What line do I go back to next? 429 00:21:22,336 --> 00:21:24,646 [inaudible audience comment] So not 5, I heard but rather, 430 00:21:25,296 --> 00:21:28,946 so I'm back up at 2 while socks on feet not equal to 2, okay, 431 00:21:29,776 --> 00:21:32,926 true so we proceed to open sock drawer, look for sock, 432 00:21:33,106 --> 00:21:35,466 if you find sock then put on sock. 433 00:21:36,186 --> 00:21:39,546 So at this point you're probably getting a little bored, right? 434 00:21:39,696 --> 00:21:41,226 Because what's gonna happen here? 435 00:21:41,846 --> 00:21:45,766 So this is one of these things called an infinite loop 436 00:21:45,846 --> 00:21:47,926 or if you've never heard of that expression, that's what it is 437 00:21:47,926 --> 00:21:50,066 and frankly you've probably experienced this before, 438 00:21:50,066 --> 00:21:52,586 whether you have a Mac, a PC or whatever, you've probably 439 00:21:52,586 --> 00:21:54,456 at least once in life gotten really ticked off 440 00:21:54,456 --> 00:21:58,186 at your machine because it's kind of just hanging and hanging 441 00:21:58,186 --> 00:22:00,786 and hanging or the stupid hourglass is just spinning 442 00:22:00,786 --> 00:22:04,426 and spinning and spinning and it never stops for some reason. 443 00:22:04,426 --> 00:22:05,166 Well what is that? 444 00:22:05,276 --> 00:22:07,256 Well technically it could be any number of things 445 00:22:07,306 --> 00:22:11,126 but it's probably a mistake that some programmer made, 446 00:22:11,366 --> 00:22:12,676 didn't anticipate the fact 447 00:22:12,676 --> 00:22:15,236 that maybe I haven't done the laundry all that often 448 00:22:15,236 --> 00:22:17,676 and therefore I might not have sufficiently many socks. 449 00:22:17,676 --> 00:22:20,326 Maybe I just don't have certain socks anymore cause they got 450 00:22:20,326 --> 00:22:21,306 torn and I threw them out. 451 00:22:21,486 --> 00:22:24,006 These are what we'll call corner cases and clearly bad, 452 00:22:24,356 --> 00:22:27,666 awkward things arise if you don't handle these cases. 453 00:22:27,666 --> 00:22:30,356 So in those cases with your own machines you probably rebooted 454 00:22:30,556 --> 00:22:32,686 or you force quit, you pulled up the task manager, 455 00:22:32,686 --> 00:22:34,846 all these stupid real world issues that boil 456 00:22:34,846 --> 00:22:37,416 down to stopping a program that's caught 457 00:22:37,416 --> 00:22:38,216 in an infinite loop. 458 00:22:38,396 --> 00:22:41,876 Are there other bugs here that we might trip over? 459 00:22:42,456 --> 00:22:42,606 [inaudible audience comment] Sorry? 460 00:22:44,896 --> 00:22:49,576 [inaudible audience comment] Yeah so I don't, 461 00:22:49,756 --> 00:22:52,156 so there's also some real world problems here perhaps, 462 00:22:52,156 --> 00:22:54,406 especially if this is again not Josh the human 463 00:22:54,406 --> 00:22:58,786 but Josh the robot where it keeps saying open, what is, 464 00:22:58,866 --> 00:23:01,076 it said open sock drawer, open sock drawer. 465 00:23:01,186 --> 00:23:02,486 Like, what does that even mean? 466 00:23:02,486 --> 00:23:04,576 I mean either the robot is gonna start ripping the thing 467 00:23:04,576 --> 00:23:07,436 out of the wall or it's just gonna get confused 468 00:23:07,436 --> 00:23:08,406 because it's ambiguous. 469 00:23:08,486 --> 00:23:12,616 And ambiguity is often a problem too because what is going 470 00:23:12,666 --> 00:23:15,976 to happen in cases that are not so explicitly specified by you. 471 00:23:16,206 --> 00:23:19,146 So why don't we put Josh out of his awkwardness here. 472 00:23:19,346 --> 00:23:22,266 You get about 6 souvenirs today. 473 00:23:22,336 --> 00:23:23,076 They are clean. 474 00:23:23,076 --> 00:23:25,016 I can give you a little shopping bag, 475 00:23:25,096 --> 00:23:26,546 if not the receipts if you want. 476 00:23:27,506 --> 00:23:29,706 But here we have some nice little Harvard souvenirs 477 00:23:29,706 --> 00:23:31,156 and perhaps a quick round of applause for Josh. 478 00:23:31,286 --> 00:23:36,796 Thank you very much. 479 00:23:36,796 --> 00:23:38,906 [applause] Alright so this is what we did 480 00:23:38,906 --> 00:23:40,086 at the command line quickly 481 00:23:40,086 --> 00:23:42,226 and this is what we'll do a week plus from now. 482 00:23:42,226 --> 00:23:44,086 This week we focus on things more intuitive 483 00:23:44,356 --> 00:23:47,946 but this is a basic C program, a canonical program 484 00:23:48,076 --> 00:23:49,776 and in fact it will get much more interesting, 485 00:23:49,776 --> 00:23:50,946 much more powerful than this 486 00:23:51,166 --> 00:23:52,956 but what really happened underneath the hood 487 00:23:52,956 --> 00:23:54,326 when I wrote this little program? 488 00:23:54,566 --> 00:23:57,046 Well if that's my program at top left and I ran 489 00:23:57,046 --> 00:23:59,596 that program called GCC, the compiler, 490 00:23:59,596 --> 00:24:01,756 well what that compiler did, it was written 491 00:24:01,756 --> 00:24:05,756 by some smart people a while ago, it converts what kind 492 00:24:05,756 --> 00:24:08,326 of looks like English but is definitely not English I'm used 493 00:24:08,326 --> 00:24:11,506 to writing in, it converts it to zeros and ones 494 00:24:11,506 --> 00:24:13,256 that my particular computer understands 495 00:24:13,316 --> 00:24:15,096 and this process is called compiling. 496 00:24:15,356 --> 00:24:17,196 And this is partly the reason why 497 00:24:17,406 --> 00:24:20,066 if you've ever bought a program, maybe Microsoft Office, 498 00:24:20,316 --> 00:24:22,636 you either buy the shrink wrapped box that says 499 00:24:22,636 --> 00:24:25,676 for Windows or you buy the shrink wrapped box that says 500 00:24:25,926 --> 00:24:28,066 for Mac OS because even companies 501 00:24:28,066 --> 00:24:29,626 like Microsoft very often have 502 00:24:29,796 --> 00:24:32,976 to essentially write two versions of programs 503 00:24:32,976 --> 00:24:35,696 because a compiler, as I hinted at with my own Mac, 504 00:24:36,006 --> 00:24:37,816 is kind of machine specific 505 00:24:37,816 --> 00:24:39,976 or at least it's operating system specific. 506 00:24:40,236 --> 00:24:43,306 So unfortunately we don't really live in a world that's 507 00:24:43,306 --> 00:24:46,186 as perfect enough whereby Microsoft writes their program 508 00:24:46,186 --> 00:24:50,486 once and they run GCC on a Mac, they run GCC on a PC. 509 00:24:50,486 --> 00:24:54,126 They copy the resulting A dot out file or MS Word. 510 00:24:54,296 --> 00:24:55,956 Exe file to a CD and ship it. 511 00:24:55,956 --> 00:24:57,746 There's a lot more to it than that. 512 00:24:58,086 --> 00:24:59,636 So one of the things we'll talk about closer 513 00:24:59,636 --> 00:25:02,736 to terms end is the notion of cross platform software. 514 00:25:02,736 --> 00:25:05,106 Java, one of the appeals of Java, if I'm familiar, 515 00:25:05,296 --> 00:25:07,086 is that in theory you're supposed to be able 516 00:25:07,086 --> 00:25:09,376 to write a Java program once and then run it 517 00:25:09,376 --> 00:25:12,906 on any computer you want, a Mac, a PC, a lynx box, 518 00:25:13,136 --> 00:25:14,626 even a cell phone these days. 519 00:25:14,876 --> 00:25:17,846 And you'll use a program like this, the virtual terminal room 520 00:25:17,846 --> 00:25:20,236 to which I referred on Wednesday for virtual office hours, 521 00:25:20,236 --> 00:25:21,316 if you decide to log on, 522 00:25:21,646 --> 00:25:24,246 that's actually you'll see a program written in Java 523 00:25:24,516 --> 00:25:26,806 because that particular company, called Illuminate, 524 00:25:27,026 --> 00:25:30,316 decided it's really expensive and it's really uninteresting 525 00:25:30,316 --> 00:25:34,146 for us to have to write our software multiple times just 526 00:25:34,286 --> 00:25:36,776 so that we can have Mac customers, PC customers 527 00:25:36,776 --> 00:25:39,036 and so forth so they went with Java. 528 00:25:39,036 --> 00:25:41,146 But there's a downside even to that. 529 00:25:41,646 --> 00:25:45,036 So, what then are we going to do this week? 530 00:25:45,196 --> 00:25:46,036 It is Scratch. 531 00:25:46,036 --> 00:25:47,986 And today what we'll do is just lay the foundation 532 00:25:47,986 --> 00:25:50,546 for just these basic programmatic constructs 533 00:25:50,656 --> 00:25:55,226 but by lectures and today we'll be already begin our transition 534 00:25:55,226 --> 00:25:57,566 to C. Your first problem set, which will be posted, 535 00:25:57,816 --> 00:26:00,166 as will be typically the case, by 7:00 pm tonight 536 00:26:00,166 --> 00:26:01,206 on the courses website. 537 00:26:01,346 --> 00:26:03,506 If you're like me you're welcome to dive in at 7:01. 538 00:26:03,976 --> 00:26:04,986 If you have more going 539 00:26:04,986 --> 00:26:07,856 on you can wait a few days cause you have ample time for this one 540 00:26:08,036 --> 00:26:10,986 but they'll be posted on the courses website at CS50.net 541 00:26:11,286 --> 00:26:14,426 but let me encourage you to take note of this too. 542 00:26:14,666 --> 00:26:17,456 So especially for this first problem set you'll find 543 00:26:17,696 --> 00:26:20,856 that it's rather fun and in fact you'll probably get a kick 544 00:26:20,856 --> 00:26:23,366 out of sharing or making your roommate take a look 545 00:26:23,366 --> 00:26:24,706 at what you've actually been working on. 546 00:26:24,956 --> 00:26:27,386 But so that you don't feel completely thrown 547 00:26:27,386 --> 00:26:29,256 into the lion's den, so to speak, 548 00:26:29,256 --> 00:26:32,086 especially if you're feeling a bit among those less 549 00:26:32,116 --> 00:26:34,836 comfortable, we will start office hours this coming week. 550 00:26:34,836 --> 00:26:36,116 So no sections just yet. 551 00:26:36,256 --> 00:26:37,556 We will start the process 552 00:26:37,556 --> 00:26:39,426 of sectioning next Wednesday onward, 553 00:26:39,426 --> 00:26:41,636 some more on that next week but office hours, 554 00:26:41,636 --> 00:26:44,966 this list will grow as our 50 plus person team begins 555 00:26:44,966 --> 00:26:47,256 to figure out their own schedules, we're gonna hang out. 556 00:26:47,256 --> 00:26:50,186 Next week on Monday, Tuesday, Wednesday, Thursday evenings, 557 00:26:50,216 --> 00:26:52,826 7:00 pm onward in Cavet [phonetic] Houses dining hall 558 00:26:53,016 --> 00:26:55,936 and in Quincy Houses dining hall where you can bring your laptop, 559 00:26:55,936 --> 00:26:57,866 you can hang out for brain break and a number 560 00:26:57,866 --> 00:27:00,006 of the teaching fellows and CA's will be there to help you, 561 00:27:00,216 --> 00:27:04,576 to chat with you and just generally tackle this as a team. 562 00:27:04,686 --> 00:27:06,326 So what are you going to be tackling? 563 00:27:06,866 --> 00:27:08,866 Well this year it's Scratch. 564 00:27:08,916 --> 00:27:12,046 I went to a website called scratch.mit.edu. 565 00:27:12,336 --> 00:27:14,196 It works for Mac's, it works for PC's. 566 00:27:14,196 --> 00:27:17,176 I downloaded this program and wala, this is what I get. 567 00:27:17,266 --> 00:27:18,576 But what is this interface? 568 00:27:18,666 --> 00:27:21,896 Well on the left hand side here we have these puzzle pieces 569 00:27:21,896 --> 00:27:23,726 and they literally look like puzzle pieces 570 00:27:23,726 --> 00:27:27,246 that in a moment I'll be able to drag into this scripts area here 571 00:27:27,426 --> 00:27:29,296 and it's by dragging and dropping these pieces 572 00:27:29,296 --> 00:27:31,856 that I will be programming, so to speak. 573 00:27:32,046 --> 00:27:33,876 Well in the top right we have what we're going 574 00:27:33,876 --> 00:27:34,936 to call the stage. 575 00:27:34,936 --> 00:27:37,786 This is where stuff happens, as we've already seen. 576 00:27:38,066 --> 00:27:40,746 This little pat by default, this is Scratch, 577 00:27:41,006 --> 00:27:42,186 sort of the mascot they went with 578 00:27:42,186 --> 00:27:43,786 but we can change his appearance at will. 579 00:27:44,016 --> 00:27:46,586 This is what we'll call a Sprite 580 00:27:46,586 --> 00:27:49,126 or really it's what we'll call more technically a thread, 581 00:27:49,516 --> 00:27:51,846 a miniature program that you can instruct 582 00:27:51,846 --> 00:27:54,366 to do things independently of anything else. 583 00:27:54,366 --> 00:27:56,526 And at bottom right here we'll see all 584 00:27:56,526 --> 00:27:59,736 of the many different Sprites that I might possibly create 585 00:27:59,976 --> 00:28:00,886 so let me go and do this. 586 00:28:01,196 --> 00:28:03,976 So control, when green flag clicked. 587 00:28:04,406 --> 00:28:07,026 So when I a moment ago typed that cryptic sequence 588 00:28:07,026 --> 00:28:08,636 of instructions into main, 589 00:28:08,906 --> 00:28:10,646 main kind of has some implied meaning, 590 00:28:10,646 --> 00:28:12,676 this is the main guts of my program? 591 00:28:12,916 --> 00:28:14,566 Well this is sort of the equivalent so those 592 00:28:14,566 --> 00:28:17,696 of you coming to the course from like a Java background, 593 00:28:17,906 --> 00:28:19,526 it's like writing your main method. 594 00:28:19,656 --> 00:28:23,006 So this block, just to be clear, has this icon on it that says 595 00:28:23,066 --> 00:28:25,166 when green flag clicked and that's why, 596 00:28:25,166 --> 00:28:26,636 if you've been paying close attention, 597 00:28:26,846 --> 00:28:29,366 every time I start these demos I click that green flag. 598 00:28:29,636 --> 00:28:30,466 So what does that mean? 599 00:28:30,466 --> 00:28:33,076 That means when we click that green flag this cat, 600 00:28:33,446 --> 00:28:36,096 this Sprite, because he's currently selected, 601 00:28:36,296 --> 00:28:39,756 is going to do whatever the blocks tell him to do 602 00:28:39,756 --> 00:28:43,106 that are attached to this particular starting point. 603 00:28:43,356 --> 00:28:44,256 So what do we want him to do? 604 00:28:44,256 --> 00:28:46,146 Well let's do something simple. 605 00:28:46,146 --> 00:28:48,276 I'm gonna click the little pallet here for looks. 606 00:28:48,526 --> 00:28:52,216 So MIT has nicely color coded things just 607 00:28:52,216 --> 00:28:54,876 so that all related blocks are similarly colored. 608 00:28:54,876 --> 00:28:56,736 I'm gonna flip through this and okay, 609 00:28:56,806 --> 00:28:58,436 this is interesting, say hello. 610 00:28:58,736 --> 00:29:01,436 So say hello for 2 seconds and just 611 00:29:01,716 --> 00:29:02,946 because it's a little small that's what 612 00:29:02,946 --> 00:29:04,606 that puzzle piece looks like and notice too, 613 00:29:04,606 --> 00:29:06,016 and this is the magic of it, 614 00:29:06,336 --> 00:29:08,256 you bring it close enough, it snaps together. 615 00:29:08,256 --> 00:29:09,956 Right, it's a lot easier 616 00:29:09,956 --> 00:29:12,816 than remembering alright I put the parenthesis 617 00:29:12,816 --> 00:29:14,636 and then the semicolon and then hit enter. 618 00:29:14,976 --> 00:29:15,906 Well you just drag and drop 619 00:29:15,906 --> 00:29:17,596 and these things will fit together logically 620 00:29:17,596 --> 00:29:18,466 if that makes since. 621 00:29:18,516 --> 00:29:22,156 Programmatically I'm gonna click the green flag, wow, 622 00:29:22,536 --> 00:29:28,636 alright so I just wrote what was this a moment ago. 623 00:29:28,636 --> 00:29:31,996 Not so bad but not that interesting, in fairness. 624 00:29:32,236 --> 00:29:34,116 So let's do something a little more interesting. 625 00:29:34,116 --> 00:29:37,136 So let me say something like this, what is your name? 626 00:29:37,946 --> 00:29:41,166 So now I need a way of actually getting input 627 00:29:41,366 --> 00:29:43,286 so how can I do this? 628 00:29:43,286 --> 00:29:45,806 Well under sensing here there's this block, 629 00:29:46,056 --> 00:29:48,206 now ask what's your name and weight? 630 00:29:48,206 --> 00:29:50,526 Well let me try and do this, what's your name, oh wait, 631 00:29:50,526 --> 00:29:52,236 this is already a book, right? 632 00:29:52,276 --> 00:29:55,486 So I've kind of gotten ahead of myself so let's do this, hello, 633 00:29:55,486 --> 00:29:58,616 we'll say that for 2 seconds, then ask this and then wait. 634 00:29:58,616 --> 00:30:02,816 Well let's see, hello, what's your name? 635 00:30:03,036 --> 00:30:04,106 Alright, David. 636 00:30:04,106 --> 00:30:07,516 [laughter] Kind of stupid program, right? 637 00:30:07,516 --> 00:30:10,746 I've asked for my name but I'm not doing anything yet. 638 00:30:10,746 --> 00:30:13,946 So let's do something a little cute perhaps so looks, 639 00:30:13,946 --> 00:30:16,756 how about, hmm, so there's the saved block, 640 00:30:16,756 --> 00:30:17,926 there's this think block. 641 00:30:17,926 --> 00:30:18,976 Let me plug this in. 642 00:30:19,196 --> 00:30:23,356 Now I don't want it to say, well okay we'll let it say hmm 643 00:30:23,356 --> 00:30:25,406 but then I'm going to be a little cute 644 00:30:25,856 --> 00:30:36,006 and say something like, let's say doesn't ring a bell 645 00:30:36,616 --> 00:30:38,216 and actually hmm is a little generic 646 00:30:38,216 --> 00:30:40,246 so let's do something even more interesting still. 647 00:30:40,246 --> 00:30:42,436 Under sensing, notice that there was this answer here 648 00:30:42,636 --> 00:30:44,036 and if I had actually gotten some help 649 00:30:44,036 --> 00:30:46,586 about this block I wouldn't have gotten a little cheat sheet. 650 00:30:46,586 --> 00:30:47,306 What does this do? 651 00:30:47,306 --> 00:30:49,546 So this programming environment, it's actually very easy 652 00:30:49,546 --> 00:30:50,956 to bootstrap yourself cause all 653 00:30:50,956 --> 00:30:52,666 of the help is right there for you. 654 00:30:52,876 --> 00:30:54,636 I'm actually gonna drag this and then notice 655 00:30:54,636 --> 00:30:57,226 that the hmm doesn't have to be me manually type. 656 00:30:57,226 --> 00:31:00,136 I can actually plug in this variable so now I go ahead 657 00:31:00,136 --> 00:31:04,536 and say this, hello, what's your name? 658 00:31:04,536 --> 00:31:08,406 Alright, David, hmm, and that's a little cartoon bubble there, 659 00:31:09,126 --> 00:31:09,836 doesn't ring a bell. 660 00:31:09,836 --> 00:31:12,126 Alright so baby steps to be sure 661 00:31:12,126 --> 00:31:13,486 and still not all that glamorous. 662 00:31:13,526 --> 00:31:14,866 Probably you're not tapping your roommate 663 00:31:14,866 --> 00:31:15,836 on the shoulder just yet. 664 00:31:16,256 --> 00:31:18,236 So what more can we do with this? 665 00:31:18,236 --> 00:31:21,816 Well let's see if we can't take things progressively higher 666 00:31:21,816 --> 00:31:22,126 and higher. 667 00:31:22,126 --> 00:31:23,386 And what you'll see is even though a lot 668 00:31:23,386 --> 00:31:25,726 of these initial demonstrations are terribly simply, 669 00:31:25,726 --> 00:31:28,026 that's what programming ultimately is about. 670 00:31:28,026 --> 00:31:30,936 It's taking some very basic building blocks and building 671 00:31:31,076 --> 00:31:34,296 on top, on top, on top of them, kind of combining them 672 00:31:34,296 --> 00:31:36,996 into things we'll start calling functions and libraries 673 00:31:37,256 --> 00:31:37,806 so that you don't have 674 00:31:37,806 --> 00:31:40,286 to reinvent these uninteresting wheels all the time 675 00:31:40,516 --> 00:31:43,606 until finally do you get much more interesting software. 676 00:31:43,606 --> 00:31:46,626 Case in point, things like Shuttleboy that we don't have 677 00:31:46,736 --> 00:31:49,346 to worry so much about these minutia before long. 678 00:31:49,346 --> 00:31:50,696 So there are these things called statements 679 00:31:50,696 --> 00:31:52,056 in programming and in Scratch. 680 00:31:52,366 --> 00:31:55,426 Anything that is a block that's sort of imperative, say this, 681 00:31:55,506 --> 00:31:57,196 wait for this amount of time, play sound, 682 00:31:57,426 --> 00:31:59,906 we'll call these statements and these are just a few examples. 683 00:32:00,196 --> 00:32:02,016 So here are two programs. 684 00:32:02,286 --> 00:32:05,126 The left one we can figure out intuitively 685 00:32:05,126 --> 00:32:06,346 so one green flag clicked, 686 00:32:06,636 --> 00:32:09,626 that example is gonna say oh hi world, for one second, 687 00:32:09,656 --> 00:32:10,446 then it's gonna pause. 688 00:32:10,676 --> 00:32:12,806 It's gonna say it again, then it's gonna pause. 689 00:32:12,916 --> 00:32:14,036 And then it's gonna say it again, 690 00:32:14,536 --> 00:32:16,126 right, rather underwhelming. 691 00:32:16,126 --> 00:32:18,886 Let's go ahead and pull up the third version of this though 692 00:32:18,886 --> 00:32:21,236 and all of these examples you can download 693 00:32:21,236 --> 00:32:23,936 from the courses website, let me go ahead and pull up hi3.sb, 694 00:32:23,936 --> 00:32:28,016 double clicking suffices, that's the program. 695 00:32:28,016 --> 00:32:30,156 Notice there's really not much complexity here. 696 00:32:30,156 --> 00:32:31,126 I still have just one cat 697 00:32:31,576 --> 00:32:33,526 but let me go ahead and click play now. 698 00:32:37,846 --> 00:32:42,636 [noise] Okay still maybe not roommate worthy but let's see 699 00:32:42,846 --> 00:32:44,036 where else we can go with this. 700 00:32:44,146 --> 00:32:46,346 Alright so there's Boolean expressions 701 00:32:46,346 --> 00:32:47,786 in program languages. 702 00:32:47,786 --> 00:32:50,866 A Boolean expression, named after a Mr. Bool, 703 00:32:51,066 --> 00:32:54,776 is an expression that in the end is either true or it's false. 704 00:32:54,776 --> 00:32:56,786 It's either 1 or it's zero. 705 00:32:56,786 --> 00:32:57,986 It's yes. It's no. 706 00:32:57,986 --> 00:32:58,886 These two dichotomies. 707 00:32:58,886 --> 00:33:01,056 It doesn't matter what label you slap on them 708 00:33:01,056 --> 00:33:02,436 but it's this notion of true and false. 709 00:33:02,646 --> 00:33:05,336 So what kinds of questions might return yes no answers, 710 00:33:05,336 --> 00:33:06,226 true false answers? 711 00:33:06,436 --> 00:33:08,996 Well is the cat touching the mouse pointer? 712 00:33:09,256 --> 00:33:11,016 In other words is my mouse hovering over him 713 00:33:11,016 --> 00:33:13,636 because if he is that could be an interesting mode 714 00:33:13,636 --> 00:33:14,366 of interaction. 715 00:33:14,646 --> 00:33:15,896 Is the mouse button down? 716 00:33:16,186 --> 00:33:17,936 That too is kind of an interesting question to ask 717 00:33:18,126 --> 00:33:20,646 because if so maybe I'll do something interesting. 718 00:33:20,956 --> 00:33:22,666 Is something less than something? 719 00:33:22,816 --> 00:33:27,156 Well sometimes you're supposed to do numeric manipulations, 720 00:33:27,376 --> 00:33:28,716 even in a context like this. 721 00:33:28,716 --> 00:33:31,766 And then there's this, this Boolean and operator. 722 00:33:31,766 --> 00:33:34,076 If any of you are particularly savvy with Google 723 00:33:34,076 --> 00:33:37,546 or search engines, sometimes you might realize that you can type, 724 00:33:37,786 --> 00:33:41,916 say, David and Harvard into Google search box or something 725 00:33:41,916 --> 00:33:46,016 like that, to get results that actually relate not to David 726 00:33:46,016 --> 00:33:48,416 or Harvard but both of them, in fact. 727 00:33:48,596 --> 00:33:52,476 And so we can merge Boolean expressions into singular units. 728 00:33:52,966 --> 00:33:54,196 Why is that interesting? 729 00:33:54,196 --> 00:33:57,446 Well if we can ask these yes no questions we can do things 730 00:33:57,806 --> 00:33:59,376 or not conditionally. 731 00:33:59,376 --> 00:34:02,456 Just like Josh put on the sock only if he found a match, 732 00:34:03,526 --> 00:34:05,196 can we similarly distinguish 733 00:34:05,196 --> 00:34:06,976 between different states of our world here? 734 00:34:06,976 --> 00:34:08,936 So there's this if block in Scratch, 735 00:34:08,936 --> 00:34:11,086 at the far left here so if something. 736 00:34:11,086 --> 00:34:13,456 And notice there's this funky triangular shape here too 737 00:34:13,456 --> 00:34:15,186 so you'll find that there are similarities, 738 00:34:15,186 --> 00:34:17,506 not just with color in this program but also 739 00:34:17,506 --> 00:34:18,476 in the shape of the block. 740 00:34:18,476 --> 00:34:20,876 So just as a visual queue, what can you put 741 00:34:20,876 --> 00:34:21,966 into that place holder? 742 00:34:22,256 --> 00:34:23,746 But it's not just interesting to say, 743 00:34:23,746 --> 00:34:26,616 if something's true do this, sometimes you want it 744 00:34:26,616 --> 00:34:28,906 to be exclusive, do this, else do that 745 00:34:29,106 --> 00:34:31,286 and so there's this if else construct. 746 00:34:31,376 --> 00:34:33,436 Now sometimes you might want to do this, 747 00:34:33,586 --> 00:34:34,836 that or the other thing. 748 00:34:35,296 --> 00:34:38,086 Well these things can nest so even though it doesn't look 749 00:34:38,086 --> 00:34:41,856 like you can cram another block inside that middle one, else, 750 00:34:41,856 --> 00:34:43,236 I mean it's only this big. 751 00:34:43,236 --> 00:34:44,876 How in the world do we fit anything in there? 752 00:34:45,096 --> 00:34:45,946 Well what you'll find is 753 00:34:45,946 --> 00:34:48,036 that Scratch automatically adapts itself. 754 00:34:48,036 --> 00:34:50,336 It just grows the size of the block if you want 755 00:34:50,336 --> 00:34:52,006 to squeeze more blocks into it. 756 00:34:52,236 --> 00:34:54,866 So now we have the ability to do things conditionally. 757 00:34:55,106 --> 00:34:56,286 Well why is that helpful? 758 00:34:56,286 --> 00:34:58,856 Well let's take a look at iteration 4 of this. 759 00:34:59,066 --> 00:35:01,116 So I'm gonna go open hi4.sb. 760 00:35:01,116 --> 00:35:06,516 It's not a very interesting program yet so let me hit play. 761 00:35:06,516 --> 00:35:10,006 [noise] I'm gonna hit it again. 762 00:35:12,046 --> 00:35:14,716 [noise] This is sort of vacuous truth, right? 763 00:35:14,716 --> 00:35:18,006 This is not [background noise] a very interesting use 764 00:35:18,676 --> 00:35:22,116 of a condition because one's value is presumably never 765 00:35:22,116 --> 00:35:24,416 changing so let's hope I did something a little smarter 766 00:35:24,416 --> 00:35:25,556 with version 5 here. 767 00:35:25,806 --> 00:35:27,686 This is version 5 of this same idea. 768 00:35:28,076 --> 00:35:32,766 Huh, so now the fact that we can plug in values from elsewhere, 769 00:35:32,766 --> 00:35:34,266 this gets a little more interesting. 770 00:35:34,266 --> 00:35:36,036 So this block here let me zoom in, 771 00:35:36,366 --> 00:35:39,586 is what we'll call a sudo random number generator. 772 00:35:39,876 --> 00:35:43,236 Now to the target audience of Scratch, which is usually people 773 00:35:43,236 --> 00:35:45,506 about yay high so 10 year olds, 12 year olds, 774 00:35:45,876 --> 00:35:47,856 MIT teachers don't whip out terms like, 775 00:35:48,136 --> 00:35:51,526 now use a sudo random number generator to make your cat move 776 00:35:51,566 --> 00:35:53,806 but that's what we're gonna call it, right? 777 00:35:53,986 --> 00:35:58,716 We're [laughter] in this case going to pick a random number. 778 00:35:58,716 --> 00:36:01,606 How that gets picked is actually an interesting question 779 00:36:01,606 --> 00:36:02,416 unto itself. 780 00:36:02,416 --> 00:36:04,996 And those of you who like mathematics, like theory, 781 00:36:04,996 --> 00:36:08,246 I mean there's a whole world of discovery and a whole world 782 00:36:08,246 --> 00:36:09,686 of research that asks the question, 783 00:36:09,916 --> 00:36:13,576 how do you make machines, which are by nature deterministic? 784 00:36:13,856 --> 00:36:15,566 They only do what you tell them. 785 00:36:15,876 --> 00:36:18,846 How do you make a machine like that pick a random number? 786 00:36:19,156 --> 00:36:20,506 It would seem to be the case that you have 787 00:36:20,546 --> 00:36:23,566 to tell the machine what number to pick but that would sort 788 00:36:23,566 --> 00:36:25,906 of defeat the point of having randomness at all. 789 00:36:26,106 --> 00:36:28,206 But in fact there are ways of doing this or there are ways 790 00:36:28,206 --> 00:36:31,246 of simulating this and as an aside you can kind of cheat. 791 00:36:31,486 --> 00:36:33,616 For instance, if your computer has a microphone, 792 00:36:33,876 --> 00:36:35,256 well odds are you're not going to be 793 00:36:35,256 --> 00:36:36,886 in a perfectly quiet room all the time 794 00:36:36,886 --> 00:36:40,146 so maybe you get some randomness, some random noise, 795 00:36:40,426 --> 00:36:41,896 by way of turning on your microphone 796 00:36:41,896 --> 00:36:44,446 so there's some juicy areas of discovery there. 797 00:36:44,626 --> 00:36:46,436 But for us, we're just gonna use it mechanically. 798 00:36:46,436 --> 00:36:49,786 Pick a random number from 1 to 10, this has the effect, 799 00:36:50,496 --> 00:36:55,676 if all goes according to plan, of the following, is that a bug? 800 00:36:55,676 --> 00:36:57,366 I click, nothing happens. 801 00:36:58,376 --> 00:37:01,496 It's kind of to be expected I think, right? 802 00:37:01,496 --> 00:37:03,926 [noise] Okay that time it worked. 803 00:37:03,976 --> 00:37:06,356 It didn't. 804 00:37:07,036 --> 00:37:07,336 [noise] Did. 805 00:37:08,346 --> 00:37:13,116 Didn't, [noise] did, now as you may recall, 806 00:37:13,116 --> 00:37:15,396 if we do this an infinite number of times, 807 00:37:15,606 --> 00:37:18,186 50 percent of them will in fact meow, 50 percent of them won't. 808 00:37:18,266 --> 00:37:19,466 It's actually a good statistic 809 00:37:19,466 --> 00:37:21,646 so last year I kept hitting the button 810 00:37:21,646 --> 00:37:23,666 and the damn thing kept meowing, meowing, meowing 811 00:37:23,836 --> 00:37:26,446 but that actually speaks to the fact that it is in fact random, 812 00:37:26,446 --> 00:37:27,496 at least in some part. 813 00:37:27,726 --> 00:37:28,896 So who cares? 814 00:37:28,896 --> 00:37:29,916 What can we do with this? 815 00:37:30,116 --> 00:37:31,876 Well let's see what else we can build on. 816 00:37:32,016 --> 00:37:33,996 So there's a way of doing things multiple times. 817 00:37:33,996 --> 00:37:37,126 Now in one of my first programs I had the cat meow 3 times. 818 00:37:37,506 --> 00:37:41,476 It said play sound, meow, wait a second, play it, wait a second, 819 00:37:41,566 --> 00:37:42,886 play it, wait a second. 820 00:37:43,126 --> 00:37:46,426 Well that's kind of a nice capability if you can tell it 821 00:37:46,426 --> 00:37:47,956 to do something multiple times. 822 00:37:47,956 --> 00:37:50,156 Let me go ahead and open up this thing here. 823 00:37:50,156 --> 00:37:52,386 And again this is what we just glanced at at the slide. 824 00:37:53,596 --> 00:37:56,046 It's rather underwhelming and in fact, 825 00:37:56,906 --> 00:37:58,536 it's perhaps more interesting if we 826 00:37:58,536 --> 00:38:00,596 at least make it slightly annoying 827 00:38:01,076 --> 00:38:02,836 by doing it this way with the sound. 828 00:38:06,376 --> 00:38:08,046 [noise] And in fact I'll indulge 829 00:38:08,046 --> 00:38:10,876 in what you inevitably will find a fun way of procrastinating, 830 00:38:10,876 --> 00:38:13,096 it turns out we don't have to use just the meow. 831 00:38:13,096 --> 00:38:14,686 We can click things like import. 832 00:38:14,926 --> 00:38:16,396 You'll find that this environment comes 833 00:38:16,396 --> 00:38:17,676 with all these various effects 834 00:38:17,956 --> 00:38:20,286 so maybe it would be more fun to do something like. 835 00:38:20,496 --> 00:38:27,936 [noise and laughter] Okay and there's the cat. 836 00:38:28,886 --> 00:38:30,206 There's other things too. 837 00:38:30,206 --> 00:38:34,036 I think if we go into vocals we've got this one. 838 00:38:34,256 --> 00:38:38,966 [noise] So that's not that bad in terms of annoying. 839 00:38:39,616 --> 00:38:42,776 [laughter] So let me do this again, and please stop me, 840 00:38:42,946 --> 00:38:44,316 we can't go on this tangent too long. 841 00:38:44,566 --> 00:38:46,096 So there's three tabs up there and again, 842 00:38:46,096 --> 00:38:48,046 this is not interesting stuff intellectually 843 00:38:48,046 --> 00:38:50,116 so we'll wave our hands at it largely today but I'm going 844 00:38:50,116 --> 00:38:52,686 to go back to my scripts now and now notice, to change the sound, 845 00:38:52,946 --> 00:38:55,126 alright so I'll do this, this, this. 846 00:38:55,436 --> 00:38:58,436 But the point I wanted to make is what do I do if I want 847 00:38:58,436 --> 00:39:01,116 to do not 3 times but 6 times? 848 00:39:01,316 --> 00:39:02,736 Well you can duplicate things. 849 00:39:03,116 --> 00:39:05,346 So actually let me go ahead and duplicate it from here 850 00:39:05,346 --> 00:39:07,566 and I'm just right clicking or control clicking. 851 00:39:07,566 --> 00:39:09,616 Let me get this stuff out of the way there. 852 00:39:09,616 --> 00:39:16,056 And yep, so now I have it going 1, 2, 3, 4, 5 times 853 00:39:16,056 --> 00:39:19,326 and actually let me detach this, 6, 7 times. 854 00:39:19,326 --> 00:39:20,566 I'm gonna drop this over there 855 00:39:20,566 --> 00:39:22,946 and let's just see how annoying this gets. 856 00:39:23,516 --> 00:39:30,546 [noise] 857 00:39:31,046 --> 00:39:37,176 Okay so the point is now not to annoy but to demonstrate 858 00:39:37,176 --> 00:39:39,896 that this is not the smartest approach to telling the computer 859 00:39:39,896 --> 00:39:41,266 to do something again and again. 860 00:39:41,436 --> 00:39:45,416 If you find yourself, whether in C, Scratch, Java Script, PHP, 861 00:39:45,416 --> 00:39:47,496 just kind of copying and pasting to get your program 862 00:39:47,496 --> 00:39:49,876 to do something multiple times, multiple times, 863 00:39:49,876 --> 00:39:52,626 which may be compelling, there's got to be another way 864 00:39:52,626 --> 00:39:53,466 and in fact there are. 865 00:39:53,466 --> 00:39:55,856 There are these constructs, as we saw with Josh's example, 866 00:39:55,916 --> 00:39:59,236 of looping in Scratch and in C and in Java Script 867 00:39:59,236 --> 00:40:00,756 and in a lot of modern languages. 868 00:40:00,996 --> 00:40:03,866 These things are called wild loops or for a loop. 869 00:40:04,066 --> 00:40:05,726 In Scratch they're called forever loops 870 00:40:05,846 --> 00:40:07,906 or repeat some number of times. 871 00:40:08,136 --> 00:40:10,776 So already can you probably imagine how you might 872 00:40:10,776 --> 00:40:11,646 integrate these. 873 00:40:11,826 --> 00:40:13,236 Well what about something like this? 874 00:40:13,236 --> 00:40:15,346 If we wanted to create that very simple effect 875 00:40:15,556 --> 00:40:19,956 of doing something ad nauseum, if we pull up version 6 of this, 876 00:40:20,296 --> 00:40:23,356 notice that the blocks I've chosen are one green flag click. 877 00:40:23,626 --> 00:40:25,166 I then grab that forever block. 878 00:40:25,406 --> 00:40:29,196 And to be clear, the forever block is not normally that tall 879 00:40:29,196 --> 00:40:31,576 so notice the mere act of dragging something 880 00:40:31,576 --> 00:40:34,736 like this inside the forever loop makes it bigger. 881 00:40:34,736 --> 00:40:36,936 And then if I want to squeeze this in it may trune [phonetic] 882 00:40:36,936 --> 00:40:38,956 but again everything is similarly shaped. 883 00:40:39,106 --> 00:40:40,376 Now I'm gonna have the same effect, 884 00:40:40,746 --> 00:40:41,746 slightly less annoying [noise] 885 00:40:42,936 --> 00:40:45,966 and now [background noise] I've arguably implemented this 886 00:40:45,966 --> 00:40:47,706 with a much better design. 887 00:40:47,706 --> 00:40:51,276 There was no reason for the copy paste and in fact if you sort 888 00:40:51,276 --> 00:40:54,336 of think about where that bad habit leads you, 889 00:40:54,336 --> 00:40:56,016 if you're copying and pasting constantly, 890 00:40:56,216 --> 00:40:58,386 imagine how much work it would be for me to go back 891 00:40:58,726 --> 00:41:00,656 into the previous version of that program 892 00:41:01,066 --> 00:41:03,546 and actually change something 893 00:41:03,546 --> 00:41:05,036 like the sound that's being played. 894 00:41:05,036 --> 00:41:08,256 I mean my God I have to like change it in 6 or 7 drop downs, 895 00:41:08,566 --> 00:41:11,376 here I just change it in 1 place. 896 00:41:11,506 --> 00:41:13,586 So we already have this ability to do things 897 00:41:13,586 --> 00:41:16,076 that are a little better designed. 898 00:41:16,076 --> 00:41:17,656 Well what about this thing at top right? 899 00:41:18,366 --> 00:41:20,796 So top right is saying when green flag is clicked, 900 00:41:21,066 --> 00:41:22,216 do the following forever. 901 00:41:22,216 --> 00:41:22,976 Well what? 902 00:41:22,976 --> 00:41:25,376 If touching mouse pointer, play sound meow 903 00:41:25,576 --> 00:41:27,816 and wait two seconds and then repeat. 904 00:41:27,866 --> 00:41:28,886 Alright so let's open this. 905 00:41:28,946 --> 00:41:30,046 This is version 7. 906 00:41:30,046 --> 00:41:33,896 I'm gonna go ahead and open this. 907 00:41:33,896 --> 00:41:35,436 If you wondered how I was doing it before, 908 00:41:35,436 --> 00:41:37,296 at the top right there's presentation mode 909 00:41:37,456 --> 00:41:39,246 where you can zoom in and make it even bigger. 910 00:41:39,726 --> 00:41:40,746 So now I hit play. 911 00:41:41,526 --> 00:41:43,646 Nothing seems to be happening but that's consistent 912 00:41:43,646 --> 00:41:47,006 with the previous slide because I didn't use the save block, 913 00:41:47,006 --> 00:41:49,216 I didn't use the think block but notice my mouse, 914 00:41:49,216 --> 00:41:50,926 if I move it closer and closer to the cat [noise], 915 00:41:50,926 --> 00:41:56,536 it's kind of like petting the cat, if you will. 916 00:41:57,306 --> 00:41:58,406 Clever? I thought so. 917 00:41:59,296 --> 00:42:02,616 Alright so kind of interesting. 918 00:42:02,616 --> 00:42:04,386 Let's take things slightly higher. 919 00:42:04,386 --> 00:42:05,656 So that's version 7. 920 00:42:05,656 --> 00:42:07,676 Let me go to version 8 here. 921 00:42:09,096 --> 00:42:11,116 Version 8 is not that much bigger 922 00:42:11,116 --> 00:42:12,926 but now notice, what's gonna happen? 923 00:42:13,046 --> 00:42:14,676 You can kind of see this one coming. 924 00:42:16,926 --> 00:42:17,146 [inaudible audience comment] Alright 925 00:42:17,146 --> 00:42:19,326 so this was me thinking this was really clever. 926 00:42:20,416 --> 00:42:22,366 Alright so let me go ahead and full screen it, 927 00:42:22,616 --> 00:42:25,366 click the green flag and now notice [background noise] he's 928 00:42:25,366 --> 00:42:28,316 just going about his business meowing, okay that's all good. 929 00:42:28,596 --> 00:42:31,956 If I now try to touch him, [noise] bad things happen 930 00:42:32,646 --> 00:42:35,286 and then he'll go back ultimately to meowing. 931 00:42:35,286 --> 00:42:37,066 Okay so now we have this ability 932 00:42:37,066 --> 00:42:39,726 to do things conditionally inside of loops. 933 00:42:39,926 --> 00:42:44,066 So already we seem to have a fairly rich amount 934 00:42:44,066 --> 00:42:45,136 of expressiveness. 935 00:42:45,316 --> 00:42:48,296 We can kind of think about doing something, like I did last night 936 00:42:48,326 --> 00:42:49,206 when putting that together. 937 00:42:49,206 --> 00:42:52,076 I wanted the cat to just meow, meow, meow and then I wanted 938 00:42:52,076 --> 00:42:54,726 to do something optionally so there you have loop. 939 00:42:54,726 --> 00:42:56,406 You have condition sandwiched in between. 940 00:42:56,406 --> 00:42:57,666 Think about what statements you want. 941 00:42:57,856 --> 00:42:59,136 And wala, we have that effect. 942 00:42:59,676 --> 00:43:00,956 Well what more can we do? 943 00:43:00,956 --> 00:43:02,016 Well we have variables. 944 00:43:02,016 --> 00:43:05,496 Now variables are useful, certainly in the context of Josh 945 00:43:05,496 --> 00:43:07,016 where he had to maintain state. 946 00:43:07,016 --> 00:43:09,486 He had to remember how many socks he had on. 947 00:43:09,736 --> 00:43:12,056 Otherwise he would just keep putting on socks, 948 00:43:12,056 --> 00:43:13,626 putting on socks, putting on socks. 949 00:43:13,896 --> 00:43:17,486 The use of that variable allowed him to short circuit that loop. 950 00:43:17,686 --> 00:43:20,506 And terminate as soon as he reached some happy place. 951 00:43:20,766 --> 00:43:22,586 Well what about these two programs here? 952 00:43:22,886 --> 00:43:26,956 Count 1.sb is at left so when green flag is clicked set a 953 00:43:27,136 --> 00:43:29,586 counter, that's apparently a variable that I chose. 954 00:43:29,986 --> 00:43:32,286 Better named than X. Better named than Y, 955 00:43:32,286 --> 00:43:34,606 which though you might have used in algebra, really isn't 956 00:43:34,606 --> 00:43:35,466 that useful when you're trying 957 00:43:35,466 --> 00:43:37,116 to remember what your own program does 958 00:43:37,116 --> 00:43:39,876 so again a good matter of style is pick variable names 959 00:43:39,876 --> 00:43:41,496 that say something interesting. 960 00:43:41,796 --> 00:43:45,946 So say counter to zero, forever, say counter for 2 seconds. 961 00:43:46,226 --> 00:43:48,666 Then wait a second, change counter by 1. 962 00:43:48,836 --> 00:43:51,076 So this is Scratch's approach to plus plus. 963 00:43:51,266 --> 00:43:56,186 Alright so in count 1.sb we have the following, 964 00:43:56,606 --> 00:44:06,006 count 1 [silence] no sound but I thought it was very clever too 965 00:44:06,006 --> 00:44:07,716 that the sheep was doing the counting. 966 00:44:08,806 --> 00:44:10,396 [laughter] Ha, ha, ha, see, geek humor. 967 00:44:10,896 --> 00:44:13,716 Alright so things are more fun with sound so we'll kill 968 00:44:13,716 --> 00:44:16,476 that thing but it's going to go on up at least 969 00:44:16,476 --> 00:44:18,236 until we hit some upper bounds. 970 00:44:18,266 --> 00:44:21,006 Can you anticipate maybe if you're coming at this 971 00:44:21,006 --> 00:44:22,586 with a background in some programming, 972 00:44:22,586 --> 00:44:28,576 what could still happen that's bad here? 973 00:44:28,576 --> 00:44:28,856 [inaudible audience comment] Sorry, 974 00:44:28,856 --> 00:44:30,636 might only be using 8 bits. 975 00:44:30,636 --> 00:44:33,366 So actually roll back mentally to Wednesday when we talked 976 00:44:33,366 --> 00:44:34,656 about representing numbers 977 00:44:34,896 --> 00:44:37,866 and by default I kept using 8 bits to represent numbers. 978 00:44:38,006 --> 00:44:41,626 Well if you have 8 bits, each one can be a zero or a 1, 979 00:44:41,896 --> 00:44:45,386 that means you have two possible values times 2 here, 2, 2, 2, 2, 980 00:44:45,386 --> 00:44:47,166 2, so that's 2 to the eighth, 981 00:44:47,376 --> 00:44:52,466 right so that meant we had 256 possible values you can 982 00:44:52,466 --> 00:44:53,636 represent with 8 bits. 983 00:44:53,906 --> 00:44:57,396 The number zero then 1 then 2 then 3 then 4 then 5, 984 00:44:57,396 --> 00:44:59,166 all the way up to 255. 985 00:44:59,356 --> 00:45:01,566 And again, why 255, not 256? 986 00:45:01,706 --> 00:45:03,706 Computer scientists tend to start counting at zero 987 00:45:03,706 --> 00:45:05,626 so we spend one of our encodings that way. 988 00:45:05,906 --> 00:45:10,216 So a problem that might arise here is MIT probably made a 989 00:45:10,216 --> 00:45:12,326 judgment call somewhere underneath the hood 990 00:45:12,326 --> 00:45:15,156 when they implemented Scratch itself where they decided, 991 00:45:15,156 --> 00:45:18,476 you know what, no 12 year old is gonna count up to 4 billion. 992 00:45:18,476 --> 00:45:20,716 We don't need 32 bit integers. 993 00:45:20,906 --> 00:45:23,106 We don't need integers that are represented 994 00:45:23,106 --> 00:45:27,806 by 32 individual bits, maybe just 8 or maybe 16 but they had 995 00:45:27,806 --> 00:45:28,946 to make a judgment call. 996 00:45:29,116 --> 00:45:32,056 And just as, think back to the Y2K problem, 997 00:45:32,056 --> 00:45:33,026 what was that problem? 998 00:45:33,236 --> 00:45:36,176 Well the world had decided, in the 1970's give or take, 999 00:45:36,716 --> 00:45:39,036 you know this codes never going to be used 30 years from now, 1000 00:45:39,036 --> 00:45:41,556 let's just use 2 digits to represent the year 1001 00:45:41,696 --> 00:45:44,366 and then we all, you may recall, bad things happened 1002 00:45:44,366 --> 00:45:46,326 or bad things were predicted to happen. 1003 00:45:46,326 --> 00:45:47,816 Certainly expensive things happen 1004 00:45:48,056 --> 00:45:49,456 when we roll back from that mistake. 1005 00:45:49,456 --> 00:45:52,916 So same thing here, let's not spend the time figuring 1006 00:45:52,916 --> 00:45:55,746 out just how high this sheep can count but that too is going 1007 00:45:55,746 --> 00:45:59,636 to be an issue we trip over when you start coding in C and in PHP 1008 00:45:59,636 --> 00:46:02,166 and in Java Script where you have much more control 1009 00:46:02,166 --> 00:46:05,536 over the representation of numbers and of data. 1010 00:46:05,806 --> 00:46:08,316 What about count 2.sb here? 1011 00:46:08,636 --> 00:46:12,076 So when green flag clicked, set number to a random number, 1012 00:46:12,566 --> 00:46:14,806 say the number and then if that number is less 1013 00:46:14,896 --> 00:46:16,296 than 6, play sheep. 1014 00:46:16,586 --> 00:46:19,926 So in English, in laymen's terms, this thing is going 1015 00:46:19,926 --> 00:46:21,536 to something 50 percent of the time 1016 00:46:21,776 --> 00:46:23,726 but at least now we're going to have some introspection, 1017 00:46:23,726 --> 00:46:27,426 we're gonna see why it's actually playing a sound or not 1018 00:46:27,656 --> 00:46:29,446 because it's going to tell me what the number is, 1019 00:46:29,446 --> 00:46:31,846 whereas before I simply had to guess. 1020 00:46:31,846 --> 00:46:36,206 So this is count 2, still involving our little sheep here. 1021 00:46:36,206 --> 00:46:42,076 If I hit play 6, nothing happens because 6 is not less than 6 . 1022 00:46:42,216 --> 00:46:46,596 Play 10, still nothing, 7, still nothing, 1023 00:46:47,596 --> 00:46:50,726 see this is where we could be tossing a coin 1024 00:46:50,726 --> 00:46:52,516 and it might be coming up heads that many times, 1025 00:46:52,516 --> 00:46:58,256 hopefully it will go [noise] the other direction 50 percent 1026 00:46:58,256 --> 00:46:58,666 of the time. 1027 00:46:58,666 --> 00:46:59,916 So if do that an infinite number 1028 00:46:59,916 --> 00:47:02,966 of times hopefully the math would in fact work out. 1029 00:47:03,146 --> 00:47:04,376 Alright so those are variables. 1030 00:47:04,766 --> 00:47:07,896 Well variables are useful but only when you want 1031 00:47:07,896 --> 00:47:09,666 to store one piece of information. 1032 00:47:10,046 --> 00:47:12,536 But you could just use then multiple variables 1033 00:47:12,536 --> 00:47:14,496 to store multiple pieces of information. 1034 00:47:14,756 --> 00:47:16,796 But if you think now even in terms of Scratch 1035 00:47:17,036 --> 00:47:19,206 that each variable right now is its own block. 1036 00:47:19,516 --> 00:47:24,506 Imagine how you might implement, maybe a game like some kind 1037 00:47:24,506 --> 00:47:26,676 of RPG game where you're a character, 1038 00:47:26,676 --> 00:47:28,656 ala Super Mario Brothers and you have to go 1039 00:47:28,656 --> 00:47:29,796 around collecting things. 1040 00:47:29,796 --> 00:47:30,686 Any game like Zelda. 1041 00:47:30,756 --> 00:47:33,886 Any game where you go, or even Halo. 1042 00:47:33,886 --> 00:47:36,116 Anytime you pick up something and have to add it 1043 00:47:36,116 --> 00:47:39,286 to your inventory, it would probably be a fairly 1044 00:47:39,286 --> 00:47:42,906 unmanageable program if you had to have a separate variable, 1045 00:47:42,906 --> 00:47:46,606 a separately named variable for every possible possession 1046 00:47:46,846 --> 00:47:50,006 that someone in a game might need to carry around. 1047 00:47:50,006 --> 00:47:52,986 It would be a lot easier if you could sort of have just a bucket 1048 00:47:53,156 --> 00:47:55,966 or a satchel, whatever the analog is in the actual game 1049 00:47:56,146 --> 00:47:58,496 that can contain zero or more items. 1050 00:47:58,926 --> 00:48:01,346 And that data structure, as we'll start calling it 1051 00:48:01,346 --> 00:48:03,316 in programming, is what's called an array, 1052 00:48:03,676 --> 00:48:07,686 sometimes called a vector, also we'll see fancier versions 1053 00:48:07,686 --> 00:48:10,306 of this ultimately called lists and trees but more 1054 00:48:10,306 --> 00:48:11,216 on that in the time to come. 1055 00:48:11,506 --> 00:48:14,796 So an array in Scratch gives us this capability. 1056 00:48:14,796 --> 00:48:16,286 We give the array a name. 1057 00:48:16,966 --> 00:48:19,786 It's called a list in the context of Scratch. 1058 00:48:19,876 --> 00:48:21,606 And then we can use this block at top. 1059 00:48:21,736 --> 00:48:23,056 Add something to inventory. 1060 00:48:23,056 --> 00:48:25,146 Delete 1 from inventory. 1061 00:48:25,146 --> 00:48:29,056 Insert thing, a string, at location 1 of inventory. 1062 00:48:29,366 --> 00:48:31,306 In other words, and we won't spend too much time 1063 00:48:31,306 --> 00:48:33,776 on using these now because you might not need them 1064 00:48:33,936 --> 00:48:35,856 but if you find ultimately that you kind of need to keep 1065 00:48:35,856 --> 00:48:37,936 to keep an arbitrary amount of information around, 1066 00:48:38,146 --> 00:48:40,216 well the data structure you use to make that happen 1067 00:48:40,216 --> 00:48:42,626 in general is this thing called an array. 1068 00:48:43,396 --> 00:48:45,086 Well let's see this for real. 1069 00:48:45,086 --> 00:48:46,646 So this is actually a program 1070 00:48:46,926 --> 00:48:49,166 that is a role playing game of sorts. 1071 00:48:49,466 --> 00:48:50,596 It's called Fruit Craft, 1072 00:48:51,176 --> 00:48:53,726 a play on War Craft, if you're familiar. 1073 00:48:53,956 --> 00:48:57,386 Let me go ahead and full screen this one here, click play, 1074 00:48:57,386 --> 00:49:00,006 and we have a little animation it seems. 1075 00:49:00,646 --> 00:49:07,376 Alright you can move me with the arrow keys. 1076 00:49:07,756 --> 00:49:09,986 So just as our little DDR game let me move, 1077 00:49:09,986 --> 00:49:13,696 notice and actually the goal is to look for fruits and so 1078 00:49:13,696 --> 00:49:15,596 like an idiot I kind of did this the first time. 1079 00:49:16,286 --> 00:49:18,836 Apparently this is a picture [laughter] of fruit. 1080 00:49:20,346 --> 00:49:22,486 So there's clearly some bugs if you will 1081 00:49:22,486 --> 00:49:25,746 or at least some disregard for our 3-dimensional world here. 1082 00:49:26,066 --> 00:49:28,246 [laughter] So I'm gonna go around 1083 00:49:28,246 --> 00:49:29,686 or over or through the table. 1084 00:49:29,686 --> 00:49:30,836 I'm gonna follow this arrow. 1085 00:49:30,836 --> 00:49:32,566 I'm just using my arrow keys right now. 1086 00:49:32,956 --> 00:49:34,656 Alright so okay, there we go. 1087 00:49:34,656 --> 00:49:35,886 So now notice the top left. 1088 00:49:36,246 --> 00:49:39,926 Scratch is showing me what is inside my list, aka array, 1089 00:49:40,216 --> 00:49:43,246 now I have cherries and orange, this looks interesting. 1090 00:49:43,246 --> 00:49:44,856 Let me go here. 1091 00:49:44,856 --> 00:49:47,086 Oh okay so we have, remember some sensing. 1092 00:49:47,086 --> 00:49:48,236 So remember just as I'm able 1093 00:49:48,236 --> 00:49:50,476 to sense is the Sprite touching my mouse pointer, 1094 00:49:50,636 --> 00:49:53,486 I can also sense is a Sprite touching another Sprite? 1095 00:49:53,616 --> 00:49:56,766 Is this person touching the sign, if so do something. 1096 00:49:56,926 --> 00:49:59,116 And even though we might not know all the blocks that we need 1097 00:49:59,116 --> 00:50:01,616 to make that happen, it's these basic fundamentals, 1098 00:50:01,616 --> 00:50:05,366 right an if condition that says if touching then say and then 1099 00:50:05,366 --> 00:50:06,766 when not touching don't say. 1100 00:50:06,766 --> 00:50:07,666 So hide that text. 1101 00:50:08,266 --> 00:50:11,526 Alright so let me go and pick up these things, pineapple, 1102 00:50:11,526 --> 00:50:13,766 bananas, another apple, 1103 00:50:13,766 --> 00:50:16,166 hopefully this is the right place, okay, interesting. 1104 00:50:17,386 --> 00:50:17,856 Great job. 1105 00:50:17,886 --> 00:50:21,786 You brought, cherries, orange, bananas, pineapple, apple, 1106 00:50:21,976 --> 00:50:22,916 here's your fruit platter. 1107 00:50:24,056 --> 00:50:29,036 [laughter] Okay so simple but again demonstrates 1108 00:50:29,166 --> 00:50:31,826 where you might, in a fairly compelling context, 1109 00:50:31,926 --> 00:50:35,126 actually need some kind of variable amount of storage. 1110 00:50:35,276 --> 00:50:37,596 Well let's make this more sophisticated still. 1111 00:50:37,796 --> 00:50:40,386 When this is actually a topic that you can't traditionally get 1112 00:50:40,486 --> 00:50:43,886 to in the first week of a course let alone an entire semester 1113 00:50:43,886 --> 00:50:46,476 sometimes, this notion of threading because a lot 1114 00:50:46,476 --> 00:50:50,166 of languages, whether it's C or Java, really don't make it easy 1115 00:50:50,606 --> 00:50:53,336 of what we're about to do and frankly what we've been doing, 1116 00:50:53,646 --> 00:50:56,606 which is having multiple things happen at once. 1117 00:50:57,076 --> 00:50:59,686 So the program I wrote earlier that was very underwhelming, 1118 00:50:59,686 --> 00:51:01,516 hello world, just printing it to the screen? 1119 00:51:01,906 --> 00:51:03,426 That's kind of the only thing it did. 1120 00:51:03,426 --> 00:51:06,356 I could add another print F line, another print F line, 1121 00:51:06,356 --> 00:51:10,376 another print F line but those are all gonna happen in series, 1122 00:51:10,426 --> 00:51:12,066 not in parallel at the same time. 1123 00:51:12,066 --> 00:51:13,866 I'm gonna get one output, one output, 1124 00:51:13,866 --> 00:51:17,826 one output so what's nice about Scratch is 1125 00:51:17,826 --> 00:51:19,346 that you can have the cat moving, 1126 00:51:19,346 --> 00:51:20,876 you can have a sheep doing something, 1127 00:51:20,876 --> 00:51:23,206 you can have this character interacting 1128 00:51:23,206 --> 00:51:24,486 with the guy behind the counter, 1129 00:51:24,626 --> 00:51:26,636 you have a multithreaded environment. 1130 00:51:26,636 --> 00:51:27,706 Well what is a thread? 1131 00:51:27,846 --> 00:51:30,996 A thread is like a miniature program that's running inside 1132 00:51:30,996 --> 00:51:33,046 the context of a larger program 1133 00:51:33,286 --> 00:51:36,366 and they can operate what essentially looks 1134 00:51:36,366 --> 00:51:37,996 like simultaneously. 1135 00:51:38,196 --> 00:51:40,496 Now this is a bit of a white lie because ultimately, 1136 00:51:40,496 --> 00:51:42,106 and we'll talk about this towards the end of the course, 1137 00:51:42,596 --> 00:51:46,046 many computers have just one CPU, central processing unit, 1138 00:51:46,046 --> 00:51:47,276 the brains of the computer. 1139 00:51:47,276 --> 00:51:49,886 When it says Intel inside it means there's an Intel 1140 00:51:49,886 --> 00:51:50,936 CPU inside. 1141 00:51:51,196 --> 00:51:53,836 Well if the computer only has one brain or one CPU, 1142 00:51:53,836 --> 00:51:58,796 the reality is that computer can only do one thing at a time. 1143 00:51:59,246 --> 00:52:01,216 But the neat thing is when you have a gigahertz, 1144 00:52:01,626 --> 00:52:02,906 so in other words a hertz, 1145 00:52:03,186 --> 00:52:05,346 something happening once per second and you can think 1146 00:52:05,346 --> 00:52:07,986 of there being a clock inside of your own CPU 1147 00:52:08,226 --> 00:52:11,026 so if you have a one gigahertz CPU there's a little clock 1148 00:52:11,026 --> 00:52:13,986 or a little crystal that's oscillating a billion times, 1149 00:52:14,086 --> 00:52:17,806 giga meaning billion, a billion times per second. 1150 00:52:18,016 --> 00:52:20,166 Well my God, if you can do something that fast, 1151 00:52:20,436 --> 00:52:23,176 clicking your fingers a billion times per second, 1152 00:52:23,366 --> 00:52:25,886 surely we can dupe humans into thinking 1153 00:52:26,116 --> 00:52:28,726 that two things are happening simultaneously. 1154 00:52:28,986 --> 00:52:31,876 So if you've ever run AOL instant messenger in a window 1155 00:52:32,096 --> 00:52:35,606 and also Microsoft Word and a browser window and any number 1156 00:52:35,606 --> 00:52:37,856 of other programs, you are multitasking. 1157 00:52:38,136 --> 00:52:40,086 But realize that you don't have to quit one 1158 00:52:40,086 --> 00:52:41,666 of those programs to load the other. 1159 00:52:41,666 --> 00:52:43,856 You're running them all simultaneously. 1160 00:52:44,006 --> 00:52:45,286 But what Windows is doing, 1161 00:52:45,286 --> 00:52:48,216 what Mac OS is doing is just giving everyone 1162 00:52:48,216 --> 00:52:51,446 of those programs only a split second of computation 1163 00:52:51,446 --> 00:52:53,486 in the CPU, a split second of attention 1164 00:52:53,696 --> 00:52:55,686 but because it does it here, here, here, here, 1165 00:52:55,686 --> 00:52:58,046 here then here, here, here, here, here where each 1166 00:52:58,046 --> 00:52:59,846 of these things has a different program running 1167 00:52:59,846 --> 00:53:02,996 in your task bar, you the human, being the slowest piece 1168 00:53:02,996 --> 00:53:05,316 of this puzzle, have the illusion of oh, 1169 00:53:05,316 --> 00:53:07,356 I am running multiple programs at once. 1170 00:53:07,596 --> 00:53:10,526 Those of you with iPhones might be somewhat annoyed these days 1171 00:53:10,526 --> 00:53:13,156 that Apple doesn't support background applications, 1172 00:53:13,426 --> 00:53:16,476 at least written by other people besides Apple. 1173 00:53:16,476 --> 00:53:19,846 In other words you can't run one program and then use another 1174 00:53:19,846 --> 00:53:21,866 at the same time, if they're written by people 1175 00:53:21,866 --> 00:53:23,236 in the app store and that's 1176 00:53:23,236 --> 00:53:27,396 because Apple has essentially said users may not run multiple 1177 00:53:27,396 --> 00:53:30,166 threads simultaneously unless we, Apple, 1178 00:53:30,286 --> 00:53:32,006 implemented those threads ourselves, 1179 00:53:32,006 --> 00:53:33,146 unless we wrote the code. 1180 00:53:33,386 --> 00:53:35,886 Now this is partly because the battery life of this thing kind 1181 00:53:35,886 --> 00:53:39,326 of sucks right now and if you allow people to write programs 1182 00:53:39,326 --> 00:53:40,356 that run in the background, 1183 00:53:41,006 --> 00:53:42,856 probably the concern they have is 1184 00:53:42,856 --> 00:53:45,206 that you're gonna drain people's batteries and most people 1185 00:53:45,206 --> 00:53:47,646 out there don't understand what a thread is and they don't care 1186 00:53:47,646 --> 00:53:49,896 and they shouldn't have to care but that's Apple's way, 1187 00:53:49,896 --> 00:53:52,586 probably, at least in part, of dealing with that issue 1188 00:53:52,586 --> 00:53:55,086 of multithreading, being expensive in terms of battery. 1189 00:53:55,356 --> 00:53:56,556 So here is a thread. 1190 00:53:56,776 --> 00:53:59,486 So move 1.sb tells us to do the following, 1191 00:53:59,486 --> 00:54:03,126 when the green flag is clicked, go to xy. 1192 00:54:03,486 --> 00:54:06,336 So it turns out this whole time that stage can be modeled 1193 00:54:06,336 --> 00:54:08,506 and in fancy terms is like a Cartesian plane 1194 00:54:08,506 --> 00:54:10,486 where you have X coordinates and Y, right, 1195 00:54:10,486 --> 00:54:11,556 just like in mathematics. 1196 00:54:11,826 --> 00:54:14,816 So zero zero is pixel location zero zero, 1197 00:54:14,816 --> 00:54:17,546 the little dot somewhere on that stage points 1198 00:54:17,546 --> 00:54:20,266 in the direction 90 degrees so that's either here, here, 1199 00:54:20,266 --> 00:54:22,416 here or here, we'll see once we orient ourselves 1200 00:54:22,416 --> 00:54:24,286 and then forever do the following. 1201 00:54:24,636 --> 00:54:27,156 If touching the edge, play sound, 1202 00:54:27,186 --> 00:54:29,446 this could be interesting, until done. 1203 00:54:29,586 --> 00:54:34,476 Turn 180 degrees, move 5 steps and then do it again and again 1204 00:54:34,476 --> 00:54:37,476 because it's inside the forever loop so this is move 1.sb. 1205 00:54:37,836 --> 00:54:44,496 So let me go down to this, move 1.sb, I'll go ahead 1206 00:54:44,496 --> 00:54:46,976 and full screen it again, hit play. 1207 00:54:47,516 --> 00:54:56,646 [noise] 1208 00:54:57,146 --> 00:54:59,286 [background noise] I don't know if this is 1209 00:54:59,286 --> 00:55:03,036 like a pedagogical technique or just a bad habit but it seems 1210 00:55:03,036 --> 00:55:06,016 if you right program loud and annoying, 1211 00:55:06,016 --> 00:55:07,756 people remember them so be it. 1212 00:55:07,756 --> 00:55:10,026 So this thing, this is the Sprite. 1213 00:55:10,236 --> 00:55:12,286 Notice I've changed his costume, how? 1214 00:55:12,286 --> 00:55:14,646 Well I probably went up here, I clicked costumes, 1215 00:55:14,646 --> 00:55:17,436 I fussed around with paint or importing a JPEG or whatever, 1216 00:55:17,656 --> 00:55:19,356 some uninteresting details intellectually 1217 00:55:19,356 --> 00:55:21,666 but you're nonetheless welcome to play with and procrastinate 1218 00:55:21,666 --> 00:55:24,146 with for project zero but I've made a duck 1219 00:55:24,146 --> 00:55:26,676 that orients himself 90 degrees and he starts moving 1220 00:55:26,916 --> 00:55:29,796 so this notion of walking is really about moving some number 1221 00:55:29,796 --> 00:55:32,066 of steps, some number of pixels and then notice 1222 00:55:32,066 --> 00:55:34,686 because I'm doing this again and again and again forever, 1223 00:55:34,856 --> 00:55:37,136 I'm constantly checking, am I touching the edge, 1224 00:55:37,136 --> 00:55:38,106 am I touching the edge? 1225 00:55:38,106 --> 00:55:42,306 If so, yell, then turn yourself around and do it again. 1226 00:55:42,596 --> 00:55:44,596 So again, not a very interactive program 1227 00:55:44,596 --> 00:55:46,776 but all these simple puzzle pieces, 1228 00:55:46,776 --> 00:55:48,886 all these simple building blocks we've been looking 1229 00:55:49,056 --> 00:55:53,016 at are really what comprised Ann's DDR like game and Andrews, 1230 00:55:53,016 --> 00:55:54,246 Raining Men and some 1231 00:55:54,246 --> 00:55:56,446 of the other more sophisticated programs that you'll write. 1232 00:55:56,666 --> 00:55:59,026 So here's one that actually is multithreaded. 1233 00:55:59,026 --> 00:56:01,246 That was just one duck doing his own thing. 1234 00:56:01,246 --> 00:56:04,946 Now we'll see a bird and a cat, each of which is a Sprite 1235 00:56:05,176 --> 00:56:07,816 in Scratch speak and each of which has its own script. 1236 00:56:08,096 --> 00:56:11,086 So notice in this program move 2.sp, I'm actually going 1237 00:56:11,346 --> 00:56:15,166 to have 2 different scripts, each associated 1238 00:56:15,166 --> 00:56:16,076 with a different Sprite. 1239 00:56:16,346 --> 00:56:19,296 So I've highlighted cat at the moment, here is the cat script. 1240 00:56:19,426 --> 00:56:21,696 If I click bird, here is the bird script 1241 00:56:21,696 --> 00:56:23,106 and they are clearly different. 1242 00:56:23,106 --> 00:56:24,446 Well how are they different? 1243 00:56:24,446 --> 00:56:25,756 Well let's look at the cat first. 1244 00:56:26,116 --> 00:56:31,246 The cat is doing this, go to negative 160, negative 160. 1245 00:56:31,246 --> 00:56:33,976 Why? Well again I was kind of fussing around one day 1246 00:56:33,976 --> 00:56:36,626 and I decided let's have the cat start there specifically, 1247 00:56:36,626 --> 00:56:37,846 trial and error in this case. 1248 00:56:38,216 --> 00:56:40,406 Point in direction, well I wanted this demo 1249 00:56:40,406 --> 00:56:41,876 to be a little different each time, 1250 00:56:41,876 --> 00:56:43,516 otherwise it's really uninteresting 1251 00:56:43,716 --> 00:56:46,896 so pick a random degree between 91 and 179, 1252 00:56:47,186 --> 00:56:50,036 then forever do the following, if touching bird, 1253 00:56:50,286 --> 00:56:54,036 here's where it gets fun, play sound, that particular sound, 1254 00:56:54,036 --> 00:56:56,086 line 4 and then stop the script. 1255 00:56:56,086 --> 00:56:59,216 In other words, game over is the implication there, exit quit. 1256 00:56:59,676 --> 00:57:03,266 Otherwise, actually I misspoke, it's not an else, 1257 00:57:03,536 --> 00:57:06,106 it's not an otherwise but no matter what, 1258 00:57:06,106 --> 00:57:08,896 point yourself toward the bird and move one step. 1259 00:57:09,146 --> 00:57:10,806 So in other words, if you're touching the bird, 1260 00:57:11,266 --> 00:57:12,956 play the sound and quit, game over. 1261 00:57:12,956 --> 00:57:15,476 No matter what though, proceed 1262 00:57:15,556 --> 00:57:18,196 to point toward the bird and move one step. 1263 00:57:18,196 --> 00:57:20,276 And actually it is implicitly an if else 1264 00:57:20,276 --> 00:57:22,686 because of the termination so I retract my retraction. 1265 00:57:23,056 --> 00:57:24,766 So, let's take a look at the bird. 1266 00:57:24,766 --> 00:57:25,666 What is the bird doing? 1267 00:57:25,666 --> 00:57:27,066 The bird's not doing all that much. 1268 00:57:27,066 --> 00:57:29,876 The bird starts at a hard coded place and I only did this 1269 00:57:29,876 --> 00:57:31,346 because it would be really stupid 1270 00:57:31,346 --> 00:57:35,526 if the bird might sudo randomly start on top of the cat and then 1271 00:57:35,526 --> 00:57:36,716 if the game has no point. 1272 00:57:36,976 --> 00:57:39,176 So I go to this location, which I know is different 1273 00:57:39,176 --> 00:57:43,066 from the cat, point in 45 degree direction and then forever. 1274 00:57:43,066 --> 00:57:44,076 Now notice what I did. 1275 00:57:44,076 --> 00:57:46,316 It turns out with Boolean Expressions not only can you 1276 00:57:46,316 --> 00:57:49,006 check for true or false, you can flip the meaning 1277 00:57:49,156 --> 00:57:51,876 so there's a block called not which inverts meaning, 1278 00:57:51,876 --> 00:57:53,886 which is good because I want to do forever, 1279 00:57:54,196 --> 00:57:58,486 the following forever if I'm not touching the cat, move 3 steps 1280 00:57:58,486 --> 00:58:00,016 but if I'm on the edge, bounce. 1281 00:58:00,016 --> 00:58:03,436 So you'll also see in Scratch, for user friendliness, 1282 00:58:03,796 --> 00:58:07,806 sometimes they merge the notion of a condition and a statement 1283 00:58:07,806 --> 00:58:11,256 like this so if on edge bounce, just happens 1284 00:58:11,256 --> 00:58:12,636 to be consolidated into 2. 1285 00:58:12,636 --> 00:58:13,356 So let's play. 1286 00:58:13,416 --> 00:58:15,076 I'll go ahead and full screen it. 1287 00:58:15,496 --> 00:58:17,086 I'll go ahead and click the green flag. 1288 00:58:17,766 --> 00:58:21,126 And notice this isn't random. 1289 00:58:21,126 --> 00:58:23,446 The cat is again point toward bird. 1290 00:58:23,446 --> 00:58:26,546 The bird is moving around somewhat randomly or [noise] 1291 00:58:26,546 --> 00:58:32,416 at least, alright so kind of sad but kind of clever nonetheless, 1292 00:58:32,536 --> 00:58:33,996 at least programmatically. 1293 00:58:34,226 --> 00:58:36,476 So what more is there? 1294 00:58:36,476 --> 00:58:40,156 Well what about if we do something like this interesting? 1295 00:58:40,156 --> 00:58:44,666 So this example, as we'll see, not only uses multiple threads, 1296 00:58:44,666 --> 00:58:46,546 multiple scripts happening in lock step 1297 00:58:46,746 --> 00:58:50,346 but I'm also using a variable to maintain state and essentially 1298 00:58:50,346 --> 00:58:52,526 to communicate across the two Sprites. 1299 00:58:52,526 --> 00:58:54,516 And this is useful if you effectively want 1300 00:58:54,516 --> 00:58:55,866 to pass information between the two. 1301 00:58:55,866 --> 00:58:59,596 So let me go to version 10 of my hi program, 1302 00:59:00,086 --> 00:59:03,776 that's not in this folder, interesting. 1303 00:59:04,326 --> 00:59:10,776 Alright well rather than drag our feet, well let's go ahead 1304 00:59:10,776 --> 00:59:14,366 and do, well you know what, we're out of tape anyway. 1305 00:59:14,496 --> 00:59:16,816 Let's go ahead and take a 5 minute break, we'll change tapes 1306 00:59:16,816 --> 00:59:18,026 and then resume once I've found this file. 1307 00:59:18,026 --> 00:59:26,716 [background noise] Alright so version 10 has just gone missing 1308 00:59:26,716 --> 00:59:29,186 so fortunately we have the source code here. 1309 00:59:29,476 --> 00:59:31,486 On the left hand side we have a script, 1310 00:59:31,616 --> 00:59:33,976 a Sprite that just says forever do the following, 1311 00:59:33,976 --> 00:59:36,596 if muted equals zero, play sound sea lion, 1312 00:59:37,336 --> 00:59:40,746 think oh hi for 2 seconds, then wait, then repeat 1313 00:59:40,746 --> 00:59:42,806 and then notice what the right hand side is doing 1314 00:59:42,806 --> 00:59:44,366 and this is really the only take away here. 1315 00:59:44,366 --> 00:59:46,926 Giving ourselves a mean of communication between Sprites. 1316 00:59:47,186 --> 00:59:49,616 One the right hand side I have this variable called muted. 1317 00:59:49,816 --> 00:59:51,146 I set muted to zero. 1318 00:59:51,246 --> 00:59:51,956 That is false. 1319 00:59:51,956 --> 00:59:53,886 It is not muted, is the implication 1320 00:59:54,046 --> 00:59:55,346 and then forever do the following. 1321 00:59:55,396 --> 00:59:58,296 If the key space pressed, then check the following. 1322 00:59:58,296 --> 01:00:01,846 If it's muted, set muted to 1, else set muted to zero. 1323 01:00:02,066 --> 01:00:03,286 So the right hand side, 1324 01:00:03,656 --> 01:00:06,186 even though there's some new blocks there, what is that doing 1325 01:00:06,186 --> 01:00:08,576 in laymen's terms, the right hand side script? 1326 01:00:09,336 --> 01:00:10,546 It's just [inaudible audience comment], 1327 01:00:10,786 --> 01:00:14,846 yeah it's just toggling the state of this variable, right? 1328 01:00:14,846 --> 01:00:15,746 We have a forever loop 1329 01:00:15,746 --> 01:00:17,466 which just means forever check the following. 1330 01:00:17,466 --> 01:00:18,426 Is the space bar pressed? 1331 01:00:18,426 --> 01:00:20,636 If so flip the value of muted. 1332 01:00:20,796 --> 01:00:22,906 So the goal of this exercise here was again 1333 01:00:22,906 --> 01:00:24,256 to have a little fun playing sound 1334 01:00:24,256 --> 01:00:26,416 but when I hit the space bar it either mutes 1335 01:00:26,746 --> 01:00:29,926 or effectively unmutes, what, unmutes the other guy 1336 01:00:30,066 --> 01:00:32,306 because the other guy, because he's also happening 1337 01:00:32,306 --> 01:00:32,876 in lock step. 1338 01:00:32,876 --> 01:00:34,766 Whether he's a cat or a bird, it really doesn't matter 1339 01:00:34,766 --> 01:00:37,696 in this case what the Sprite is, who's using this script, 1340 01:00:37,966 --> 01:00:41,316 the left hand side guy is only going to play the sound sea lion 1341 01:00:41,416 --> 01:00:44,376 if that value of muted is zero. 1342 01:00:44,576 --> 01:00:47,156 So in other words, if I hit the space, I make him bark. 1343 01:00:47,386 --> 01:00:49,586 If I hit it again, it makes him stop 1344 01:00:49,586 --> 01:00:51,296 and this just goes on in perpetuity. 1345 01:00:51,526 --> 01:00:53,976 So it's not such a loss if we have that one here. 1346 01:00:53,976 --> 01:00:55,816 This one's a little more interesting anyway. 1347 01:00:56,146 --> 01:00:58,776 So I'll go ahead and beat myself up here, if you will. 1348 01:00:59,036 --> 01:01:01,326 This is actually borrowed from a version 1349 01:01:01,326 --> 01:01:02,606 of this program someone else wrote, 1350 01:01:02,606 --> 01:01:04,176 it wasn't called David at the time. 1351 01:01:04,176 --> 01:01:06,246 I took some liberties since I didn't think it would be the 1352 01:01:06,246 --> 01:01:08,906 friendliest thing to have our student punch him in the face 1353 01:01:09,286 --> 01:01:11,946 so I swapped in my own face here and now think about it 1354 01:01:11,946 --> 01:01:13,866 for just a moment, what's probably going 1355 01:01:13,866 --> 01:01:15,246 on underneath the hood here? 1356 01:01:15,576 --> 01:01:19,156 So I'm apparently moving back and forth, as any boxer might 1357 01:01:19,456 --> 01:01:23,026 and if I use my arrow keys here I can start punching 1358 01:01:23,366 --> 01:01:23,816 and missing. 1359 01:01:24,256 --> 01:01:27,426 [laughter] But there we have here and actually 1360 01:01:27,426 --> 01:01:30,536 if we turn this feature here on, notice too, 1361 01:01:31,116 --> 01:01:39,046 [music] notice my face is clearly changing color. 1362 01:01:45,266 --> 01:01:48,166 [music and laughter] So what's going on? 1363 01:01:48,166 --> 01:01:50,636 Well we don't have to even dissect all the pieces cause 1364 01:01:50,636 --> 01:01:52,806 for the most part we presented all the fundamentals. 1365 01:01:53,066 --> 01:01:55,576 There's more than just 1 script now, more than just 2. 1366 01:01:55,576 --> 01:01:58,246 There's actually 4 that belong to the left glove. 1367 01:01:58,246 --> 01:02:00,976 It looks like there are as many that belong to the right. 1368 01:02:01,236 --> 01:02:05,206 My own self has this slightly larger one but it all boils 1369 01:02:05,206 --> 01:02:07,136 down if you start to focus on the specifics, 1370 01:02:07,136 --> 01:02:09,576 and again all this code will be online, it just boils 1371 01:02:09,576 --> 01:02:13,186 down to making the computer do what you want it to do and each 1372 01:02:13,186 --> 01:02:15,926 of these things are Sprites therefore they can detect 1373 01:02:15,926 --> 01:02:18,846 themselves, if touching one Sprite, do this and so 1374 01:02:18,846 --> 01:02:20,676 with these very simple building blocks, 1375 01:02:20,676 --> 01:02:22,616 almost all of which we've rattled off already, 1376 01:02:22,616 --> 01:02:25,206 can you do things that are increasingly compelling 1377 01:02:25,206 --> 01:02:28,176 and much more interesting than just hello world. 1378 01:02:28,376 --> 01:02:32,126 Well this is one final functionality that's provided 1379 01:02:32,126 --> 01:02:33,006 by Scratch. 1380 01:02:33,006 --> 01:02:35,966 It is not often found in at least introductory courses 1381 01:02:36,116 --> 01:02:39,126 and this notion of events, we'll actually see this in the context 1382 01:02:39,126 --> 01:02:41,566 of web programming later in the semester with Java Script. 1383 01:02:41,826 --> 01:02:43,966 For C we won't really play around with this idea 1384 01:02:43,966 --> 01:02:46,976 but this example here simply has 2 Sprite's, 1385 01:02:46,976 --> 01:02:48,286 each of which has one script, 1386 01:02:48,496 --> 01:02:50,306 but these guys too can communicate, 1387 01:02:50,436 --> 01:02:52,396 not by using a single variable, 1388 01:02:52,506 --> 01:02:54,696 so when I discussed a moment ago the notion of muting, 1389 01:02:54,696 --> 01:02:57,486 that's what we'll call a global variable, global in the sense 1390 01:02:57,486 --> 01:02:59,606 that anyone can use or manipulate it. 1391 01:02:59,776 --> 01:03:01,936 We'll see that that's not often the best practice 1392 01:03:02,216 --> 01:03:04,286 but with events can you do the same thing. 1393 01:03:04,286 --> 01:03:06,806 You can broadcast an event and the idea is 1394 01:03:06,806 --> 01:03:08,826 that any other Sprite that wants to listen 1395 01:03:08,826 --> 01:03:11,086 for that event can somehow respond to it. 1396 01:03:11,316 --> 01:03:15,666 So in this case here, Marko.sb we have essentially 2 Sprite's 1397 01:03:15,666 --> 01:03:17,226 playing a game with each other. 1398 01:03:17,606 --> 01:03:21,406 If I go ahead and play this, we'll see on the left hand side, 1399 01:03:21,656 --> 01:03:24,436 Marko is waiting forever for me to do what? 1400 01:03:25,186 --> 01:03:26,456 Hit the space so I'm gonna hit the space. 1401 01:03:26,846 --> 01:03:32,156 Marko and now the female says polo so why did she say that? 1402 01:03:32,156 --> 01:03:33,556 Well let's take a look at her. 1403 01:03:33,556 --> 01:03:37,736 Oh, when I receive event, say polo for 2 seconds. 1404 01:03:37,946 --> 01:03:40,466 So we have cross thread communication, 1405 01:03:40,466 --> 01:03:43,506 inter-thread communication, which is useful, you know yes 1406 01:03:43,576 --> 01:03:46,136 to make this simple example happen but we offer it 1407 01:03:46,136 --> 01:03:49,066 for today's purposes as a little tool for ones tool kit, 1408 01:03:49,106 --> 01:03:51,596 so to speak, that if you need 2 Sprites 1409 01:03:51,596 --> 01:03:53,966 to somehow communicate you've got a couple of options. 1410 01:03:53,966 --> 01:03:56,766 One, share a variable that both of them check the value of 1411 01:03:56,766 --> 01:03:58,576 and maybe change or you have events 1412 01:03:58,836 --> 01:04:00,646 which might be a little cleaner than that, 1413 01:04:00,646 --> 01:04:02,116 if you want to send something arbitrary. 1414 01:04:02,336 --> 01:04:05,156 Well it turns out that Scratch also supports these things 1415 01:04:05,156 --> 01:04:08,596 and in problem said zero, it already is coming 1416 01:04:08,596 --> 01:04:11,186 in 2 different editions, standard and hacker. 1417 01:04:11,506 --> 01:04:13,786 So the standard edition is, you'll see when it goes online 1418 01:04:13,786 --> 01:04:17,316 in PDF form tonight, invites you to pretty much make anything 1419 01:04:17,316 --> 01:04:21,546 of interest to you so long as it checks off a few boxes. 1420 01:04:21,546 --> 01:04:22,866 You need to use at least a variable. 1421 01:04:22,866 --> 01:04:24,726 You need to have at least a few different Sprites. 1422 01:04:24,726 --> 01:04:25,636 You need to use a sound. 1423 01:04:25,866 --> 01:04:28,786 So we lay some basic ground rules just to kind of push you 1424 01:04:28,786 --> 01:04:30,326 to do more than just scratch the surface. 1425 01:04:30,326 --> 01:04:32,916 But if you're really the type who wants to get his 1426 01:04:32,916 --> 01:04:35,826 or her hands dirty very early and maybe do have more of it, 1427 01:04:36,056 --> 01:04:38,716 a more comfortable background, there's these things here. 1428 01:04:38,986 --> 01:04:41,616 So these are sensor boards that we bought a whole bunch 1429 01:04:41,616 --> 01:04:44,416 of for students in the course to use and they have buttons, 1430 01:04:44,476 --> 01:04:46,646 they have little sliders that move left to right. 1431 01:04:46,876 --> 01:04:49,376 They have little inputs that you can connect alligator clips 1432 01:04:49,476 --> 01:04:51,216 to so that you can check resistance, 1433 01:04:51,486 --> 01:04:54,396 whether through some physical medium like yourself, left hand 1434 01:04:54,396 --> 01:04:57,236 and right hand, water, metal, anything like that. 1435 01:04:57,456 --> 01:04:58,496 There's a light sensor. 1436 01:04:58,496 --> 01:04:59,736 There's an audio sensor. 1437 01:04:59,846 --> 01:05:03,356 And so these things too, very simple ideas, taking sounds, 1438 01:05:03,476 --> 01:05:05,576 do something with it, you can actually detect it 1439 01:05:05,576 --> 01:05:07,186 programmatically within the program. 1440 01:05:07,436 --> 01:05:10,676 So this one is actually a simple example of precisely this, 1441 01:05:10,756 --> 01:05:12,816 singer.sb, let me go ahead 1442 01:05:12,816 --> 01:05:15,056 and open this one here, I'll full screen him. 1443 01:05:15,336 --> 01:05:20,076 He doesn't do all that much and here we go, 1444 01:05:20,076 --> 01:05:23,006 [noise] he responds to sounds. 1445 01:05:23,546 --> 01:05:25,656 Now he also responds to this and now it just looks 1446 01:05:25,656 --> 01:05:27,706 like I'm talking so it's not terribly sensitive 1447 01:05:27,706 --> 01:05:33,336 to different types of speech but we're clearly still responding 1448 01:05:33,336 --> 01:05:35,286 to something a little binary in nature. 1449 01:05:35,286 --> 01:05:36,776 I wonder how long we could give lecture like, 1450 01:05:36,776 --> 01:05:39,146 and see how distracting that is. 1451 01:05:39,456 --> 01:05:42,416 [laughter] Alright so this one, let me go ahead 1452 01:05:42,416 --> 01:05:43,606 and full screen this one. 1453 01:05:44,516 --> 01:05:49,586 [noise] 1454 01:05:50,086 --> 01:05:52,716 We need to raise the house lights again. 1455 01:05:53,936 --> 01:05:57,736 [background music] Can we pull them down again? 1456 01:05:59,566 --> 01:06:01,376 So it is in fact a light sensor 1457 01:06:01,376 --> 01:06:04,276 and the game gets a little freaky when the lights go down. 1458 01:06:04,276 --> 01:06:06,006 That was actually unintentional the first time. 1459 01:06:06,006 --> 01:06:07,446 Normally I use my hand but taking 1460 01:06:07,446 --> 01:06:10,166 down Sanders Theatre lights also works as well there. 1461 01:06:10,436 --> 01:06:11,856 Well we have one other here 1462 01:06:11,856 --> 01:06:15,176 so this one was particularly sophisticated but again speaks 1463 01:06:15,276 --> 01:06:18,266 to the relative complexity you can create 1464 01:06:18,266 --> 01:06:21,026 with what is ultimately quite simple building blocks. 1465 01:06:21,376 --> 01:06:24,486 This is one by David Woo, now a junior. 1466 01:06:24,486 --> 01:06:26,056 I'm gonna click green flag here. 1467 01:06:27,766 --> 01:06:29,886 [background music] This one uses the little sensor bar 1468 01:06:30,516 --> 01:06:34,566 to move this guy back and forth and the goal is I think 1469 01:06:34,596 --> 01:06:37,196 to get the bananas and not get hit by these, 1470 01:06:37,196 --> 01:06:40,416 the monkey's thumb counter has just gone up which means we're 1471 01:06:40,416 --> 01:06:43,626 like 174 milliseconds or whatever, I'm stunned 1472 01:06:43,626 --> 01:06:45,806 and he's not responding now, notice his eyes. 1473 01:06:45,866 --> 01:06:46,906 So that's bad. 1474 01:06:48,626 --> 01:06:54,016 [noise and laughter] So that actually use the slider back 1475 01:06:54,016 --> 01:06:55,976 and forth but this one actually gets some notion 1476 01:06:55,976 --> 01:06:57,326 of some friction coefficient. 1477 01:06:57,546 --> 01:06:59,856 So it's not just going left and right in response to me. 1478 01:06:59,856 --> 01:07:02,786 He kind of skids out on what is ultimately supposed to be ice. 1479 01:07:02,786 --> 01:07:04,726 So again, very simple primitives, right? 1480 01:07:04,726 --> 01:07:08,006 Light on, light off, sound on, sound off, slider left, 1481 01:07:08,006 --> 01:07:10,426 slider right but even Scratch can interface with this. 1482 01:07:10,456 --> 01:07:15,346 So realize that for problems set zero the hacker edition is 1483 01:07:15,346 --> 01:07:20,656 by nature meant to be very, very accessible and we have over 100 1484 01:07:20,656 --> 01:07:22,736 or 200 of these things out in the hall that you'll be able 1485 01:07:22,736 --> 01:07:25,516 to collect today and at any office hours next week 1486 01:07:25,866 --> 01:07:28,466 for problem set zero. 1487 01:07:28,746 --> 01:07:30,566 So this is this program now that I promised. 1488 01:07:30,566 --> 01:07:32,536 This is the first thing that I wrote and this is one 1489 01:07:32,536 --> 01:07:35,046 of those either sad or really impressive stories 1490 01:07:35,046 --> 01:07:38,086 of late Friday night 8 hours straight playing this. 1491 01:07:38,426 --> 01:07:41,056 You'll hear in a moment that I chose a song 1492 01:07:41,056 --> 01:07:43,696 that is perhaps the most annoying song in the world 1493 01:07:43,696 --> 01:07:47,516 to play ad nauseum for 8 hours while you debug your own program 1494 01:07:47,766 --> 01:07:49,906 but this is a little something called Oscar time 1495 01:07:50,366 --> 01:07:52,066 and its source code too will be distributed 1496 01:07:52,066 --> 01:07:54,396 on the courses website just so that you can see a lot 1497 01:07:54,396 --> 01:07:56,246 of different interactions going on. 1498 01:07:56,526 --> 01:07:58,846 So I stole this actually from an old piece 1499 01:07:58,846 --> 01:08:00,226 of software from Mac OS. 1500 01:08:00,226 --> 01:08:03,626 So back when I was a kid growing up with like a Macintosh SC, 1501 01:08:03,796 --> 01:08:08,776 there was this plug in called Oscar and you could install it 1502 01:08:08,776 --> 01:08:11,706 and then anytime you dragged trash to your trash can, 1503 01:08:11,966 --> 01:08:13,996 Oscar would pop out, sing a little song, 1504 01:08:13,996 --> 01:08:14,986 and then go back in, right? 1505 01:08:14,986 --> 01:08:17,536 [laughter] Completely amusing for like the first few weeks, 1506 01:08:17,536 --> 01:08:20,346 first few months and then you realize this is just, 1507 01:08:20,636 --> 01:08:22,276 you get, the slippery slope. 1508 01:08:22,636 --> 01:08:25,236 So let me go ahead and play this, 1509 01:08:25,326 --> 01:08:28,096 perhaps not in its entirety less you never again be able 1510 01:08:28,096 --> 01:08:31,296 to watch Sesame Street but as the instructions say, 1511 01:08:31,296 --> 01:08:34,156 drag as much falling trash as you can to Oscar's trashcan. 1512 01:08:35,286 --> 01:08:38,446 [background music] So notice this game too is going 1513 01:08:38,446 --> 01:08:40,106 to interact with my keyboard. 1514 01:08:40,136 --> 01:08:49,126 This is trash, click, drag and I won't go too far but notice 1515 01:08:49,126 --> 01:08:52,846 if I pull it into the trash quickly, the trash reappears 1516 01:08:52,846 --> 01:08:55,346 and now notice it's appearing in a different place each time 1517 01:08:55,676 --> 01:09:00,426 so even this game is using some sudo randomness. 1518 01:09:00,426 --> 01:09:03,996 And what we'll see is that this gets progressively faster 1519 01:09:03,996 --> 01:09:06,996 and progressively faster, much like the DDR game before. 1520 01:09:07,696 --> 01:09:12,866 So I thought a fun note, perhaps to conclude on, is this, 1521 01:09:13,166 --> 01:09:15,526 throughout today we've been dragging 1522 01:09:15,526 --> 01:09:18,186 and dropping puzzle pieces, it's a very intuitive language, 1523 01:09:18,186 --> 01:09:20,286 problem set zero will task you with precisely that. 1524 01:09:20,286 --> 01:09:23,456 Implementing the program that most amuses you. 1525 01:09:24,336 --> 01:09:26,186 We spoke of cell phones earlier today, 1526 01:09:26,186 --> 01:09:28,456 albeit in the context of iPhones. 1527 01:09:28,456 --> 01:09:30,576 Yes it is the case, if you read this morning's Crimson, 1528 01:09:30,576 --> 01:09:33,876 that Google very kindly donated some 20 telephones to CS50 1529 01:09:33,916 --> 01:09:38,746 and CS1 so that we could empower some of you, if you so desire 1530 01:09:38,746 --> 01:09:41,576 at terms end, to implement any final project that you want 1531 01:09:41,576 --> 01:09:43,526 but also one if you so choose, 1532 01:09:43,796 --> 01:09:47,086 on what's called an android cell phone, marketed by T-Mobile 1533 01:09:47,086 --> 01:09:48,436 and some other folks throughout the world. 1534 01:09:48,676 --> 01:09:49,746 Well we read this article. 1535 01:09:49,746 --> 01:09:51,406 The Crimson has always been very good to us 1536 01:09:51,406 --> 01:09:52,716 in recent years, press wise. 1537 01:09:52,756 --> 01:09:54,646 We were quite flattered that they took an interest 1538 01:09:54,646 --> 01:09:57,486 in this project but we were also quite amused. 1539 01:09:57,486 --> 01:09:59,766 And within minutes of this article going online, 1540 01:09:59,926 --> 01:10:03,526 on the front page of today's Crimson, I got this note here, 1541 01:10:03,526 --> 01:10:05,426 whose names and such have been redacted. 1542 01:10:11,436 --> 01:10:15,396 [laughter] So let's be clear, this is the email I got. 1543 01:10:16,886 --> 01:10:18,586 So then I got a little curious, right? 1544 01:10:18,646 --> 01:10:21,756 So I'm like okay, interesting, there's an article here 1545 01:10:21,756 --> 01:10:24,666 about CS50 so thecrimson.com. 1546 01:10:25,466 --> 01:10:28,596 Okay, yep we are in fact on the front page there as well 1547 01:10:28,596 --> 01:10:29,656 as in the print version. 1548 01:10:30,366 --> 01:10:37,896 So what you hopefully realize today, paragraph 2, look for it. 1549 01:10:38,716 --> 01:10:40,596 [laughter] So bless their hearts, right? 1550 01:10:42,506 --> 01:10:45,026 This is in fact drag and drop programming 1551 01:10:45,026 --> 01:10:47,766 that you will be focusing on for problem set zero 1552 01:10:47,766 --> 01:10:51,306 but I did think, in fairness cause again we're big fans 1553 01:10:51,306 --> 01:10:53,256 and frankly a lot of our team members are 1554 01:10:53,256 --> 01:10:54,826 on the Crimson so it's all good. 1555 01:10:55,006 --> 01:10:57,636 I did think that I could at least make this so. 1556 01:10:57,686 --> 01:11:00,806 So instead of Oscar Time which drops this trash, I went ahead 1557 01:11:00,806 --> 01:11:01,956 and decided we could do this. 1558 01:11:01,956 --> 01:11:03,456 You know so I'm gonna go to the trash here, 1559 01:11:03,736 --> 01:11:05,256 some little on the fly programming. 1560 01:11:05,256 --> 01:11:05,506 You know what? 1561 01:11:05,506 --> 01:11:07,046 I need a different costume here. 1562 01:11:07,266 --> 01:11:08,466 I'm gonna click import. 1563 01:11:08,706 --> 01:11:10,596 I kind of prepared in advance here. 1564 01:11:10,596 --> 01:11:12,156 I'm going to go to my desktop folder. 1565 01:11:12,156 --> 01:11:15,876 I'm going to go into my zero folder where I hid it. 1566 01:11:15,876 --> 01:11:17,546 I've got a little costume here. 1567 01:11:19,096 --> 01:11:22,446 [laughter] So if you will, let me go to this thing here. 1568 01:11:22,446 --> 01:11:23,926 I'm gonna go ahead and click this button 1569 01:11:23,926 --> 01:11:25,606 to shrink him down a little bit. 1570 01:11:25,976 --> 01:11:28,596 You have the ability to rotate Sprites as you'll see. 1571 01:11:28,596 --> 01:11:31,246 I'm gonna tell it not to rotate anymore and now 1572 01:11:31,246 --> 01:11:34,216 if we could crank the volume up just a little bit we have 1573 01:11:34,276 --> 01:11:37,966 for you, in conclusion today, drag and drop programming. 1574 01:11:38,516 --> 01:11:45,786 [laughter and music] 1575 01:11:46,286 --> 01:11:46,976 We'll see you on Wednesday. 1576 01:11:47,516 --> 01:12:21,890 [applause and music]