...
Code Block |
---|
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 |
f(frivillig) g)
Code Block |
---|
% 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 |