Explore BrainMass
Share

# Writing Programs Using Top-Down Design and Common Sense

This content was STOLEN from BrainMass.com - View the original, and get the already-completed solution here!

Create a simulation race between the tortoise and the hare. For this project, use random-number generation to move the creatures. To make things more interesting, the animals have to race up the side of a slippery mountain, which could cause them to lose ground. In this race either animal could win or there could be a tie with no clear winner.

The animals begin at "Square 1" of 70 squares. Each of the squares represents a position the animal can hold along the racetrack. The finish line is at Square 70. When an animal wins, a winning message of your choice should be posted. For example:

• Yay! The rabbit won! He hops the fastest!

• Woo-hooo! Slow and steady wins the race! Congratulations, turtle!

• Tie score--no winner! Want to race again?

To start the race, print a message similar to:

• Bang! Off they go!

There is a clock that ticks once per second. With each tick of the clock, your program should adjust the position of the animals according to the following rules:

Animal Move Type Percentage of the

Tortoise Fast Plod 50% 3 squares to the right

Tortoise Slip 20% 6 squares to the left

Tortoise Slow Plod 30% 1 squares to the right

Hare Sleep 20% No move at all

Hare Big Hop 20% 9 squares to the right

Hare Big Slip 10% 12 squares to the left

Hare Small Hop 30% 1 square to the right

Hare Small Slip 20% 2 squares to the left

Time

Keep track of the positions of the animals by using variables. If an animal slips, the lowest it can go is back to position 1. The highest it can go is to position 70 when it wins the race.

You will work with the percentages in the table above by generating a random integer current in the range 1 <current ≤ 10.

For the tortoise, a "fast plod" is when 1 ≤ current ≤ 5, a "slip" when 6 ≤ current ≤ 7, or a "slow plod" 8 ≤ current ≤ 10. A similar approach would be used to set up the moves for the hare.

For each tick of the clock (each repetition of the loop), print a 70-position line showing the letter T in the tortoise's position and the letter H in the hare's position. If the two animals land on the same square, which may happen, the animals will bump. If this happens, print BUMP! at the current position. (There is no Bump penalty for either animal.)

After you print each line, check to see if either animal has landed on Square 70. If this happens, print a winning-type message.

It will make the simulation more interesting if you have the user press any key after each iteration of the loop, so that they can see the movement of the animals.

https://brainmass.com/computer-science/cpp/writing-programs-using-top-down-design-and-common-sense-569149

#### Solution Preview

One way to solve this problem is to declare an array that will have 70 elements to it. That will represent the "track". Since you will need to keep track of both runners' positions, it will have two dimensions, so it will look like this:

int track[2][70];

The runners will be starting at the first square, so initially they will both be at array position 0, right? (The C language begins its arrays at zero.) So initially both arrays will have the runners' positions at the 0th element of the corresponding second dimension of the track. Let's assume that a 1 in the array signifies the position of the runner, and all other elements of the array's second dimension will always be 0.

Let's define a couple of constants first:

#define HARE 0
#define TORTOISE 1

track[HARE][0] = 1;
track[TORTOISE][0] = 1;

And the rest of the array will be filled with 0s to make sure it is initialized properly.

Now, if the hare advances nine squares, the array will look like this:

track[HARE][0] = 0;
track[HARE][1] = 0;
track[HARE][2] = 0;
track[HARE][3] = 0;
track[HARE][4] = 0;
track[HARE][5] = 0;
track[HARE][6] = 0;
track[HARE][7] = 0;
track[HARE][8] = 0;
track[HARE][9] = 1;
track[HARE][10] = 0;

It should be easy to see that what you'd need to do to keep track of the position is to zero out the current array position, where the runner is before any advancement or "slips", then count however many squares they advance from there, and put a 1 there.

(I'm assuming, of course, that your instructor intends the 70th square to be to the "right" according to your assignment. And if an animal moves to the "left" or "slips", that means they go to a lower element in our arrays.)

It will also make it easier if we keep track of the current position of both racers. That will just be two int variables, that will index into the particular element on the "track" that each animal is in. Like this:

int hare_position = 0, tortoise_position = 0;

That would be how they both begin. In the example above, assuming the hare was now at the 9th square, (hare_position == 9) would certainly be a true conditional expression assuming everything was working correctly and that the two position markers were kept accurate.

Just to make sure you understand this, if the hare then advances 3 squares, you would only need to do the following:

track[HARE][hare_position] = 0;
hare_position += 3;
track[HARE][hare_position] = 1;

Now that we've got a way to keep track of the race, printing it out shouldn't be much of a problem, either. Each time you print it, you can loop through the entire array, printing the "line" (perhaps the minus character from the keyboard, the "-") when there's a zero in that array element and a "T" or an "H" if there is a 1 in the array's position.

Now we need a way to run the race. Your teacher told you you'll need a loop of some kind. Any kind will do for this particular task. Let's assume it will be a while loop:

int race_is_over = 0;
while (!race_is_over)
{

}

I've left the body ...

#### Solution Summary

Implementing a solution through common sense, top-down programming logic. In this (approximately) 2,300 word document, the logic for a simple two entity Tortoise/Hare Race will be discussed, using C++ in the examples.

\$2.19