{"id":280,"date":"2008-06-24T13:55:25","date_gmt":"2008-06-24T18:55:25","guid":{"rendered":"https:\/\/blogs.mathworks.com\/videos\/2008\/06\/24\/puzzler-six-card-poker\/"},"modified":"2017-03-28T12:57:21","modified_gmt":"2017-03-28T17:57:21","slug":"puzzler-six-card-poker","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/videos\/2008\/06\/24\/puzzler-six-card-poker\/","title":{"rendered":"Puzzler: Six card poker"},"content":{"rendered":"<p>Update:  This solution was found faster than expected.  I have replaced the original with a stronger solution that I was holding in reserve.  Congrats to DanK for solving the original (<a href=\"https:\/\/blogs.mathworks.com\/images\/pick\/puzzlerPokerOriginal.zip\"> found <\/a> here in a zip).  The contest is still running with the upgraded computerHand.p<\/p>\n<hr \/>\n<p>I have been known to play the occasional hand of poker, so I have this poker-inspired challenge.  I made up a simple poker type of game.  The deck consists of 16 cards, four each of the values 1-4.  Each player is dealt six cards.<\/p>\n<p><img src='https:\/\/blogs.mathworks.com\/pick\/..\/images\/pick\/cards1.JPG' alt='cards1.JPG' \/><\/p>\n<p>Once you have the cards, you set three hands: High (3 cards), Middle (2 cards), Low (1 card).  Each of these is scored differently.  <strong>High hand<\/strong> is ranked by the sum of the cards.  In the <strong>Middle hand<\/strong> largest pair wins.  Pairs beat non-pairs, then high card wins in un-paired hands, ties broken by second card.  In the <strong>Low hand<\/strong>, high card wins.<\/p>\n<p><img src='https:\/\/blogs.mathworks.com\/pick\/..\/images\/pick\/cards2.JPG' alt='cards2.JPG' \/><\/p>\n<p>Lets look at two examples with the same cards.  Notice the right hand player loses or draws depending on his strategy.<br \/>\n<img src='https:\/\/blogs.mathworks.com\/pick\/..\/images\/pick\/cards3.JPG' alt='cards3.JPG' \/><\/p>\n<p><img src='https:\/\/blogs.mathworks.com\/pick\/..\/images\/pick\/cards4.JPG' alt='cards4.JPG' \/><\/p>\n<p>You should be able to see that the right hand player could have won also!<\/p>\n<p>Each hand is worth 1 point for a win, 0.5 for a draw.  Win more than 1.5 of the three total points per round for the win.<br \/>\n<img src='https:\/\/blogs.mathworks.com\/pick\/..\/images\/pick\/cards5.JPG' alt='cards5.JPG' \/><\/p>\n<p>I coded up a good, but simple strategy.  I am confident that someone can make a strategy that will consistently win.  Free MATLAB t-shirt for the first to come up with it.<\/p>\n<p>Modify humanHand.m and submit it in its entirety in the comments.<\/p>\n<pre><code>\r\nfunction [high, middle, low] = humanHand(hand)\r\n% Make a valid, random hand for the human\r\n% hand will consist of six random 'cards' selected from\r\n% [1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4];\r\n%\r\n% Create three hands to be judged against the computer\r\n% doing the smae with six other cards selected from the remainder\r\n% of the above deck.\r\n%\r\n% HIGH hand consists for three cards.  The value of\r\n% the hand is the sum of the cards.  Higher is better.\r\n%\r\n% MIDDLE hand consists of two cards.  Any paired hand beats any non-paired hand.\r\n% If the competeing hands are both paired, highest pair wins.  If the\r\n% competing hands are not paired, then the highest card in each hand\r\n% determines winner.  If highest cards are the same, second highest cards\r\n% are compared.\r\n%\r\n% LOW hand consists of a single card.  Highest card wins.\r\n%\r\n\r\n% This is a terrible plan that makes random hands.\r\n\r\nrandomIndex = randperm(numel(hand));\r\n\r\nhand = hand(randomIndex);\r\n\r\nhigh   = hand(1:3);\r\nmiddle = hand(4:5);\r\nlow    = hand(6);\r\n\r\n<\/code><\/pre>\n<p>Here is the code to run the contest: (No need to modify this code)<\/p>\n<pre><code>\r\nfunction humanPerWins = main(numPlays)\r\n\r\nnumCardsInDeck = 16;\r\nnumCardsInHand =  6;\r\n\r\nfor i = 1:numPlays;\r\ndeck = ceil(randperm(numCardsInDeck)\/4);\r\n\r\nhand.computer = sort(deck(1:2:numCardsInHand*2 - 1));\r\nhand.human    = sort(deck(2:2:numCardsInHand*2    ));\r\nhand.deck     = sort(deck(numCardsInHand*2 + 1:numCardsInDeck));\r\n\r\n\r\n[highC, middleC, lowC] = computerHand(hand.computer);\r\n[highH, middleH, lowH] =    humanHand(hand.human);\r\n\r\nhumanScore(3) = compareHigh  (  highH ,   highC);\r\nhumanScore(2) = compareMiddle(middleH , middleC);\r\nhumanScore(1) = compareLow   (   lowH ,    lowC);\r\n\r\nhumanFinal(i) = compareLow(sum(humanScore), 1.5);\r\n\r\nend\r\n\r\n\r\nclf\r\nhist(humanFinal); ylim([0 numPlays])\r\nclc\r\nhumanPerWins = sum(humanFinal)\/numPlays * 100;\r\n\r\ndisp(['Your score against the computer: ' num2str(humanPerWins) '%.'])\r\n\r\nfunction score = compareHigh(H, C)\r\n\r\nif sum(H) > sum(C)\r\n    score = 1;\r\nelseif sum(H) < sum(C)\r\n    score = 0;\r\nelse\r\n    score = 0.5;\r\nend\r\n\r\nfunction score = compareMiddle(H, C)\r\n\r\npairH = (H(1) == H(2));\r\npairC = (C(1) == C(2));\r\n\r\nscore = compareLow(pairH, pairC);\r\n\r\nif score ~= 0.5 %is pair vs non-pair\r\n    return\r\nelse %is non-pairs or pairs\r\n\r\n    H = sort(H);\r\n    C = sort(C);\r\n\r\n    highH = H(2);\r\n    highC = C(2);\r\n\r\n    score = compareLow(highH, highC);\r\n\r\n    if score ~= 0.5\r\n        return\r\n    else\r\n        lowH = H(1);\r\n        lowC = C(1);\r\n\r\n        score = compareLow(lowH, lowC);\r\n    end\r\nend\r\n\r\nfunction score = compareLow(H, C)\r\n\r\nif H > C\r\n    score = 1;\r\nelseif H < C\r\n    score = 0;\r\nelse\r\n    score = 0.5;\r\nend\r\n<\/code><\/pre>\n<p>Remember to use the &lt;pre>&lt;code> and &lt;\/code>&lt;\/pre> tags around your code in the comments.<\/p>\n<p>The p-coded file that dictates the computer strategy is: <a href=\"https:\/\/blogs.mathworks.com\/images\/pick\/computerHand.p\">computerHand.p<br \/>\n<\/a><\/p>\n<p>You can find all three files <a href=\"https:\/\/blogs.mathworks.com\/images\/pick\/puzzlerPoker.zip\">here<\/a>.<\/p>\n<hr \/>\n<p>Some interesting notes:  My strategy against itself is about 50% win rate (of course).  I tried several strategies, and used the best one overall.  However, for any given \"hand vs hand\" one of the \"lesser\" strategies would beat mine.  I think a blended strategy that chooses from many strategies based on the cards will win.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Update:  This solution was found faster than expected.  I have replaced the original with a stronger solution that I was holding in reserve.  Congrats to DanK for solving the original ( found  here&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/videos\/2008\/06\/24\/puzzler-six-card-poker\/\">read more >><\/a><\/p>\n","protected":false},"author":68,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/280"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/users\/68"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/comments?post=280"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/280\/revisions"}],"predecessor-version":[{"id":2954,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/280\/revisions\/2954"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/media?parent=280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/categories?post=280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/tags?post=280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}