{"id":1899,"date":"2016-05-01T00:01:38","date_gmt":"2016-05-01T07:01:38","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1899"},"modified":"2016-05-08T07:52:45","modified_gmt":"2016-05-08T14:52:45","slug":"off-to-the-races","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1899","title":{"rendered":"Off to the Races!"},"content":{"rendered":"<p>Computer games were simple back in the early days. Output was printed on a teletype or displayed on a text-only CRT. Input wasn&#8217;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.<br \/>\n<!--more--><br \/>\nThe key to most text-based computer games is the <em>rand()<\/em> function, which generates a pseudorandom number. That&#8217;s the foundation even for the complex 3D shoot-em-up games of today. This month&#8217;s exercise uses the <em>rand()<\/em> function to simulate a horse race.<\/p>\n<p>In the horse race, a given number of ponies start off at the same value, the distance that they&#8217;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.<\/p>\n<p>Your task isn&#8217;t to code the entire game; I&#8217;ve written the essential code for you. You can <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/04\/05exercise.c\"\">click here<\/a> to see what I&#8217;ve done. My code lets you set the number of horses in the race by defining the <code>HORSES<\/code> constant. The race then adds up how many yards per furlong the horses run during the course of the race. Here is sample output:<\/p>\n<pre class=\"screen\">\r\nFurlong\tPony 1\tPony 2\tPony 3\tPony 4\tPony 5\tPony 6\r\n  1\t  608\t  623\t  620\t  601\t  610\t  638\r\n  2\t 1257\t 1235\t 1244\t 1245\t 1220\t 1296\r\n  3\t 1867\t 1842\t 1903\t 1873\t 1846\t 1953\r\n  4\t 2502\t 2466\t 2549\t 2489\t 2485\t 2571\r\n  5\t 3156\t 3123\t 3150\t 3143\t 3111\t 3218\r\n  6\t 3796\t 3731\t 3802\t 3747\t 3724\t 3874\r\n  7\t 4419\t 4331\t 4426\t 4356\t 4333\t 4489\r\n  8\t 5022\t 4945\t 5085\t 4984\t 4971\t 5114\r\n  9\t 5628\t 5576\t 5712\t 5598\t 5619\t 5734\r\n 10\t 6239\t 6233\t 6324\t 6257\t 6236\t 6347\r\n 11\t 6846\t 6873\t 6956\t 6880\t 6848\t 6959\r\n 12\t 7460\t 7517\t 7582\t 7515\t 7465\t 7567\r\nAnd the winner is Pony 0!<\/pre>\n<p>Your job is to write the <em>winner()<\/em> function, which determines which horse came in first. Above, that would be Pony 3 with the value 7582 yards.<\/p>\n<p>Please attempt this Exercise on your own before you check out <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1912\">my solution<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Code a horse race game. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1899\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1899","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1899","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1899"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1899\/revisions"}],"predecessor-version":[{"id":1935,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1899\/revisions\/1935"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}