E. Final Program with Comments

clc
clear
close all

%% Yahtzee SDP

% Initialize scene
my_scene = simpleGameEngine('retro_dice.png',16,16,8);

% Initialize score
score = 0;

% Initialize category markers (markers used to make sure each category can only be used once) 
ones_marker = 0;
twos_marker = 0;
threes_marker = 0;
fours_marker = 0;
fives_marker = 0;
sixes_marker = 0;
tok_marker = 0;
fok_marker = 0;
fh_marker = 0;
ss_marker = 0;
ls_marker = 0;
yahtzee_marker = 0;
chance_marker = 0;

% Initialize round count
roundcount = 0;

% while loop to roll dice and score for 13 rounds
while roundcount < 13
    roundcount = roundcount + 1;
    fprintf('Round %.f:',roundcount)
    disp(' ')
    disp(' ')
    % display round count
    
% Random roll of 5 dice (1 row by 5 columns) - only allow values up to 6
roll = randi(6, 1, 5);

% Set up variables to name the various sprites
empty_sprite = 1;
face_sprites = 11:16;
unselected_die_sprite = 2;
selected_die_sprite = 7;
dice_number = (1:1:5);

% create a vector of the corresponding die face sprites
roll_sprites = face_sprites(roll);
dice_number = roll;

% initally all dice are unselected
die_sprites = unselected_die_sprite *  ones(1,5);

% display first dice roll
drawScene(my_scene,die_sprites,roll_sprites)

% ask user to select dice
% loop to reroll dice for roll 2/3 and roll 3/3
for r=1:2;
    fprintf('Roll %.f/3:', r)
    disp(' ')
    
k = 1;

% while loop to select whether to reroll each die
while k < 6;
    fprintf('For Die Number %.f...', k)
    disp(' ')
    user_selected_die = input('Would you like to keep this die? (Enter 1 for yes and anything else for no):  ');
    if user_selected_die == 1
        die_sprites(k)=selected_die_sprite; 
    else user_selected_die = 0;
        roll(k)=randi(6,1);
    end
    k = k+1;
end
    
    % display the updated roll with the kept dice highlighted
    roll_sprites = face_sprites(roll);
    drawScene(my_scene,die_sprites,roll_sprites)
    
    % reset the dice to be unselected
    die_sprites = unselected_die_sprite *  ones(1,5);
end

% display the final roll number
fprintf('Roll 3/3')
disp(' ')



% initialized variable and while loop for scoreboard    
o = 0; 
while o < 1
    % user input to determine scoring category
    score_decision = input('Below is a list of possibilities to score your roll on your scorecard: \n Enter "1" to sum your roll in the "ones" place \n Enter "2" to sum your roll in the "twos" place \n Enter "3" to sum your roll in the "threes" place \n Enter "4" to sum your roll in the "fours" place \n Enter "5" to sum your roll in the "fives" place \n Enter "6" to sum your roll in the "six" place \n Enter "7" to sum your roll as a "three of a kind" \n Enter "8" to sum your roll as a "four of a kind" \n Enter "9" for a "full house" to recieve 25 points \n Enter "10" for a "small straight" to recieve 30 points \n Enter "11" for a "large straight" to recieve 40 points \n Enter "12" for "Yahtzee" to recieve 50 points \n Enter "13" for "chance" to sum up roll \n Enter your decision number here: ');
    
    % if statement to place roll into correct scoring category
    % "ones" section
    if score_decision == 1; 
        % if statement to ensure category can only be used once
        if ones_marker == 1;
            fprintf('You already used this category.')
        else
            for d = 1:5;
                if roll(d) == 1;
                ones_check(d) = 1;
                else
                ones_check(d) = 0;
                end
            end
            score = score + sum(ones_check);
            fprintf('You placed your score in the "ones" section. \n Score: ')
            disp(score)
            ones_marker = 1;
            o = 1;
        end
    
    % "twos" section
    elseif score_decision == 2; 
        if twos_marker == 1;
            fprintf('You already used this category.') 
        else
            for d = 1:5;
                if roll(d) == 2;
                twos_check(d) = 2;
                else
                twos_check(d) = 0;
                end
            end
            score = score + sum(twos_check);
            fprintf('You placed your score in the "twos" section. \n Score: ')
            disp(score)
            twos_marker = 1;
            o = 1;
        end
    
    % "threes" section
    elseif score_decision == 3; 
        if threes_marker == 1;
            fprintf('You already used this category.') 
        else
            for d = 1:5;
                if roll(d) == 3;
                threes_check(d) = 3;
                else
                threes_check(d) = 0;
                end
            end
            score = score + sum(threes_check);
            fprintf('You placed your score in the "threes" section. \n Score: ')
            disp(score)
            threes_marker = 1;
            o = 1;
        end
    
    % "fours" section
    elseif score_decision == 4; 
        if fours_marker == 1;
            fprintf('You already used this category.') 
        else
            for d = 1:5;
                if roll(d) == 4;
                fours_check(d) = 4;
                else
                fours_check(d) = 0;
                end
            end
            score = score + sum(fours_check);
            fprintf('You placed your score in the "fours" section. \n Score: ')
            disp(score)
            fours_marker = 1;
            o = 1;
        end
    
    % "fives" place
    elseif score_decision == 5; 
        if fives_marker == 1;
            fprintf('You already used this category.') 
        else
            for d = 1:5;
                if roll(d) == 5;
                fives_check(d) = 5;
                else
                fives_check(d) = 0;
                end
            end
            score = score + sum(fives_check);
            fprintf('You placed your score in the "fives" place. \n Score: ')
            disp(score)
            fives_marker = 1;
            o = 1;
        end
    
    % "sixes" place
    elseif score_decision == 6; 
         if sixes_marker == 1;
            fprintf('You already used this category.') 
        else
            for d = 1:5;
                if roll(d) == 6;
                sixes_check(d) = 6;
                else
                sixes_check(d) = 0;
                end
            end
            score = score + sum(sixes_check);
            fprintf('You placed your score in the "sixes" place. \n Score: ')
            disp(score)
            sixes_marker = 1;
            o = 1;
         end
        
    % "three of a kind" section
    elseif score_decision == 7; 
        if tok_marker == 1;
            fprintf('You already used this category.') 
        else
            count = [0 0 0 0 0 0];
            for d = 1:5;
                j = roll(d);
                count(j) = count(j) + 1;
            end
            for d = 1:6;
                if count(d) >= 3
                    score = score + sum(roll);
                else
                    score = score;
                end
            end
            fprintf('You placed your score in the "three of a kind" section. \n Score: ')
            disp(score)
            tok_marker = 1;
            o = 1;
        end
    
   % "four of a kind" section
   elseif score_decision == 8; 
        if fok_marker == 1;
            fprintf('You already used this category.') 
        else
            count = [0 0 0 0 0 0];
            for d = 1:5;
                j = roll(d);
                count(j) = count(j) + 1;
            end
            for d = 1:6;
                if count(d) >= 4
                    score = score + sum(roll);
                else
                    score = score;
                end
            end
            fprintf('You placed your score in the "four of a kind" section. \n Score: ')
            disp(score)
            fok_marker = 1;
            o = 1;
        end
    
   % "full house" section
   elseif score_decision == 9;
        if fh_marker == 1;
            fprintf('You already used this category.') 
        else
            count = [0 0 0 0 0 0];
            for d = 1:5;
                j = roll(d);
                count(j) = count(j) + 1;
            end
            counter1 = 0;
            counter2 = 0;
            for d = 1:6;
                if count(d) == 3
                    counter1 = 1;
                elseif count(d) == 2
                    counter2 = 1;
                end
            end
            if counter1 == 1 & counter2 == 1
                score = score + 25;
            else
                score = score;
            end
            fprintf('You placed your score in the "full house" section. \n Score: ')
            disp(score)
            fh_marker = 1;
            o = 1;
        end
  
   % "small straight" section
   elseif score_decision == 10;
        if ss_marker == 1;
            fprintf('You already used this category.') 
        else
            count = [0 0 0 0 0 0];
            for d = 1:5;
                j = roll(d);
                count(j) = count(j) + 1;
            end
            if count(1)>0 & count(2)>0 & count(3)>0
                score = score + 30;
            elseif count(2)>0 & count(3)>0 & count(4)>0
                score = score + 30;
            elseif count(3)>0 & count(4)>0 & count(5)>0
                score = score + 30;
            elseif count(4)>0 & count(5)>0 & count(6)>0
                score = score + 30;
            else
                score = score;
            end
            fprintf('You placed your score in the "small straight" section. \n Score: ')
            disp(score)
            ss_marker = 1;
            o = 1;
        end
 
   % "large straight" section
   elseif score_decision == 11;
        if ls_marker == 1;
            fprintf('You already used this category.') 
        else
            count = [0 0 0 0 0 0];
            for d = 1:5;
                j = roll(d);
                count(j) = count(j) + 1;
            end
            if count(1)>0 & count(2)>0 & count(3)>0 & count(4)>0 & count(5)>0
                score = score + 40;
            elseif count(2)>0 & count(3)>0 & count(4)>0 & count(5)>0 & count(6)>0
                score = score + 40;
            else
                score = score;
            end
            fprintf('You placed your score in the "large straight" section. \n Score: ')
            disp(score)
            ls_marker = 1;
            o = 1;
        end
  
   % "Yahtzee" section
   elseif score_decision == 12;
        if yahtzee_marker == 1;
            fprintf('You already used this category.') 
        else
            count = [0 0 0 0 0 0];
            for d = 1:5;
                j = roll(d);
                count(j) = count(j) + 1;
            end
            for d = 1:6;
                if count(d) == 5
                    score = score + 50;
                else
                    score = score;
                end
            end
            fprintf('You placed your score in the "Yahtzee" section. \n Score: ')
            disp(score)
            yahtzee_marker = 1;
            o = 1;
         end
    
    % "chance" section
    else score_decision == 13;
        if chance_marker == 1;
            fprintf('You already used this category.') 
        else
            score = score + sum(roll);
            fprintf('You placed your score in the "chance" section. \n Score: ')
            disp(score)
            chance_marker = 1;
            o = 1;
        end
    end
    
end

end

disp('Game Over!')