CS101: Introduction to Computers and Computing
CS101: Introduction to Computers and Computing

Programming Lab 3: Learning About Loops, Parameters, and Variables

 
There are 4 parts to this lab:

Part 1: Boot computer into Ubuntu operating system, printout grade sheet, and create folder.

Part 2: Learn about loops, parameters, and variables using Alice.

Part 3: Recreate game from last programming lab using loops, variables, and a more efficient algorithm.

Part 4: Check all tasks are complete, email assignment to instructor and self, shut down computer, and staple together and turn in grade sheet and code printouts.


Lab Directions:
Part 1: Open Ubuntu operating system, printout grade sheet, and create folder.

When you first start your computer it will be running the MacOS operating system and your screen will look like this:

Current Desktop

For our project we will be using the Ubuntu operating system running in a virtualization environment called VMware Fusion.  In a moment we will be starting the VMware Fusion software.  Once VMware Fusion starts running your screen will look like this:

Ubuntu in pause mode.

To unsuspend Ubuntu just click the white arrow in the middle of the screen. Once Ubuntu is
unsuspend then on the left side of the screen click on the Firefox web browser icon as shown below ...

Click the Firefox icon.

... and once the web browser is running reopen these directions.

To start the software running click on the dock icon called VMware Fusion shown below:


Fusion Icon


Once your computer is running Ubuntu and you have opened today's lab directions in Firefox your screen should similar to the one below:

Drag the                     lab directions window to the right side of the                    Desktop.

To setup your computer to work more efficiently drag the directions to right side of the screen as shown below:

move directions to                     right side of screen

For this lab, and each of the remaining labs that require you to complete a project, there will be a grade sheet.  The grade sheet informs you of the tasks you are required to complete in that day's lab and how many points each of the tasks are worth towards the 100 points available for the project.

You are about to download and open the
Prog3GradeSheet.xls spreadsheet file using LibreOffice Calc.  When it opens in Calc your screen will look like this:

 Type your                     name in cell B1 and lab section number in celll f1.

To download today's grade sheet click on the following link: 


In the now open spreadsheet click in cell B1, type your name, and press return on your keyboard.  Next click in cell H1, type your lab section number, and press the return key on your keyboard.   The spreadsheet should now contain your name and lab section number similar to the spreadsheet below:

Type your name in cell                     B1 and lab section number in celll f1.

Now click once on the grade sheet window to make Calc the active program. 
At the top of the Calc window  select File menu > Print as shown below...

file menu                     print

... to open the following dialog box:


print box


Select either the By Cabinet or the By Door printer and then click on the OK button.

Now walk up to the printer you selected and place one piece of paper at a time into the manual feed tray and retrieve your printout of the grade sheet.

Close the grade sheet by clicking the red close window button in the upper left corner of the window as shown below...

click red button

... and when the following dialog box appears...

click the don't                     save button


... select Close without saving.


Your screen should now look like this:

Drag the lab                       directions window to the right side of the                      Desktop.

Look over grade sheet printout to find the requirements for today's lab.  If you have any questions about the grading ask your instructor.

Later in lab we will need to save several items that need to turned in for grading so we will need to create a folder on the desktop.  Right click on the Desktop next to the Firefox icon on the Launcher and from the menu that appears select New Folder as shown below to create a folder on the Ubuntu desktop:

Create a folder on the                     desktop by right clicking.

A new untitled folder will appear on the desktop as shown below:

new untitled folder on                     desktop

Ubuntu computers make new folders with the name untitled folder.  Since untitled folder is already highlighted you can replace untitled folder with the name you desire by just starting to type the new name (if your folders name accidentally becomes not highlighted just right click on the the folder and select Rename).  Use the following format for naming the folder: 

lastname_firstinitial_prog3

(i.e. John Smith would call his folder smith_j_prog3)


When done your folder should look like this (with of course your own name):

Folder named first name                     last name first intial.

When done your computer screen should look similar to this:

just directions

Part 2: Learn about loops, parameters, and variables using Alice.

IMPORTANT NOTE: 
Alice has many features.  Normally we would encourage you to try out as many of the features as possible in a new program.  In fact, playing with a new programming editor and language can be a great way to learn a new computer programming language.  However, it is highly recommended that you follow the directions below exactly as written.  Clicking on the wrong feature in Alice can have drastic consequences to your program and decrease your ability to finish today's lab in the time allotted.  When you are finished with today's lab you can reopen Alice and create without worrying about a grade.

Loops, parameters, and variables are all very important programming concepts and powerful programming tools.  We will now create an Alice world that will help us learn about loops, parameters, and variables.  Once we have learned about loops, parameters, and variables we will use these concepts to recreate the Crazy Cube game we made in our last programming lab.  However, this time when we make the game we will use loops, parameters, and variables to create a more efficient algorithm for our game and make the game more exciting.

We are about to launch the program Alice.  O
nce Alice loads up it will take up the whole screen. To return to these directions click on the Firefox icon on the Launcher as shown below:

Firefox icon

To start Alice click once on the Alice icon found on the Dock at the bottom of the screen as shown below:

Alice launched from application menu.

Once Alice loads up it will take up the whole screen as shown below:

Alice whole screen

I
n the dialog box make sure to select the Snow template and then click on Open.

Your screen will now look like this:

Alice full screen


Next move your mouse to the
upper left corner of the screen and click the third button in as shown below...

click third button

... to adjust the Alice window to the size shown below:

screen

Your Alice window should now look like the following:

Alice with Snow background.

Alice calls the programs you create in it worlds.  When you create a world in Alice you are creating a program.  When you run a world you have created in Alice you watch the world's instructions take place in a window.  To see our world (or program) as it exists now click on the Play button near the upper left corner of the Alice window as shown below...

click the play button to run your                     world.

...to open a window that will play your world as shown below:

empty world window

Since we have not created any instructions in our world (program) yet we just have a very boring world with just a blue sky and white snow where nothing is happening.  Click the Click the stop button button at the top of the Alice World window to stop the world (program) from running and also close the window.

Alice is a object oriented program language (or OOP).  Object oriented programming is a modern programming concept.  Among the many advantages of object oriented programming is that it allows programmers to easily reuse code in other programs.  Since code can be easily reused in other programs creating programs becomes quicker.  Reusing code is accomplished partly by having objects.  Our project today is made easier by the fact we already have four objects in our Alice world.  When we picked out the snow template to start our world the template already contained these objects.  If you look under where you clicked on the Play button you will see the Object Tree panel as shown below:

object tree panel.


Objects can be shared among programs through the use of object libraries.  We will now bring one of the pre-made objects into our world from the object library.  Alice calls these object libraries galleries.  In the world viewer panel click once on the green ADD OBJECTS button as shown below (and highlighted by a red rectangle):

click the add object                     button to access the object library

The Local Gallery (objects stored on this computer) will appear at the bottom of the Alice window as shown below:

add object

C
lick on the Animals to show the content of the Animals gallery as shown below:

select the animals library

Once in the Animals gallery use the scroll bar at the bottom to scroll right until you find the class of objects called Class Bunny as shown below:

Stop at the bunny class.

Click once on the picture of the bunny to open the Bunny window as shown below:

Click on the bunny to add                     to the world.

Click once on the Add instance to world button and watch as the Class Bunny object is added to the world as shown below:

click the bunny

Now to complete adding the Bunny Class to the world click the green Done button underneath the move object buttons (see above picture) to close the object galleries (libraries) and return the Alice window to its default view as shown below:

bunny in world

Since we have actually added something to our world now would be a good time to save the world into a folder with the following process.  Select File menu > Save World as shown below:

select file menu save                     world

We need to save our project into our folder, however, the Save World As.. dialog box is setup to save your project on the Desktop as shown below:

saving to desktop

Since we need to save the project into our folder, and not on the Desktop, we need to double click on the folder icon in front of our folder name to open the folder as shown below:

double click on your                     folders icon

In the File Name: box type in the name of the project using the following naming strategy:

lastname_firstinitial_bunny

(i.e. John Smith would name his project smith_j_bunny)

Once your Save World As... dialog box is setup similarly to the one below...

click on save

... click on the Save button to save your world.

Click once on the bunny object in the object tree to select the bunny object and display the bunny details as shown below:

the details of the penguin are                       displayed on the details panel

If you recall from our earlier programming labs each object comes with a set of primitive (build-in) methods.  If you look at the bunny methods you will notice it does not have a primitive method that we could use to make the bunny hop.  We will now create a method to make the bunny hop.

Click on the create new method button as shown below...

create new method

... and when the new method dialog box appears type in hop as shown below:

name the method hop

Our new method bunny.hop will now be listed in the bunny objects methods as shown below:


new world


The new bunny.hop method will also be open in the method editor as shown below:

bunny.hop open in                     editor

Now drag the bunny move method into the new method and set the parameters to up and 1/2 meter as shown below:

drag move method and                       select 1/2 up

Now repeat the above process but this time select down and 1/2 meter as shown below:

select down 1/2 meter

Your new method should now look like this:


new method with up and down

Now click on the world.my first method tab to make it active and drag your new bunny.hop method into the world.my first method as shown below:

drag hop method into world.my first                       method

Now click on the Play button to see if your bunny now moves up and down 1/2 meter.

Now that we have a method to use that allows the bunny to hop we can use this to learn about loops, parameters, and variables.

When we play the world the bunny hops once.  What if we needed the bunny to hop twice?  We could drag the bunny.hop method into the world.my first world method again to accomplish two hops.  However, this would be an inefficient way to get the bunny to hop twice.  To demonstrate this think what would happen if we used this technique to make the bunny hop 50 times.  We would have to place the bunny.hop method into the world.my first method 50 times.  This would make for a long and cumbersome program.

Fortunately, a conditional loop can handle this situation well.  A conditional loop is a repetition structure.  A conditional loop is an instruction that tells the program to repeat a set of instructions until a condition is meet.  In this case we will have the loop repeat until the condition of 2 times meet.

Click on the bunny.hop method tab to make it active as shown below:

make the bunny.hop tab active

At the bottom of the method editor is the conditional Loop repetition structure.  Drag the Loop repetition structure under the current instructions in the editor as shown below and when you release the mouse select 2 times from the drop down menu as shown below:

drag the loop contorl structure under the                       current instructions

After the Loop repetition structure is in place your editor will look like this:

loop in place

Now drag the two bunny.move methods into the Loop repetition structure while being careful to keep them in their same order as shown below:

loop with other instructions inside

Click the Play button to play your world (program).  The bunny should hop twice because we set up the condition on the loop to 2 times..  Click the Stop button at the top of the World Running... window to stop the world (program) from running and close the window.  This is a much more efficient way to get our bunny to hop twice.

What if we needed the bunny to hop 5 times.  To do this we could adjusting the loop.  In the Loop instruction click on 2 times and select 5 times as shown below:

change the loop to 5                       times

Now click the Play button and see if your bunny hops 5 times.

A loop works well when you need to repeat some instructions.  However, there is a problem with our current loop.  Click on the world.my first methods tab to make it active as shown below:

make the world.my first method                       active

Imagine if we needed to use the bunny.hop method a lot in our program and each time we called the method we needed the bunny to hop a different amount of times.  We could not do this.  Every time we changed the number of times the loop repeated it would change it everywhere the bunny.hop method was used.  What we would need is a way to tell the method how many times to loop each time we called the method.  To accomplish this we will need to learn about variables and parameters.

A variable is a named location in the computer's memory. You can store information to this location, retrieve the information from the location when needed, and change the information at the location. 

For instance, say you started to play a game on a computer.  At the beginning of the game the computer asked you to type in your name.   Later, when you are done playing the game, the computer could refer to you by your name when it tells you your final score.  For example:

Nice job John you have the high score of 1200! 

How did the program remember your name while you played the game?  It stored your name in the computer memory at a certain location as a variable.  If another person plays the game after you the computer will change the information stored in the name location to the new person's name.  At the end of the game they would would get a message such as:

Nice job Jane you have the high score of 1300!

A particular type of variable is a parameter.  A parameter is when you pass information contained in a variable over to a method.  We need a parameter to solve our bunny hopping dilemma.  If we use a parameter to pass information over to the method on how many times we want the bunny to hop, since it is a variable, it can be different each time we use the method.  Let's give it a try.

Click on the bunny.hop method tab to make it active as shown below:

loop with other instructions inside

Now click on the create new parameter button at the top of the method editor as shown below:

click on create new                       parameter

When the Create New Parameter dialog box appears type in number of hops as shown below...

type in count jumps

... and then click OK.

Notice that the following number of hops parameter tile just appeared at the top of the method editor as shown below:

new parameter appears

Now drag the new parameter variable tile to the condition box on the loop that current reads 5 times and let go when the box becomes highlighted green as shown below:

drag parameter to condition box

Your loop should now look like this:

new condition on loop

Now click on the world.my first method tab to make it active as shown below...

new parameter showing

... and notice how the name of the method (bunny.hop) is now followed by the parameter variable number of hops and that the value of the parameter is set to 1 (as highlighted by the red box).

Click the Play button to see if the bunny hops 1 time as shown in the parameter.

Now we can see how many times the bunny will hop when we look at the method.  Even better if we need to use the method again we can have the bunny hop a different amount of times.   To try this drag the bunny.hop method into the world.my first method and notice that when you let go of the mouse the method asks you set the value of the new number of hops parameter; set the parameter variable to 2 as shown below:

set hops to two

Your editor should now look like this:

editor

Click the Play button to see if the bunny first hops 1 time, and then 2 times, for a total of three times. 

By placing the number of hops parameter variable into the loop we can set each bunny.hop method call we use to a different value for how many time our conditional loop repeats.

A parameter is a particular type of variable.  It is used to pass information to a method when it is called. 

We will now create and use a different type of variable.  We will create a variable to keep track of something that is taking place within a method.  We will now create a variable in the bunny.hop method to count how many times the bunny hops.  Click on the bunny.hop method tab to make the method active in the editor as shown below:

select the bunny.hop tab

With the bunny object selected in the object tree click on the Properties tab as shown below:

select properties tab

In the Properties details (not in the methods editor) click on the create new variable button as shown below:

click the create new variable button

Type count hops into the create new variable dialog box as shown below...

type count hops and                       click OK

... and click OK.

The new variable will appear at the top of the bunny.hop properties tab as shown below (and highlighted in red in these directions):


new variable

We have now created a place in the computers memory were the number of times the bunny hops can be stored while the bunny.hops method is running.  When you create a variable Alice will default to making the value of the variable the number 1(notice that count hops = 1).  Since when the program starts the bunny will not have hopped yet we need to change the initial value of the count hops variable to zero.  To do so click on the 1 after the equal sign and select other... as shown below:

select other...

When the calculator appears click once on the 0 as shown below...

set value to zero

.. and then click on Okay.

The count hops variable should now look like this:

set to 0

This means the current value of the count hops variable in the computers memory is now set to zero each time we call the bunny.hops method. 

To use the variable in the loop drag it to after the bunny move down method in the loop and when you let go select increment bunny.count hops by 1 as shown below:

use variable

Your loop should now look like this:

loop

We just instructed Alice to add 1 to the value of bunny.count hops variable each time through the loop.  What should happen now is Alice should set the value of bunny.count hops to 0 when the program starts and then each time through the loop it should increment (add) 1 to the value of the bunny.count hops variable. 

Click Play and see what happens.

You should have notice no difference when we played the world.  This is because the variable is keep track of in the computer's memory and we cannot see the content of the computer's memory.  Since we cannot see the content of the computer's memory we cannot see the value of the variable bunny hops changing. Fortunately, Alice has a special ability built into the program to allow use to watch what is happening to the variable in the computer's memory.  In the properties tab right click on the count hops variable and select watch this variable as shown below:

select watch this                       variable

Click the Play and notice that a new edition is added to the right side of the window as shown below:

notice that as the bunny jumps the                       count hops


Notice that as the bunny hops the count hops variable changes values each time through the loop starting at 0 and ending with the value of 3 as shown below:

sdf


Click the Restart button in the player window to watch the variable in action as many times as you like.

For 6 points add the bunny.hop method to the world.my first method again and set the parameter to 7.


Play the world and for 6 more points write in the final number of hops the bunny does (or times through the loop) in the correct location on your grade sheet.

Select File menu > Save World to save your program.

We will be grading your code.  To make it easier to grade we need a printout of your code.  Alice exports your code as an HTML file on the desktop that we can later open in Firefox and printout.  To achieve this first select File menu > Export Code for Printing... as shown below:

select file menu export                     code for printing

In the Export Code For Printing ... dialog box click once on the Select All button as shown below:

click once on selec all

When the Export Code dialog box opens find the Author's name: box near the bottom and type in your name (see the example of John Smith below ) and then click Select All button, and when your Export to HTML dialog box looks similar to this....

Type your name in the                     author field and then click on export code button

... click on the the Export Code button.

Exit Alice by selecting File menu > Exit as shown below:

select file menu exit

You should now see the HTML file containing your code on the desktop as shown below:

fine the html file on the                     desktop

Double click on the icon to open your exported code in Firefox as
shown below:

click on print preview

 

Click once in the window containing your Alice code in Firefox to make sure it is the active window and then select File menu > Print as shown below...

select file menu print

... when the following print dialog box opens select the printer of your chose (either ByCabinet or ByDoor) and then click on Print:

safari print dialog box


Now go place a blank piece of paper in the printer to print one copy of your code.

After your get your printout of your code from the printer click on the window containing your Alice code
close the Firefox tab containing your code as shown below:

close code tab

Drag your HTML file on the desktop into your folder on the desktop as shown below:

drag your html file into                     your folder

Your screen should now look like the one below:

just directions


Part 3: Recreate game from last programming lab using loops, variables, and a more efficient algorithm.
IMPORTANT NOTE: It is very important that you Exited Alice at the end to the of Part 2.  If you are not sure you have Exited Alice ask your instructor to check your computer.

In the last programming lab your created a game in which a player had to drive a car to a cube.  The game worked but was not that exciting.  It was not very exciting since the cube was always easy to find and you had all the time in the world to drive to the cube.  We will now recreate that same game, but since we can use new more powerful programming tools such as loops and variables we should be able to make a much more exciting game.

We will remake the game adding a countdown timer.  This will make the player find the cube before time runs out.  This will be accomplished while coming up with a better algorithm (a better way to solve the problem of creating the game).  This will demonstrate that there are usually many algorithms that can solve a problem and the best programmers find the best algorithms.

To make the project go quicker we will first download a Alice world that has the new game already started.  Click on the following link to download the Alice World and when the following dialog box appears click on Save File as shown below:

click on save file

On the Desktop you should now find the following icon:

new file

We need to rename the file by right clicking on the files icon and selecting Rename... as shown below:

click the name once

Type the following name for the file:

lastname_firstinitial_game2.a2w
(for example John Smith would call the file:  smith_j_game2.a2w)

as shown below:

renamed file


We are about to launch the program Alice.  Once Alice loads up it will take up the whole screen. To return to these directions click on the Firefox icon on the Launcher as shown below:

Firefox icon

To start Alice click once on the Alice icon found on the Dock at the bottom of the screen as shown below:

Alice launched from application menu.

Once Alice loads up it will take up the whole screen as shown below:

Alice whole screen


Once Alice opens click on the open the Open World tab as shown below and highlighted with a red box:

click on open world

Select the file you renamed...

select your file


... and then click on Open.

Your screen should now look like the one below:

game2 file                   open

Move the mouse to the upper left corner of the screen and when the window button reveal themselves select the one most right one as shown below:

click third button

Your screen should now look like the one below:

new world
:o make the project go quicker you will notice we have already imported the car and cube objects and created an event to move the car object when the arrow keys are pressed.  Click the Play button and try driving the car to the cube.

Nothing happens when we get the cube. In the previous game an event monitored if the car came within 1 meter of the cube.  Click the Stop button to close the Alice World player window.

This time we will use a different way to see when the car finds the cube.  To start the game the player drives the car around looking for the cube.  We will create a method to take care of this part of the program.  To keep our program logical and easy to understand we should give our methods descriptive names.  With the world object selected under the methods tab click on create new method button as shown below:

click on create new method

In the new method dialog box type look for cube as shown below...

type look for cube

... and then click OK.

If we think of how we would like the game to play we need to have a timer countdown while the car is at least 1 meter away from the cube.  It turns out that there is a programming concept that can take care of this situation.

At the bottom of the look for cube method you will find the While tile.  Drag the While tile into the new method and select true as shown below:

drag the while tile to                     the method

Your method should now look like this:

new method

The While is another kind of loop.  We used a conditional loop to get the bunny to hop a certain number of times in our first world we created. 

A While loop is called a pretest loop.  The instructions in the While pretest loop are only run while the test at the beginning of the loop is true (we choose true when dragging the tile into the editor).

In the case of our game we would only want the timer to countdown while the car is at least 1 meter away from the cube.  This means the test at the beginning of the pretest While loop should be:

Is the car at least 1 meter away from the cube?

In other words, the timer in the While loop should only run while the player has NOT found the cube.   To make this happen we will need one of the car's functions.  A function allows the program to ask a question and return an answer.  In this case we need to ask the question of how far is the car from the cube?

Click on the car object in the object tree and then click on the functions tab as shown below:

select the car object and its functions                     tab.

Now drag the car is at least threshold away from object (second function) to the true box of the While statement and do not let go until the true box has turned green as shown below...

drag to ture box

... and then select 1 meter and cube as shown below:

select 1 meter and cube

Your method should now look like this:

current method

Let us look at the While pretest loop (above).  As constructed it will pretest if the car is at least 1 meter away from the cube before it runs the instructions it contains. Currently, the While loop only contains the instructions Do Nothing. This means the loop will only be executed while the player is currently losing the game because they have not found the cube.  The only way the loop will stop running is if the player moves the car within 1 meter of the cube, at which point the pretest for the loop becomes false, and the player has won the game.  Only when the player wins the game will the instructions after the pretest loop be executed.  This means we should put an instruction after the While pretest loop that would let the player know they have won the game.

With the car still selected click on the car's methods tab and drag the car says method under the While pretest loop and select other... as shown below...

use car say method

... and then type You found the cube! You win! in the dialog box that appears as shown below...

type you win

... and click OK.

Your look for cube method should now look like this:

found cube method

Select the world object on the object tree and then click on the world.my first method tab in the method editor.  Then drag the world.look for cube method into the world.my first method as shown below:

current state

L
et's trace how our world runs.   In the picture below:
  1. The Play button is clicked, which triggers the first event.
  2. The event is expressed by calling the world.my first method.
  3. The world.my first method calls the world.look for cube method
  4. The world.look for cube method runs the instructions it contains

4 steps

Click on the world.look for cube tab to make it active so we can view its instructions. 

the while statement

Once the world.look for cube method is called the While pretest loop is executed.  Before it executes the instructions it contains it must check if the pretest is true.  The pretest:

Is the car is at least 1 meter away from the cube? 

Since when the game starts the car is more then one meter from the cube the test results in the value of true. Since the pretest is true the instructions in the While pretest loop are executed.  The While pretest loop currently contains the instructions Do Nothing. Which means while the car is more then 1 meter from the cube the program will currently do nothing except keep testing if the car gets within 1 meter of the cube. However, when the car does get within 1 meter of the cube the While pretest loop condition will become false and the loop will end. Once the loop ends the car say method, which comes after the while pretest loop, will be called and inform the player, "You found the cube! You win!".

Click the Play button to see if the program works correctly (note that once you find the cube you will need to click the Restart button to try again).

Now we will replace the Do Nothing in the While pretest loop with the instructions that will create a countdown timer.  This way while the car is at least 1 meter away from the cube the timer will count down from 10 seconds.

From the bottom of the world.look for cube editor drag the If/Else control structure into the area of the While pretest loop that current says Do Nothing and select true as shown below:

drag the if/else to the                     do nothing

Your method should now look like this:

the if/eles in place

In the game we made in the last programming lab we used the If/Else control structure.  An If/Else control structure allows you to choose one of two branches in the programs instructions.  In the game we created in the earlier lab we used a If/Else control structure to ask the player if they wanted to play the game again?  If they answered yes (true) we branched to the instructions in the program that restarted the game; if the player answered no (false) we took a different branch and went to the instructions in the program that ended the game.

We can use a If/Else control structure in our current game to branch to one part of the program if the player has time left on the timer, or we can branch to another part of the program if the player has run out of time.

To keep track of the time we will need a variable (remember that a variable is a place in the computers memory were the program can store information). 

Make sure the world object is selected in the object tree and then make sure that the world.look for cube tab is the active tab in the editor.  Click on the properties tab for the world so that it looks like this...

world properties tab

... and then click on the create new variable button as shown below:

click the new variable                     button

In the create new variable dialog box type in countdown timer...

type countdown timer

... and then click OK.

The countdown timer variable tile should show up in the world properties as shown below (and highlighted in red for these directions):

countdown timer variable

Alice, by default, has set the value of the countdown timer variable to 1.  We will need to countdown from 10 so to change the value of the variable click on the 1 and select other... as shown below...

select other...

... and when the calculator appears type in 10 as shown below...

type 10 into calculator

... and then click Okay.


The countdown timer variable should now look like this:

varible set to 10

Now we can use the value of the countdown timer variable to determine which branch of the If/Else structure will be taken.  Drag the countdown timer variable to the true in the If/Else and do not let go until the true box turns green as shown below...

drag the variable to correct                   location
and then select countdown timer > other... as shown below:

select countdown timer                     > 10

... and when the calculator set the value to 0 as shown below...

set value to zero
... and then click on Okay.


The If/Else control structure should now look like this:

value set

The If/Else will now test if the world.countdown timer variable is greater then zero.  If the variable is greater then zero (the player has time left) we can branch to a set of instructions that will take one second to lower the value of the world.countdown timer by one.  If the world.countdown timer variable is not greater then zero then time has run out and we can branch to a set of instructions that will tell the player they have run out of time.

First let us create the set of instructions that would take a second to lower the value of the world.countdown timer variable by one.

Drag the countdown timer variable tile to the the first Do Nothing in the If/Else and select decrement world.countdown timer by 1 as shown below:

set varible in if/else

 The If/Else should now look like this:

current if/else

Click on the down arrow after more... on the decrement tile and select duration > 1 second as shown below:

pick one second

Your decrement method should now look like this:

set to 1 second

The above decrement method is now setup to take one full second to lower the value of the countdown timer variable by one.

Now let us put in some instructions for if the world countdown timer is not greater than zero, in which case the player is out of time. 

Click on the car object in the object tree and then click on the methods tab as shown below:

car and methods tab selected

Drag the car say method to the remaining Do Nothing in the If/Else control structure and select other... as shown below...

drag car say

... in the Enter a string dialog box type You have run out of time! as shown below...

type you have run out of                     time

... and click OK.


Your If/Else control structure should now look like this:

current if/else

Your world.look for cube method should now look like this:

current world.look for cube method

Let's take a close look at the world.look for cube method to see how the While pretest loop and the If/Else control structure work together to create the current game:

if/else look

However, does the program run correctly.  We will need to do some testing.   If the game is working correctly then when the Play button is clicked you should have 10 seconds to find the cube.  If you find the cube in less then 10 seconds you should be informed you have won the game.  If you do not find the cube in less then 10 seconds you should be informed you have run out of time.  Click the Play button (keep in mind the you will need to click the Restart button to play the game again) and test out both scenarios. 

If your game does not work correctly then ask your instructor for help. 

If the game currently works correctly you may have noticed you cannot tell the value of the variable world.countdown timer since the timer only keeps track of the value in the computer's memory. It would be nice to see the the value of world.countdown timer to make sure it is working correctly.  Remember in our bunny hop program we watched the bunny.count hops variable while the program was running.  Let's do the same for our world.countdown timer variable. 

Click on the world in the object tree and then click the properties tab and note the in the graphic below where you can look to see the 'countdown timer' value change as the game runs as shown below:

world properties

Now Play the world and watch the world.countdown timer variable countdown.  Test both finding the cube in time along with NOT finding the cube in time.

Did you notice that the timer does not count down correctly to zero if the cube is not found in time?  We have a logic error in our program.   A logic error is when the program runs but does not perform the required tasks correctly.

We will need to debug this error.  However, before debugging select File menu > Save World.  Now if you need to return your world to its current state you can select File menu > Open World and you can open the world without saving.

It is worth 14 points to correct the error and get the timer to stop a zero (not the current -1 or -0) if the cube is not found.  Test the program at least 8 times once you think you have solved the problem to make sure it works correctly consistently.


Select File menu > Save World to save your program.

The following is for extra credit points.

Extra Credit 1: For 10 extra credit points create a new variable called 'score' and set an initial value of zero.  If the player finds the cube before time expires have 'score' increase by one.

Extra Credit 2: For 20 extra credit points have the variable 'score' decrease by one if time runs out before the cube is found.  If the variable 'score' keeps decreasing by one after the player does not find the cube then either find a way to stop 'score' from decreasing by one or explain why 'score' keeps decreasing by one.
 
We will be grading your code.  To make it easier to grade we need a printout of your code.  Alice exports your code as an HTML file on the desktop that we can later open in Firefox and printout.  To achieve this first select File menu > Export Code for Printing... as shown below:

select file menu export                     code for printing

When the Export Code dialog box opens find the Author's name: box near the bottom and type in your name and lab section number (see the example of John Smith below ) and then click Select All button, and when your Export to HTML... dialog box looks similar to this....

type name and lab section                     code here

... click on the the Export Code button.

Exit Alice by selecting File menu > Exit as shown below:

select file menu exit


If Alice asks you to save your word click Yes.

You should now see the HTML file containing your code on the desktop as shown below:

fine the html file on the                     desktop

Double click the icon on your Desktop to open the Alice code in Firefox as shown below:


drag your alice html document to the safri icon                     on the dock

Click once in the window containing your Alice code in Firefox to make sure it is the active window and then select File menu > Print. ...as shown below...

select file menu print

... when the following print dialog box opens select the printer of your chose (either ByCabinet or ByDoor) and then click on Print:

safari print dialog box


Now go place a blank piece of paper in the printer to print one copy of your code.

After your get your printout of your code from the printer click on the window containing your Alice code and then
close the Firefox tab containing your code as shown below:

close code tab

Your screen should now look similar to the one below:

game done

Drag your HTML file on the desktop into your prog3 folder on the desktop as shown below:

drag your html file into                     your folder

Drag your game2 file on the desktop into your prog3 folder on the desktop as shown below:

drag your html file into                     your folder

Drag your Alice backup folder on the desktop into your prog3 folder on the desktop as shown below:

drag your html file into                     your folder

Your screen should now look like the one below

just directions




Part 4: Check all tasks are complete, email assignment to instructor and self, shut down computer, and staple together and turn in grade sheet and code printouts.
Open your folder on the desktop by double clicking.   When your folder opens make sure it contains the following 6 items shown below:

check the                     contents of your folder

If you are missing any items ask your instructor for help.  Close the window containing your folder items.

Now look over your grade sheet to make sure you have completed all items.  If you are unsure of any of the steps ask your instructor for help.

Your screen should look similar to this:

just                         directions


Next we compress a copy of your folder so that we can email it to both yourself as a backup and to your instructor for grading. Right click on our folder and select Compress... as shown below:

compress your folder

Make sure that the dialog box that appears is setup similar to the one shown below...

click create

... and then click on Create. A confirmation box will open as shown below...

click ok

... click Close.

A compress version of your folder and files will show up on the desktop as shown below:

compressed file

Follow the steps below to send the new compressed folder to yourself and your instructor:

Step 1) Login to your online SSU email account (or other online email account).

Step 2) Create a new email.

Step 3) Fill in the subject for the email as:

lastname_firstinitial_prog3
(example: smith_j_prog3)

Step 4) Attach the compressed folder to the new email.

Step 5) Address the email to your instructor's email address (highlight and copy the email address for your instructor from the list below):

Mr. Carter's lab section's use this email address = cs101carter@cs.sonoma.edu

Step 6) CC the email to yourself.

Step 7) Send the email.

Once you have emailed in your assignment make sure you have your three printouts. You will turn these into your instructor with the grade sheet stapled to the top.

Lastly, to exit out of Ubuntu running in VMware Fusion and shut down the computer place your mouse at the top of the screen and wait until the VMware Fusion menus appear and then select Apple menu > Shut Down... as shown below...
Select shut down.

... and when the following dialog box appears select Shut Down as shown below:

Select Shut Down

End of Lab.