Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

% Function Name: WAR % Inputs (2): - (char) A string containing Player 1\'s card

ID: 3537344 • Letter: #

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%               ['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.'

PLEASE PROVIDE EXPLANATIONS TO SOLUTION!

Explanation / Answer

function out = WAR(p1,p2)
% initialize empty pile, set turn as not burn, and turns to 0
pile=[];
burn =0;
turns=0;
while(length(p2)>0 && length(p1)>0 && turns <2000);
% get the first card in each players deck
> two=p2(1);
% add cards to pile
pile=strcat(pile,one,two);
% check if burn turn
if(burn == 0)
%proceed if not in burn
%replace char value of cards with numeric values which can be compared
if(one == 'X')
   > end
if(one == 'J')
   > end
if(one == 'Q')
   > end
if(one == 'K')
   > end
if(one == 'A')
   > 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
% change char to numeric type
if(ischar(one))
   > end
if(ischar(two));
    two=str2num(two);
end
% compare card values
if( one> two)
% append pile to winner
    p1=strcat(p1(2:length(p1)),pile);
    p2=p2(2:length(p2));
    %empty pil
    pile=[];
% similarly for the rest two cases
elseif(one == two)
% if equal, set burn to 1
    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
%increase turns count
turns=turns+1;
else
% if burn turn is true
% burn the cards
    burn=0;
    p1=p1(2:length(p1));
    p2=p2(2:length(p2));
end
end
% display output accordingly
if( turns>=2000)
    out=sprintf('Players quit after 2000 turns');
elseif(length(p1)==0 && length(p2)==0)
% if both decks empty player 1 wins by default
    out=sprintf('Player 1 wins in %d turns ',turns);
elseif(length(p1)==0)
    out=sprintf('Player 2 wins in %d turns ',turns);
else
    out=sprintf('Player 1 wins in %d turns ',turns);
   
end

end