{"id":3980,"date":"2020-02-08T00:01:28","date_gmt":"2020-02-08T08:01:28","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3980"},"modified":"2020-02-01T09:40:48","modified_gmt":"2020-02-01T17:40:48","slug":"hunt-the-wumpus-code","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3980","title":{"rendered":"Hunt the Wumpus Code"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/01\/wumpus-150x150.png\" alt=\"Original Wumpus artwork\" width=\"150\" height=\"150\" class=\"alignleft size-thumbnail wp-image-3946\" \/><br \/>\nI had a great time coding <em>Hunt the Wumpus<\/em>. Like most programming challenges, it was fun, exciting, and frustrating all at once. Some code ran beautifully the first time. Other code contained silly mistakes that bogged me down. And solutions unveiled themselves magically like a sugar cube dissolving in water.<br \/>\n<!--more--><br \/>\nMy solution, which you can <a href=\"https:\/\/github.com\/dangookin\/interesting\/blob\/master\/hunt%20the%20wumpus.c\" rel=\"noopener noreferrer\" target=\"_blank\">view on GitHub<\/a> in its entirety, runs at just under 400 lines of code. A lot of the code is commentary and documentation, which I find necessary for a large project. No way I&#8217;m keeping all those details in my head!<\/p>\n<p>I use two defined constants:<\/p>\n<p><code>#define ROOMS 20<br \/>\n#define SIZE 32<\/code><\/p>\n<p><code>ROOMS<\/code> sets the size of the labyrinth, <code>SIZE<\/code> is used for text input.<\/p>\n<p>I also created two sets of enumerated constants:<\/p>\n<p><code>enum { EMPTY,WUMPUS,BAT,PIT };<br \/>\nenum { FALSE, TRUE };<\/code><\/p>\n<p>The first set is used for the <code>labyrinth[]<\/code> array and the second for various Booleans throughout the code.<\/p>\n<p>Here are the functions as they appear in the code, top-down. I wrote these in the order in which they appear:<\/p>\n<p><code>void prep_room( int laby[], int c, int pp )<\/code><\/p>\n<p>This function populates a room in the labyrinth (argument <code>laby[]<\/code>) with a <code>BAT<\/code>, <code>PIT<\/code>, or <code>WUMPUS<\/code>, represented by argument <code>c<\/code>. Argument <code>pp<\/code> represents the player&#8217;s position. The goal is to set an item into to an empty chamber. This function is called to initialize the labyrinth array, but also to move a <code>BAT<\/code> or the <code>WUMPUS<\/code> as required.<\/p>\n<p><code>char getinput(void)<\/code><\/p>\n<p>This function processes input, eating a line from the stream and returning only the first character. The ? character (for the Help command) is filtered out, otherwise the character returned is converted to uppercase.<\/p>\n<p><code>int player_alive(int laby[],int pp)<\/code><\/p>\n<p>Here the player&#8217;s position (<code>pp<\/code>) is tested in the array (<code>laby[]<\/code>) to determine whether the <code>WUMPUS<\/code> or a <code>PIT<\/code> is stumbled upon. If so, the function returns <em>FALSE<\/em>; the player is no longer alive.<\/p>\n<p><code>void shot_missed(int laby[],int pp)<\/code><\/p>\n<p>When an arrow is fired and it doesn&#8217;t hit the wumpus, there is a 75-percent chance the wumpus moves. This function handles the situation, setting the wumpus in a new, random room &mdash; or not.<\/p>\n<p><code>int arrow_loose(int laby[], int pp, char direction)<\/code><\/p>\n<p>This function is rather complex, prompting the player to shoot an arrow into one of the rooms, Left, Right, or Back. It handles the situation if the arrow misses and deals with bad input. If the shot does miss, the <em>shot_missed()<\/em> function is called. If the arrow hits, the function returns <em>TRUE<\/em>, which is processed as a victory in the <em>main()<\/em> function.<\/p>\n<p><code>int bat_move(int laby[],int pp)<\/code><\/p>\n<p>When the player moves into a room with a bat, his position is relocated. This function moves the player into an unoccupied room, but also moves the bat into an unoccupied room.<\/p>\n<p><code>int main()<\/code><\/p>\n<p>Beyond the items mentioned in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3956\">last week&#8217;s Lesson<\/a>, the <em>main()<\/em> function handles the player&#8217;s condition, <code>player.alive<\/code>, as well as <code>player.won<\/code> and <code>player.arrows<\/code>. The status of these items determines when the endless <em>while<\/em> loop breaks and the game is over.<\/p>\n<p>After the loop, messages are output depending on whether the player won or has died. (Running out of arrows is equivalent to dying.)<\/p>\n<p>Due to the addicting nature of programming, it&#8217;s difficult to let a large project go. My implementation of <em>Hunt the Wumpus<\/em> is no exception. I&#8217;d enjoy adding more flowery language to describe the maze, but I sought to code the game as close to the original as I could, based on the data I gathered elsewhere. Big projects are fun, and one of the joys of programming is the satisfaction you receive when the task is complete.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The blow-by-blow of my C code for the game <em>Hunt the Wumpus<\/em>. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3980\">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-3980","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\/3980","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=3980"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3980\/revisions"}],"predecessor-version":[{"id":3985,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3980\/revisions\/3985"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}