{"id":4297,"date":"2020-08-15T00:01:56","date_gmt":"2020-08-15T07:01:56","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4297"},"modified":"2020-08-15T08:26:17","modified_gmt":"2020-08-15T15:26:17","slug":"the-21-number-game","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4297","title":{"rendered":"The 21 Number Game"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/08\/0822-figure1.png\" alt=\"illustration\" width=\"350\" height=\"263\" class=\"alignnone size-full wp-image-4310\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/08\/0822-figure1.png 350w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/08\/0822-figure1-300x225.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><br \/>\nI occasionally visit <a href=\"http:\/\/rosettacode.org\/wiki\/Rosetta_Code\" rel=\"noopener noreferrer\" target=\"_blank\">Rosetta Code<\/a> to look for C language inspiration. The site offers a programming puzzle, then presents solutions in various languages. A recent challenge involved the 21 number game &mdash; but the C language had no solution!<br \/>\n<!--more--><br \/>\nA C language solution <a href=\"http:\/\/rosettacode.org\/wiki\/21_Game\" rel=\"noopener noreferrer\" target=\"_blank\">exists now,<\/a> but back way back when, I figured I&#8217;d give a shot.<\/p>\n<p>The game works like this: You play against the computer to process numbers in sequence. You can say up to three numbers, increasing a cumulative total. The computer takes its turn. The first player to reach 21 wins.<\/p>\n<p>In my version of this game, I wanted the starting opponent (player or computer) to be randomized. Either way, the player is prompted for a value to add: 1, 2, or 3. The computer then guesses its random value: 1, 2, or 3. Back and forth they go, until computer or player hits 21.<\/p>\n<p>This game isn&#8217;t complex! Here&#8217;s sample output:<\/p>\n<p><code>21 Game!<br \/>\nWhoever reaches 21 first wins<br \/>\nYou go first<br \/>\nThe tally is 0<br \/>\nEnter 1, 2, or 3: 3<br \/>\nThe tally is 3<br \/>\n&nbsp;Me: +2<br \/>\nThe tally is 5<br \/>\nEnter 1, 2, or 3: 3<br \/>\nThe tally is 8<br \/>\n&nbsp;Me: +2<br \/>\nThe tally is 10<br \/>\nEnter 1, 2, or 3: 3<br \/>\nThe tally is 13<br \/>\n&nbsp;Me: +1<br \/>\nThe tally is 14<br \/>\nEnter 1, 2, or 3: 3<br \/>\nThe tally is 17<br \/>\n&nbsp;Me: +1<br \/>\nThe tally is 18<br \/>\nEnter 1, 2, or 3: 3<br \/>\nYou win!<\/code><\/p>\n<p>I use <em>scanf()<\/em> for player input. A test is made for out-of-range values, in which case the computer assigns a random value 1, 2, or 3 for the silly player. Other than this trick, I programmed a tiny bit of logic into my code: When the computer recognizes the running total is 18 or greater, it &#8220;guesses&#8221; the proper value to win the game. Otherwise, its guesses are random.<\/p>\n<p>Here&#8217;s the code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_08_15-Lesson.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_08_15-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\n<span class=\"comments\">\/* generate random value 1, 2, 3 *\/<\/span>\r\nint input(void)\r\n{\r\n    return( rand() % 3 + 1 );\r\n}\r\n\r\nint main()\r\n{\r\n    int player,tally,pguess,cguess;\r\n\r\n    <span class=\"comments\">\/* title info *\/<\/span>\r\n    puts(\"21 Game!\");\r\n    puts(\"Whoever reaches 21 first wins\");\r\n\r\n    <span class=\"comments\">\/* determine who goes first *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n    player = rand() % 2;        <span class=\"comments\">\/* 0 = computer, 1 = human *\/<\/span>\r\n\r\n    if( player )\r\n        puts(\"You go first\");\r\n    else\r\n        puts(\"I go first\");\r\n\r\n    <span class=\"comments\">\/* run the game *\/<\/span>\r\n    tally = 0;\r\n    cguess = 0;\r\n    while( tally&lt;21 )\r\n    {\r\n        printf(\"The tally is %d\\n\",tally);\r\n        if( player )\r\n        {\r\n            printf(\"Enter 1, 2, or 3: \");\r\n            scanf(\"%d\",&amp;pguess);\r\n            if( pguess &lt; 1 || pguess&gt;3 )\r\n            {\r\n                printf(\"Out of range value. Let me guess for you: \");\r\n                pguess = input();\r\n                printf(\"%d\\n\",pguess);\r\n            }\r\n            tally += pguess;\r\n            player = 0;\r\n        }\r\n        else\r\n        {\r\n            if( tally&gt;=18 )\r\n                cguess = 21 - tally;\r\n            else\r\n                cguess = rand() % 3 + 1;\r\n            printf(\" I add %d\\n\",cguess);\r\n            tally += cguess;\r\n            player = 1;\r\n        }\r\n    }\r\n    <span class=\"comments\">\/* conclusion *\/<\/span>\r\n    if( player )\r\n        puts(\"I win!\");\r\n    else\r\n        puts(\"You win!\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The core of the code is the <em>while<\/em> loop at Line 31. A random draw (Line 21) determines who goes first, but also sets the value of the <code>player<\/code> variable. An <em>if-else<\/em> structure within the <em>while<\/em> loop uses the <code>player<\/code> variable to determine whose turn it is. This variable is reset at Line 55.<\/p>\n<p>For the player&#8217;s turn, the code obtains input at Line 37. If the input is out of range, <code>if( pguess &lt; 1 || pguess>3 )<\/code>, the computer guesses for the player. The tally is increased.<\/p>\n<p>The computer&#8217;s only cheat is offered at Line 49: When the tally is 18 or greater, the computer uses the exact value to reach 21:<\/p>\n<p><code>if( tally&gt;=18 )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;cguess = 21 - tally;<\/code><\/p>\n<p>My original version of this game had a more complex input scheme, but was too clunky. So I went with <em>scanf()<\/em>, which still isn&#8217;t perfect, but it works.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Another silly number game worthy of a programming challenge. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4297\">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":[2],"tags":[],"class_list":["post-4297","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4297","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=4297"}],"version-history":[{"count":9,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4297\/revisions"}],"predecessor-version":[{"id":4318,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4297\/revisions\/4318"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}