[Univ of Cambridge] [Dept of Engineering]
next up previous contents
Next: Exceptions Up: Dodgems: A longer exercise Previous: Creating the Dodgem class

Putting it all together

The main routine is going to initialise the randomiser (using srandom(time(0)) and graphics, create some dodgems, then for 100 time intervals move each dodgem in turn, checking each time to see if the dodgem has crashed into another. If the dodgem crashes into another team's dodgem, the latter is eliminated and the first dodgem gains a point. If a dodgem crashes into a fellow team member the former loses a point and is shoved 90 degrees clockwise.

At the end the scores of the remaining dodgems are printed out and wait_for_key() called.

If you store the dodgems in a list<dodgem> then its easy to add, remove and loop through them.

And that's it! you can develop this simulation if you want, changing the direction and speed to make the dodgems more predatory, and making the dodgems bounce off walls.

A solution is in the anwers section at the end of the document (page [*]).


next up previous contents
Next: Exceptions Up: Dodgems: A longer exercise Previous: Creating the Dodgem class
Tim Love
2001-07-05