Function Name: WAR % Inputs (2): - (char) A string containing Player 1\'s cards
ID: 3537250 • Letter: F
Question
Function Name: WAR
% Inputs (2): - (char) A string containing Player 1's cards
% (char) A string containing Player 2's cards
% Outputs (2): - (char) A string containing the winner and the number of
% turns taken
%
% Function Description:
% WAR: one of the simplest and most time consuming two-player card games
% known to man. Grab a friend, divide a deck of cards in half, each of
% you flips a card, highest card wins and the winner takes them both.
% Play continues until one of you is out of cards...or, like most people,
% you both get sick of playing after 20 minutes and quit.
%
% Not anymore! With the help of this function, the fun is over before it
% begins! Given two 'decks' (strings) of cards, you can now predict the
% winner of a game of war and exactly how long the victory would take.
%
% RULES:
% -The hierarchy of the cards is as follows:
% A > K > Q > J > X > 9 > 8 > 7 > 6 > 5 > 4 > 3 > 2
% *10's are represented by the Roman numeral X*
%
% -For each round, the pair of cards is appended to the winner's 'deck'
% in the order: [P1 card, P2 card]
%
% -In the event of a war (cards are equal), players 'burn' the next card
% in their decks. Burning a card means that you do not check to see who
% won the round, you just add the cards into the pile according to the
% rules (P1 first, then P2), and play continues to the next turn, and
% that winner takes all six cards, war pair first, burn pair second, and
% turn pair thrid ('burning' cards does not count as a turn).
%
% ex) P1 = ['6', '7' '2' '5'] P2 = ['6' '5' '4' '8']
% ~ We look at the first round
% P1 "plays" '6' P2 "plays" '6'
% ~ We have a war.... so add those cards to the "pile", then
% "burn a card"
% pile = ['6' '6' '7' 5'] --> or [P1(1), P2(1), P1(2), P2(2)]
% P1 = ['2' '5'] P2 = ['4' '8']
% ~ We now play another hand...
% pile = ['6' '6' '7' 5'] --> or [P1(1), P2(1), P1(2), P2(2)]
% P1 "plays" '2' P2 "plays" '4'
% ~ P2 beats P1, so add the cards to the pile and put the pile at the
% back of P2's hand
% pile = ['6' '6' '7' '5' '2' '4']
% **UPDATE P2**
% P1 = ['5'] P2 = ['8' '6' '6' '7' '5' '2' '4']
% ~ Continue on play as normal
%
% -If a player's last card is played in a war, he or she is out of cards
% and loses by default (with the unfinished war counting as the last
% turn).
%
% -If a game takes more than 2000 turns, assume players quit.
%
% ex.: P1=['6' '4' '7' 'K' 'J'] P2=['A' '4' '3' '8' '2']
% TURN1 6<A
% ['4' '7' 'K' 'J'] ['4' '3' '8' '2' '6' 'A']
% TURN2 4=4
% 7 & 3 burned
% TURN3 K>8
% ['J' '4' '4' '7' '3' 'K' '8'] ['2' '6' 'A']
% etc...
%
% The output should be a string formatted as follows:
%
% 'Player (1 or 2) wins in # turns.'
% or
% 'Players quit after 2000 turns.'
%
% Notes:
% - If both players run out of cards simultaneously, Player 1 is
% considered the winner.
%
% Test Cases:
%
% PA = ['9' '7' 'A' '2' 'K'];
% PB = ['5' '6' 'A' 'Q' 'K'];
%
% [out1] = WAR(PA,PB);
% out1 => 'Player 1 wins in 4 turns.'
%
% PC = ['6' '4' '7' 'K' 'J']
% PD = ['A' '4' '3' '8' '2']
%
% [out2] = WAR(PC,PD);
% out2 => 'Players quit after 2000 turns.'
%
% PE = '6478JQ45674XK823AKQ';
% PF = 'A43422223XA24XQ4767';
%
% [out3] = WAR(PE,PF);
% out3 => 'Player 1 wins in 229 turns.'
Explanation / Answer
function result = WAR(p1,p2)
pile=[];
burn =0;
turns=0;
while(length(p2)>0 && length(p1)>0 && turns <2000)
one=p1(1);
two=p2(1);
pile=strcat(pile,one,two);
if(burn == 0)
if(one == 'X')
one = 10;
end
if(one == 'J')
one= 11;
end
if(one == 'Q')
one = 12;
end
if(one == 'K')
one= 13;
end
if(one == 'A')
one = 14;
end
if(two == 'X')
two = 10;
end
if(two == 'J')
two= 11;
end
if(two == 'Q')
two = 12;
end
if(two == 'K')
two= 13;
end
if(two == 'A')
two = 14;
end
if(ischar(one))
one=str2num(one);
end
if(ischar(two))
two=str2num(two);
end
if( one> two)
p1=strcat(p1(2:length(p1)),pile);
p2=p2(2:length(p2));
pile=[];
elseif(one == two)
p1=p1(2:length(p1));
p2=p2(2:length(p2));
burn =1;
elseif(two>one)
p2=strcat(p2(2:length(p2)),pile);
p1=p1(2:length(p1));
pile=[] ;
end
turns=turns+1;
else
burn=0;
p1=p1(2:length(p1));
p2=p2(2:length(p2));
end
end
if( turns>=2000)
disp('Players quit after 2000 turns');
elseif(length(p1)==0 && length(p2)==0)
fprintf('Player 1 wins in %d turns ',turns);
elseif(length(p1)==0)
fprintf('Player 2 wins in %d turns ',turns);
else
fprintf('Player 1 wins in %d turns ',turns);
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.