Computer games were simple back in the early days. Output was printed on a teletype or displayed on a text-only CRT. Input wasn’t interactive or real-time. These games were fun to play back then, but are kind of lame now. They still exist with regards to simple programming exercises. In fact, you can pound out a older type computer game in a few minutes if you know the basics of the C language.
The key to most text-based computer games is the rand() function, which generates a pseudorandom number. That’s the foundation even for the complex 3D shoot-em-up games of today. This month’s exercise uses the rand() function to simulate a horse race.
In the horse race, a given number of ponies start off at the same value, the distance that they’ve run. As the race progresses, each pony adds more distance at variable rates. You can use simple text graphics to show the action, or display the results numerically. Some of the horse racing games I played long ago allowed for wagering. All that may seem silly, but if you add graphics and sound, you get what is essentially a modern computer game.
Your task isn’t to code the entire game; I’ve written the essential code for you. You can click here to see what I’ve done. My code lets you set the number of horses in the race by defining the HORSES
constant. The race then adds up how many yards per furlong the horses run during the course of the race. Here is sample output:
Furlong Pony 1 Pony 2 Pony 3 Pony 4 Pony 5 Pony 6 1 608 623 620 601 610 638 2 1257 1235 1244 1245 1220 1296 3 1867 1842 1903 1873 1846 1953 4 2502 2466 2549 2489 2485 2571 5 3156 3123 3150 3143 3111 3218 6 3796 3731 3802 3747 3724 3874 7 4419 4331 4426 4356 4333 4489 8 5022 4945 5085 4984 4971 5114 9 5628 5576 5712 5598 5619 5734 10 6239 6233 6324 6257 6236 6347 11 6846 6873 6956 6880 6848 6959 12 7460 7517 7582 7515 7465 7567 And the winner is Pony 0!
Your job is to write the winner() function, which determines which horse came in first. Above, that would be Pony 3 with the value 7582 yards.
Please attempt this Exercise on your own before you check out my solution.