E. Final Program with Comments

%***************************************************************
%* Chris Lenart, Nathan Murphy, Eddy Basharat, Michael Zhang *
%* Team C Class_17_App.m *
%* Dr. Elsaadany *
%***************************************************************

 

replay = true; %Flag variable keeps track of if the player wants to restart the game
while replay == true
clc
clear

warning(‘off’,’all’); % turns off all warning messages
load ‘Adventure.mat’;
mode = input(‘Choose difficulty. Press 1 for easy or 2 for hard: ‘);
playerX = 1; %Player initial x poition:
playerY = 1; %Player initial y poition:
if(mode == 1) %Easy Mode Stats for player and monster:
phealth = 10; %player’s health stat for easy mode
pattack = 2; %player’s attack stat for easy mode
pdefense = 1; %player’s defense stat for easy mode
mhealth = 5; %monster’s health stat for easy mode
mattack = 7; %monster’s attack stat for easy mode
mdefense = 1; %monster’s defense stat for easy mode
elseif(mode == 2) %Hard Mode Stats for player and monster:
phealth = 5; %player’s health stat for hard mode
pattack = 2; %player’s attack stat for hard mode
pdefense = 1; %player’s defense stat for hard mode
mhealth = 5; %monster’s health stat for hard mode
mattack = 7; %monster’s attack stat for hard mode
mdefense = 1; %monster’s defense stat for hard mode
end
isAtDoor = false; %Testing win or lose.
monsterX = 7; %Set monster initial position:
monsterY = 7;
World{playerY, playerX} = Player; %Loading inital world setup:
World{monsterY, monsterX} = Monster;
World{10,10} = Door;
World{2,3} = Sword;
World{6,1} = Shield;
World{5,8} = Health;
mhistory = Blank; %Keeps track of what’s in the spot before the monster moves to it:
phistory = Blank; %Keeps track of what’s in the spot before the player moves to it:

%Display board:
imshow([World{1,:};World{2,:};World{3,:};World{4,:};World{5,:};World{6,:};World{7,:};World{8,:};World{9,:};World{10,:}])

 

while (phealth > 0 && ~(isAtDoor)) %Test win or lose condition:
fprintf(‘Use “w” (up), “a” (left), “s” (down), “d” (right) to move. Press “e” to exit at any time.\nHealth: %i Attack: %i Defense: %i\n’, phealth, pattack, pdefense)
move = input(‘Move: ‘, ‘s’); %Prompts player to move and takes input.
clc
while(~strcmp(move, ‘w’) && ~strcmp(move, ‘d’) && ~strcmp(move, ‘s’) && ~strcmp(move, ‘a’) && ~strcmp(move, ‘e’))
fprintf(‘Choose a move that actually exists. Use “w” (up), “a” (left), “s” (down), “d” (right)\n’) %Failed to move to valid location.
move = input(‘Move: ‘, ‘s’);
clc
end
canmove = canMove(playerX, playerY);
if(strcmp(move, ‘e’)) %Exits game.
break
end
if(strcmp(move, ‘w’) && canmove(1)) %Move up.
World{playerY, playerX} = Blank;
playerY = playerY – 1;
phistory = World{playerY, playerX};
World{playerY, playerX} = Player;
elseif(strcmp(move, ‘d’) && canmove(2)) %Move right.
World{playerY, playerX} = Blank;
playerX = playerX + 1;
phistory = World{playerY, playerX};
World{playerY, playerX} = Player;
elseif(strcmp(move, ‘s’) && canmove(3)) %Move down.
World{playerY, playerX} = Blank;
playerY = playerY + 1;
phistory = World{playerY, playerX};
World{playerY, playerX} = Player;
elseif(strcmp(move, ‘a’) && canmove(4)) %Move left.
World{playerY, playerX} = Blank;
playerX = playerX – 1;
phistory = World{playerY, playerX};
World{playerY, playerX} = Player;
else
fprintf(‘That move is invalid. Please choose a different direction\n’)
end

%check if the player is at the door
if(phistory == Door)
isAtDoor = true;
break; %stop the execution of the while loop, ending the game
end
%check if the player picked up any items
if(phistory == Sword) %Sword item, increases player’s attack.
pattack = pattack + 5;
fprintf(‘You picked up the Large Broad Sword! It is pretty heavy for you but you pretend you can lift it anyway.\nYour attack increases by 5!\n’)
end
if(phistory == Shield) %Shield item, increases player’s defense and attack.
pdefense = pdefense + 5;
pattack = pattack + 2;
fprintf(‘You pick up the Titan”s Spiked Shield. Your arm starts to hurt but you continue to weild it.\nYour defense increases by 5 and your attack increases by 2!\n’)
end
if(phistory == Health) %Health item, increases health if consumed.
imshow([World{1,:};World{2,:};World{3,:};World{4,:};World{5,:};World{6,:};World{7,:};World{8,:};World{9,:};World{10,:}])
fprintf(‘You pick up the mysterious potion. It emits a very potent odor.\n’)
drink = input(‘Drink mysterious potion? Press 1 for yes or 2 for no: ‘);
if(drink == 1) %Player chooses to drink potion.
phealth = phealth + 3;
fprintf(‘You decide to finally act like the manly man you”ve always told yourself you were and you chug the potion.\nYour health increases by 3!\n’)
else %Player chooses not to drink potion.
fprintf(‘Wow… You are a coward.\n’)
end
end

%monster moves if it is alive.
if(mhealth > 0)
if(mode == 1) %Easy mode:
Mmove = monsterMove(monsterX, monsterY); %vector keeps track of the monster’s new x and y position after moving
elseif(mode == 2) %Hard mode:
Mmove = monsterMoveHard(monsterX, monsterY, playerX, playerY); %vector keeps track of the monster’s new x and y position after moving
end
World{monsterY, monsterX} = mhistory;
monsterX = Mmove(1);
monsterY = Mmove(2);
mhistory = World{monsterY, monsterX}; %Check item position realative to the monster.
World{monsterY, monsterX} = Monster;
else

end

%Battle Time!
if(phistory == Monster | mhistory == Player)
healths = battle(phealth, pattack, pdefense, mhealth, mattack, mdefense); %vector containing both player and monster healths after a battle occurs
phealth = healths(1);
mhealth = healths(2);
if(mhealth <= 0) %You beat the monster!
World{monsterY, monsterX} = mhistory;
mhistory = Blank; %fixes the problem of dublicating the player after a battle
end
end

%show the board after everyone moves
imshow([World{1,:};World{2,:};World{3,:};World{4,:};World{5,:};World{6,:};World{7,:};World{8,:};World{9,:};World{10,:}])
end

imshow([World{1,:};World{2,:};World{3,:};World{4,:};World{5,:};World{6,:};World{7,:};World{8,:};World{9,:};World{10,:}])
if(phealth <= 0) %Prompt player they lost if their health is less than 0
fprintf(‘You lost and The Curse continues on!\n’)
elseif(isAtDoor) %Prompt player they won if they are at the door
fprintf(‘You”re Winner!\n’) %Refrence to Big Rigs Racing.
end

if(strcmp(move, ‘e’)) %If player didn’t choose the option to quit the game, promp them if they want to play again
replay = false;
else
p = input(‘Would you like to play again? Enter “y” for yes or “n” for no. ‘, ‘s’);
while ~strcmp(p, ‘y’) && ~strcmp(p, ‘n’) %if the player enters an option that doesn’t exist then make them choose again
p = input(‘That is an invalid option. Enter “y” for yes or “n” for no. ‘, ‘s’);
end
if strcmp(p, ‘y’) %If they want to play again then the loop continues
replay = true;
else %If they don’t want to play again then the loop ends
replay = false;
end
end
end
fprintf(‘Thank you for playing!\n’) %Final message to player after they are finished playing

%Easy mode monster movement:
function [ position ] = monsterMove ( monsterx, monstery )
%Randomly generates monster movement.

moveTest = false; %flag variable keeps track if the generated move is valid

while moveTest == false
%Generates random movement (rng):
x = randi([-1 1]); %keeps track of the generated movement for the monster in the x direction
if (x == 0)
y = randi([-1, 1]); %keeps track of the generated movement for the monster in the y direction
else
y = 0;
end
canMonsterMove = canMove(monsterx, monstery); %the directions the monster is able to move

%Tests if the move is valid:
if (x == -1 && canMonsterMove(4))
moveTest = true;
elseif (x == 1 && canMonsterMove(2))
moveTest = true;
elseif (y == -1 && canMonsterMove(1))
moveTest = true;
elseif (y == 1 && canMonsterMove(3))
moveTest = true;
else
moveTest = false;
end

end
position = [monsterx + x, monstery + y];
end

%Hard mode monster movement (pursues player):
function [ position ] = monsterMoveHard ( monsterx, monstery, playerx, playery )
distx = playerx – monsterx; %the distance between the monster and player in the x direction
disty = playery – monstery; %the distance between the monster and player in the y direction
x = 0; %keeps track of the generated movement for the monster in the x direction
y = 0; %keeps track of the generated movement for the monster in the y direction
canMonsterMove = canMove(monsterx, monstery); %the directions the monster is able to move
if(abs(distx) >= abs(disty)) %moves in the direction that is farthest from the player
if(distx < 0 && canMonsterMove(4)) %if the distance is negative then move left
x = -1;
elseif(canMonsterMove(2)) %if the distance is positive then move right
x = 1;
end
elseif(abs(disty) > abs(distx))
if(disty < 0 && canMonsterMove(1)) %if the distance is negative then move up
y = -1;
elseif(canMonsterMove(3)) %if the distance is positive then move down
y = 1;
end
else

end

position = [monsterx + x, monstery + y];

end

 

function [ canmove ] = canMove( x, y )
%canMove Returns if the player can move in any direction
%Returns a four part vector [a, b, c, d] of either true or false. a is for
%moving up, b is for moving right, c is for moving down, and d is for
%moving left
up = ~(y-1 < 1); %boolean tells if the player can move up
right = ~(x+1 > 10); %boolean tells if the player can move right
down = ~(y+1 > 10); %boolean tells if the player can move down
left = ~(x-1 < 1); %boolean tells if the player can move left

canmove = [up, right, down, left];

end

 

%Battle function that randomly chooses numbers between 0 and attack
%and takes these values away from health. Player attacks first them
%monster:
function [ healths ] = battle(ph, patt, pdef, mh, matt, mdef)
fprintf(‘You are about to enter a battle with a ferocious beast! Prepare yourself soldier!\n’) %Prompts player:
while(mh > 0 && ph > 0)
delay = 3; %Delay the program for 3 seconds (for drama):
tic
t = 0;
while (t < delay) %How long battle is lasting:
t = toc;
end
pdamage = randi([2, patt]) – mdef; %Random damage:
fprintf(‘You hit a %i!\n’, pdamage) %Shows player damage output:
if(pdamage > 0) %Subtracts damage from mhealth.
mh = mh – pdamage;
end
if(mh > 0)
mdamage = randi([0, matt]) – pdef; %Random damage:
fprintf(‘The monster hit a %i!\n’, mdamage) %Shows monster damage output:
if(mdamage > 0) %Subtracts damage from phealth.
ph = ph – mdamage;
end
else
fprintf(‘You have slain the beast!\n’) %Prompts user that you beat the monster!
delay = 1;
tic
t = 0;
while (t < delay) %One sec delay (for drama)
t = toc;
end
end
end
healths = [ph, mh];
end %End code.