%------------------------------------------------------------
%  A prize is hidden behind one of three doors: 1, 2, or 3.
%  The user guesses which door.  Computer then tells him
%  another door that the prize is NOT behind and offers to
%  let him change his guess.  User decides whether or not
%  to change and computer then tells him whether or not he
%  wins.
%
%  Play this game many times with each strategy and count
%  number of wins / number of times played to see which
%  strategy is better.
%------------------------------------------------------------

%  Put prize behind one of the doors (i=1,2,3), with equal
%  probability 1/3 for each.

x=rand;
if x < 1/3,  i=1;  end;     
if x >= 1/3 & x < 2/3,  i=2;  end;
if x >= 2/3,  i=3;  end;

%  Ask user to pick a door (j).

j = input(' Pick a door (1,2,3): ');

%  Decide which door to tell him it is NOT behind.
%  If he guessed wrong, tell him the other wrong door;
%  if he guessed right, tell him one of the other two, with
%  equal probability.

if j~=i,                            % Guess is wrong.
  if j~=1 & i~=1,  noti = 1;  end;
  if j~=2 & i~=2,  noti = 2;  end;
  if j~=3 & i~=3,  noti = 3;  end;
else                                % Guess is right.
  if j==1,  noti1 = 2;  noti2 = 3;  end;
  if j==2,  noti1 = 1;  noti2 = 3;  end;
  if j==3,  noti1 = 1;  noti2 = 2;  end;
  y=rand;
  if y < .5,  noti=noti1;  end;
  if y >= .5,  noti=noti2;  end;
end;

fprintf(' Prize is not behind door number %i', noti)

%  Give user the opportunity to change his guess.

yesno = input(' Do you want to change doors? (yes=1, no=0) ');

if yesno==1,                       % Change to the door that he
  if j~=1 & noti~=1, newj=1; end;  % did not guess and that you
  if j~=2 & noti~=2, newj=2; end;  % did not tell him does not
  if j~=3 & noti~=3, newj=3; end;  % contain the prize.
else
  newj = j;                        % Stick with original guess.
end;

%  Print result.

if i==newj,
  fprintf(' You win.  Prize is behind door %i \n', i)
else
  fprintf(' Sorry, you lose.  Prize is behind door %i \n', i)
end;
