clear all; close all;
rounds=1000;
hearts=1:13;
spades=1:13;
clubs=1:13;
diamonds=1:13;
deck= [diamonds,clubs,spades,hearts];
[m,n]=size(deck);
idx=randperm(n);
Sdeck=deck;
Sdeck(1,idx)=deck(1,:);
[player1,player2]=deal(zeros(52,rounds));
player1(1:26)= Sdeck(1:26);
player2(1:26)= Sdeck(27:52);
[war,wwar,wwwar,win1,win2,warwin1,warwin2,wwarwin1,wwarwin2,a1,a2,b1,b2]=deal(0);
[mean1,mean2,sum1,sum2,size1,size2]=deal(zeros(1,rounds));
for round=1:rounds
if player1(1,round)>player2(1,round)
a1=find(~player1(:,round),1,'first');
player1(:,round+1)=player1(:,round);
player2(:,round+1)=player2(:,round);
player1(a1+0,round+1)=player1(1,round);
player1(a1+1,round+1)=player2(1,round);
for j=1:51
player1(j,round+1)=player1(j+1,round+1);
player2(j,round+1)=player2(j+1,round+1);
end
win1=win1+1;
elseif player1(1,round)<player2(1,round)
a2=find(~player2(:,round),1,'first');
player1(:,round+1)=player1(:,round);
player2(:,round+1)=player2(:,round);
player2(a2+0,round+1)=player2(1,round);
player2(a2+1,round+1)=player1(1,round);
for j=1:51
player1(j,round+1)=player1(j+1,round+1);
player2(j,round+1)=player2(j+1,round+1);
end
win2=win2+1;
elseif player1(1,round)==player2(1,round)
war=war+1;
if player1(3,round)>player2(3,round)
a1=find(~player1(:,round),1,'first');
player1(:,round+1)=player1(:,round);
player2(:,round+1)=player2(:,round);
player1(a1+0,round+1)=player1(1,round);
player1(a1+1,round+1)=player1(2,round);
player1(a1+2,round+1)=player1(3,round);
player1(a1+3,round+1)=player2(1,round);
player1(a1+4,round+1)=player2(2,round);
player1(a1+5,round+1)=player2(3,round);
for k=1:3
for j=1:51
player1(j,round+1)=player1(j+1,round+1);
player2(j,round+1)=player2(j+1,round+1);
end
end
win1=win1+1;
warwin1=warwin1+1;
elseif player1(3,round)<player2(3,round)
a2=find(~player2(:,round),1,'first');
player1(:,round+1)=player1(:,round);
player2(:,round+1)=player2(:,round);
player2(a2+0,round+1)=player2(1,round);
player2(a2+1,round+1)=player2(2,round);
player2(a2+2,round+1)=player2(3,round);
player2(a2+3,round+1)=player1(1,round);
player2(a2+4,round+1)=player1(2,round);
player2(a2+5,round+1)=player1(3,round);
for k=1:3
for j=1:51
player1(j,round+1)=player1(j+1,round+1);
player2(j,round+1)=player2(j+1,round+1);
end
end
win2=win2+1;
warwin2=warwin2+1;
elseif player1(3,round)==player2(3,round)
wwar=wwar+1;
if player1(5,round)>player2(5,round)
a1=find(~player1(:,round),1,'first');
player1(:,round+1)=player1(:,round);
player2(:,round+1)=player2(:,round);
player1(a1+0,round+1)=player1(1,round);
player1(a1+1,round+1)=player1(2,round);
player1(a1+2,round+1)=player1(3,round);
player1(a1+3,round+1)=player1(4,round);
player1(a1+4,round+1)=player1(5,round);
player1(a1+5,round+1)=player2(1,round);
player1(a1+6,round+1)=player2(2,round);
player1(a1+7,round+1)=player2(3,round);
player1(a1+8,round+1)=player2(4,round);
player1(a1+9,round+1)=player2(5,round);
for k=1:5
for j=1:51
player1(j,round+1)=player1(j+1,round+1);
player2(j,round+1)=player2(j+1,round+1);
end
end
win1=win1+1;
wwarwin1=wwarwin1+1;
elseif player1(3,round)<player2(3,round)
a2=find(~player2(:,round),1,'first');
player1(:,round+1)=player1(:,round);
player2(:,round+1)=player2(:,round);
player2(a2+0,round+1)=player2(1,round);
player2(a2+1,round+1)=player2(2,round);
player2(a2+2,round+1)=player2(3,round);
player2(a2+3,round+1)=player2(4,round);
player2(a2+4,round+1)=player2(5,round);
player2(a2+5,round+1)=player1(1,round);
player2(a2+6,round+1)=player1(2,round);
player2(a2+7,round+1)=player1(3,round);
player2(a2+8,round+1)=player1(4,round);
player2(a2+9,round+1)=player1(5,round);
for k=1:5
for j=1:51
player1(j,round+1)=player1(j+1,round+1);
player2(j,round+1)=player2(j+1,round+1);
end
end
win2=win2+1;
wwarwin2=wwarwin2+1;
elseif player1(7,round)==player2(7,round)
wwwar=wwwar+1;
end
end
end
b1=find(~player1(:,round),1,'first');
b2=find(~player2(:,round),1,'first');
size1(round)=b1-1;
size2(round)=b2-1;
mean1(round)=mean(player1(1:size1(round),round));
mean2(round)=mean(player2(1:size2(round),round));
sum1(round)=sum(player1(:,round));
sum2(round)=sum(player2(:,round));
if sum(player1(:,round))==0
fprintf('player 2 wins!');
elseif sum(player2(:,round))==0
fprintf('player 1 wins!')
end
end
totalrounds=find(~player1(1,:),1,'first');
figure(1)
plot(1:totalrounds,size1(1:totalrounds),'b');
hold on
plot(1:totalrounds,size2(1:totalrounds),'r');
plot(1:totalrounds,size1(1:totalrounds)+size2(1:totalrounds),'k');
legend('player 1 deck size','player 2 deck size','deck size should be constant');
figure(2)
plot(1:totalrounds,sum1(1:totalrounds),'b');
hold on
plot(1:totalrounds,sum2(1:totalrounds),'r');
legend('player 1 sum of cards','player 2 sum of cards');
0 Comments
Sign in to comment.