E. Final Program with Comments

%%
clear
load Mastermind

fprintf(‘Colors: Blue, Green, Orange, Pink, Purple, Red, Turquoise, Yellow\n\n’)

 

% *****************************Generate random code *********************

 

Colorarray = {“Blue”, “Green”, “Orange”, “Pink”, “Purple”, “Red”, “Turquoise”, “Yellow”};

 

c1 = Colorarray{randi(numel(Colorarray))};

c2 = Colorarray{randi(numel(Colorarray))};

c3 = Colorarray{randi(numel(Colorarray))};

c4 = Colorarray{randi(numel(Colorarray))};

B_target = sum([string(c1), string(c2), string(c3), string(c4)] == “Blue”);
G_target = sum([string(c1), string(c2), string(c3), string(c4)] == “Green”);
O_target = sum([string(c1), string(c2), string(c3), string(c4)] == “Orange”);
Pi_target = sum([string(c1), string(c2), string(c3), string(c4)] == “Pink”);
Pur_target = sum([string(c1), string(c2), string(c3), string(c4)] == “Purple”);
R_target = sum([string(c1), string(c2), string(c3), string(c4)] == “Red”);
T_target = sum([string(c1), string(c2), string(c3), string(c4)] == “Turquoise”);
Y_target = sum([string(c1), string(c2), string(c3), string(c4)] == “Yellow”);
B = 0;
G = 0;
O = 0;
Pi = 0;
Pur = 0;
R = 0;
T = 0;
Y = 0;

count =0;

 

 

% ************************* display user input **************************

numturns = 0;

for i = 1 : 1 : 10

imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:};Board{7,:};Board{8,:};Board{9,:};Board{10,:}])

firstpeg = input(‘Enter a color for the first peg: ‘, ‘s’);

secondpeg = input(‘Enter a color for the second peg: ‘, ‘s’);

thirdpeg = input(‘Enter a color for the third peg: ‘, ‘s’);

fourthpeg = input(‘Enter a color for the fourth peg: ‘, ‘s’);

%*********************** Checking for first peg ***************************

if firstpeg == “Blue”
Board{i,1} = Blue;
B = B + 1;
elseif firstpeg == “Green”
Board{i,1} = Green;
G = G + 1;
elseif firstpeg == “Orange”
Board{i,1} = Orange;
O = O + 1;
elseif firstpeg == “Pink”
Board{i,1} = Pink;
Pi=Pi+1;
elseif firstpeg == “Purple”
Board{i,1} = Purple;
Pur=Pur+1;
elseif firstpeg == “Red”
Board{i,1} = Red;
R=R+1;
elseif firstpeg == “Turquoise”
Board{i,1} = Turquoise;
T=T+1;
elseif firstpeg == “Yellow”
Board{i,1} = Yellow;
Y=Y+1;
end

%********************* Checking for second peg ***************************

if secondpeg == “Blue”
Board{i,2} = Blue;
B = B + 1;
elseif secondpeg == “Green”
Board{i,2} = Green;
G = G + 1;
elseif secondpeg == “Orange”
Board{i,2} = Orange;
O = O + 1;
elseif secondpeg == “Pink”
Board{i,2} = Pink;
Pi=Pi+1;
elseif secondpeg == “Purple”
Board{i,2} = Purple;
Pur=Pur+1;
elseif secondpeg == “Red”
Board{i,2} = Red;
R=R+1;
elseif secondpeg == “Turquoise”
Board{i,2} = Turquoise;
T=T+1;
elseif secondpeg == “Yellow”
Board{i,2} = Yellow;
Y=Y+1;
end

%********************** Checking for third peg ***************************

if thirdpeg == “Blue”
Board{i,3} = Blue;
B = B + 1;
elseif thirdpeg == “Green”
Board{i,3} = Green;
G = G + 1;
elseif thirdpeg == “Orange”
Board{i,3} = Orange;
O = O + 1;
elseif thirdpeg == “Pink”
Board{i,3} = Pink;
Pi=Pi+1;
elseif thirdpeg == “Purple”
Board{i,3} = Purple;
Pur=Pur+1;
elseif thirdpeg == “Red”
Board{i,3} = Red;
R=R+1;
elseif thirdpeg == “Turquoise”
Board{i,3} = Turquoise;
T=T+1;
elseif thirdpeg == “Yellow”
Board{i,3} = Yellow;
Y=Y+1;
end

%********************** Checking for fourth peg ***************************

if fourthpeg == “Blue”
Board{i,4} = Blue;
B = B + 1;
elseif fourthpeg == “Green”
Board{i,4} = Green;
G = G + 1;
elseif fourthpeg == “Orange”
Board{i,4} = Orange;
O = O + 1;
elseif fourthpeg == “Pink”
Board{i,4} = Pink;
Pi=Pi+1;
elseif fourthpeg == “Purple”
Board{i,4} = Purple;
Pur=Pur+1;
elseif fourthpeg == “Red”
Board{i,4} = Red;
R=R+1;
elseif fourthpeg == “Turquoise”
Board{i,4} = Turquoise;
T=T+1;
elseif fourthpeg == “Yellow”
Board{i,4} = Yellow;
Y=Y+1;
end

imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:};Board{7,:};Board{8,:};Board{9,:};Board{10,:}])

%*********************** Checking the colors *****************************

 

blackLight = 0;
whiteLight = 0;

 

% check if the color the user entered is on the right place

if firstpeg== c1
blackLight = blackLight + 1;

end
if secondpeg== c2
blackLight = blackLight + 1;

end
if thirdpeg== c3
blackLight = blackLight + 1;

end
if fourthpeg== c4
blackLight = blackLight + 1;

end
%****************************************

%*****************************************

B = sum([string(firstpeg), string(secondpeg), string(thirdpeg), string(fourthpeg)] == “Blue”);
G = sum([string(firstpeg), string(secondpeg), string(thirdpeg), string(fourthpeg)] == “Green”);
O = sum([string(firstpeg), string(secondpeg), string(thirdpeg), string(fourthpeg)] == “Orange”);
Pi = sum([string(firstpeg), string(secondpeg), string(thirdpeg), string(fourthpeg)] == “Pink”);
Pur = sum([string(firstpeg), string(secondpeg), string(thirdpeg), string(fourthpeg)] == “Purple”);
R = sum([string(firstpeg), string(secondpeg), string(thirdpeg), string(fourthpeg)] == “Red”);
T = sum([string(firstpeg), string(secondpeg), string(thirdpeg), string(fourthpeg)] == “Turquoise”);
Y = sum([string(firstpeg), string(secondpeg), string(thirdpeg), string(fourthpeg)] == “Yellow”);

whiteLight = min(B, B_target) + min(G, G_target) + min(O, O_target) + min(Pi, Pi_target) + min(Pur, Pur_target) + min(R, R_target) + min(T, T_target) + min(Y, Y_target) – blackLight;

%***************** Displaying the pegs ******************************
%********************************************************************

p = 5;

if blackLight == 1
Board{i,p}= BlackPeg;
end
if blackLight == 2
Board{i,p}= BlackPeg; Board{i,p+1}= BlackPeg;
end
if blackLight == 3
Board{i,p}= BlackPeg; Board{i,p+1}= BlackPeg; Board{i,p+2}= BlackPeg;
end
if blackLight == 4
Board{i,p}= BlackPeg; Board{i,p+1}= BlackPeg; Board{i,p+2}= BlackPeg; Board{i,p+3}= BlackPeg;
end

q = 8;

if whiteLight == 1
Board{i,q}= WhitePeg;
end
if whiteLight == 2
Board{i,q}= WhitePeg; Board{i,q-1}= WhitePeg;
end
if whiteLight == 3
Board{i,q}= WhitePeg; Board{i,q-1}= WhitePeg; Board{i,q-2}= WhitePeg;
end
if whiteLight == 4
Board{i,q}= WhitePeg; Board{i,q-1}= WhitePeg; Board{i,q-2}= WhitePeg; Board{i,q-3}= WhitePeg;
end

if blackLight < 4
fprintf(‘black light = %i White light = %i \n’, blackLight, whiteLight)
elseif blackLight == 4
fprintf(‘Good Job!\n’)
imshow([Board{1,:};Board{2,:};Board{3,:};Board{4,:};Board{5,:};Board{6,:};Board{7,:};Board{8,:};Board{9,:};Board{10,:}])
break
end

%*********************************************************************

 

end

 

if blackLight < 4
fprintf(‘You lose! Try again! \n\n’)
end

 

fprintf(‘The code is: %s, %s, %s, %s \n\n’, c1, c2, c3, c4)