Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB: I am struggling with this question I have wrote a code and am stuck at h

ID: 3765435 • Letter: M

Question

MATLAB: I am struggling with this question I have wrote a code and am stuck at how to keep the most common numbers and roll the others. I have got all the way to rolling the numbers again but don't know how to show which ones I have kept along with rolling the ones that arent the same in order to end up with all the same number.

The question is:

Find the Dice to Throw Again: With the knowledge of the most common outcome it is time to decide which dice to save and which to throw again. There are two ways to do this:

1. One alternative is to modify the function from the last step so that it re- turns a vector containing the indices of the dice to throw again; that is, the dice not showing the number computed in Step 2.3. Test the function!

• The dice [4 5 4 4 1] should yield [2 5] (throw dice number 2 and 5 again).

• The dice [1 4 2 2 4] should yield [1 2 5] or [1 3 4] (you decide).

2. The second alternative is to put the dice to save at the beginning of the dice vector. Since the value that the dice you save (from Section 2.3) and how many such dice you have (from Section 2.2) is known, it is possible to create a vector with the correct number of dice and values. After modifying the function make sure to test it before you proceed! Example:

• The outcome [4 5 4 4 1] should yield [4 4 4 * *] (where * will be thrown again and so the value is unimportant).

• The outcome [1 4 2 2 4] should yield either [2 2 * * *] or [4 4 * * *] (you decide).

Here is my script code:

rounds=1;

rollCount=0;

rule{1}=[1 1 1 1 1];

count1=0;

count2=0;

count3=0;

count4=0;

count5=0;

count6=0;

while rounds<=1

fprintf('Rolling the dice... ');

roll=randi(6,1,5);

roll=sort(roll);

fprintf('You rolled:');

disp(roll);

rollCount=rollCount+1;

for x=rule

if roll==rule{1};

fprintf('Condition Met');

break;

end

end

rounds=rounds+1;

end

for rounds = 1

y=roll;

if y(1,1)==1

count1=count1+1;

elseif y(1,1)==2

count2=count2+1;

elseif y(1,1)==3

count3=count3+1;

elseif y(1,1)==4

count4=count4+1;

elseif y(1,1)==5

count5=count5+1;

elseif y(1,1)==6

count6=count6+1;

end

end

for rounds = 1

y=roll;

if y(1,2)==1

count1=count1+1;

elseif y(1,2)==2

count2=count2+1;

elseif y(1,2)==3

count3=count3+1;

elseif y(1,2)==4

count4=count4+1;

elseif y(1,2)==5

count5=count5+1;

elseif y(1,2)==6

count6=count6+1;

end

end

for rounds = 1

y=roll;

if y(1,3)==1

count1=count1+1;

elseif y(1,3)==2

count2=count2+1;

elseif y(1,3)==3

count3=count3+1;

elseif y(1,3)==4

count4=count4+1;

elseif y(1,3)==5

count5=count5+1;

elseif y(1,3)==6

count6=count6+1;

end

end

for rounds = 1

y=roll;

if y(1,4)==1

count1=count1+1;

elseif y(1,4)==2

count2=count2+1;

elseif y(1,4)==3

count3=count3+1;

elseif y(1,4)==4

count4=count4+1;

elseif y(1,4)==5

count5=count5+1;

elseif y(1,4)==6

count6=count6+1;

end

end

for rounds = 1

y=roll;

if y(1,5)==1

count1=count1+1;

elseif y(1,5)==2

count2=count2+1;

elseif y(1,5)==3

count3=count3+1;

elseif y(1,5)==4

count4=count4+1;

elseif y(1,5)==5

count5=count5+1;

elseif y(1,5)==6

count6=count6+1;

end

end

Y=[count1 count2 count3 count4 count5 count6];

disp(Y);

disp('Call forward: FindCommon( Y )');

The Call Forward Function: FindCommon(Y)' for the above code (In function window):

function FindCommon( Y )

max=Y(1,1);

[row, col]=size(Y);

for r=1:row

for c=1:col

if Y(r,c) > max

max = Y(r,c);

end

end

end

if max==Y(r,1)

disp('Most common number is 1');

elseif max==Y(r,2)

disp('Most common number is 2');

elseif max==Y(r,3)

disp('Most common number is 3');

elseif max==Y(r,4)

disp('Most common number is 4');

elseif max==Y(r,5)

disp('Most common number is 5');

elseif max==Y(r,6)

disp('Most common number is 6');

end

fprintf('max is: ');

disp(max);

rule1{1}=[1 1 1 1 1];

rule2{1}=[1 1 1 ];

rule3{1}=[1 1];

rule4{1}=(1);

if max == 1 %ReRoll all five dice

rounds=1;

rollCount=0;

rule1{1}=[1 1 1 1 1];

while rounds<=1

fprintf('Rolling the dice... ');

roll=randi(6,1,5);

roll=sort(roll);

fprintf('You rolled:');

disp(roll);

rollCount=rollCount+1;

for x=rule1

if roll==rule1{1};

fprintf('Condition Met');

break;

end

end

rounds=rounds+1;

end

elseif max == 2 %Reroll three dice

rounds=1;

rollCount=0;

rule2{1}=[1 1 1];

while rounds<=1

fprintf('Rolling the dice... ');

roll=randi(6,1,3);

roll=sort(roll);

fprintf('You rolled:');

disp(roll);

rollCount=rollCount+1;

for x=rule2

if roll==rule2{1};

fprintf('Condition Met');

break;

end

end

rounds=rounds+1;

end

elseif max == 3 %Reroll two dice

rounds=1;

rollCount=0;

rule3{1}=[1 1];

while rounds<=1

fprintf('Rolling the dice... ');

roll=randi(6,1,2);

roll=sort(roll);

fprintf('You rolled:');

disp(roll);

rollCount=rollCount+1;

for x=rule3

if roll==rule3{1};

fprintf('Condition Met');

break;

end

end

rounds=rounds+1;

end

elseif max == 4 %ReRoll one dice

rounds=1;

rollCount=0;

rule4{1}=(1);

while rounds<=1

fprintf('Rolling the dice... ');

roll=randi(6,1,1);

roll=sort(roll);

fprintf('You rolled:');

disp(roll);

rollCount=rollCount+1;

for x=rule4

if roll==rule4{1};

fprintf('Condition Met');

break;

end

end

rounds=rounds+1;

end

else

disp('YAHTZEE!!!');

end

end

Explanation / Answer


          
    while game
            input('Press enter to roll dice');
            d1 = floor(6*rand)+1;
            d2 = floor(6*rand)+1;
  
            disp('You rolled:');
            disp(d1);
            disp(d2);
            x=d1+d2;
  
            if x==win1 || x==win2
                streak=streak+1;
                disp('WINNER!')
            elseif x==lose1 || x==lose2
                disp('YOU LOST!')
                disp('streak:')
                disp(streak)
                game = false;
  
                %reset winning and losing numbers
                win1=7;
                win2=11;
                lose1=0;
                lose2=0;
                streak = 0;
              
            elseif
                 firstroll
                win1 = x;
                win2 = x;
                lose1 = 7;
                lose2 = 11;
                firstroll = false;
                continue;
            end
        end
    end
disp('Thanks for playing!')