{"id":3972,"date":"2020-02-08T00:01:19","date_gmt":"2020-02-08T08:01:19","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3972"},"modified":"2020-02-01T09:38:53","modified_gmt":"2020-02-01T17:38:53","slug":"automated-guessing-game-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3972","title":{"rendered":"Automated Guessing Game &#8211; Solution"},"content":{"rendered":"<p>In the climatic scene of the movie <em>Wargames<\/em>, the WOPR computer plays Tic-Tac-Toe. A question is asked, &#8220;Can the computer play itself?&#8221; This postulation forms the basis of all computer learning, but that wasn&#8217;t the puzzle for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3963\">this month&#8217;s Exercise<\/a>.<br \/>\n<!--more--><br \/>\nNo, the challenge was for the computer to generate a random number between 1 and 100 and then attempt to guess the number generated. Coding in a cheat would be simple, but the point was to see how many times it takes a computer, using logic, to guess the correct value.<\/p>\n<p>The magic number is seven, though I hope you ran your solution enough to see the occasional quicker guess. In fact, in one of my test runs, the random number was 50 and the computer guessed it immediately!<\/p>\n<p>For my solution, I use three <em>int<\/em> variables:<\/p>\n<p><code>guess_high<\/code> is the value on the high range of the scale of possible numbers. It starts with the <code>MAX<\/code> defined constant, set to 100.<\/p>\n<p><code>guess_low<\/code> is the low range value, which starts at 1.<\/p>\n<p><code>guess_current<\/code> is calculated as: <code>(guess_high - guess_low)\/2 + guess_low<\/code> The result is the value midpoint between the <code>guess_high<\/code> and <code>guess_low<\/code> values &mdash; a good guess.<\/p>\n<p>After calculating the guess, a comparison is made:<\/p>\n<pre class=\"screen\">\r\nif( number==guess_current )\r\n{\r\n    puts(\"Correct\");\r\n    break;\r\n}<\/pre>\n<p>If the value of <code>number<\/code>, the computer&#8217;s original random number, is equal to the value guessed, <code>guess_current<\/code>, an endless loop is broken and the program exits. Otherwise an <em>if-else<\/em> structure resets the variables <code>guess_high<\/code> or <code>guess_low<\/code>:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* output results if the guess is wrong *\/<\/span>\r\nif( number &lt; guess_current ) \r\n{\r\n    puts(\"^ high\");\r\n    <span class=\"comments\">\/* adjust high value *\/<\/span>\r\n    guess_high = guess_current;\r\n}\r\nelse\r\n{\r\n    puts(\"v low\");\r\n    <span class=\"comments\">\/* adjust low value *\/<\/span>\r\n    guess_low = guess_current;\r\n}<\/pre>\n<p>And the loop repeats.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<pre class=\"screen\">Generating a random number 1 to 100...\r\nTry\tNumber\tGuess\tResult\r\n#1\t68\t50\tv low\r\n#2\t68\t75\t^ high\r\n#3\t68\t62\tv low\r\n#4\t68\t68\tCorrect<\/pre>\n<p>To view the full code for my solution, <a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_02-Exercise.c\" rel=\"noopener noreferrer\" target=\"_blank\">click here<\/a> (GitHub). Keep in mind that mine is only one of an infinite range of possibilities. Providing that your solution also completes the task, using logical guesses to find the random number by, consider that you passed the challenge.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the climatic scene of the movie Wargames, the WOPR computer plays Tic-Tac-Toe. A question is asked, &#8220;Can the computer play itself?&#8221; This postulation forms the basis of all computer learning, but that wasn&#8217;t the puzzle for this month&#8217;s Exercise.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-3972","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3972","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=3972"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3972\/revisions"}],"predecessor-version":[{"id":3988,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3972\/revisions\/3988"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}