{"id":3816,"date":"2019-11-02T00:01:56","date_gmt":"2019-11-02T07:01:56","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3816"},"modified":"2019-10-26T11:12:43","modified_gmt":"2019-10-26T18:12:43","slug":"the-poker-program","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3816","title":{"rendered":"The Poker Program"},"content":{"rendered":"<p>With all the hand-ranking functions complete (see <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3810\">last week&#8217;s Lesson<\/a>), the final version of my poker program restores the randomizer, draws and sorts a hand, then outputs the hand&#8217;s value. It&#8217;s quite disappointing.<br \/>\n<!--more--><br \/>\nNo, the code isn&#8217;t disappointing. I think the code is nifty, but the output shows you how long the odds are on a typical 5-card hand of poker.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Your hand is 6&#x2666; Q&#x2663; Q&#x2666; 10&#x2665; 4&#x2663; - One Pair<\/code><\/p>\n<p>Yeah!  A pair of queens.<\/p>\n<p>You can see that the output is unsorted, through internally the code sorts the hand by card value, which is what the various evaluation functions expect. Here&#8217;s another sample run:<\/p>\n<p><code>Your hand is 4&#x2660; A&#x2665; 10&#x2663; 2&#x2660; 9&#x2663; - High card: 10&#x2663;<\/code><\/p>\n<p>Whoops! I noticed that my high-card <em>if<\/em> test doesn&#8217;t count for the value of an Ace as 1, low. So I fixed up the relevant part of code:<\/p>\n<pre class=screen>\r\n<span class=\"comments\">\/* account for ace as the high card *\/<\/span>\r\nif( hand[0].value==1 )\r\n    wprintf(L\" - High card: %s%lc\\n\",hand[0].face,hand[0].suit);\r\nelse\r\n    wprintf(L\" - High card: %s%lc\\n\",hand[4].face,hand[4].suit);<\/pre>\n<p>This test fixed the problem:<\/p>\n<p><code>Your hand is A&#x2663; 6&#x2666; 8&#x2665; K&#x2663; 9&#x2666; - High card: A&#x2663;<\/code><\/p>\n<p>The output you see above is most typical. Single pairs may also appear, but mostly you get a lot of high-card hands. Getting a flush or full house is truly rare. I ran the program over and over and the best I saw was three-of-a-kind. This outcome is the disappointing part.<\/p>\n<p>At this point, the code can be modified further. Don&#8217;t worry! I&#8217;m not going to extend this series one more Lesson. But given what I have now, the code could be modified to be more interactive, to allow for cards to be discarded and new cards drawn. You could add a routine to draw a second hand for &#8220;the computer&#8221; and pit player&#8217;s hand against the computer&#8217;s.<\/p>\n<p>If I were to modify the game further, I&#8217;d code it in Ncurses and use a full terminal window display. That way the cards can be displayed graphically (well, in text mode) and the program can truly be interactive. I may complete this task one day when I&#8217;m bored, but I&#8217;m rarely bored.<\/p>\n<p><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2019_11_02-Lesson.c\" rel=\"noopener noreferrer\" target=\"_blank\">Click here<\/a> to view the full code on Github. Do with it what you will.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At long last, she is done! <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3816\">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-3816","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\/3816","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=3816"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3816\/revisions"}],"predecessor-version":[{"id":3837,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3816\/revisions\/3837"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}