How to create War card game on MatLab?

28 次查看(过去 30 天)
# # How to create War card game on MatLab?
Anybody has a complete code to use to play the card game War with MatLab?

回答(1 个)

Schuyler MacDonald
I tried my best for a few days and this is what I got:
%%MacDonald Schuyler
%%4/19/2019
%%WAR
clear all; close all;
%%ordered deck set up
rounds=1000;
hearts=1:13;
spades=1:13;
clubs=1:13;
diamonds=1:13;
deck= [diamonds,clubs,spades,hearts];
%shuffle deck
[m,n]=size(deck);
idx=randperm(n);
Sdeck=deck;
Sdeck(1,idx)=deck(1,:);
%deal half the deck to each player
[player1,player2]=deal(zeros(52,rounds));
player1(1:26)= Sdeck(1:26);
player2(1:26)= Sdeck(27:52);
%preallocate round stats
[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));
%%game play
for round=1:rounds
if player1(1,round)>player2(1,round)
a1=find(~player1(:,round),1,'first'); %find next open spot
player1(:,round+1)=player1(:,round); %create next rounds sequence
player2(:,round+1)=player2(:,round);
player1(a1+0,round+1)=player1(1,round); %add cards to end of winners deck
player1(a1+1,round+1)=player2(1,round);
for j=1:51 %remove cards just played from beginning of sequence and shift up
player1(j,round+1)=player1(j+1,round+1);
player2(j,round+1)=player2(j+1,round+1);
end
win1=win1+1; %tally player 1's wins
elseif player1(1,round)<player2(1,round)
a2=find(~player2(:,round),1,'first'); %find next open spot
player1(:,round+1)=player1(:,round); %create next rounds sequence
player2(:,round+1)=player2(:,round);
player2(a2+0,round+1)=player2(1,round); %add cards to end of winners deck
player2(a2+1,round+1)=player1(1,round);
for j=1:51 %remove cards just played from beginning of sequence and shift up
player1(j,round+1)=player1(j+1,round+1);
player2(j,round+1)=player2(j+1,round+1);
end
win2=win2+1; %tally player 2's wins
elseif player1(1,round)==player2(1,round) %%WAR
war=war+1;
if player1(3,round)>player2(3,round)
a1=find(~player1(:,round),1,'first'); %find next open spot
player1(:,round+1)=player1(:,round); %create next rounds sequence
player2(:,round+1)=player2(:,round);
player1(a1+0,round+1)=player1(1,round); %add player 1's winning cards to end of winners deck
player1(a1+1,round+1)=player1(2,round);
player1(a1+2,round+1)=player1(3,round);
player1(a1+3,round+1)=player2(1,round); %add player 2's loosing cards to end of winners deck
player1(a1+4,round+1)=player2(2,round);
player1(a1+5,round+1)=player2(3,round);
for k=1:3
for j=1:51 %remove cards just played from beginning of sequence and shift up
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'); %find next open spot
player1(:,round+1)=player1(:,round); %create next rounds sequence
player2(:,round+1)=player2(:,round);
player2(a2+0,round+1)=player2(1,round); %add player 2's winning cards to end of winners deck
player2(a2+1,round+1)=player2(2,round);
player2(a2+2,round+1)=player2(3,round);
player2(a2+3,round+1)=player1(1,round); %add player 1's loosing cards to end of winners deck
player2(a2+4,round+1)=player1(2,round);
player2(a2+5,round+1)=player1(3,round);
for k=1:3
for j=1:51 %remove cards just played from beginning of sequence and shift up
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=wwar+1;
if player1(5,round)>player2(5,round)
a1=find(~player1(:,round),1,'first'); %find next open spot
player1(:,round+1)=player1(:,round); %create next rounds sequence
player2(:,round+1)=player2(:,round);
player1(a1+0,round+1)=player1(1,round); %add player 1's winning cards to end of winners deck
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); %add player 2's loosing cards to end of winners deck
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 %remove cards just played from beginning of sequence and shift up
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'); %find next open spot
player1(:,round+1)=player1(:,round); %create next rounds sequence
player2(:,round+1)=player2(:,round);
player2(a2+0,round+1)=player2(1,round); %add player 2's winning cards to end of winners deck
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); %add player 1's loosing cards to end of winners deck
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 %remove cards just played from beginning of sequence and shift up
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=wwwar+1;
end
end
end
%round stats
b1=find(~player1(:,round),1,'first'); %find next open spot
b2=find(~player2(:,round),1,'first'); %find next open spot
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));
%check for a winner and end loops
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'); %find total rounds played
figure(1) %display stats
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');
%%assumptions
%The highest or winning card is placed under the winners deck first
%Perfectly evenly shuffled
%When two cards played are equal (war) one card is played face down and the
%next cards are compared

类别

Help CenterFile Exchange 中查找有关 Card games 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by