{"id":3940,"date":"2020-01-18T00:01:38","date_gmt":"2020-01-18T08:01:38","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3940"},"modified":"2020-01-25T08:17:16","modified_gmt":"2020-01-25T16:17:16","slug":"the-essence-of-the-text-game","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3940","title":{"rendered":"The Essence of the Text Game"},"content":{"rendered":"<p>I was perusing <a href=\"http:\/\/rosettacode.org\/wiki\/Rosetta_Code\" rel=\"noopener noreferrer\" target=\"_blank\">Rosetta Code<\/a> the other day, looking for more programming ideas to explore. One of the tasks provided, with examples in a variety of programming languages, was to code the old computer game <em>Hunt the Wumpus<\/em>.<br \/>\n<!--more--><br \/>\nSurprisingly, a version of the game wasn&#8217;t available in C on Rosette Code (probably because other programmers hadn&#8217;t gotten around to the task). So, I reviewed the rules and determined to code my own version of the game. How hard could it be?<\/p>\n<p>Well, it wasn&#8217;t easy, but it wasn&#8217;t a monumental task either. I&#8217;ve coded text games before. Most of them follow a simple format:<\/p>\n<ol>\n<li>Initialize stuff<\/li>\n<li>Loop to process input<\/li>\n<li>Exit when certain conditions are met<\/li>\n<\/ol>\n<p>As an example, consider the rudimentary &#8220;Guess the Number&#8221; game, which most beginning programmers code as an initial assignment:<\/p>\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\nint main()\r\n{\r\n    int guess,number,tries;\r\n\r\n    <span class=\"comments\">\/* intialize stuff *\/<\/span>\r\n    srand( (unsigned)time(NULL) );     <span class=\"comments\">\/* seed the randomizer *\/<\/span>\r\n    tries = 6;\r\n    number = rand() % 100 + 1;        <span class=\"comments\">\/* range is 1 to 100 *\/<\/span>\r\n\r\n    <span class=\"comments\">\/* loop to process input *\/<\/span>\r\n    while( tries )\r\n    {\r\n        printf(\"You have %d tries left\\n\",tries);\r\n        printf(\"Guess the number, 1 to 100: \");\r\n        scanf(\"%d\",&amp;guess);\r\n        <span class=\"comments\">\/* check for winning condition *\/<\/span>\r\n        if( guess==number )\r\n        {\r\n            printf(\"%d was the number! You won!\\n\",number);\r\n            break;\r\n        }\r\n        <span class=\"comments\">\/* update the user *\/<\/span>\r\n        if( guess &lt; number )\r\n            printf(\"%d is too low\\n\",guess);\r\n        else\r\n            printf(\"%d is too high\\n\",guess);\r\n        <span class=\"comments\">\/* update the exit condition *\/<\/span>\r\n        tries--;\r\n    }\r\n    <span class=\"comments\">\/* if the user lost, inform them *\/<\/span>\r\n    if( tries==0 )\r\n    {\r\n        printf(\"Too bad! You lost\\n\");\r\n        printf(\"The number was %d\\n\",number);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The program starts by initializing variables and the randomizer at Line 9. Variable <code>tries<\/code> is set equal to 6 because I read somewhere that it&#8217;s possible to guess a value between 1 and 100 in six or fewer tries (though I&#8217;m probably incorrect).<\/p>\n<p>The <em>while<\/em> loop at Line 15 processes input, comparing the user&#8217;s choice <code>guess<\/code> (gathered at Line 19) with the random value stored in <code>number<\/code>.<\/p>\n<p>When the user guesses correctly (Line 21), a success message is output and the loop breaks. Otherwise, a message is output indicating whether input was high or low, variable <code>tries<\/code> is decremented, and the loop continues. When tries is equal to zero, the <em>while<\/em> loop halts.<\/p>\n<p>After the loop is completed, a final <em>if<\/em> test compares variable <code>tries<\/code> with zero. When true, a consolation message is output and the value of <code>number<\/code> is revealed.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>You have 6 tries left<br \/>\nGuess the number, 1 to 100: 50<br \/>\n50 is too high. Try again.<br \/>\nYou have 5 tries left<br \/>\nGuess the number, 1 to 100: 25<br \/>\n25 is too low. Try again.<br \/>\nYou have 4 tries left<br \/>\nGuess the number, 1 to 100: 37<br \/>\n37 is too high. Try again.<br \/>\nYou have 3 tries left<br \/>\nGuess the number, 1 to 100: 31<br \/>\n31 is too low. Try again.<br \/>\nYou have 2 tries left<br \/>\nGuess the number, 1 to 100: 34<br \/>\n34 is too low. Try again.<br \/>\nYou have 1 tries left<br \/>\nGuess the number, 1 to 100: 36<br \/>\n36 was the number! You won!<\/code><\/p>\n<p>The game <em>Hunt the Wumpus<\/em> is coded similarly to this random number guessing game, though <em>Wumpus<\/em> has more complexities. I cover the details in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3942\">next week&#8217;s Lesson<\/a>. Still, the code from this Lesson is representative of how most computer text games work.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most of the early computer games were text-based number-guessing challenges. This simplicity forms the basics of today, interactive, graphical, realtime games. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3940\">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-3940","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\/3940","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=3940"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3940\/revisions"}],"predecessor-version":[{"id":3971,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3940\/revisions\/3971"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}