This code simulates a dual between 3 players and compares two strategies

ThreeWayDualProbability.m

%Author:Alex Dytso
%Date: 02/18/2013
%Description: This code simulates a dual between 3 players. Where first has a probability of hiting 30%
%second man has probability of hiting 50%
%third man has probability of hiting 100%
%This porgram similates possible outcomes for two different strategies.
%First strategy all players shoot
%Second strategy player 1 missus his first shot on purpose.
%This code will show that suprisingly the second strategy is much better
%for player 1
clc, clear all, clf
t=input('how many times do you want to run the simulation \n')
rounds=0;
count=[0,0,0];
%First Strategy
while rounds<t % number of itereation of the simulation
% if alive alivei=1 if dead alivei=0
alive1=1;
alive2=1;
alive3=1;% all alive
while (alive1==1 && alive2==1) ||( alive1==1 && alive3==1) || (alive2==1 && alive3==1) %simulation runs while at least 2 is alive
%Player 1 makes a shot. By problems statement his probability of success
%is 30% so the following is how he shoots
if alive1==1 %player 1 must be alive to shoot
s1=floor(3*rand)+1;% makes a shot. If s1=1 or 2 it's miss if s1=3 target is shoot
if s1==3 && alive3==1 % Player 1 shoots at Player 3 first
alive3=0; %shoots player
elseif s1==3 && alive2==1 % if plyer 3 is dead, shoots player 2
alive2=0;
end
end
if alive2==1 %% if second player is alive
s2=floor(2*rand)+1; %makes a shot. If s1=1 it's miss if s1=2 target is shoot
if s2==2 && alive3==1 %Player 2 shoots at Player 3 first
alive3=0;
elseif s2==2 && alive1==1
alive1=0;
end
end
if alive3==1 %% third player is alive
%Player 3 never misses
if alive2==1 %if player 2 is alive shoots player 2
alive2=0;
else
alive1=0; %else shoots player 2
end
end
end
rounds=rounds+1; %count of simulation rounds
%counting frequencey of wins
count(1)=count(1)+alive1;
count(2)=count(2)+alive2;
count(3)=count(3)+alive3;
end
subplot(1,2,1)
pie(count)
title('First Strategy')
count
%Second strategy. Fisrt Player misses on purpose on the first shot.
%As you will see such a strategy improves his odds
rounds=0;
count=[0,0,0];
%First Strategy
while rounds<t % number of itereation of the simulation
% if alive alivei=1 if dead alivei=0
alive1=1;
alive2=1;
alive3=1;% all alive
d=0;
while (alive1==1 && alive2==1) ||( alive1==1 && alive3==1) || (alive2==1 && alive3==1) %simulation runs while at least 2 is alive
%Player 1 makes a shot. By problems statement his probability of success
%is 30% so the following is how he shoots
if d==1 %this skips the first round
if alive1==1 %player 1 must be alive to shoot
s1=floor(3*rand)+1;% makes a shot. If s1=1 or 2 it's miss if s1=3 target is shoot
if s1==3 && alive3==1 % Player 1 shoots at Player 3 first
alive3=0; %shoots player
elseif s1==3 && alive2==1 % if plyer 3 is dead, shoots player 2
alive2=0;
end
end
end
d=1;
if alive2==1 %% if second player is alive
s2=floor(2*rand)+1; %makes a shot. If s1=1 it's miss if s1=2 target is shoot
if s2==2 && alive3==1 %Player 2 shoots at Player 3 first
alive3=0;
elseif s2==2 && alive1==1
alive1=0;
end
end
if alive3==1 %% third player is alive
%Player 3 never misses
if alive2==1 %if player 2 is alive shoots player 2
alive2=0;
else
alive1=0; %else shoots player 2
end
end
end
d=0;
rounds=rounds+1; %count of simulation rounds
%counting frequencey of wins
count(1)=count(1)+alive1;
count(2)=count(2)+alive2;
count(3)=count(3)+alive3;
end
subplot(1,2,2)
pie(count)
title('Second Strategy')
legend('Player1-30% Accur.','Player2-50% Accur.','Player3-100% Accur.')
count