Code covered by the BSD License  

Highlights from
Cribbage Suite

image thumbnail
from Cribbage Suite by Bryan
Cribbage Suite is a GUI interface designed to help teach and play the game of cribbage.

score5(card,isCrib)
function score = score5(card,isCrib)
%score = score4(hand) takes your hand of four cards and a starter and
%calculates the total score for you. The hand of four is the first 4 cards
%in "card" and the starter is number 5.
%
%Though the code I wrote is my own, I had to resort to internet research to
%find the algorithm for this scoring method. I was confused as to how I was
%going to keep from counting a pair twice, or how to see if cards were in a
%run. I wound up coming across this delightful little website
%(http://www.codeproject.com/useritems/criblib.asp) that had the algorithm,
%but his code was written in C instead of MATLAB, so I've borrowed that
%algorithm and wrote it out in MATLAB on my own. IT'S SO HUGE! Again, the
%code is my own; I have not copied it from the website; I merely used his
%method.
%
%One of these major hurdles was calculating 15's and pairs: once you know
%that card A and card B have been compared, how do you avoid counting card
%B and card A? Do you want to store that as a variable somewhere, and flag
%it? Turns out, you have to list every combo of cards possible in quints,
%quads, triples, doubles and singles. You can then look for 15's that way without repeating a count.
%
%Runs were the other major issue. Once you count a run of 3, how do you
%avoid counting a run of 4? Or re-counting the same run? The latter is easy
%to avoid; you simply use the Combination Method as listed above. The
%former, however, requires that you start with 5-card combos and work your
%way down. This way, once you find a 5-card run, you don't look for 4- or
%3-card runs.
%
%(C) Bryan Clark SID 18180234 bryanclark@berkeley.edu


%% INITIALIZATION

score = 0;
num15s = 0;
numPairs = 0;
numRuns = 0;
numRuns5 = 0;
numRuns4 = 0;
numRuns3 = 0;
flush4 = 0;
flush5 = 0;
nobs = 0;

%The following three cell arrays are the heart of this function. They are
%lists of every possible combination of cards. 
sets2Of5 = {[1 2] [1 3] [1 4] [1 5] [2 3] [2 4] [2 5] [3 4] [3 5] [4 5]}; %Every pair of cards within a 5-card group.
sets3Of5 = {[1 2 3] [1 2 4] [1 2 5] [1 3 4] [1 3 5] [1 4 5] [2 3 4] [2 3 5] [2 4 5] [3 4 5]}; %Every possible triplet of cards within a 5-card group.
sets4Of5 = {[1 2 3 4] [1 2 3 5] [1 2 4 5] [1 3 4 5] [2 3 4 5]}; %Every possible quad of cards within a 5-card group.

%% FLUSHES
%In cribbage, flushes are counted as follows:
%IF IT'S NOT THE CRIB: Only the four cards in your hand must be the same
%suit (4 points) but if the starter is as well, then you get 5 points.
%IF IT IS THE CRIB: Every card in the crib AND the starter MUST be the same
%suit; 5 points.

one = card(1);
two = card(2);
three = card(3);
four = card(4);
five = card(5);

if isCrib == 1 %IF it is the crib hand
    if isequal(get(one,'suit'),get(two,'suit'),get(three,'suit'),get(four,'suit'),get(five,'suit')) %Every card must have the same suit.
        flush5 = 1;
    end
else %IF it's not the crib hand.
    if isequal(get(one,'suit'),get(two,'suit'),get(three,'suit'),get(four,'suit'),get(five,'suit')) %All the same --> 5 points.
        flush5 = 1;
    elseif isequal(get(one,'suit'),get(two,'suit'),get(three,'suit'),get(four,'suit')) %Four in your hand the same --> 4 points.
        flush4 = 1;
    end
end


%% FIVE CARDS
%This section covers every possible 15 and run involved in a 5-card combo.
one = card(1);
two = card(2);
three = card(3);
four = card(4);
five = card(5);

%Fifteen
if get(one,'count') + get(two,'count') + get(three,'count') + get(four,'count') + get(five,'count') == 15;
    num15s = num15s + 1;
end

%Run
counts = [];
for j = 1:5;
    counts(j) = get(card(j),'order');
end

if isContinuous(counts)
    numRuns = 1;
    numRuns5 = 1;
end

%% FOUR CARDS

for i = 1:length(sets4Of5) %Looping over every 4-card combination
    quad = sets4Of5{i};
    %Create the temporary hand called for by the combination.
    one = card(quad(1));
    two = card(quad(2));
    three = card(quad(3));
    four = card(quad(4));
    
    % Fifteens
    if get(one,'count') + get(two,'count') + get(three,'count') + get(four,'count') == 15
        num15s = num15s + 1;
    end
    
    %Runs of four
    if numRuns == 0;
        orders = [];
        orders(1) = get(one,'order');
        orders(2) = get(two,'order');
        orders(3) = get(three,'order');
        orders(4) = get(four,'order');
        if isContinuous(orders)
            numRuns4 = numRuns4 + 1;
        end
    end
    
end

if numRuns4 > 0
    numRuns = numRuns4;
end        
           
%% THREE CARDS

for i = 1:length(sets3Of5) %Looping over every 3-card combination
    triplet = sets3Of5{i};
    %Create the temporary hand called for by the combination.
    one = card(triplet(1));
    two = card(triplet(2));
    three = card(triplet(3));
    
    %Fifteens
    if get(one,'count') + get(two,'count') + get(three,'count') == 15
        num15s = num15s + 1;
    end
    
    %Runs of three
    if numRuns == 0;
        orders = [];
        orders(1) = get(one,'order');
        orders(2) = get(two,'order');
        orders(3) = get(three,'order');
        if isContinuous(orders)
            numRuns3 = numRuns3 + 1;
        end
    end
    if numRuns3 > 0;
        numRums = numRuns3;
    end

end


%% TWO CARDS

for i = 1:length(sets2Of5) %Looping over every 2-card combination
    pair = sets2Of5{i};
    %Create the temporary hand called for by the combination.
    one = card(pair(1));
    two = card(pair(2));
    
    %Pairs
    if isequal(get(one,'face'),get(two,'face'))
        numPairs = numPairs + 1;
    end
    
    %Fifteens
    if get(one,'count') + get(two,'count') == 15
        num15s = num15s + 1;
    end
    
end

%% ONE CARD (NOBS)

for i = 1:4
    %Nobs
    one = card(i);
    starter = card(5);
    if isequal(get(one,'face'),'J');
        if isequal(get(one,'suit'),get(starter,'suit'));
            nobs = 1;
        end
    end
end

%% TALLY THE SCORE
%Fifteens
score = score + num15s*2;
%Pairs
score = score + numPairs*2;
%Runs
score = score + 5*numRuns5 + 4*numRuns4 + 3*numRuns3;
%Nobs
score = score + nobs;
%Flush
score = score + 5*flush5 + 4*flush4;

%% Display the score in the command window (to help teach players how to
%% count)
display('----------------------------------------------------------------------------------');
display('Here''s the stats for your last hand: ');
display(['Number of 15''s: ' num2str(num15s)]);
display(['Number of pairs: ' num2str(numPairs)]);
display(['Number of runs of 5: ' num2str(numRuns5)]);
display(['Number of runs of 4: ' num2str(numRuns4)]);
display(['Number of runs of 3: ' num2str(numRuns3)]);
display(['Nobs? ' num2str(nobs)]);
display(['Flush? ' num2str(flush5 + flush4)]);
display(['Total Score: ' num2str(score)]);
display('----------------------------------------------------------------------------------');


Contact us at files@mathworks.com