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.

choose4Callback.m
%choose4Callback.m
%This is the callback function for the choose4 GUI.
%It creates the 5-card hand from the data in the GUI, checks to see if it's
%feasible, runs the hand through the function "score5" and then updates the
%GUI's.


%% ASSEMBLE THE HAND AND ALL THE NEEDED DATA FOR IT.
%Gather the suit info
suit1 = get(BchooseSuit1,'Value');
suit2 = get(BchooseSuit2,'Value');
suit3 = get(BchooseSuit3,'Value');
suit4 = get(BchooseSuit4,'Value');
suit5 = get(BchooseStarterSuit,'Value');

%Gather the order info...this allows us to find the "face" and "count"
%values for the cards.
order1 = get(BchooseFace1,'Value');
order2 = get(BchooseFace2,'Value');
order3 = get(BchooseFace3,'Value');
order4 = get(BchooseFace4,'Value');
order5 = get(BchooseStarterFace,'Value');

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

%Create the hand using the data given above.
hand(1) = cribCard('face',faces{order1},'count',counts(order1),'order',order1,'suit',suits{suit1});
hand(2) = cribCard('face',faces{order2},'count',counts(order2),'order',order2,'suit',suits{suit2});
hand(3) = cribCard('face',faces{order3},'count',counts(order3),'order',order3,'suit',suits{suit3});
hand(4) = cribCard('face',faces{order4},'count',counts(order4),'order',order4,'suit',suits{suit4});
hand(5) = cribCard('face',faces{order5},'count',counts(order5),'order',order5,'suit',suits{suit5});

%% CHECK TO SEE IT'S NOT A BOGUS HAND...i.e. NO REPEATED CARDS
setsOf2 = {[1 2] [1 3] [1 4] [1 5] [2 3] [2 4] [2 5] [3 4] [3 5] [4 5]}; %Every combination of 2 cards out of a set of 5
for i = 1:length(setsOf2)
    pair = setsOf2{i};
    one = hand(pair(1));
    two = hand(pair(2));
    if isequal(one,two) %If the cards are identical (uses the class method for cardDeck)
        error('That hand is not possible...cards can''t be repeated!');
    end
end
isCrib = get(Bcrib,'Value'); %See if this is the crib hand.
score = score5(hand,isCrib); %Calculate the score
set(Bcalc,'String',num2str(score)); %Insert this value onto the choose4 GUI

%Insert this value onto the Scoreboard
playerNum = get(Bplayer,'Value');
switch playerNum
    case 1 %If it's player one's score
        toAddLocation = Splayer1Input; %Put it in player one's toAdd box.
    case 2 %If it's player two's score
        toAddLocation = Splayer2Input; %Put it in player two's toAdd box.
end
set(toAddLocation,'String',num2str(score)); %Set the data in the toAdd box.

Contact us at files@mathworks.com