I have created a structure that replicates a deck of cards (Deck)... now I need
ID: 3534301 • Letter: I
Question
I have created a structure that replicates a deck of cards (Deck)... now I need a code to "deal" out 3 cards to X number of players. The code cannot deal the same card twice! I also need code that will evaluate every players hand and determine a winner in this order: 3 of a kind, 2 of a kind, high card. I suppose there could be more than one winner. Any help would be greatly appreciated! MATLAB code only, please. Thanks!
function [ D ] = Deck( )
D=struct([]);
for j=1:4
for i=2:14
D(end+1).value=i;
D(end).suit=j;
if i<11
D(end).card=[num2str(i),sStr{j}];
elseif i==11
D(end).card=['Jack',sStr{j}];
elseif i==12;
D(end).card=['Queen',sStr{j}];
elseif i==13;
D(end).card=['King',sStr{j}];
elseif i==14;
D(end).card=['Ace',sStr{j}];
end
end
end
end
Explanation / Answer
% ante up (poker)
clear all
disp('###############################################################')
disp('## ANTE UP ##')
disp('###############################################################')
players = input('Enter the number of players: ');
cardnum = input('Enter the number of cards to deal: ');
deck = struct('Hearts', {'A' '2' '3' '4' '5' '6' '7' '8' '9' '10' 'J' 'Q' 'K'},...
'Diamonds', {'A' '2' '3' '4' '5' '6' '7' '8' '9' '10' 'J' 'Q' 'K'},...
'Clubs',{'A' '2' '3' '4' '5' '6' '7' '8' '9' '10' 'J' 'Q' 'K'},...
'Spades',{'A' '2' '3' '4' '5' '6' '7' '8' '9' '10' 'J' 'Q' 'K'});
if players*cardnum <= 52
for p = 1:players
disp(' ')
disp(['player ', num2str(p)])
for ii = 1:cardnum
s = randi([1 4]);
switch s
case {1}
suit = 'Hearts';
case{2}
suit = 'Diamonds';
case {3}
suit = 'Clubs';
case{4}
suit = 'Spades';
end
card = randi([1 13]);
switch deck(card).(suit)
case {'0'}
ii = ii-1; % supposed to back up the for loop by one if encounter a card already drawn
otherwise
draw(p,ii) = deck(card).(suit);
disp([' card = ', draw(p,ii), ' of ', suit])
deck(card).(suit) = '0';
end
end
end
else
disp('error. not enough cards to deal to all players')
end