E1. Final Program with Comments (MAT-Rim)

Mat-rim Source Code

Note: the (‘) within the fprintfs are changed when put within this website, turning them into quotes, which will effect how they act within MATLAB if copied directly from the website.

 

clc
clear

fprintf(‘\n************************************************\n’)
fprintf(‘* Group: C Date: 11/09/20 *\n’)
fprintf(‘* Final Project Game File: Mat-rim.m *\n’)
fprintf(‘* Instructor: Michelle Soledad *\n’)
fprintf(‘************************************************\n\n\n\n’)

%fprintf(‘\nYou find yourself alone in a cold, damp dungeon shackled to the wall.\n’)
%fprintf(‘You are a normal adventurer, imprisoned for student loans you didn”t pay off.\n’)
%fprintf(‘One morning, while the guards are away, an earthquake rattles the dungeon,\n’)
%fprintf(‘tearing your shackles and leaving a crack in the wall wide enough to slip\n’)
%fprintf(‘through, what are you to do?\n\n’)

%This is the basic template for each decision within the game.

% an initial value is set for a so the while loop will loop until the
% player has made a decision
%Variable=0;

%A while loop is set so that the player has to input a value, if they
%mistype, the loop repeats until they choose an accepted value

%while Variable == 0

% A string variable called choice# is set by an input, prompting the player
% with either 1 or 2, and sometimes 3.

% choice# = input(‘decision (1) or decision (2)?: ‘, ‘s’);

%An if statement is embedded within the while loop, so if the player sets
%an accepted value to choice#, the while loop is broken.

% if strcmpi(choice#, ‘1’)
% Variable=1;
% elseif strcmpi(choice#, ‘2’)
% Variable=2;

% end
%end

%The chosen value for choice# is then evaluated within a following if
%statement

%First if evaluates if the Variable is a certain value, sam with the elseif

%if Variable == 1
% fprintf(‘\n *Description of what happens within the story next* \n\n’)
%elseif Variable == 2
% fprintf(‘\n *Typically the wrong choice, explaining how the character met a terrible fate* \n\n’)

% the return value is then used to end the game, in accordance with the
% players character dying or failing their quest.

% return
%end

%%
%Scene 0

%*Main screen* greets the player and prompts them to enter a name.
fprintf(‘Welcome to the World of Mat-rim\n\n’)
name = input(‘Enter Your Name: ‘,’s’);

%fprintf statement explains what is happening within the story
fprintf(‘\nYou find yourself alone in a cold, damp dungeon shackled to the wall.\n’)
fprintf(‘You are a normal adventurer, imprisoned for student loans you didn”t pay off.\n’)
fprintf(‘One morning, while the guards are away, an earthquake rattles the dungeon,\n’)
fprintf(‘tearing your shackles and leaving a crack in the wall wide enough to slip\n’)
fprintf(‘through, what are you to do?\n\n’)
A=0;

while A == 0
choice1 = input(‘Leave the dungeon (1) or Turn yourself in (2)?: ‘, ‘s’);

if strcmpi(choice1, ‘1’)
A=1;
elseif strcmpi(choice1, ‘2’)
A=2;

end
end

if A == 1
fprintf(‘\nyou find a sleeping guard with two weapons and a shield.\n\n’)
elseif A == 2
fprintf(‘\nYour journey has been cut short, you will remain this this dungeon\n’)
fprintf(‘until your student debt has been paid off.\n\n’)
return
end

%%
%Scene 2
B=0;

while B == 0
choice2 = input(‘\nTake the Shield (1), Sword (2), or Axe (3)? (Choose 2. Ex:1,3 for Shield and Axe): ‘, ‘s’);

%if statements include || to allow player any combination of weapons
%regardless of how they input it
if strcmpi(choice2, ‘1,2’) || strcmpi(choice2, ‘2,1’)
B=1;
elseif strcmpi(choice2, ‘1,3’) || strcmpi(choice2, ‘3,1’)
B=2;
elseif strcmpi(choice2, ‘2,3’) || strcmpi(choice2, ‘3,2’)
B=3;

end
end

if B == 1
fprintf(‘\nYou pick up the sword and shield and continue to escape.\n\n’)
elseif B == 2
fprintf(‘\nYou pick up the axe and shield and continue to escape.\n\n’)
elseif B == 3
fprintf(‘\nYou pick up the axe and sword and continue to escape.\n\n’)
end

%%
%Scene 3
C=0;

fprintf(‘\nFreshly out of the prison, you find yourself at a crossroads.\n’)
fprintf(‘between a road and a beaten path in the forest.\n\n’)

while C == 0
choice3 = input(‘Take the road (1) or path (2)?: ‘, ‘s’);

if strcmpi(choice3, ‘1’)
C=1;
elseif strcmpi(choice3, ‘2’)
C=2;

end
end

if C == 1
fprintf(‘\nYou travel the road and find yourself in a town called Eleven Eighty-one-ville.\n\n’)
elseif C == 2
fprintf(‘\nYou travel through the forest. However, not knowing the area,\n’)
fprintf(‘you soon become lost, days pass and you still can”t find a way out.\n’)
fprintf(‘Your journey is cut short as you starve in the wilderness.\n\n’)
return
end

%%
%Scene 4
D=0;

fprintf(‘\nOnce in the village you are tempted by a tavern down the road,\n’)
fprintf(‘but are tasked with the decision of reporting to the king first.\n\n’)

while D == 0
choice4 = input(‘Report to the king (1) or visit the tavern first (2)?: ‘, ‘s’);

if strcmpi(choice4, ‘1’)
D=1;
elseif strcmpi(choice4, ‘2’)
D=2;
end
end

if D == 2
fprintf(‘\nYou enter the tavern, and find a table of drunk locals and the barmaid\n\n’)
elseif D == 1
fprintf(‘\nAs you enter the keep, a guard recognizes you and throws you in the dungeon.\n\n’)
return
end

%%
%Scene 5
E=0;

while E == 0
choice5 = input(‘Do you talk to the locals (1) or keep conversation with the barmaid (2)?: ‘,’s’);

if strcmpi(choice5, ‘1’)
E=1;
elseif strcmpi(choice5, ‘2’)
E=2;
end
end

if E == 1
fprintf(‘\nThe inebriated locals don”t want to talk, and subsequently get\n’)
fprintf(‘into a fist fight with you, alerting the guards and leading to your arrest.\n\n’)
return
elseif E ==2
fprintf(‘\nYou talk to the bar maid. While talking, she tells you about \n’)
fprintf(‘a local dungeon rumored to have a wealth of loot within, yet \n’)
fprintf(‘no one has had the will to enter it.\n\n’)
end

%%
%Scene 6
F=0;

fprintf(‘\nYou contemplate going to the dungeon to gain loot to pay off your debt.\n\n’)

while F == 0
choice6 = input(‘Do you decided to embark on this adventure (1) or stay in the bar (2)?: ‘,’s’);
if strcmpi(choice6, ‘1’)
F=1;
elseif strcmpi(choice6,’2′)
F=2;
end
end

if F == 1
fprintf(‘\nYou embark to the location of the dungeon with a will strong\n’)
fprintf(‘enough to conquer anything in your path.\n\n’)
elseif F ==2
fprintf(‘\nYou decide to stay in the bar, deterred by the thought of \n’)
fprintf(‘danger, and waste away until the guards put you in the dungeon.\n\n’)
return
end

%%
%Scene 7

%Set Variable as 0 for the while loop. Additionally, introduce amulet value
%that plays into the later parts of the game.

G=0;
amulet=0;

fprintf(‘Along the way to the dungeon you come across a band of traveling merchants.\n’);
fprintf(‘You can buy a few healing potions or save the money.\n\n’)

while G == 0
choice7 = input(‘Do you buy the potions (1) or save money (2): ‘,’s’);
if strcmpi(choice7, ‘1’)
G=1;
elseif strcmpi(choice7, ‘2’)
G=2;
end
end

%if G == 1, the story continues without giving you the option to buy the
%amulet, however, if you choose G == 2, then you are given the option to
%buy the amulet.

if G == 1
fprintf(‘\nAs you”re bartering, you tell them of your valiant quest. They”re \n’)
fprintf(‘struck by your bravery and offer you a one-time deal: an amulet\n’)
fprintf(‘that supposedly gives you luck for half-price. A storm is brewing\n’)
fprintf(‘and you notice the smell of oncoming rain. You don”t have any\n’)
fprintf(‘more money, however, and have to deny the offer.\n\n’)
fprintf(‘All of the sudden, a massive, winding oak tree splits at its base \n’)
fprintf(‘and falls right on top of you! Your leg is broken, but alas, you \n’)
fprintf(‘have your healing potions! You take a swig and in a few long \n’)
fprintf(‘minutes, you have the strength to wriggle your way out from under\n’)
fprintf(‘the tree. Your leg feels like new! You continue along the path.\n\n’)
elseif G == 2
fprintf(‘As you”re bartering, you tell the merchants of your valiant quest. \n’)
fprintf(‘They”re struck by your bravery and offer you a \n’)
fprintf(‘one-time deal: an amulet that supposedly gives you luck for half-price. \n’)
fprintf(‘A storm is brewing and you notice the smell of oncoming rain.’)
end

%%
%Scene 8
%This if statement only occurs if you passed on buying the potions, ie, G=2

H=0;
%Save money to amulet
if G == 2
while H == 0
choice8 = input(‘\n\nDo you buy the amulet (1) or save money again (2)?: ‘,’s’);
if strcmpi(choice8, ‘1’)
H=1;
elseif strcmpi(choice8, ‘2’)
H=2;
end
end
end

%Within this statement, the player refuse to buy the potions, but bought
%the amulet, saving them from death, and setting the amulet value as 1,
%later used in the game.
if G == 2 && H == 1
amulet = 1;
fprintf(‘\n\nThe storm has come, the winds are tremendous and the rain torrential!\n’)
fprintf(‘You stay the course. The trees sway a drunkard above your head.\n’)
fprintf(‘During the storm, a strange glint catches your eye iff to the \n’)
fprintf(‘side of the path, and you investigate. All of a sudden, a massive,\n’)
fprintf(‘winding oak tree splits at its base onto the path right beside you!\n’)
fprintf(‘Once you”ve regained your senses, you finally look back to the\n’)
fprintf(‘source of the glint, but it”s gone. You look at the amulet dangling\n’)
fprintf(‘from your neck, then continue on.\n\n’)

%Within this if statement, it is evaluated if the player refused to buy
%both the potions and the amulet, leading to a death.
elseif G == 2 && H == 2
fprintf(‘The storm has come, the winds are tremendous and the rain torrential!\n’)
fprintf(‘You stay the course. The trees sway like a drunkard above your head.\n’)
fprintf(‘All of a sudden, a massive, winding oak tree splits at its base\n’)
fprintf(‘ and falls right on top of you! Your leg is broken. You”re trapped \n’)
fprintf(‘under the tree with miles of forest between you and the nearest town.\n’)
fprintf(‘The storm fades, but under the crushing weight of the tree, so do you.’)
return
end
%%
%Scene 9
I=0;

fprintf(‘\nAfter a few paces you stumble upon a small encampment not far from \n’)
fprintf(‘your path. You think long and hard about approaching the encampment \n’)
fprintf(‘or continuing on your way. You ultimately decide to leave it up to \n’)
fprintf(‘a coin toss. Heads, you move forward; tails, you decide to proceed to the encampment.\n\n’)

while I == 0
choice9 = input(‘Heads (1) or Tails (2)?: ‘,’s’);
if strcmpi(choice9, ‘1’)
I=1;
elseif strcmpi(choice9, ‘2’)
I=2;
end
end

if I == 1
fprintf(‘\nAlas! It was tails. You proceed into the encampment and are \n’)
fprintf(‘see a dame warming by the fire. She introduces herself as Michelle \n’)
fprintf(‘and asks if she can join you on your adventure. After a warm \n’)
fprintf(‘meal you agree to let her join.\n\n’)

fprintf(‘Shortly after you leave the encampment you are clubbed over the\n’)
fprintf(‘head and robbed by Michelle. You do not have the suppies to make \n’)
fprintf(‘it to aid and succumb to your injuries.\n\n’)
return
elseif I == 2
fprintf(‘\nTails! You proceed into the encampment and are greeted by a \n’)
fprintf(‘young healer. She intoduces herself as Michelle and after pleading, \n’)
fprintf(‘she decides to join you on your quest.\n\n’)
end

%%
%Scene 10

fprintf(‘You continue along your path and make it to the dungeon. You realize \n’)
fprintf(‘there are two entrances one on the left and one on the right.\n\n’)

J=0;

while J == 0
choice10 = input(‘Left (1) or right (2) entrance?: ‘,’s’);
if strcmpi(choice10, ‘1’)
J=1;
elseif strcmpi(choice10, ‘2’)
J=2;
end
end

if J == 1
fprintf(‘\nIT”S A TRAP! You fall into a pit of punji stakes! \n’)
fprintf(‘Despite her best work, Michelle cannot heal the two of you.\n’)
fprintf(‘You spend eternity in the pit.\n’)
return
elseif J == 2
fprintf(‘\nYou take the right path, followed by your companion.\n’)
fprintf(‘The dungeon smells of old flesh and decay, but you decide to trudge on.\n\n’)
end

%%
%Scene 11

K=0;

while K == 0
choice11 = input(‘You find yourself upon an old corpse with a ring around it”s finger, take it (1) or not (2)?: ‘,’s’);
if strcmpi(choice11, ‘1’)
K=1;
elseif strcmpi(choice11, ‘2’)
K=2;
end
end

%Within this statement, the player can only take the ring if they have the
%amulet from beforehand, which grants them luck, however, if they do not
%have the ring, the corpse will notice them, prompting a game over.
if K == 1

if amulet == 1
fprintf(‘\nYou take the ring without any sudden movements, and add it to your loot.\n’)
fprintf(‘\nyou delve deeper in to the dungeon and suddenly find yourself \n’)
fprintf(‘in a large room. On the other end of the room, a chest filled \n’)
fprintf(‘with unkown fortune. Suddenly, from a casket in the center bursts \n’)
fprintf(‘an undead crowned as the ancient ruler of Eleven Eighty-one-ville, titled JOSEPH WORKINGMAN. \n’)
fprintf(‘You must fight to reach the treasure and rid yourself of student debt.\n\n’)
ring = 1;
elseif amulet == 0
fprintf(‘\nThe corpse catches you by suprise and drives a dagger in your\n’)
fprintf(‘chest. Michelle runs away, and you are left alone in the dungeon.\n’)
return
end

elseif K == 2
ring = 0;
fprintf(‘\nyou delve deeper in to the dungeon and suddenly find yourself \n’)
fprintf(‘in a large room. On the other end of the room, a chest filled \n’)
fprintf(‘with unkown fortune. Suddenly, from a casket in the center bursts \n’)
fprintf(‘an undead crowned as the ancient ruler of Eleven Eighty-one-ville, titled JOSEPH WORKINGMAN. \n’)
fprintf(‘You must fight to reach the treasure and rid yourself of student debt.\n\n’)
end

%%
%Scene 12: Boss Fight (1)

fprintf(‘Joseph Workingman hurls a fireball at you, what do you do?’)

L=0;

while L == 0
choice12 = input(‘\n\nDodge (1) or try to block (2): ‘,’s’);
if strcmpi(choice12, ‘1’)
L=1;
elseif strcmpi(choice12, ‘2’)
L=2;
end
end

if L == 2
fprintf(‘\nYou try and block the fireball, which explodes upon impacting your defenses,\n’)
fprintf(‘\nThe flames envelop you and Michelle, and as the flames flicker out, so too does your journey.\n’)
return
elseif L == 1
fprintf(‘\nYou dodge the fireball and land ready for another attack.\n\n’)
end

%%
%Scene 13: Boss Fight (2)

fprintf(‘Joseph Workingman begins to charge something originating form his staff, what do you do?’)

M=0;

while M == 0
choice13 = input(‘\n\nBlock (1) or Charge Joseph (2): ‘,’s’);
if strcmpi(choice13, ‘1’)
M=1;
elseif strcmpi(choice13, ‘2’)
M=2;
end
end

if M == 1
fprintf(‘\nYou try and block Joseph”s attack, which turns out to be a \n’)
fprintf(‘chain lighting strike, striking Michelle, and then chaining to you. \n’)
fprintf(‘You both failed.\n’)
return
elseif M == 2
fprintf(‘\nYou charge Joseph, leaving him no other choice but to stop the spell and go on the defensive.\n\n’)
end

%%
%Scene 14: Boss Fight (3)

N=0;

while N == 0
choice14 = input(‘Block (1) or Attack (2): ‘,’s’);
if strcmpi(choice14, ‘1’)
N=1;
elseif strcmpi(choice14, ‘2’)
N=2;
end
end

if N == 1
fprintf(‘\nJoseph, confused at why the adventurer took this opportunity \n’)
fprintf(‘to block instead of attack, immediately switched gear and charges \n’)
fprintf(‘his spell again, releasing it into your shield, travelling through you and then to Michelle, \n’)
fprintf(‘you both failed.\n’)
return
elseif N == 2
fprintf(‘\nYou slash your weapon at Joseph, landing a critical hit! Joseph crumbles to the ground defeated..\n\n’)
end

%%
%Scene 15: End Scene

% This is the end of the game, first fprintfs will detail what happens
% after you defeat the boss
fprintf(‘\nYou and Michelle travel to the treasure chamber, which is filled \n’)
fprintf(‘with loot, Gold as far as the eye can see! you both take as much \n’)
fprintf(‘as you you can carry, and leave the dungeon.\n\n’)

fprintf(‘You both travel back to Eleven Eighty-one-ville and sell of the \n’)
fprintf(‘loot to the lord, just enough to pay off your student debt.\n’)

%This section is based on whether you attained the ring from the corpse,
%which is only possible if you attained the amulet from the vendor earlier.
%This give the player a secret ending, whereas their name they inputted
%initially will be displayed, and they crowned ruler of Eleven Eighty-one-ville
if ring == 1
fprintf(‘The lord of Eleven Eighty-one-ville looks at you hand and notices \n’)
fprintf(‘your ring. He proclaims that is the Ring of KEEN, which makes you \n’)
fprintf(‘the true ruler of Eleven Eighty-one-ville. You take the throne, \n’)
fprintf(‘lower college tuition costs, and make Michelle your advisor. \n’)
fprintf(‘All is well in Buckeye-land, long live %s, Ruler of Eleven Eighty-one-ville.\n’,name)

elseif ring == 0
fprintf(‘You pay off your debt, however, you are still broke. You and Michelle \n’)
fprintf(‘decide to continue questing about the world of Buckeye-land, \n’)
fprintf(‘and gain new experiences everyday.\n’)

end