# greenfoot project (turtle race) -- 2

Objectives

• writing a Greenfoot application on your own

• working in an object-oriented environment

• using arrays and supporting algorithms

• utilizing files

You will be writing and designing you own classes to build a “turtle race” application. Essentially, turtles will start 50 pixels from the left hand side of the world (which should be 800x600 pixels in size), and move towards the finish line that is 25 pixels away from the right hand side of the world. When the world first loads, 8 turtles (with single character text labels ranging from ’A’ through ’H’) should be evenly spaced along the left hand side of the world. The top turtle (’A’) will have odds of moving of 1 of 2; the next turtle (’B’) should have odds of 2 of 3, and so on through the 8th turtle (’H) with odds of 8 of 9. The top of the world should have 5 buttons (you are being provided with a Button class) across it, labeled “Load Turtles”, “Start Race”, “Reset Race”, “Sort By Odds”, and “Sort By ID”; the actions when each of these is clicked are described below:

• Load Turtles. This should first remove all of the turtles currently in the world. It should then display a FileDialog window, allowing the user to select a file. The contents of this file will always begin with a positive integer. This represents the number of turtles described in the file (which will never be more than 25). Turtles are then described in the file by three values for each turtle: the first will be a single character that represents the turtle’s unique identification; the remaining two values will be (positive) integers representing the odds of the turtle moving. For example, a sequence like:

M 222 334

means that this data is for a turtle with ID ’M’ that will move roughly 222 out of every

334 possible times. Phrased another way, there should be a turtle called ’M’ with a 222/334

(which is approximately .664670658) probability of moving. Each such turtle found in the input file should be added into the world, evenly spread out from top to bottom (between y values 125 and 525, 50 pixels from the left side of the world).

• Start Race. This should start the turtles moving. In any one call to act(), a turtle should move one pixel right or remain stationary according to its odds. That is, for example, if the turtle has odds of 3 of 4 (or 0.75), it should have a 75% chance of moving. One way to implement this is to generate a random floating point number between 0 and 1 and if that number is less than or equal to the turtle’s odds (0.75 in our example), then the turtle should move one pixel to the right; otherwise it should not move during this call to act(). Of course, once any turtle touches the finish line, the race is over and all turtles should stop moving.

• Reset Race. This should stop the race and take all of the turtles back to the starting point (i.e. 50 pixels from the left side of the world).• Sort By Odds. This should re arrange the turtles from top to bottom sorted so that the turtle with the highest corresponding odds (percentage wise) is at the top and the one with the lowest odds (percentage wise) is at the bottom. Turtles should maintain their current x location, and a race should not stop or restart because this button was clicked.

• Sort By ID. This should re-arrange the turtles from top to bottom sorted so that the turtle with the lowest character identifier (according to Java’s lexicographic ordering) is at the top and the one with the highest character identifier is at the bottom. Turtles should maintain their current x location, and a race should not stop or restart because this button was clicked. As one final part, when the mouse is moved over a turtle, a sticky “Info Note” should appear that contains that turtle’s character ID and odds. This note should disappear after a short period of time. Note that you are being given an InfoNote class that is completely coded to aid in helping you get such done.