E. Final Program with Comments

War Final Program with Comments

load(‘CardDeck.mat’);
ShuffledDeck=randperm(52);
player=ShuffledDeck(1:26);
Computer=ShuffledDeck(27:52);

DeckValues = [14 2:13 14 2:13 14 2:13 14 2:13];
won=0;

fprintf(‘Press 1 and enter when is says “Go?” to take a turn. Press any key to quit\n’);
while won==0 %Game goes until someone wins

A=input(‘Go? ‘); %Ask user to take their turn
if isempty(Computer) %Check if a player has won the game and print ‘Winner’
fprintf(‘Player Wins!!’);
won=1;
elseif isempty(player) %Check if the computer has won the game and print ‘Winner’
fprintf(‘Computer Wins!!’);
won=1; %Break loop
elseif A~=1
won=1;
end

if A==1 %Begin turn
P=DeckValues(player(1)); %Set the value of each card every turn
C=DeckValues(Computer(1));

if P>C %If player wins round
subplot(1,1,1) %Display player and computer card, player on top
imshow([RedDeck{player(1)};RedDeck{Computer(1)}]); %Display function
fprintf(‘Player Wins this Round\n’);
player(length(player)+1)=Computer(1); %Add computer card to the end of player deck
player=[player player(1)]; %Duplicate player card and place at the end of deck
player(1)=[]; %Get rid of original player card
Computer(1)=[]; %Get rid of original computer card

elseif C>P %If computer wins round
subplot(1,1,1) %Display player and computer card, player on top
imshow([RedDeck{player(1)};RedDeck{Computer(1)}]); %Display function
fprintf(‘Computer Wins this Round\n’);
Computer(length(Computer)+1)=player(1); %Add player card to the end of computer deck
Computer= [Computer Computer(1)]; %Duplicated computer card and place at the end of deck
player(1)=[]; %Get rid of original player card
Computer(1)=[]; %Get rid of original computer card

else %If there is a tie
subplot(6,1,1) %Display tie setup for all 3 player and computer cards
imshow([RedDeck{player(5)}]);
subplot(6,1,2)
imshow([RedDeck{55} RedDeck{55} RedDeck{55}])
subplot(6,1,3)
imshow([RedDeck{player(1)}]);
subplot(6,1,4)
imshow([RedDeck{Computer(1)}]);
subplot(6,1,5)
imshow([RedDeck{55} RedDeck{55} RedDeck{55}]);
subplot(6,1,6)
imshow([RedDeck{Computer(5)}]);
fprintf(‘Tie!!\n’);
Pt=DeckValues(player(5)); %Establish player card value for tie breaker
Ct=DeckValues(Computer(5)); %Establish player card value for the tie breaker
%Tie Breaker
if Pt>Ct %If player wins tie breaker

if length(Computer)<5 %Incase computer doesn’t have enough cards for a full tie breaker

for i=1:1:length(Computer)
player(length(player)+1)=Computer(i);
Computer(i)=[];
end
player=[player player(1)];
player(1)=[];
else %When computer has enough cards
fprintf(‘Player Wins the Tie!\n’);
player(length(player)+1)=Computer(1); %Add all cards involved in tie breaker to player deck
player(length(player)+1)=Computer(2);
player(length(player)+1)=Computer(3);
player(length(player)+1)=Computer(4);
player(length(player)+1)=Computer(5);

player=[player player(1)]; %Create duplicate of player cards and place at back of deck
player(1)=[]; %Get rid of original player card
Computer(1)=[]; %Get rid of original computer cards
Computer(2)=[];
Computer(3)=[];
Computer(4)=[];
Computer(5)=[];
end

elseif Ct>Pt %If computer wins tie breaker

if length(player)<5 %Incase player doesn’t have enough cards for a full tie breaker

for j=1:1:length(player)
Computer(length(Computer)+1)=player(j);
player(j)=[];
end
Computer= [Computer Computer(1)];
Computer(1)=[];
else %When player has enough cards
fprintf(‘Computer Wins the Tie!\n’);
Computer(length(Computer)+1)=player(1); %Add all cards involved in tie breaker to computer deck
Computer(length(Computer)+1)=player(2);
Computer(length(Computer)+1)=player(3);
Computer(length(Computer)+1)=player(4);
Computer(length(Computer)+1)=player(5);

Computer= [Computer Computer(1)]; %Create duplicate of computer cards and place at back of deck
player(1)=[]; %Get rid of original player card
player(2)=[];
player(3)=[];
player(4)=[];
player(5)=[];
Computer(1)=[]; %Get rid of original computer cards
end

elseif Pt==Ct %DOUBLE TIE!!

Pt=DeckValues(player(6)); %Take value of second tie breaking card from player deck
Ct=DeckValues(Computer(6)); %Take value of second tie breaking card from computer deck
fprintf(‘DOUBLE TIE\n’)

if Pt>Ct %If player wins second tie breaker

if length(Computer)<5

for i=1:1:length(Computer) %in case computer does not have enough cards
player(length(player)+1)=Computer(i);
Computer(i)=[];
end
player=[player player(1)];
player(1)=[];
else
fprintf(‘Player Wins the Tie!\n’);
player(length(player)+1)=Computer(1); %Adding all of the computer’s cards to the players deck
player(length(player)+1)=Computer(2);
player(length(player)+1)=Computer(3);
player(length(player)+1)=Computer(4);
player(length(player)+1)=Computer(5);
player(length(player)+1)=Computer(6);
player(length(player)+1)=Computer(7);
player(length(player)+1)=Computer(8);
player(length(player)+1)=Computer(9);

player=[player player(1)]; %puts player card back in deck
player(1)=[]; deleting %deletes duplicate player card
Computer(1)=[]; %Removes the cards the computer lost
Computer(2)=[];
Computer(3)=[];
Computer(4)=[];
Computer(5)=[];
Computer(6)=[];
Computer(7)=[];
Computer(8)=[];
Computer(9)=[];
end

elseif Ct>Pt %Computer wins the second tie

if length(player)<5

for j=1:1:length(player) %Checking if player ahs enough cards
Computer(length(Computer)+1)=player(j);
player(j)=[];
end
Computer= [Computer Computer(1)];
Computer(1)=[];
else
fprintf(‘Computer Wins the Tie!\n’);
Computer(length(Computer)+1)=player(1); %Takes players cards that it won
Computer(length(Computer)+1)=player(2);
Computer(length(Computer)+1)=player(3);
Computer(length(Computer)+1)=player(4);
Computer(length(Computer)+1)=player(5);
Computer(length(Computer)+1)=player(6);
Computer(length(Computer)+1)=player(7);
Computer(length(Computer)+1)=player(8);
Computer(length(Computer)+1)=player(9);
Computer= [Computer Computer(1)]; %Puts computer card back in deck
player(1)=[];
player(2)=[]; %Removes cards player lost
player(3)=[];
player(4)=[];
player(5)=[];
player(6)=[];
player(7)=[];
player(8)=[];
player(9)=[];
Computer(1)=[]; %deletes duplicate
end

end

end

end

fprintf(‘Comp Cards: %.0f\n’,length(Computer));
fprintf(‘Player Cards: %.0f\n’,length(player));

end

end

 

 

 

 

Yahtzee Final Program with Comments

          Yahtzee Driver/ Main Script File

%Loads the Dice
load(‘Dice.mat’);
%Creates the first two columns of the scorecard
score = {1 ‘ones’;2 ‘twos’;3 ‘threes’;4 ‘fours’;5 ‘fives’;6 ‘sixes’;7 ‘Subtotal’;8 ‘Bonus’;9 ‘Upper Total’;10 ‘3 of a kind’;11 ‘4 of a kind’;12 ‘Full House’;13 ‘Small Straight’;14 ‘Large Straight’;15 ‘Yahtzee’;16 ‘Chance’;17 ‘Bonus Yahtzee’;18 ‘Lower Total’;19 ‘Grand Total’};
%Double For loop that adds three columns for the different players filled with zeros
for k = 1:19
for p = 3:5
score{k,p}=0;
end
end
%Shows the scorecard
score
%Creates a counter for the number of turns the game will run
turns = 48;
%Starts a while loop for the main game that will run until the turns are 0
while turns >=1
%A for loop for the 3 different players
for i = 1:3
%Prints out which player’s turn it is
fprintf(‘It is player %.0f turn\n’,i)
%Creates 5 random values 1-6 and stores them as roll
roll = randi([1 6] ,[1 5]);
%roll = [6 6 6 6 6];
%Shows the roll values as dice
imshow([Dice{roll}]);
%Calls the function rerolling to reroll the dice
roll = rerolling(roll,Dice);
%Asks the player which category of the scorecard they want to use
fprintf(‘Player %.0f Which category would you like to use these die for? \n(Enter 19 to skip turn)’,i)
categ = input(‘(enter category number) ‘);
%Uses the sort function to order the numbers numerically
roll = sort(roll);
%Starts a while loop for the category check that will break if the
%category has been used or the person skips their turn
while score{categ,(i+2)} == 0 & categ ~=19
%Runs the function to calculate the score and stores it into the
%proper index
score(categ,(i+2)) = {ScoreCalc(categ,roll,score,i)};
%If loop for if the category has already been used or there are no
%matching dice
if score{categ,(i+2)} == 0
%Asks for a different category number
fprintf(‘Player %.0f Which category would you like to use these die for? \n(Enter 19 to skip turn)’,i)
categ = input(‘(Enter category number) ‘);
end
end
%Checks if the bonus yahtzee has been used if so it adds 100 points to
%the yahtzee score and resets the bonus yahtzee score
if score{17,(i+2)} ~= 0
score{15,(i+2)} = score{15,(i+2)} + 100;
score{17,(i+2)} = 0;
end
%Creates a variable to store the sub total
sub = 0;
%Runs through a for loop to add the upper scores together and store it
%in sub
for k = 1:6
sub = sub + score{k,(i+2)};
end
%Creates a variable for the bonus
bonus = 0;
%Checks if the subtotal is greater than 63 if so it activates the bonus
if sub >= 63
bonus = 35;
end
%Creates a variable to store the lower total
lowertotal = 0;
%Creates a for loop to add up all the scores on the lower half of the scorecard
for k = 10:17
lowertotal = lowertotal + score{k,(i+2)};
end
%Adds the subtotal and the bonus to get the upper total
uppertotal = sub + bonus;
%Adds the lower total and upper total to get the overall score
grandtotal = uppertotal + lowertotal;
%Adds all the scores to the scorecard based on which players turn it is
score(7,(i+2)) = {sub};
score(8,(i+2)) = {bonus};
score(9,(i+2)) = {uppertotal};
score(18,(i+2)) = {lowertotal};
score(19,(i+2)) = {grandtotal};
%Subtracts a turn from the total
turns = turns – 1 ;
%Displays the scorecard at the end of the turn
score
end
end
%Checks if Player 2 had the highest score
if score{19,3} < score{19,4} & score{19,5} < score{19,4}
%Prints out Player 2 won
fprintf(‘Player 2 has won the game! With a score of %.0f points!\nThank you for playing!\n’,score{19,4})
%Checks if player 1 has the highest score
elseif score{19,4} < score{19,3} & score{19,5} < score{19,3}
%Prints out Player 1 won
fprintf(‘Player 1 has won the game! With a score of %.0f points!\nThank you for playing!\n’,score{19,3})
%Checks if player 3 has the highest score
elseif score{19,4} < score{19,5} & score{19,3} < score{19,5}
%Prints out Player 3 won
fprintf(‘Player 3 has won the game! With a score of %.0f points!\nThank you for playing!\n’,score{19,5})
%Checks if all the players have the same score
elseif score{19,3} == score{19,4} & score{19,4} == score{19,5}
%Prints out everyone tied
fprintf(‘Everyone has tied with a score of %.0f points\nThanks for playing!\n’,score{19,3})
%Checks if players 1 and 2 have the same score
elseif score{19,3} == score{19,4}
%Prints out Players 1 and 2 tied
fprintf(‘Players 1 and 2 have tied with a score of %.0f points!\nThanks for playing!\n’,score{19,3})
%Checks if players 2 and 3 have the same score
elseif score{19,4} == score{19,5}
%Prints out players 2 and 3 tied
fprintf(‘Players 2 and 3 have tied with a score of %.0f points! \nThanks for playing!\n’,score{19,4})
elseif score{19,3} == score{19,5}
%Prints out players 1 and 3 tied
fprintf(‘Players 1 and 3 have tied with a score of %.0f points! \nThanks for playing!\n’,score{19,5})
end

 

 

          Yahtzee Rerolling Function

function [roll] = rerolling(roll,Dice)
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
%Creates a counter an input variable, and an empty vector
reroll = -1;
c = 0;
v = [];
while (reroll ~= 0) & (c ~= 5);
%Asks
reroll = input(‘Which die would you like to reroll? (Die Number or 0 to cancel): ‘);
c = c + 1;
if reroll > 5 | reroll < 0;
fprintf(‘That is not a valid die please try again\n’);
elseif reroll >=1 & reroll <=5;
v(reroll) = 1;
end
end
for i =1:length(v)
if v(i)
roll(i) = randi([1 6]);
end
end
imshow([Dice{roll}]);
for i = 1:length(v)
if v(i)
fprintf(‘Would you like to reroll die %.0f? ‘,i);
reroll = input(‘(1 for yes or 0 for no) ‘);
v(i) = reroll;
end
end
for i =1:length(v)
if v(i)
roll(i) = randi([1 6]);
end
end
imshow([Dice{roll}]);
end

 

 

           Yahtzee Score Calculation Function

function [output] = ScoreCalc(i,roll,score,player)
%ScoreCalc Calculates the score gained from a set of dice
% Detailed explanation goes here
%Creates a variable for the total
total = 0;
%Checks if the category is between 1 and 6
if i >=1 & i <=6
%Runs through the dice and adds up all the ones that equal the category
%number
for k = 1:length(roll);
if roll(k) == i
total = total + roll(k);
end
end
%Sets the calculated total to the output of the function
output = total;
%Checks if the category is between 10 and 17
elseif i >=10 & i <=17
%Checks if the category is 10
if i == 10
%Checks if dice 1 2 and 3 are the same
if roll(1) == roll(2) & roll(2) == roll(3)
%Adds up dice 1 2 and 3
total = roll(1) + roll(2) + roll(3);
%Checks if dice 2 3 and 4 are the same
elseif roll(2) == roll(3) & roll(3) == roll(4)
%Adds up dice 2 3 and 4
total = roll(2) + roll(3) + roll(4);
%Checks if dice 3 4 and 5 are the same
elseif roll(3) == roll(4) & roll(4) == roll(5)
%Adds up dice 3 4 and 5
total = roll(5) + roll(4) + roll(3);
end
end
%Checks if the category is 11
if i == 11
%Checks if dice 1 2 3 and 4 are the same
if roll(1) == roll(2) & roll(2) == roll(3) & roll(3) == roll(4)
%Adds up dice 1 2 3 and 4
total = roll(1) + roll(2) + roll(3) + roll(4);
%Checks if dice 2 3 4 and 5 are the same
elseif roll(2) == roll(3) & roll(3) == roll(4) & roll(4) == roll(5)
%Adds up dice 2 3 4 and 5
total = roll(4) + roll(2) + roll(3) + roll(5);
end
end
%Checks if the category is 12
if i == 12
%Checks if the first 2 are the same and the last 3 are the same
if roll(1) == roll(2) & roll(3) == roll(4) & roll(4) == roll(5)
%sets the total to 25
total = 25;
%Checks if the first 3 are the same and the last 2 are the same
elseif roll(1) == roll(2) & roll(2) == roll(3) & roll(4) == roll(5)
%Sets the total to 25
total = 25;
else
end
end
%Checks if the category is 13
if i == 13
%Creates a vector u with the unique dice sorted in numerical order
%from the roll
u = unique(roll);
%Checks if dice 1 = 1 2 = 2 3 = 3 and 4 = 4
if u(1) == 1 & u(2) == 2 & u(3) == 3 & u(4) == 4
%Set the total to 30
total = 30;
%Checks if dice 1 = 2 2 = 3 3 = 4 and 4 = 5
elseif u(1) == 2 & u(2) == 3 & u(3) == 4 & u(4) == 5
%Sets the total to 30
total = 30;
%Checks if the dice 1 = 3 2 = 4 3 = 5 and 4 = 6
elseif u(1) == 3 & u(2) == 4 & u(3) == 5 & u(4) == 6
%Sets the total to 30
total = 30;
end
end
%Checks if the category is 14
if i == 14;
%Checks using the unique function if there are 5 different dice
if length(unique(roll)) == 5
%Sets the total to 40
total = 40;
end
end
%Checks if the category is 15
if i == 15
%Checks if all the dice are the same
if roll(1) == roll(2) & roll(2) == roll(3) & roll(3) == roll(4) & roll(4) == roll(5)
%Sets total to 50
total = 50;
end
end
%Checks if the category is 16
if i == 16
%Adds up all the dice values
for j = 1:5
total = total + roll(j);
end
end
%Checks if the category is 17
if i == 17
%Checks if the using the unique function if all the dice is the
%same and the that the yahtzee category has already been used
if length(unique(roll)) == 1 & score{15,(player+2)} ~= 0
%Sets the total to 50
total = 50;
else
%Prints out a message if the player tries to use the bonus
%Yahtzee without using the first yahtzee
fprintf(‘Please use the first Yahtzee before the bonus Yahtzee!\n’)
end
end
%Sets the output to be total
output = total;
end
end

 

(3H. References 5).