Code Clarification on If statements

1 visualizzazione (ultimi 30 giorni)
I am looking at Ryan Showden's code on the Monty Hall problem and I am confused about one section.
Some background on the Monty Hall problem: If you are a contestant on a game show. There are three doors, one of which conceals a new car, while the other two are empty. After picking a door, the host opens one door to show that it is empty. The host then gives you the option to switch doors. If you switch you win around 60% of the time but if you dont swtich you win 30% of the time.
Can someone please explain how this piece works in relation to the output and rest of the code?
if PromptMessage == 1
Choice2 = PlayerOption(1);
elseif PromptMessage == 2
Choice2 = PlayerOption(2);
end
I have added the rest of the code below.
Doors = [1 0 0]; %Initial digits for door options, randomized later
W = 0; %Initialized win
L = 0; %Initialized lose
Choice1 = 3; %Initial door choice
PromptMessage = 2; %1=stay with original choice, 2=switch to remaining door
for i = 1:100
%Randomize winning door position
Doors(randperm(numel(Doors))) = Doors;
%Determine winning door position
Win = find(Doors);
%Determine the two losing doors the host could reveal
HostOption = find(Doors==0);
if Choice1 == Win
%
% %Randomize which losing door is revealed by the host
HostRevealRand = datasample(HostOption,1);
%
% %Zero out whichever door the host revealed
HostCheck = HostOption - HostRevealRand;
%
% %Find the door the host did not reveal i.e. whichever one wasn't
% %zeroed out
HostCheckIndex = find(HostCheck);
HostLeftOver = HostOption(HostCheckIndex);
%
% %Determine doors now available to player: original winning choice
% %and a losing doorj
PlayerOption = [Choice1 HostLeftOver];
%Determine door player chose depending on input to PromptMessage
if PromptMessage == 1
Choice2 = PlayerOption(1);
elseif PromptMessage == 2
Choice2 = PlayerOption(2);
end
elseif Choice1 == HostOption(1)
%Determine doors now available to player: original losing choice
%and winning door
PlayerOption = [Choice1 Win];
%Determine door user chose depending on input to PromptMessage
if PromptMessage == 1
Choice2 = PlayerOption(1);
elseif PromptMessage == 2
Choice2 = PlayerOption(2);
end
elseif Choice1 == HostOption(2)
%Determine doors now available to player: original losing choice
%and winning door
PlayerOption = [Choice1 Win];
%Determine door user chose depending on input to PromptMessage
if PromptMessage == 1
Choice2 = PlayerOption(1);
elseif PromptMessage == 2
Choice2 = PlayerOption(2);
end
end

Risposta accettata

Geoff Hayes
Geoff Hayes il 13 Dic 2019
Julia - all this code
if PromptMessage == 1
Choice2 = PlayerOption(1);
elseif PromptMessage == 2
Choice2 = PlayerOption(2);
end
seems to be doing is to select a door given the contestant's decision to either remain with his/her current decision (1) or choose the remaining door (2). PlayerOption is simply an array of the two remaining doors (Monty Hall has already opened one door and shown that there is nothing behind it) one of which is the door to the car. The author of the code is creating an array of those two doors with the first one being the door that the user has already selected. So if PromptMessage is 1, then the user will stick with his original choice and so Choice2 is the id for that door (since his/her original choice is the first in the array, the code assigns PlayerOption(1) to Choice2). If the PromptMessage is 2, then the user switches to the other door and it's id is assigned to Choice2. The code then compares this final choice to true winning door id and increments the winning or losing count appropriately.
The code may be a little easier to understand if the variables were named differently. For example,
if playerDecision == 1
% player decides to stick with original door selection
finalDoorId = RemainingDoorIds(1);
elseif PromptMessage == 2
% player decides to choose the other door
finalDoorId = RemainingDoorIds(2);
end
and
%% Determine number of wins and losses
if finalDoorId == winningDoorId
numberOfWins = numberOfWins + 1
else
numberOfLosses = numberOfLosses + 1
end

Più risposte (0)

Categorie

Scopri di più su Number games in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by