CSE 113 - Spring 2009 - Banner
UB -
University at Buffalo, The State University of New York Computer Science and Engineering
  • CSE 113 - Spring 2009
CSE 113 - Spring 2009 - Navigation
CSE 113 - Spring 2009 - Announcements

Lab 3

CSE 113 - Spring 2009 - Announcements

Week 1 (3/16-3/20)

Exploring the Greenfoot Environment

Copy the Scenario to your account

If you followed the directions in Lab 1 and made the proper directories for the course, type the following:

cd cse113

cd lab3 (or cd Lab3 depending on how you spelled the directory's name)

If you did not create the directories in Lab 1, then you can simply copy the scenario into your root directory by skipping the above steps and continuing with the next step.

Now, copy the zip file that contains the scenario using the following command:

cp /eng/home/adrienne/turtleGraphics.zip .

Note that there is a dot (.) at the end of that command.

Type:

ls

and you should see the name of the file turtleGraphics.zip in your directory. Now, you need to unzip the scenario, type:

unzip turtleGraphics.zip

If you type ls again, you will see the original zip file plus another file named turtleGraphics which now holds all the files for the scenario.

Start Greenfoot

Start the Greenfoot environment by opening the terminal and typing

use greenfoot

This will bring up the Greenfoot program. You may get a pop up window talking about how Greenfoot uses scenarios and asks what you want to do. Select the option to choose a scenario and open the scenario you just copied to your directory named turtleGraphics.

The next time you start greenfoot, you will probably not see a similar window. If the scenario we are working on does not come up when you start up the next time, then you will need to use the Scenario menu to open it again.

You will probably see a warning about versions of Greenfoot. Simply click OK and you can ignore this warning.

When you load the scenario, if the names of the actors and world appear with the gray diagonal lines, you will need to click Compile All before continuing.

 

Create an instance of an Actor in the world

On the right, you see a listing of the classes in the scenario. There is a class that describes the world and several that are classified as Actors. The Actors are all different types of turtles. Left click on the box that says "Turtle" so that a bold outline appears around it. Right clicking will bring up a menu that has an option to create a new Turtle(). Selecting this option will create an image of a turtle under your mouse pointer. Move the turtle over to the world and click on the world to create the turtle in the world. You can move the turtle around the world by clicking on it and dragging it to a new location. Create more instances of the simplest turtle if you'd like.

Call a method on an Actor in the world

Right click on the image of the turtle in the world. This action will give you a list of methods that are available for you to call on the turtle. Select to call the penDown method. Use the same process to set the color of the turtle's pen. Note that this method works differently than the turtle you worked with previously. Have the turtle move. Draw something (like a square or triangle). While we can interact with the objects in the world like this, there is something more powerful that greenfoot can do.

Create an instance of an Actor that can act in the world

Create an instance of the SquareTurtle. Place it anywhere on the screen and then click the run button on the bottom of the greenfoot window. You will see a square appear. Select Pause. Pick up the turtle and move it. Change the color of the turtle (notice that this method is under the submenu "inherited from Turtle" now). Select Run again. Now, you have two squares. Pause the scenario again.

Create an instance of each of the other turtles (in turn or all at once) and run the scenario again. Move them around. Putting another spiral turtle on top of a previous spiral makes some nice designs.

If you are interested

Double clicking on the name of a class on the right will open the code for that class in an editor. You can look to see how the coding for the turtles was accomplished. In the next two weeks, you will be using this editor to create and modify your own actors in your own scenarios.

 

Feel free to play with the greenfoot environment as long as you'd like to get comfortable making objects and running simulations. You can see some scenarios that students have completed using greenfoot by visiting the greenfoot gallery (http://greenfootgallery.org/).


Week 2 & 3 (3/23 - 4/3)

Copy the Crabs Scenario to your account

If you followed the directions in Lab 1 and made the proper directories for the course, type the following:

cd cse113

cd lab3 (or cd Lab3 depending on how you spelled the directory's name)

If you did not create the directories in Lab 1, then you can simply copy the scenario into your root directory by skipping the above steps and continuing with the next step.

Now, copy the zip file that contains the scenario using the following command:

cp /eng/home/adrienne/little-crab.zip .

Note that there is a dot (.) at the end of that command.

Type:

ls

and you should see the name of the file little-crab.zip in your directory. Now, you need to unzip the scenario, type:

unzip little-crab.zip

If you type ls again, you will see the original zip file plus another file named little-crab which now holds all the files for the scenario.

Start Greenfoot

Start Greenfoot and open the little-crab scenario. Make sure that all of the files are compiled in the scenario.

Modifying the Crab Scenario

Make a crab in the world and click to run the scenario. Nothing happens with the crab. It's act method is empty. Open the code for the crab and insert the line

move();

into the act method. Now compile and place another crab into the world and run again. Now the crab will run to the edge of the world and stop. However, we would like it to keep going when it hits the edge of the world.

Notice that crab inherits the method atWorldEdge() from the Animal class. This method returns a boolean value (true if the crab is at the edge of the world, false otherwise). We can use this to help direct the crab's movements. If we use an if-statement that questions if the crab is at the world's edge, then we should turn(26) [or whatever other number of degrees you'd like the crab to turn]. Otherwise, we should just move. Note that this works best as an if without the else. So, if we are at the world's edge, we turn, but we move regardless of our status at the edge of the world.

Try out the crab now. It should turn as it hits the edges of the screen.

 

Create Food for the Crab

Next, create a type of food for the crab to eat. This food can be another type of Animal (if so, then it should probably be a subclass of Animal), or a vegetable (then it should be a subclass of Actor).

Add code into the crab so that if it sees a piece of its food, it will eat it.

 

Create an Enemy for the Crab

Create a subclass of the Animal class that will be an Animal that eats crabs. Make this enemy move around the screen and eat a crab whenever it sees one. At this point, the enemy and the crab have the same programming. We should change that so that the simulation is slightly more interesting.

Change the enemy in one of the following two ways (you can do both if you'd like):

  • Add random movement to the enemy
  • Have the enemy respond to the keyboard to control when it turns (left or right)

To do random movement, you can first, get a random number. If the number is within a certain range, do one action, if it is within another range, do another action, and so on depending on how many actions you want. Here's an example in code:

int number = Greenfoot.getRandomNumber(100);
if (number > 0 && number <= 20) {
    //do action #1
    //action 1 has a 20% chance of being chosen each time
}
else if (number >= 20 && number < 60) {
    //do action #2
    //action 2 has a 40% chance of being chosen each time
}
else {
    //do action #3
    //action 3 has a 40% chance of being chosen each time
}

To resond to key presses, you can use the Greenfoot.isKeyDown() method. This method takes a string that represents which key you are interested in. Depending on the key pressed, you can do different actions: Here is an example in code:

if (Greenfoot.isKeyDown("a") {
    //do action #1 when the a key is pressed on the keyboard
}
if (Greenfoot.isKeyDown("left") {
    //do action #2 when the left arrow key is pressed on the keyboard
}
if (Greenfoot.isKeyDown("n") {
    //do action #3 when the n key is pressed on the keyboard
}

 

Have the simulation add things upon startup

Go into the constructor of the Crab world and add the following line:

addObject( new Crab(), 150, 100 );

Now when you start up the simulation, a crab appears at the location (150, 100). Add more code to the constructor so that when you start up, you have 4-5 crabs at different locations on the screen, 1 enemy, and 6-10 pieces of food at different locations on the screen. If you wanted to be fancy, you could have the food and crabs show up at random locations by using the random number generator like we did if we wanted the enemy to move randomly. You can use the size of the world as the parameter to the random number function so you get a number between 1 and the size of the world.


Installing Greenfoot at Home

Go to http://www.greenfoot.org/download/

and find the download for your operating system. The only system requirement is that you have Java downloaded, but there is a link on the Greenfoot download page that will take you to download Java if you don't already have it. These downloads are self-installers, so all you need to do after downloading is click to run the installer.

If you are working from home, you can download the scenarios turtleGraphics.zip and little-crab.zip from this webpage.


Submission

First, you need to zip up your scenario. Then, you submit using Web-CAT as you did for Lab 2.

To zip up your scenario, you need to go to the directory where the scenario is located. If you followed the directions for Lab 1, then you would:

cd cse113/Lab3

If you type ls, you should see a directory named little-crab here.

At the prompt, type:

zip -r little-crab.zip little-crab

You have now overwritten the code for the original little-crab.zip file. Type:

ls -l

You will see a detailed listing of your files. The little-crab.zip file should indicate a date that it today's date and a time that is the time it is when you are executing these commands. Right before the date, there is a number. That number should be greater than 0. If it is zero, go back and do these steps again, because the file you created is empty.

Now, you are ready to submit.

Submission for this assignment will be the same as Lab 2. We will use an entirely web-based system for submission. This means, you do not need to transfer or email your files to yourself if you are working on your home machine. You only need to be able to access the Internet.

To log onto webcat, go to this page:

http://web-cat.cse.buffalo.edu:8180//Web-CAT/WebObjects/Web-CAT.woa

You will see a set of assignments that are currently accepting submissions for our course on the first page after you log in. If you would like to submit, simply hover over the icons to the right of the assignment name and select the icon for uploading the submission. The icon looks like a piece of paper with a world next to it and an up arrow. Once you select this, you will be taken to an upload screen. Select the browse button and find the file you are interested in submitting. Select the file and then click Next. You will need to confirm your submission on the next screen by clicking Next again. You will see a results page indicating if your submission was successful.

You will need to submit the zip file for this assignment to its appropriate assignment in Web-CAT. You can submit as many times as you would like. We will only grade the last submission you make. You can use Web-CAT to view what submissions you have made to ensure you have sent the correct file.


Due date

Your lab submission is due no later than 11:59:59 pm on April 3rd. Remember, no late labs will be accepted.


Lab authored by Adrienne Decker

CSE 113 - Spring 2009 - Footer

Page maintained by Adrienne Decker

Contact: adrienne@cse.buffalo.edu | 130 Bell Hall | (716)645-3180 x 161