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

Matlab Skeleton vs. Warrior extra credit. Due beginning of class Oct. 4th, 2017

ID: 2248589 • Letter: M

Question

Matlab Skeleton vs. Warrior extra credit. Due beginning of class Oct. 4th, 2017 Inputs: User inputs each ROUND what they want to use a sword or an axe to attack the skeleton. Program: -a sword deals 2D4 damage, and an axe that deals 1D10 damage. (1#D2# 1# means how many dice are rolled. 2# means how many sides the particular dice has, ex. 2d4 means two dice are rolled and these dice only have 4 sides.) -Use a random number generator with DISCRETE probability, not Gaussian probability, for coming up with the random dice rolls. -Have the program keep track of Hero's life, and Skeleton's life. Hero always starts with 110 life, while Skeleton always starts with 100 life. -Any damage dealt to skeleton or hero subtracts from their present life amount. -Skeleton attacks hero each time after hero attacks. Skeleton has one weapon that deals 1D12 to Hero. -Victory condition is Skeleton's life is less than 0 BEFORE Skeleton's attack. -Defeat condition is Hero's life is less that 0 AFTER Skeleton's attack. -If no Victory or Defeat condition is meet, start over and do another ROUND of attacks. Output: -Display to User amount of damage Hero deals to Skeleton. -Display to User amount of damage Skeleton deals to Hero. -Display to User Current life of Hero and Current Life of Skeleton. -If Victory/Defeat conditions are meet, display to User VICTORY!!!!! or DEFEAT......

Explanation / Answer

Discrete probability function (Save as gendist.m in same working folder):

function T = gendist(P,N,M)
if nargin~=3
error('Error: Invalid number of input arguments.')
end

if min(P)<0
error('Error: All elements of first argument, P, must be positive.')
end

if or(N<1,M<1)
error('Error: Output matrix dimensions must be greater than or equal to one.')
end

%normalize P
Pnorm=[0 P]/sum(P);

%create cumlative distribution
Pcum=cumsum(Pnorm);

%create random matrix
N=round(N);
M=round(M);
R=rand(1,N*M);

%calculate T output matrix
V=1:length(P);
[~,inds] = histc(R,Pcum);
T = V(inds);

%shape into output matrix
T=reshape(T,N,M);

Main Code(Saves as <file_name.m> in same working directory and run this file):


warrior_health = 110;
skeleton_health = 100; % Hero and Skeleton health initialization
round_count = 1; % battle round counter
weapon = '';
sword=[1/4 1/4 1/4 1/4]; %sword attack probability
roll1 = 0;
roll2 = 0;
axe=[1/10 1/10 1/10 1/10 1/10 1/10 1/10 1/10 1/10 1/10]; %axe attack probability
skeleton_weapon=[1/12 1/12 1/12 1/12 1/12 1/12 1/12 1/12 1/12 1/12 1/12 1/12]; %skeleton attack probability
result = 0;

while and(~(warrior_health < 0),~(skeleton_health < 0))
weapon = input([' Select your weapon for round ' num2str(round_count) ' [s/a] '],'s');
if strcmp(weapon,'s')
roll1 = gendist(sword,1,1);
roll2 = gendist(sword,1,1);
damage_warrior = roll1 + roll2; %sword attack calculation
elseif strcmp(weapon,'a')
damage_warrior = gendist(axe,1,1); %axe attack calculation
else
disp('Enter correct value')
continue
end
  
damage_skeleton = gendist(skeleton_weapon,1,1); %skeleton attack calculation
  
round_count = round_count + 1;
disp(['Hero unleashes ' num2str(damage_warrior) ' attack points'])
skeleton_health = skeleton_health - damage_warrior; %skeleton health caclculation
if skeleton_health<0
break
end
disp(['Skeleton retaliates with ' num2str(damage_skeleton) ' attack points'])
warrior_health = warrior_health - damage_skeleton; %hero health caclculation
if warrior_health<0
break
end
disp(['Hero health = ' num2str(warrior_health) ' Skeleton health = ' num2str(skeleton_health)])
end

result = (warrior_health<0)*1 + (skeleton_health<0)*2;

if result == 1
disp('DEFEAT......')
elseif result == 2
disp('VICTORY!!!!!')
else
disp('Rematch')
WarriorVsSkeleton
end