<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Puzzler: Six card poker</title>
	<atom:link href="http://blogs.mathworks.com/videos/2008/06/24/puzzler-six-card-poker/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/videos/2008/06/24/puzzler-six-card-poker/</link>
	<description>Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.</description>
	<lastBuildDate>Fri, 10 Feb 2012 20:31:50 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Doug</title>
		<link>http://blogs.mathworks.com/videos/2008/06/24/puzzler-six-card-poker/#comment-1040</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Wed, 16 Jul 2008 14:03:04 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2008/06/24/puzzler-six-card-poker/#comment-1040</guid>
		<description>Jim,

This is the kind of strategy that I expected would be needed to win against my computerHand.  I noted that no matter what strategy is picked by the computer there is almost always a strategy that will win by humanHand.  So, playing against an omniscient player would guarantee a landslide.

Your code does the best to be that omniscient player by generating all possible hands and figuring a strategy based on that.  There is hidden knowledge as to what cards are available and what the computer strategy is, so your 55% is very respectable, and the best I have seen.

The computer strategy that finally got used was this:
Make the best possible one card hand, then the best possible two card hand.

There are six possible &quot;simple, greedy&quot; strategies, basically make the best N card hand, then the best possible M card hand.  Of these six &quot;simple greedy&quot; strategies, [1 2 3] is the best against the others.  [2 1 3] is the second best.  I started the contest with [2 1 3] and then moved to [1 2 3] once DanK bested me.

-Thanks for playing!
Doug</description>
		<content:encoded><![CDATA[<p>Jim,</p>
<p>This is the kind of strategy that I expected would be needed to win against my computerHand.  I noted that no matter what strategy is picked by the computer there is almost always a strategy that will win by humanHand.  So, playing against an omniscient player would guarantee a landslide.</p>
<p>Your code does the best to be that omniscient player by generating all possible hands and figuring a strategy based on that.  There is hidden knowledge as to what cards are available and what the computer strategy is, so your 55% is very respectable, and the best I have seen.</p>
<p>The computer strategy that finally got used was this:<br />
Make the best possible one card hand, then the best possible two card hand.</p>
<p>There are six possible &#8220;simple, greedy&#8221; strategies, basically make the best N card hand, then the best possible M card hand.  Of these six &#8220;simple greedy&#8221; strategies, [1 2 3] is the best against the others.  [2 1 3] is the second best.  I started the contest with [2 1 3] and then moved to [1 2 3] once DanK bested me.</p>
<p>-Thanks for playing!<br />
Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jim Hokanson</title>
		<link>http://blogs.mathworks.com/videos/2008/06/24/puzzler-six-card-poker/#comment-1039</link>
		<dc:creator>Jim Hokanson</dc:creator>
		<pubDate>Tue, 15 Jul 2008 19:45:39 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2008/06/24/puzzler-six-card-poker/#comment-1039</guid>
		<description>[Jim&#039;s code is extensive, it will be easier to download it from here: &lt;a href=&quot;http://&quot; rel=&quot;nofollow&quot;&gt; http://blogs.mathworks.com/images/pick/humanHand.m &lt;/a&gt; -Doug]

Any thoughts? I think this (55.1 - 55.2%) may be the best you can do. Thanks.</description>
		<content:encoded><![CDATA[<p>[Jim's code is extensive, it will be easier to download it from here: <a href="http://" rel="nofollow"> </a><a href="http://blogs.mathworks.com/images/pick/humanHand.m" rel="nofollow">http://blogs.mathworks.com/images/pick/humanHand.m</a>  -Doug]</p>
<p>Any thoughts? I think this (55.1 &#8211; 55.2%) may be the best you can do. Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jim Hokanson</title>
		<link>http://blogs.mathworks.com/videos/2008/06/24/puzzler-six-card-poker/#comment-1038</link>
		<dc:creator>Jim Hokanson</dc:creator>
		<pubDate>Sun, 06 Jul 2008 17:28:31 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2008/06/24/puzzler-six-card-poker/#comment-1038</guid>
		<description>[Jim&#039;s code is extensive, it will be easier to download it from here: &lt;a href=&quot;http://&quot; rel=&quot;nofollow&quot;&gt; http://blogs.mathworks.com/images/pick/humanHand.m &lt;/a&gt; (copying from the code below might not work) -Doug]

Here&#039;s the last function, which keeps on getting chopped off.  The 3 functions used to determine the score for high, medium, and low are also needed.
&lt;pre&gt;&lt;code&gt;
function Total = genAllHands(hand)
%Total -&gt; n x 6 of High,Middle,Low
%all unique combinations with the card game rules i.e 2 2 2 3 4 4
%is the same as 2 2 2 4 3 4 but not as 2 2 3 2 4 4
    persistent perm5
    if isempty(perm5)
        perm5 = perms(1:5);
    end
    uniqNums = unique(hand);
    count = 0;
    for i = 1:length(uniqNums)
        tempHand = hand;
        I = find(hand == uniqNums(i),1);
        tempHand(I) = [];
        for j = 1:120
            count = count + 1;
            H(count,:) = sort(tempHand(perm5(j,1:3)));
            M(count,:) = sort(tempHand(perm5(j,4:5)));
            L(count,:) = uniqNums(i);
        end
    end
        Total = unique([H M L],&#039;rows&#039;);
&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>[Jim's code is extensive, it will be easier to download it from here: <a href="http://" rel="nofollow"> </a><a href="http://blogs.mathworks.com/images/pick/humanHand.m" rel="nofollow">http://blogs.mathworks.com/images/pick/humanHand.m</a>  (copying from the code below might not work) -Doug]</p>
<p>Here&#8217;s the last function, which keeps on getting chopped off.  The 3 functions used to determine the score for high, medium, and low are also needed.</p>
<pre><code>
function Total = genAllHands(hand)
%Total -&gt; n x 6 of High,Middle,Low
%all unique combinations with the card game rules i.e 2 2 2 3 4 4
%is the same as 2 2 2 4 3 4 but not as 2 2 3 2 4 4
    persistent perm5
    if isempty(perm5)
        perm5 = perms(1:5);
    end
    uniqNums = unique(hand);
    count = 0;
    for i = 1:length(uniqNums)
        tempHand = hand;
        I = find(hand == uniqNums(i),1);
        tempHand(I) = [];
        for j = 1:120
            count = count + 1;
            H(count,:) = sort(tempHand(perm5(j,1:3)));
            M(count,:) = sort(tempHand(perm5(j,4:5)));
            L(count,:) = uniqNums(i);
        end
    end
        Total = unique([H M L],'rows');
</code></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jim Hokanson</title>
		<link>http://blogs.mathworks.com/videos/2008/06/24/puzzler-six-card-poker/#comment-1037</link>
		<dc:creator>Jim Hokanson</dc:creator>
		<pubDate>Sun, 06 Jul 2008 17:23:29 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2008/06/24/puzzler-six-card-poker/#comment-1037</guid>
		<description>[Jim&#039;s code is extensive, it will be easier to download it from here: &lt;a href=&quot;http://&quot; rel=&quot;nofollow&quot;&gt; http://blogs.mathworks.com/images/pick/humanHand.m &lt;/a&gt; (copying from the code below might not work) -Doug]

Oops, sorry for the second long attachment, I correctly implemented the probability model to get about 55.1 - 55.2%, also some code at the end was cut off.
&lt;pre&gt;&lt;code&gt;
function [high, middle, low] = humanHand(hand)
persistent strategy
handInput = hand; %a bit sloppy, I overwrote this in the persistent code
if isempty(strategy)
    %Generation of all possible hands
    %===================================
    numCardsInDeck = 16;
    uniqueFound = 0;
        possHands = [];
    while uniqueFound ~= 68
        runSize = 1000;
        tempPossHands = zeros(runSize,6);
        for i = 1:runSize %Not sure what the best way of dividing this up is
            deck = ceil(randperm(numCardsInDeck)/4);
            tempPossHands(i,:) = sort(deck(1:6));
        end
        tempPossHands = unique(tempPossHands,&#039;rows&#039;);
        possHands = [possHands; tempPossHands]; %#ok
        possHands = unique(possHands,&#039;rows&#039;);
        uniqueFound = size(possHands,1);
    end

    %The computer strategy, I think
    %========================================
    cS = possHands; %cs -&gt; computer Strategy
    totalCS = zeros(size(cS,1),12);
    for i = 1:size(cS,1)
       hand = cS(i,:); %Note these values are already sorted
       L = hand(end);
       hand(end) = [];
       N = hist(hand,[1 2 3 4]);
       I = find(N &gt;= 2); %You&#039;ll always get a pair by the nature of the game
       I = I(end); %Grab the highest valued set
       M = [I I]; %Note, there will always be at least 2 pairs, so this always works
       I = find(hand == I,2); %find 2 of these values and delete them
       hand(I) = [];
       H = hand;
       totalCS(i,:) = [cS(i,:) H M L]; %Note going back to cS because we&#039;ve deleted from hand
    end

    %AT THIS POINT WE HAVE THE FULL STRATEGY OF THE OPPONENT NOW TO SEE WHAT WE SHOULD DO
    %======================================================================
    allCount = 0;
    for i = 1:size(possHands,1)
        for j = 1:size(possHands,1)
            BothHands = [possHands(i,:) possHands(j,:)];
            N = hist(BothHands,[1 2 3 4]);
            if isempty(find(N &gt; 4,1))

                %His hand -&gt; possHands(i,:)
                %My hand -&gt; possHands(j,:)
                %with my cards removed, what is his probability of getting his hand
                counts = [4 4 4 4];
                mH = possHands(j,:);
                counts = counts - hist(mH,[1 2 3 4]);
                counts2 = hist(possHands(i,:),[1 2 3 4]);

                p = 1;
                for k = 1:4
                    if counts(k) == 0 &amp;&amp; counts2(k) ~= 0
                        p = 0; %This should never occur with if statement above
                    else
                        p = p*nchoosek(counts(k),counts2(k));
                    end
                end

                [Total] = genAllHands(possHands(j,:));
                for k = 1:size(Total,1)
                    wins = determineWin(Total(k,1:3),Total(k,4:5),Total(k,6),totalCS(i,7:9),totalCS(i,10:11),totalCS(i,12));
                    allCount = allCount + 1;
                    allPossible(allCount,:) = [possHands(i,:) possHands(j,:) Total(k,:) wins(4) p];   %#ok
                end
            end
        end
    end

    %Form of allPossible
    %==========================================================================
    %cols 1-6 -&gt; his cards, 7-12 -&gt; my cards,  13-18 -&gt; an approach with my cards H M L
    %19 whether or not I would win,lose,tie
    %20 -&gt; probability of that hand (really ways to get hand with my hand being what it is)

    %NOW, KNOWING EVERYTHING I CAN PLAY AGAINST THE COMPUTER I DETERMINE THE BEST STRATEGY, FOR WHATEVER HAND I GET
    strategy = zeros(size(possHands,1),12);
    for i = 1:length(possHands)
        hand = possHands(i,:);
        badRows = ismember(allPossible(:,1:6),hand,&#039;rows&#039;); %The computer won&#039;t have my hand!
        rowsMine = ismember(allPossible(:,7:12),hand,&#039;rows&#039;);
        rowsMine(badRows == 1) = 0;
        remainder = allPossible(rowsMine,13:end);

        %This was some combining of code from different files
        %I had possHands here originally, but not I have possHands from above
        %so I made this one possHands2
        [possHands2,I,J] = unique(remainder(:,1:6),&#039;rows&#039;); %get all the computers hands &amp; what I can play against them

        scoresTot = zeros(size(possHands2,1),1);
        scoresTot2 = zeros(size(possHands2,1),1);
        for j = 1:size(possHands2,1)
            temp = remainder(J == j,7);
            p = remainder(J == j,8);
            scoresTot(j) = sum(temp.*p);
            scoresTot2(j) = scoresTot(j)/length(temp);
        end
        [junk,I] = max(scoresTot);
        [junk2,I2] = max(scoresTot2);
        if I ~= I2
            keyboard
        end

        high   = possHands2(I,1:3);
        middle = possHands2(I,4:5);
        low    = possHands2(I,6);
        strategy(i,:) = [hand high middle low];
    end

end %if isempty (strategy)

    %=========================================
    %The code that runs every trial
    %=========================================
    I = ismember(strategy(:,1:6),handInput,&#039;rows&#039;);
    rowToUse = strategy(I,7:12);

    high   = rowToUse(1:3);
    middle = rowToUse(4:5);
    low    = rowToUse(6);


%=======================================================================
%EXTRA FUNCTIONS
%=====================================================================

function wins = determineWin(highH,middleH,lowH,highC,middleC,lowC)
    humanScore(3) = compareHigh  (  highH ,   highC);
    humanScore(2) = compareMiddle(middleH , middleC);
    humanScore(1) = compareLow   (   lowH ,    lowC);
    win = compareLow(sum(humanScore), 1.5);
    wins = [humanScore(3),humanScore(2),humanScore(1),win];

function score = compareHigh(H, C)
    if sum(H) &gt; sum(C)
        score = 1;
    elseif sum(H)  C
        score = 1;
    elseif H  n x 6 of High,Middle,Low
%all unique combinations with the card game rules i.e 2 2 2 3 4 4
%is the same as 2 2 2 4 3 4 but not as 2 2 3 2 4 4
    persistent perm5
    if isempty(perm5)
        perm5 = perms(1:5);
    end
    uniqNums = unique(hand);
    count = 0;
    for i = 1:length(uniqNums)
        tempHand = hand;
        I = find(hand == uniqNums(i),1);
        tempHand(I) = [];
        for j = 1:120
            count = count + 1;
            H(count,:) = sort(tempHand(perm5(j,1:3)));
            M(count,:) = sort(tempHand(perm5(j,4:5)));
            L(count,:) = uniqNums(i);
        end
    end
        Total = unique([H M L],&#039;rows&#039;);&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>[Jim's code is extensive, it will be easier to download it from here: <a href="http://" rel="nofollow"> </a><a href="http://blogs.mathworks.com/images/pick/humanHand.m" rel="nofollow">http://blogs.mathworks.com/images/pick/humanHand.m</a>  (copying from the code below might not work) -Doug]</p>
<p>Oops, sorry for the second long attachment, I correctly implemented the probability model to get about 55.1 &#8211; 55.2%, also some code at the end was cut off.</p>
<pre><code>
function [high, middle, low] = humanHand(hand)
persistent strategy
handInput = hand; %a bit sloppy, I overwrote this in the persistent code
if isempty(strategy)
    %Generation of all possible hands
    %===================================
    numCardsInDeck = 16;
    uniqueFound = 0;
        possHands = [];
    while uniqueFound ~= 68
        runSize = 1000;
        tempPossHands = zeros(runSize,6);
        for i = 1:runSize %Not sure what the best way of dividing this up is
            deck = ceil(randperm(numCardsInDeck)/4);
            tempPossHands(i,:) = sort(deck(1:6));
        end
        tempPossHands = unique(tempPossHands,'rows');
        possHands = [possHands; tempPossHands]; %#ok
        possHands = unique(possHands,'rows');
        uniqueFound = size(possHands,1);
    end

    %The computer strategy, I think
    %========================================
    cS = possHands; %cs -&gt; computer Strategy
    totalCS = zeros(size(cS,1),12);
    for i = 1:size(cS,1)
       hand = cS(i,:); %Note these values are already sorted
       L = hand(end);
       hand(end) = [];
       N = hist(hand,[1 2 3 4]);
       I = find(N &gt;= 2); %You'll always get a pair by the nature of the game
       I = I(end); %Grab the highest valued set
       M = [I I]; %Note, there will always be at least 2 pairs, so this always works
       I = find(hand == I,2); %find 2 of these values and delete them
       hand(I) = [];
       H = hand;
       totalCS(i,:) = [cS(i,:) H M L]; %Note going back to cS because we've deleted from hand
    end

    %AT THIS POINT WE HAVE THE FULL STRATEGY OF THE OPPONENT NOW TO SEE WHAT WE SHOULD DO
    %======================================================================
    allCount = 0;
    for i = 1:size(possHands,1)
        for j = 1:size(possHands,1)
            BothHands = [possHands(i,:) possHands(j,:)];
            N = hist(BothHands,[1 2 3 4]);
            if isempty(find(N &gt; 4,1))

                %His hand -&gt; possHands(i,:)
                %My hand -&gt; possHands(j,:)
                %with my cards removed, what is his probability of getting his hand
                counts = [4 4 4 4];
                mH = possHands(j,:);
                counts = counts - hist(mH,[1 2 3 4]);
                counts2 = hist(possHands(i,:),[1 2 3 4]);

                p = 1;
                for k = 1:4
                    if counts(k) == 0 &amp;&amp; counts2(k) ~= 0
                        p = 0; %This should never occur with if statement above
                    else
                        p = p*nchoosek(counts(k),counts2(k));
                    end
                end

                [Total] = genAllHands(possHands(j,:));
                for k = 1:size(Total,1)
                    wins = determineWin(Total(k,1:3),Total(k,4:5),Total(k,6),totalCS(i,7:9),totalCS(i,10:11),totalCS(i,12));
                    allCount = allCount + 1;
                    allPossible(allCount,:) = [possHands(i,:) possHands(j,:) Total(k,:) wins(4) p];   %#ok
                end
            end
        end
    end

    %Form of allPossible
    %==========================================================================
    %cols 1-6 -&gt; his cards, 7-12 -&gt; my cards,  13-18 -&gt; an approach with my cards H M L
    %19 whether or not I would win,lose,tie
    %20 -&gt; probability of that hand (really ways to get hand with my hand being what it is)

    %NOW, KNOWING EVERYTHING I CAN PLAY AGAINST THE COMPUTER I DETERMINE THE BEST STRATEGY, FOR WHATEVER HAND I GET
    strategy = zeros(size(possHands,1),12);
    for i = 1:length(possHands)
        hand = possHands(i,:);
        badRows = ismember(allPossible(:,1:6),hand,'rows'); %The computer won't have my hand!
        rowsMine = ismember(allPossible(:,7:12),hand,'rows');
        rowsMine(badRows == 1) = 0;
        remainder = allPossible(rowsMine,13:end);

        %This was some combining of code from different files
        %I had possHands here originally, but not I have possHands from above
        %so I made this one possHands2
        [possHands2,I,J] = unique(remainder(:,1:6),'rows'); %get all the computers hands &amp; what I can play against them

        scoresTot = zeros(size(possHands2,1),1);
        scoresTot2 = zeros(size(possHands2,1),1);
        for j = 1:size(possHands2,1)
            temp = remainder(J == j,7);
            p = remainder(J == j,8);
            scoresTot(j) = sum(temp.*p);
            scoresTot2(j) = scoresTot(j)/length(temp);
        end
        [junk,I] = max(scoresTot);
        [junk2,I2] = max(scoresTot2);
        if I ~= I2
            keyboard
        end

        high   = possHands2(I,1:3);
        middle = possHands2(I,4:5);
        low    = possHands2(I,6);
        strategy(i,:) = [hand high middle low];
    end

end %if isempty (strategy)

    %=========================================
    %The code that runs every trial
    %=========================================
    I = ismember(strategy(:,1:6),handInput,'rows');
    rowToUse = strategy(I,7:12);

    high   = rowToUse(1:3);
    middle = rowToUse(4:5);
    low    = rowToUse(6);

%=======================================================================
%EXTRA FUNCTIONS
%=====================================================================

function wins = determineWin(highH,middleH,lowH,highC,middleC,lowC)
    humanScore(3) = compareHigh  (  highH ,   highC);
    humanScore(2) = compareMiddle(middleH , middleC);
    humanScore(1) = compareLow   (   lowH ,    lowC);
    win = compareLow(sum(humanScore), 1.5);
    wins = [humanScore(3),humanScore(2),humanScore(1),win];

function score = compareHigh(H, C)
    if sum(H) &gt; sum(C)
        score = 1;
    elseif sum(H)  C
        score = 1;
    elseif H  n x 6 of High,Middle,Low
%all unique combinations with the card game rules i.e 2 2 2 3 4 4
%is the same as 2 2 2 4 3 4 but not as 2 2 3 2 4 4
    persistent perm5
    if isempty(perm5)
        perm5 = perms(1:5);
    end
    uniqNums = unique(hand);
    count = 0;
    for i = 1:length(uniqNums)
        tempHand = hand;
        I = find(hand == uniqNums(i),1);
        tempHand(I) = [];
        for j = 1:120
            count = count + 1;
            H(count,:) = sort(tempHand(perm5(j,1:3)));
            M(count,:) = sort(tempHand(perm5(j,4:5)));
            L(count,:) = uniqNums(i);
        end
    end
        Total = unique([H M L],'rows');</code></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jim Hokanson</title>
		<link>http://blogs.mathworks.com/videos/2008/06/24/puzzler-six-card-poker/#comment-1036</link>
		<dc:creator>Jim Hokanson</dc:creator>
		<pubDate>Sun, 06 Jul 2008 07:50:25 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2008/06/24/puzzler-six-card-poker/#comment-1036</guid>
		<description>[Jim&#039;s code is extensive, it will be easier to download it from here: &lt;a href=&quot;http://&quot; rel=&quot;nofollow&quot;&gt; http://blogs.mathworks.com/images/pick/humanHand.m &lt;/a&gt; (copying from the code below might not work) -Doug]

Here&#039;s my approach.  It is a brute force method as well.  Using the computers input (knowing their hand) about the best one can expect to do is 65%. I replicate the computer strategy in creating the persistent variable (the look up table).  Then I figure out for every possible hand of mine (68 total) what high, middle, and low to play that will beat the computer most often.  In the end the code that runs every time just looks up what hand I got, then plays the one most likely to win.

On average I get between 54 to 55% correct.  I tried for a few hours to consistently get above 55% but it just wasn&#039;t happening.  The only idea which I didn&#039;t implement as it would mean losing a lot of the up front calculations would have been to build some history into my hands so that whatever hands I&#039;ve had recently, I figure the computer won&#039;t have.  Although even that is tricky as there are so many ways to get the same hand. My attempt at building in a probability as to which hands were more or less likely for the current hand like 111144 being less likely then 112233 ended up hurting my predictions.

&lt;pre&gt;&lt;code&gt;
function [high, middle, low] = humanHand(hand)

persistent strategy
handInput = hand; %a bit sloppy, I overwrote this in the persistent code

if isempty(strategy)
    %Generation of all possible hands
    %===================================
    numCardsInDeck = 16;
    uniqueFound = 0;
        possHands = [];
    while uniqueFound ~= 68
        runSize = 1000;
        tempPossHands = zeros(runSize,6);
        for i = 1:runSize %Not sure what the best way of dividing this up is
            deck = ceil(randperm(numCardsInDeck)/4);
            tempPossHands(i,:) = sort(deck(1:6));
        end
        tempPossHands = unique(tempPossHands,&#039;rows&#039;);
        possHands = [possHands; tempPossHands]; %#ok
        possHands = unique(possHands,&#039;rows&#039;);
        uniqueFound = size(possHands,1);
    end

    %calculating the probability -&gt; I didn&#039;t actually use this
    %========================================
    totalP = zeros(1,size(possHands,1));
    for i = 1:size(possHands,1)
       count = [4 4 4 4];
       p = 1;
       for j = 1:6
           index = possHands(i,j);
           p = p*count(index)/sum(count);
           count(index) = count(index) - 1;
       end
       totalP(i) = p;
    end

    %The computer strategy, I think
    %========================================
    cS = possHands; %cs -&gt; computer Strategy
    totalCS = zeros(size(cS,1),12);
    for i = 1:size(cS,1)
       hand = cS(i,:); %Note these values are already sorted
       L = hand(end);
       hand(end) = [];
       N = hist(hand,[1 2 3 4]);
       I = find(N &gt;= 2); %You&#039;ll always get a pair by the nature of the game
       I = I(end); %Grab the highest valued set
       M = [I I]; %Note, there will always be at least 2 pairs, so this always works
       I = find(hand == I,2); %find 2 of these values and delete them
       hand(I) = [];
       H = hand;
       totalCS(i,:) = [cS(i,:) H M L]; %Note going back to cS because we&#039;ve deleted from hand
    end

    %AT THIS POINT WE HAVE THE FULL STRATEGY OF THE OPPONENT NOW TO SEE WHAT WE SHOULD DO
    %==========================================================================

    allCount = 0;
    for i = 1:size(possHands,1)
        for j = 1:size(possHands,1)
            BothHands = [possHands(i,:) possHands(j,:)];
            N = hist(BothHands,[1 2 3 4]);
            if isempty(find(N &gt; 4,1))
                [Total] = genAllHands(possHands(j,:));
                for k = 1:size(Total,1)
                    wins = determineWin(Total(k,1:3),Total(k,4:5),Total(k,6),totalCS(i,7:9),totalCS(i,10:11),totalCS(i,12));
                    allCount = allCount + 1;
                    allPossible(allCount,:) = [possHands(i,:) possHands(j,:) Total(k,:) wins(4) totalP(i)];   %#ok
                end

            end
        end
    end

    %Form of allPossible
    %==========================================================================
    %cols 1-6 -&gt; his cards, 7-12 -&gt; my cards,  13-18 -&gt; an approach with my cards H M L
    %19 whether or not I would win,lose,tie
    %20 -&gt; probability of that hand


    %NOW, KNOWING EVERYTHING I CAN PLAY AGAINST THE COMPUTER
    %I DETERMINE THE BEST STRATEGY, FOR WHATEVER HAND I GET


    strategy = zeros(size(possHands,1),12);
    for i = 1:length(possHands)
        hand = possHands(i,:);
        badRows = ismember(allPossible(:,1:6),hand,&#039;rows&#039;); %The computer won&#039;t have my hand!
        rowsMine = ismember(allPossible(:,7:12),hand,&#039;rows&#039;);
        rowsMine(badRows == 1) = 0;
        remainder = allPossible(rowsMine,13:end);

        %This was some combining of code from different files
        %I had possHands here originally, but not I have possHands from above
        %so I made this one possHands2
        [possHands2,I,J] = unique(remainder(:,1:6),&#039;rows&#039;); %get all the computers hands &amp; what I can play against them

        scoresTot = zeros(size(possHands2,1),1);
        scoresTot2 = zeros(size(possHands2,1),1);
        for j = 1:size(possHands2,1)
            temp = remainder(J == j,7);
            scoresTot(j) = sum(temp);
            scoresTot2(j) = scoresTot(j)/length(temp);
        end
        [junk,I] = max(scoresTot);
        [junk2,I2] = max(scoresTot2);
        if I ~= I2
            keyboard
        end

        high   = possHands2(I,1:3);
        middle = possHands2(I,4:5);
        low    = possHands2(I,6);
        strategy(i,:) = [hand high middle low];
    end

end %if isempty (strategy)

    %=========================================
    %The code that runs every trial
    %=========================================
    I = ismember(strategy(:,1:6),handInput,&#039;rows&#039;);
    rowToUse = strategy(I,7:12);

    high   = rowToUse(1:3);
    middle = rowToUse(4:5);
    low    = rowToUse(6);


%=======================================================================
%EXTRA FUNCTIONS
%=====================================================================

function wins = determineWin(highH,middleH,lowH,highC,middleC,lowC)
    humanScore(3) = compareHigh  (  highH ,   highC);
    humanScore(2) = compareMiddle(middleH , middleC);
    humanScore(1) = compareLow   (   lowH ,    lowC);
    win = compareLow(sum(humanScore), 1.5);
    wins = [humanScore(3),humanScore(2),humanScore(1),win];

function score = compareHigh(H, C)
    if sum(H) &gt; sum(C)
        score = 1;
    elseif sum(H)  C
        score = 1;
    elseif H  n x 6 of High,Middle,Low
%all unique combinations with the rules i.e 2 2 2 3 4 4
%is the same as 2 2 2 4 3 4
    persistent perm5
    if isempty(perm5)
        perm5 = perms(1:5);
    end

    uniqNums = unique(hand);

    count = 0;
    for i = 1:length(uniqNums)
        tempHand = hand;
        I = find(hand == uniqNums(i),1);
        tempHand(I) = [];
        for j = 1:120
            count = count + 1;
            H(count,:) = sort(tempHand(perm5(j,1:3)));
            M(count,:) = sort(tempHand(perm5(j,4:5)));
            L(count,:) = uniqNums(i);
        end
    end
        Total = unique([H M L],&#039;rows&#039;);&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>[Jim's code is extensive, it will be easier to download it from here: <a href="http://" rel="nofollow"> </a><a href="http://blogs.mathworks.com/images/pick/humanHand.m" rel="nofollow">http://blogs.mathworks.com/images/pick/humanHand.m</a>  (copying from the code below might not work) -Doug]</p>
<p>Here&#8217;s my approach.  It is a brute force method as well.  Using the computers input (knowing their hand) about the best one can expect to do is 65%. I replicate the computer strategy in creating the persistent variable (the look up table).  Then I figure out for every possible hand of mine (68 total) what high, middle, and low to play that will beat the computer most often.  In the end the code that runs every time just looks up what hand I got, then plays the one most likely to win.</p>
<p>On average I get between 54 to 55% correct.  I tried for a few hours to consistently get above 55% but it just wasn&#8217;t happening.  The only idea which I didn&#8217;t implement as it would mean losing a lot of the up front calculations would have been to build some history into my hands so that whatever hands I&#8217;ve had recently, I figure the computer won&#8217;t have.  Although even that is tricky as there are so many ways to get the same hand. My attempt at building in a probability as to which hands were more or less likely for the current hand like 111144 being less likely then 112233 ended up hurting my predictions.</p>
<pre><code>
function [high, middle, low] = humanHand(hand)

persistent strategy
handInput = hand; %a bit sloppy, I overwrote this in the persistent code

if isempty(strategy)
    %Generation of all possible hands
    %===================================
    numCardsInDeck = 16;
    uniqueFound = 0;
        possHands = [];
    while uniqueFound ~= 68
        runSize = 1000;
        tempPossHands = zeros(runSize,6);
        for i = 1:runSize %Not sure what the best way of dividing this up is
            deck = ceil(randperm(numCardsInDeck)/4);
            tempPossHands(i,:) = sort(deck(1:6));
        end
        tempPossHands = unique(tempPossHands,'rows');
        possHands = [possHands; tempPossHands]; %#ok
        possHands = unique(possHands,'rows');
        uniqueFound = size(possHands,1);
    end

    %calculating the probability -&gt; I didn't actually use this
    %========================================
    totalP = zeros(1,size(possHands,1));
    for i = 1:size(possHands,1)
       count = [4 4 4 4];
       p = 1;
       for j = 1:6
           index = possHands(i,j);
           p = p*count(index)/sum(count);
           count(index) = count(index) - 1;
       end
       totalP(i) = p;
    end

    %The computer strategy, I think
    %========================================
    cS = possHands; %cs -&gt; computer Strategy
    totalCS = zeros(size(cS,1),12);
    for i = 1:size(cS,1)
       hand = cS(i,:); %Note these values are already sorted
       L = hand(end);
       hand(end) = [];
       N = hist(hand,[1 2 3 4]);
       I = find(N &gt;= 2); %You'll always get a pair by the nature of the game
       I = I(end); %Grab the highest valued set
       M = [I I]; %Note, there will always be at least 2 pairs, so this always works
       I = find(hand == I,2); %find 2 of these values and delete them
       hand(I) = [];
       H = hand;
       totalCS(i,:) = [cS(i,:) H M L]; %Note going back to cS because we've deleted from hand
    end

    %AT THIS POINT WE HAVE THE FULL STRATEGY OF THE OPPONENT NOW TO SEE WHAT WE SHOULD DO
    %==========================================================================

    allCount = 0;
    for i = 1:size(possHands,1)
        for j = 1:size(possHands,1)
            BothHands = [possHands(i,:) possHands(j,:)];
            N = hist(BothHands,[1 2 3 4]);
            if isempty(find(N &gt; 4,1))
                [Total] = genAllHands(possHands(j,:));
                for k = 1:size(Total,1)
                    wins = determineWin(Total(k,1:3),Total(k,4:5),Total(k,6),totalCS(i,7:9),totalCS(i,10:11),totalCS(i,12));
                    allCount = allCount + 1;
                    allPossible(allCount,:) = [possHands(i,:) possHands(j,:) Total(k,:) wins(4) totalP(i)];   %#ok
                end

            end
        end
    end

    %Form of allPossible
    %==========================================================================
    %cols 1-6 -&gt; his cards, 7-12 -&gt; my cards,  13-18 -&gt; an approach with my cards H M L
    %19 whether or not I would win,lose,tie
    %20 -&gt; probability of that hand

    %NOW, KNOWING EVERYTHING I CAN PLAY AGAINST THE COMPUTER
    %I DETERMINE THE BEST STRATEGY, FOR WHATEVER HAND I GET

    strategy = zeros(size(possHands,1),12);
    for i = 1:length(possHands)
        hand = possHands(i,:);
        badRows = ismember(allPossible(:,1:6),hand,'rows'); %The computer won't have my hand!
        rowsMine = ismember(allPossible(:,7:12),hand,'rows');
        rowsMine(badRows == 1) = 0;
        remainder = allPossible(rowsMine,13:end);

        %This was some combining of code from different files
        %I had possHands here originally, but not I have possHands from above
        %so I made this one possHands2
        [possHands2,I,J] = unique(remainder(:,1:6),'rows'); %get all the computers hands &amp; what I can play against them

        scoresTot = zeros(size(possHands2,1),1);
        scoresTot2 = zeros(size(possHands2,1),1);
        for j = 1:size(possHands2,1)
            temp = remainder(J == j,7);
            scoresTot(j) = sum(temp);
            scoresTot2(j) = scoresTot(j)/length(temp);
        end
        [junk,I] = max(scoresTot);
        [junk2,I2] = max(scoresTot2);
        if I ~= I2
            keyboard
        end

        high   = possHands2(I,1:3);
        middle = possHands2(I,4:5);
        low    = possHands2(I,6);
        strategy(i,:) = [hand high middle low];
    end

end %if isempty (strategy)

    %=========================================
    %The code that runs every trial
    %=========================================
    I = ismember(strategy(:,1:6),handInput,'rows');
    rowToUse = strategy(I,7:12);

    high   = rowToUse(1:3);
    middle = rowToUse(4:5);
    low    = rowToUse(6);

%=======================================================================
%EXTRA FUNCTIONS
%=====================================================================

function wins = determineWin(highH,middleH,lowH,highC,middleC,lowC)
    humanScore(3) = compareHigh  (  highH ,   highC);
    humanScore(2) = compareMiddle(middleH , middleC);
    humanScore(1) = compareLow   (   lowH ,    lowC);
    win = compareLow(sum(humanScore), 1.5);
    wins = [humanScore(3),humanScore(2),humanScore(1),win];

function score = compareHigh(H, C)
    if sum(H) &gt; sum(C)
        score = 1;
    elseif sum(H)  C
        score = 1;
    elseif H  n x 6 of High,Middle,Low
%all unique combinations with the rules i.e 2 2 2 3 4 4
%is the same as 2 2 2 4 3 4
    persistent perm5
    if isempty(perm5)
        perm5 = perms(1:5);
    end

    uniqNums = unique(hand);

    count = 0;
    for i = 1:length(uniqNums)
        tempHand = hand;
        I = find(hand == uniqNums(i),1);
        tempHand(I) = [];
        for j = 1:120
            count = count + 1;
            H(count,:) = sort(tempHand(perm5(j,1:3)));
            M(count,:) = sort(tempHand(perm5(j,4:5)));
            L(count,:) = uniqNums(i);
        end
    end
        Total = unique([H M L],'rows');</code></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug</title>
		<link>http://blogs.mathworks.com/videos/2008/06/24/puzzler-six-card-poker/#comment-1035</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Thu, 26 Jun 2008 20:40:40 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2008/06/24/puzzler-six-card-poker/#comment-1035</guid>
		<description>Adam,

Yes, I should have pre-allocated that array.  Pre-allocation does help speed when you can do it.  The effects are most noticeable on larger vectors and arrays.

Good catch,
Doug</description>
		<content:encoded><![CDATA[<p>Adam,</p>
<p>Yes, I should have pre-allocated that array.  Pre-allocation does help speed when you can do it.  The effects are most noticeable on larger vectors and arrays.</p>
<p>Good catch,<br />
Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adam</title>
		<link>http://blogs.mathworks.com/videos/2008/06/24/puzzler-six-card-poker/#comment-1034</link>
		<dc:creator>Adam</dc:creator>
		<pubDate>Thu, 26 Jun 2008 20:13:27 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2008/06/24/puzzler-six-card-poker/#comment-1034</guid>
		<description>you should pre-allocate your humanFinal array.

If I can ever do better than 45% I&#039;ll post a result :-/</description>
		<content:encoded><![CDATA[<p>you should pre-allocate your humanFinal array.</p>
<p>If I can ever do better than 45% I&#8217;ll post a result :-/</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug</title>
		<link>http://blogs.mathworks.com/videos/2008/06/24/puzzler-six-card-poker/#comment-1033</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Wed, 25 Jun 2008 17:47:15 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2008/06/24/puzzler-six-card-poker/#comment-1033</guid>
		<description>Pete,

Thank you for that.  I have updated all the files and confirmed they work from 2007a forward.

Doug</description>
		<content:encoded><![CDATA[<p>Pete,</p>
<p>Thank you for that.  I have updated all the files and confirmed they work from 2007a forward.</p>
<p>Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pete</title>
		<link>http://blogs.mathworks.com/videos/2008/06/24/puzzler-six-card-poker/#comment-1032</link>
		<dc:creator>pete</dc:creator>
		<pubDate>Wed, 25 Jun 2008 17:36:52 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2008/06/24/puzzler-six-card-poker/#comment-1032</guid>
		<description>Is there a version problem here?

People without 2008A can&#039;t seem to run the p-code.  Any chance of a 2007-compatible computerHand.p?</description>
		<content:encoded><![CDATA[<p>Is there a version problem here?</p>
<p>People without 2008A can&#8217;t seem to run the p-code.  Any chance of a 2007-compatible computerHand.p?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Timur</title>
		<link>http://blogs.mathworks.com/videos/2008/06/24/puzzler-six-card-poker/#comment-1031</link>
		<dc:creator>Timur</dc:creator>
		<pubDate>Wed, 25 Jun 2008 14:44:57 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/videos/2008/06/24/puzzler-six-card-poker/#comment-1031</guid>
		<description>Sorry, I accidently altered the hand I was given in the code.</description>
		<content:encoded><![CDATA[<p>Sorry, I accidently altered the hand I was given in the code.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

