% ProbComb5CardGameCalc2.m
%
% Compute odds for 5-card game using #'s 1-5 and suits red, green, blue,
% yellow.
%
% Create arrays that represent two 3-card hands.
% Allow use to input which cards are face-up and which are face-down.
% Then step through all possible hands by incrementing arrays odometer-style
%  for face-down cards, going through all possible cards (excluding the face-up
%  cards.
% Count how often types of hands, such as straight-flush, 3-of-a-kind, etc. occur.
% Also count total number of possible hands.
%
% Numbering of suits:
%  1  red = R
%  2  green = G
%  3  blue = B
%  4  yellow =Y
% Increment suits first, then increment number on card (#'s 1 thru 5 used).

% Initial deal input by user.  Two 3-card hands.  One hand at a time input.
% Format is e.g., 4Y X X, meaning 4 Yellow, face-down, face-down.

cardnum_vec = [1 1 1];
suit_vec = [1 2 3];

% Zero out counts of types of hands.  Two entries, count for hand 1, count for hand 2.
StraightFlushCnt = zeros(2,1);
ThreeOfKindCnt = zeros(2,1);
FlushCnt = zeros(2,1);
StraightCnt = zeros(2,1);
PairCnt = zeros(2,1);
LowHandCnt = zeros(2,1);

% 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
