% ProbComb5CardGameCalc.m
%
% Compute odds for 5-card game using #'s 1-5 and suits red, green, blue,
% yellow.

% Create arrays that represent 3-card hands.  Test all possible hands by
% incrementing arrays odometer-style so first entry is always less than the
% next card to the right in the array.  Numbering of suits:
%  1  red
%  2  green
%  3  blue
%  4  yellow
% Increment suits first, then increment #.

% Initial hand
cardnum_vec = [1 1 1];
suit_vec = [1 2 3];

% Zero out counts of types of hands.
StraightFlushCnt = 0;
ThreeOfKindCnt = 0;
FlushCnt = 0;
StraightCnt = 0;
PairCnt = 0;
LowHandCnt = 0;

% Loop over all possible hands.
hands_done = 0;
loopcnt = 0;
while hands_done == 0
  
  % Print hand when debugging.
  cardnum_vec = cardnum_vec;
  suit_vec = suit_vec;
  
  % Tally types of hands.  Only highest ranking category applies.
  if sort(cardnum_vec) == sort(cardnum_vec(1) + [0:2]) & var(suit_vec) == 0
    StraightFlushCnt = StraightFlushCnt + 1;
    
  elseif var(cardnum_vec) == 0
    ThreeOfKindCnt = ThreeOfKindCnt + 1
    
  elseif var(suit_vec) == 0
    FlushCnt = FlushCnt + 1
   
  elseif sort(cardnum_vec) == sort(cardnum_vec(1) + [0:2])
    StraightCnt = StraightCnt + 1
    
  elseif var(cardnum_vec([1,2])) == 0 | ...
         var(cardnum_vec([2,3])) == 0 | ...
         var(cardnum_vec([1,3])) == 0
    PairCnt = PairCnt + 1
    
  else
    LowHandCnt = LowHandCnt + 1;
    
  end
  % Increment to next hand.
  % Cntr points to which card in hand is being incremented.  Work from
  % right to left.
  CardIndex = 3;
  
  % Increment last card, check for rollover.
  inc_card_done = 0;
  while inc_card_done == 0
    % Step to next card.
    [cardnum_vec(CardIndex), suit_vec(CardIndex)] ...
      = ProbComb5CardGameNextCard(cardnum_vec(CardIndex), ...
                                  suit_vec(CardIndex))
    
    % Initialize all cards to right of card that was incremented.
    for InitCardIndex = CardIndex:2
      display('Initialize next card to right')
      [cardnum_vec(InitCardIndex+1), suit_vec(InitCardIndex+1)] ...
        = ProbComb5CardGameNextCard(cardnum_vec(InitCardIndex), ...
                                    suit_vec(InitCardIndex))
    end
   
    % If last card past end of deck, prepare to increment next card
    %  to left.
    if cardnum_vec(3) == 0
      CardIndex = CardIndex - 1;
      display('Increment next card to left') 
      
      % Check if done with all hands.
      if CardIndex == 0
        inc_card_done = 1;
        hands_done = 1;
      end
    else
      inc_card_done = 1;
    end
    
  end
  

      loopcnt = loopcnt + 1;
      if loopcnt == 10000
        break
      end
      
end

% Print results.
display('Final Results')
StraightFlushCnt = StraightFlushCnt
ThreeOfKindCnt = ThreeOfKindCnt
FlushCnt = FlushCnt
StraightCnt = StraightCnt
PairCnt = PairCnt
LowHandCnt = LowHandCnt
