a)
function out = getDeck()
deck = [];
suits = 'HDCS';
for i = 1 : 4
for j = 2:14
card.suit = suits(i);
card.value = j;
deck = [deck card];
end
end
out = deck;
end
b)
function hand = randomHand(n) deck = getDeck(); hand = deck(randperm(52,n)); end
c)
function visualRepresentation(hand)
cards = '023456789TJQKA';
reference = 'HDCS';
suits = [9828:9831];
for card = hand
suit = char(suits(find(card.suit == reference)));
fprintf('%s%s, ', cards(card.value), suit);
end
fprintf('\n')
end
d)
function value = evaluateHand(hand)
possibilites = {@isNothing, @isPair, @isTwoPairs, @isTrips, ...
@isStraight, @isFlush, @isFullHouse, @isQuads, @isStraightFlush};
for poss = possibilites
[isValid, name] = poss{1}(hand);
if isValid
value = name;
break
end
end
end
function list = getNumbers(hand)
list = sort([hand.value]);
end
function list = getSuits(hand)
list = sort([hand.suit]);
end
function [bool, name] = isNothing(hand)
copy = hand;
hand = getNumbers(hand);
bool = length(unique(hand)) == 5 && ~isStraight(copy) && ~isFlush(copy);
name = 'High Card';
end
function [bool, name] = isPair(hand)
bool = length(unique(getNumbers(hand))) == 4;
name = 'Pair';
end
function [bool, name] = isTwoPairs(hand)
hand = getNumbers(hand);
bool = length(unique(hand)) == 3 ...
&& sum(mode(hand) == hand) == 2;
name = 'Two Pairs';
end
function [bool, name] = isTrips(hand)
hand = getNumbers(hand);
bool = length(unique(hand)) == 3 ...
&& sum(mode(hand) == hand) == 3;
name = 'Trips';
end
function [bool, name] = isStraight(hand)
copy = hand;
hand = getNumbers(hand);
bool = false;
if length(unique(hand)) == 5
bool = hand(5) - hand(1) == 4;
if sum(hand == 14) == 1
hand(hand == 14) = 1;
hand = sort(hand);
bool = bool || hand(5) - hand(1) == 4;
end
if bool && isFlush(copy)
name = 'Straight Flush';
return
end
end
name = 'Straight';
end
function [bool, name] = isFlush(hand)
bool = length(unique(getSuits(hand))) == 1;
name = 'Flush';
end
function [bool, name] = isFullHouse(hand)
hand = getNumbers(hand);
bool = length(unique(hand)) == 2 ...
&& sum(mode(hand) == hand) == 3;
name = 'Full House';
end
function [bool, name] = isQuads(hand)
hand = getNumbers(hand);
bool = sum(mode(hand) == hand) == 4;
name = 'Quads';
end
function [bool, name] = isStraightFlush(hand)
bool = isStraight(hand) && isFlush(hand);
name = 'Straight Flush';
end
e)
function data = main(num)
data = strings(1, num);
for i = 1 : num
hand = randomHand(5);
value = getValue(hand);
data(i) = string(value);
end
names = ["High Card", "Pair", "Two Pairs", "Trips", "Straight", "Flush", "Full House", "Quads", "Straight Flush"];
for name = names
num = sum(data == name);
fprintf('%s occurred %i times. That is %.3f%%\n', name, num, 100 * num/length(data));
end
end
(frivillig) g)
% For å optimalisere koden kan man blant annet innføre persistente variabler i getDeck og evaluateHand som følger:
function out = getDeck()
persistent deck % henter inn deck fra tidligere og sjekker om den allerede er opprettet.
if isempty(deck)
deck = [];
suits = 'HDCS';
for i = 1 : 4
for j = 2:14
card.suit = suits(i);
card.value = j;
deck = [deck card];
end
end
end
out = deck;
end
function value = evaluateHand(hand)
persistent possibilites;
if isempty(possibilites)
possibilites = {@isNothing, @isPair, @isTwoPairs, @isTrips, ...
@isStraight, @isFlush, @isFullHouse, @isQuads, @isStraightFlush};
end
.
.
.
% resten av koden