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<AGROW>
        possHands = unique(possHands,'rows');
        uniqueFound = size(possHands,1);
    end
    
    %The computer strategy, I think
    %========================================
    cS = possHands; %cs -> 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 >= 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 > 4,1))
                
                %His hand -> possHands(i,:)
                %My hand -> 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 && 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<AGROW>
                end
            end
        end
    end

    %Form of allPossible
    %==========================================================================
    %cols 1-6 -> his cards, 7-12 -> my cards,  13-18 -> an approach with my cards H M L 
    %19 whether or not I would win,lose,tie
    %20 -> 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 & 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) > sum(C)
        score = 1;
    elseif sum(H) < sum(C)
        score = 0;
    else
        score = 0.5;
    end

function score = compareMiddle(H, C)
    pairH = (H(1) == H(2));
    pairC = (C(1) == C(2));
    score = compareLow(pairH, pairC);
    if score ~= 0.5 %is pair vs non-pair
        return
    else %is non-pairs or pairs
        H = sort(H);
        C = sort(C);
        highH = H(2);
        highC = C(2);
        score = compareLow(highH, highC);
        if score ~= 0.5
            return
        else
            lowH = H(1);
            lowC = C(1);
            score = compareLow(lowH, lowC);
        end
    end

function score = compareLow(H, C)
    if H > C
        score = 1;
    elseif H < C
        score = 0;
    else
        score = 0.5;
    end

function Total = genAllHands(hand)
%Total -> 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');