% File: score.m
% Author: Corey Hebert
% Purpose: score a hand of 4 cards (with a given cut card) according
%          to the rules of cribbage
%
% Inputs: hand     (52 element column array representing the hand)
%         cut      (52 element column array representing the cut card)
%         crib     (flag: 1 if this is the crib hand, and 0 otherwise)
% Outputs: score   (1 element, integer score for input hand)
%
% Method: exhaustively consider all possibilities, efficiency be damned

function score = score(hand, cut, crib)

score = 0;  % default score

by_value = zeros(13, 1);

for i = 1:13
    by_value(i,1) = by_value(i,1) + hand(i,1) + hand(i+13,1) + hand(i+26,1) + hand(i+39, 1);
    by_value(i,1) = by_value(i,1) + cut(i,1) + cut(i+13,1) + hand(i+26,1) + cut(i+39,1);
end

values = zeros(5,1); % pure values
counts_as = zeros(5,1); % what each card counts as (ie jacks 'count as' 10s)
count = 1;

for i = 1:52
    if (hand(i,1) == 1)
        values(count,1) = mod(i, 13);
        counts_as(count,1) = mod(i,13);
        count = count + 1;
    end
    if (cut(i,1) == 1)
        values(count,1) = mod(i, 13);
        counts_as(count,1) = mod(i,13);
        count = count + 1;
    end
end

for i = 1:count-1
    if (values(i,1) == 0)
        counts_as(i,1) = 10;
        values(i,1) = 13;
    end
    if (values(i,1) == 11)
        counts_as(i,1) = 10;
    end
    if (values(i,1) == 12)
        counts_as(i,1) = 10;
    end            
end


% 15's

for k = [1,1,1,1,2,2,2,3,3,4;2,3,4,5,3,4,5,4,5,5]
    if (counts_as(k(1,1),1) + counts_as(k(2,1),1) == 15)
        score = score + 2;
    end
end
for k = [1,1,1,1,1,1,2,2,2,3;2,2,2,3,3,4,3,3,4,4;3,4,5,4,4,5,4,5,5,5]
    if (counts_as(k(1,1),1) + counts_as(k(2,1),1) + counts_as(k(3,1),1) == 15)
        score = score + 2;
    end
end
for k = [1,1,1,1,2;2,2,2,3,3;3,3,4,4,4;4,5,5,5,5]
    if (counts_as(k(1,1),1) + counts_as(k(2,1),1) + counts_as(k(3,1),1) + counts_as(k(4,1),1) == 15)
        score = score + 2;
    end
end
if (counts_as(1,1) + counts_as(2,1) + counts_as(3,1) + counts_as(4,1) + counts_as(5,1) == 15)
    score = score + 2;
end

% pairs

for k = [1,1,1,1,2,2,2,3,3,4;2,3,4,5,3,4,5,4,5,5]
    if (values(k(1,1),1) == values(k(2,1),1))
        score = score + 2;
    end
end

% runs

sorted = sort(values);

if (sorted(1,1) + 4 == sorted(5,1))
    score = score + 5;
elseif (sorted(1,1) + 3 == sorted(4,1) || sorted(2,1) + 3 == sorted(5,1))
    if (sorted(1,1) + 3 == sorted(4,1))
        score = score + 4;
    end
    if (sorted(2,1) + 3 == sorted(5,1))
        score = score + 4;
    end
elseif (sorted(1,1) + 2 == sorted(3,1) || sorted(3,1) + 2 == sorted(4,1) || sorted(3,1) + 2 == sorted(5,1))
    if (sorted(1,1) + 2 == sorted(3,1))
        score = score + 3;
    end
    if (sorted(2,1) + 2 == sorted(4,1))
        score = score + 3;
    end
    if (sorted(3,1) + 2 == sorted(5,1))
        score = score + 3;
    end
else
    % nothing necessary here, there's no runs score
end

% flush

for f = 1:4
    count = 1;
    for i = 1:13
        if (hand(i*f,1) == 1)
            count = count + 1;
        end
        if (cut(i*f,1) == 1)
            count = count + 1;
        end
    end
    
    if (count == 5)
        score = score + 5;
    end
    if (count == 4 && crib == 0)
        score = score + 4;
    end
end

% nob

for n = 1:4
    for i = 1:13
        if (hand(11*n,1) == 1 && cut(i*n,1) == 1)
            score = score + 1;
        end
    end
end
        
