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.

choose6Callback.m
%choose6Callback.m
%choose6Callback is the Callback function for the choose6 GUI. 
%Assembles the hand, runs every combination through "score4", finds the
%maximum score and displays it on the right side of the "choose6" GUI.

%% ASSEMBLE THE HAND AND ALL THE NEEDED DATA FOR IT.
suit6(1) = get(DchooseSuit1,'Value');
suit6(2) = get(DchooseSuit2,'Value');
suit6(3) = get(DchooseSuit3,'Value');
suit6(4) = get(DchooseSuit4,'Value');
suit6(5) = get(DchooseSuit5,'Value');
suit6(6) = get(DchooseSuit6,'Value');

order6(1) = get(DchooseFace1,'Value');
order6(2) = get(DchooseFace2,'Value');
order6(3) = get(DchooseFace3,'Value');
order6(4) = get(DchooseFace4,'Value');
order6(5) = get(DchooseFace5,'Value');
order6(6) = get(DchooseFace6,'Value');

sets4Of6 = {[1 2 3 4] [1 2 3 5] [1 2 3 6] [1 2 4 5] [1 2 4 6] [1 2 5 6] [1 3 4 5] [1 3 4 6] [1 3 5 6] [1 4 5 6] [2 3 4 5] [2 3 4 6] [2 3 5 6] [2 4 5 6] [3 4 5 6]};
sets2Of4 = {[1 2] [1 3] [1 4] [2 3] [2 4] [3 4]};

suits = {'clubs' 'diamonds' 'hearts' 'spades'};
faces = {'A' num2str(2) num2str(3) num2str(4) num2str(5) num2str(6) num2str(7) num2str(8) num2str(9) num2str(10) 'J' 'Q' 'K'};
counts = [1 2 3 4 5 6 7 8 9 10 10 10 10];

hand6(1) = cribCard('face',faces{order6(1)},'suit',suits{suit6(1)},'count',counts(order6(1)),'order',order6(1));
hand6(2) = cribCard('face',faces{order6(2)},'suit',suits{suit6(2)},'count',counts(order6(2)),'order',order6(2));
hand6(3) = cribCard('face',faces{order6(3)},'suit',suits{suit6(3)},'count',counts(order6(3)),'order',order6(3));
hand6(4) = cribCard('face',faces{order6(4)},'suit',suits{suit6(4)},'count',counts(order6(4)),'order',order6(4));
hand6(5) = cribCard('face',faces{order6(5)},'suit',suits{suit6(5)},'count',counts(order6(5)),'order',order6(5));
hand6(6) = cribCard('face',faces{order6(6)},'suit',suits{suit6(6)},'count',counts(order6(6)),'order',order6(6));

%% 6 CHOOSE 4 and SCORE IT
%For this callback, I want to loop over every combination of 4 cards in the
%6 card "hand". Each combo will be checked to make sure it doesn't contain
%repeats and then scored. The highest scoring hand will be set to the right
%side of the choose6 GUI.

for i = 1:length(sets4Of6) %Looping over every possible combination of 6choose4 cards
    quad6 = sets4Of6{i};
    tempHand6 = hand6(quad6);
        
    % CHECK TO SEE IT'S NOT A BOGUS HAND...i.e. NO REPEATED CARDS
    for x = 1:length(sets2Of4) %Looping over every possible combination of 4choose2 cards
        pair = sets2Of4{x};
        one = tempHand6(pair(1));   %First card
        two = tempHand6(pair(2));   %Second card
       if isequal(one,two) %If a card has been repeated...
          error('That hand is not possible...cards can''t be repeated!');
       end
    end
    
    score(i) = score4(tempHand6); %Score the hand.
end

%% LOCATE THE MAXIMUM SCORING HAND
maxPoss = max(score); %Calculate the maximum score
maxPossLoc = find(maxPoss == score); %Locate it within the combinations of cards.
if length(maxPossLoc) > 1 %If two hands are tied, then choose the first one.
    maxPossLoc = maxPossLoc(1);
end
bestQuad = sets4Of6{maxPossLoc}; %This is the location of the best hand, as assigned in line 60
for i = 1:length(bestQuad) %Assemble this best hand.
    bestHand(i) = hand6(bestQuad(i));
end

%% SET THE RIGHT PANEL TO 'VISIBLE' and CHANGE THE STRINGS.
best1 = bestHand(1);
best2 = bestHand(2);
best3 = bestHand(3);
best4 = bestHand(4);

set(Dans1,'Visible','on','String',[get(best1,'face') ' of ' get(best1,'suit')]);
set(Dans2,'Visible','on','String',[get(best2,'face') ' of ' get(best2,'suit')]);
set(Dans3,'Visible','on','String',[get(best3,'face') ' of ' get(best3,'suit')]);
set(Dans4,'Visible','on','String',[get(best4,'face') ' of ' get(best4,'suit')]);
set(Dtext3,'String',['For a score of ' num2str(maxPoss) ' points.'],'Visible','on');

Contact us at files@mathworks.com