Netlogo Hack – Activity from “Hackensack Hacker” [Hour of Code]

A Taste of Programming, Appendix to the Book: The Harrowing Case of the Hackensack Hacker
by Barnas Monteith & Robert Tinker

This activity is based on the book The Harrowing Case of the Hackensack Hacker - which we suggest you purchase prior to doing your activity, but this is not required.

This activity/tutorial is a beginner’s intro to programming and hacking, using a simple,  safe & free visual graphing program to “hack” different colorful patterns right on your computer screen. 

 

WHAT YOU NEED:

1. a computer

2. the program Netlogo, described below

3. minimal or no programming skills! (this activity is good for children 7+)

 

First, here is the short “hack” program written in NetLogo that you will use for cutting & pasting in order to do this tutorial (don’t worry, you don’t need to understand the program yet):

globals [counter]
to startup
if counter = 0 [
clear-all
create-turtles 100 [pen-down]]
;if counter > 20000 [stop]
;if counter > 10000 [ask turtles [forward .5 right random 360]]
;if counter > 15000 [ask turtles [forward 10 left 119]]
ask turtles [ forward .01 right .1]
set counter counter + 1
end

Second, if you prefer to watch this tutorial, rather than read it, here is the Youtube version of this program/hack:

So, let’s begin – before you can start this tutorial, you need the software program Netlogo which works on many computers.  Netlogo is provided for FREE as an educational tool from the nice people at Northwestern University who developed this handy programming language.

NETLOGO LOGO

Be sure to download NetLogo here: http://ccl.northwestern.edu/netlogo/. (or click on the logo above — don’t worry, it’s virus free if you download it directly from the Northwestern website)

Once you have downloaded the Netlogo program, be sure to install it, with permission and help from your parent/s or teacher/mentor.

To understand the small “hack” program we have provided above, find the three lines above that begin with a semi-colon (;). Each of these lines is a different hack. When a line starts with a semi-colon, the computer ignores the line. To activate a hack, simply remove the semi-colon. Without any hacks, if you press the “start” button, the program forever produces a pretty design that looks like this:

graphic2

The first hack is a sleeper that kills the program after 20,000 steps (be patient, it may take a while before this hack occurs). Once it stops, it is very hard to get started again. You have to reload the program!

Press on “Add” and then click in the white space to create a button. You should see a form that looks like this:

graphic1
Enter “startup” and “Start” as shown, click the “Forever” checkbox,
and press “OK”. This setup causes the button to tell the computer to
execute the program called “startup” over and over again, forever.
Now to make a way to clear the screen, we’ll create another button.
Repeat what you did above with the start button, only this time make
another button with the Command “ca” (which stands for Clear All) and
use the Display Name “Clear”. You can drag this button below the start
button, and you can click on it anytime you want to start the program
over again.
Next, press the button called “Code” and enter the program. When you
are done, be sure to save the program, or you will have to enter it again
later. Press the “Interface” button to get back to the “front” of the program.
If you have made an error, the computer will give an error message.
Read the code very carefully and correct any mistakes.
To run the program, press the “Start” button.

Here is how this program works, line by line.

globals [counter]
This tells the computer that we are going to use “counter” as a
place to store how many times the program is run.

to startup
This announces to the computer that we are about to tell it what to
do when it is told to startup by the button that you just made.

if counter = 0 [
This checks to see whether the counter is equal to zero. When the
counter is first made, it is automatically set to zero. Notice the left
square bracket “[“. This is the start of a group of commands that the
computer executes only if counter is zero. The last command in this
group is indicated by the matching right square bracket “]”. Later
in the program we will add one to the counter. When this happens
the commands inside the square brackets will NOT be executed.

clear-all
This clears the screen, turning it black. Because this is inside the
square brackets, it is executed only the first time through the program.

create-turtles 100 [pen-down]]
This creates 100 “turtles” at the center of the screen. Each has a
random color and direction. These are not really turtles, they are
tiny arrowheads. The “pen-down” command ensures that later,
when we ask the turtles to move, they will leave a trail.

if counter > 20000 [stop]
This is the first hack. It tells the computer to stop dead when the
counter becomes larger than 20,000. You can experiment with the other
hacks. Turn one on at a time by removing just one semicolon. Try to
understand how they work.

ask turtles [ forward .01 right .1]
This command asks each of the 100 turtles to go forward just .01 of
a step and then turn right .1 degrees. That isn’t much, but remember
that this whole program will be executed over and over very quickly
because of the “forever” you clicked on the “Start” button. After 3,600
repeats, the turtle will trace a complete circle!

set counter counter + 1
This adds one to the counter, so that the next time this program is executed,
counter will be one larger. It started at zero, so the next time
through it will be one, and so on. When it reaches 20,000 the hack will
stop the program.

end
This tells the computer that the definition of “startup” is complete.
Now you can experiment with the code. Try substituting different
numbers in the program. Try creating more or fewer turtles. Make the
hacks work differently. Change the patterns of the turtles by having
them move further and turn more. Have fun! You cannot hurt the
computer, but sometimes you have to reload the NetLogo program.

 

IF YOU ENJOYED THIS ACTIVITY, PLEASE LIKE US & BUY OUR BOOKS! 

 

CLICK ON THE LINK BELOW TO LEARN MORE ABOUT THE COOL COMPUTER SCIENCE MYSTERY BOOK,

THE HARROWING CASE OF THE HACKENSACK HACKER:

 HH COVER FINAL 6_glowongaslog_brighterfrontimage_smallerspine C w ISBN lowered logos - CROPPED JPG