Assignment 10

Due: Wednesday, December 9, 2015 at noon 100 pts.

Instructions: For this assignment you will submit multiple files, but not quite in the usual way. These files will contain a c++ program that you have named appropriately with appropriate extensions. You should have a file for your main, a header for each class definition, an implementation file for each class. Submit using the cssubmit script. For this final project, you are going to be allowed to work in groups of 0 to 2; that means 0, 1, or 2. We encourage you to work with someone, but discourage you from an uneven distribution of workload. NO SPONGERS! A group of 0 is suboptimal. Since we are allowing you to work in groups, you will submit only under one name for the group. Be sure to put all group members' names on every file.

Remember: This program will represent 10% of your final grade.

Background: assignment #9....Now, you are going to modify and combine what you produced in assignment #9 with new constructions to produce a simulation of Homer's drive down the road to perdition. His new autono-monstrosity car has a mind of its own. It will move down the road that it is on in an effort to gain Homer's destination hoping for battery power to spare, the least damage possible, and without raising the ire of PETA and the environmentalists. However, being one of a new breed of autonomously driven vehicles, it tends to wander as it goes (Barney programmed it). Not only that, at every instant of this drive, there is a significant chance that some animal will leap into its path causing damage to the car. There is also the possibility that a pedestrian will attempt to cross the road, halting progress in the journey...at least until the pedestrian has exited the road. Unfortunately, as time slips by, the battery loses power. Fortunately, Dr. Frink has installed on the front of the car a device that converts kinetic energy (imparted by any animal the car hits) into potential energy (positive battery life). So, you can see that Homer is in for quite a ride, avoiding drunks on the road and possibly hitting suicidal animals.

Specifications: In this program, you are going to declare objects of type car, road, animals, and pedestrian. You will have them interact in a way that simulates the car object driving on the road object, hitting animal objects and avoiding pedestrian objects. Sounds like real life! Start by declaring a car, a road, and an array of 10 animals. Put the car on the road. You will have the car enter the road at the center of the roadbed. Use a loop, every iteration of which will represent advancement of 1 kilometer down the road. During each iteration of the drive-loop:

  • the car will lose one unit of battery power just by moving that far.

  • the car has equal likelihood of wandering one unit left, staying on course, or wandering one unit right. In so doing, it must stay on the road and cannot wander off.

  • your main, with a chance of 5%, may decide to introduce a pedestrian into the scene. If so, your code declares a pedestrian object and places it on the left end of the roadbed. The pedestrian will call its walk() function until it leaves the roadbed. Pedestrian is described below. The car will halt further progress down the road until the pedestrian is no longer on the road. And, if the pedestrian walks over the car, it's possible interaction with the car is also described below.

  • if no pedestrian is on the road, your main will choose an animal at random from the animal array, and that animal will "decide" to jump on the road or not. Where on the road it jumps is randomly determined by your code. If the location does not coincide with the car, then the car passes it up and nothing happens. If the sectors of the roadbed occupied by the car and those occupied by the animal overlap, show the full animal (symbol) and not the car (symbol) for those coincident sectors (simulating that the animal is 'on' the car. In this case, it is understood that the car has hit the animal and your code must:

  1. tally damage to the car: new_damage = ((100% - current_damage%)/100) * animal_wt/10. (Not to exceed 100% damage, of course)

  2. tally the animal hit - save the info of the animal hit because it will be part of the final output.

  3. increase the power of the battery by 1 unit for every 5 lbs weight of the animal hit.

  4. the car, being equipped with LoserStar® Roadside Assistance, will say (output to the screen): "<insert randomly chosen line from LoserStar file below>. You've hit a <insert animal name> !!! Can I be of assistance?"

  5. update the car's e-score. This is described below.

Your simulation should have the car continue down the road until the battery runs out, or the damage to the car is 100%, or until Homer arrives at his destination (which is 100 km away). When the journey ends, for whatever reason, your code will output to the screen:

  1. The reason for the termination.

  2. A list of the animals hit along the way.

  3. Battery life left.

  4. Damage assessment.

  5. The car's e-score

  6. The distance traveled.

Details: Ok, so you have already defined the car, animal, and road classes. Clearly, you are going to have to add functions and possibly data members to each of these classes. We will leave it up to you how to do this. But be sure that when you add data, the data is essential to what a car is, what a road is, and what an animal is. Also, think carefully about the functions you add to each class, making sure they represent the behavior you want the objects to display/perform. Here are some thoughts:

  • animal class:

    • you will need to add a member variable for a char to represent the object on a road. 'a' is a good value. Though we haven't yet covered it in class, a static member variable would be good here...and for the car class too.

    • you will need appropriate set and get functions as needed. For example, you'll need to retrieve the weight of the animal.

    • trash the default constructor you made in hw 9. Now, since you will declare an array of animals and only a default constructor will work for an array of any class type, make a default constructor that pretty much does nothing.

    • to follow up on the last point....Once you have created an array of animals, write a non-member function that will walk down that array of animals "building" the animals from the information in the animal.dat file below. (Remember: don't copy and paste data!)

  • road class:

    • the road in hw 9 was an array of characters. But now each sector of the road is going to have to store more information than just a single character. It will have to know whether or not a car is there, an animal is there, a person is there. Sooo, think struct for a sector of the roadbed.

    • you will need to add a function to place an animal on the road. Think very carefully how to do this! Do you need two functions to place cars and animals, or will one do?

  • car class:

    • add a member to car called eScore to help us identify how "environmentally friendly" the car is. You know, of course, that one of the design goals of a self-driving car is that it can avoid collisions better than humans. Let's see just how e-conscientious this car is! eScore is to be the poundage of the carnage created by the car hitting animals on the journey; the higher the number, the worse the car is doing. Of course, it should always be initialized to 0.

So, here's a thought. You could have your code "act" like the car's driver is throwing the dead animals in the trunk. With this thought in mind, it would be reasonable to include in the member variables of the car an array of animals....dead ones....carnage...n'stuff. Then, at the end of the journey, you have all the info about e-score and the list of animals to output....by looking into the ... uh, .... trunk. Yuk. You can assume that an animal hit shrinks down to nothing, so you effectively have an infinitely large trunk....like a Navigator. If you don't want a bunch of dead animals in your trunk, there are other ways to do this.

In addition to the above suggestions, you are required to do the following:

  • initialize the battery strength in a car to a random value between and including 50 and 75 percent.

  • at the beginning of the simulation, output a statement very much like "The car with battery life xxxx now begins its trip..." so that you know what it is starting with.

  • read randomly from the file of LoserStar announcements each time needed and NOT read them all into an array and stored internally in the program.

  • add the following pedestrian class:

    • Member variables:

      • a char to represent it on the roadbed

      • a blood alcohol content (bac)

      • a name

    • Member functions:

      • a constructor that will initialize the object's name by choosing a name at random from either a file of names or a const array of such, initialize the bac to a value between and including 0.0 and .2, and the char to 'P'.

      • all necessary accessor and mutator functions.

      • a walk() function that will

        • advance the object one step (one sector of the roadbed) in the direction of the far side of the roadbed if bac < 0.10 (not drunk)

        • either advance the object one step (sector) or back up the object one step (sector) if bac >= 0.10 (very drunk). The movement back and forth is to be random with a probability of advancing 75% and a probability of back-tracking 25%...like a drunk!

  • any time that a pedestrian object is coincident with a car object on the road and it is drunk, it will impart 1% damage (for each step). If it is not drunk, the pedestrian does 1% repair .... which implies mechanics don't drink. Yes.

  • Now, your output for the road should look like this:

    • | | | | c | c | c | c | | a | a | --> Distance: ## km, Battery: ##%, Damage: ##%

    • This will keep you informed of the current status of the car as it moves.

Special Note: Once again, you can work with another person on this assignment. We urge you to take advantage of this opportunity. This is not an easy assignment.

Mucho special note: You may have the time, inclination, and wit to add more to this assignment. You are welcome to do so! We certainly don’t want to stanch creativity. If you want to add other kinds of obstacles or more animals or weather conditions or an alien invaders class or a zombie class or ….. then let your imagination run wild and have fun with this assignment. IF you do, be sure to include at the top of your main function file a BIG note to the grader of what you have included. Also, don't let your additions/modifications alter the requirements set out above.

The animals.dat file:

animal width weight stupidity

cat 2 20 89

possum 2 20 90

deer 3 100 50

slug 1 1 4

penguin 2 75 55

armadillo 2 40 95

chicken 1 10 80

giraffe 4 1500 40

dolphin 3 200 65

whale 10 500000 5

LoserStar announcements:

  1. Hi, my name is Mike.

  2. Holy Moly! You idiot!

  3. Ya loser, you did it again.

  4. Uhh, I don't know you. But ...

  5. This car is NOT a bumper-car from a county fair.

  6. You've been in an accident.

  7. Your insurance is sure to go up now!

  8. Homer, you're sooo stupid.

  9. Welcome to WalMart.

  10. Do you want fries with that?

When you submit: do so.

As always, if you have questions, be sure to ask!!