I have a project that requires me to use a structure for a text based adventure game I made. I've completely made the game and I have a structure for a character, similar to what a D&D character sheet would look like. I need to have values in the structure change, such as my character's hitpoints or armor. What I had before was just a base "Oxygen" variable that would change in every room that he went to, but now I need to alter specific parts of the structure.
I also don't know how to put the structure into the program, as it has its own int main() that I made in a separate project.
Here is my structure,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
struct patient
{
int gender;
int hitpoints;
int sanity;
double armor;
double damage;
};
patient initializePatient();
void print(patient);
int main()
{
patient David = initializePatient();
patient Timothy = initializePatient();
print(David);
cout << endl;
print(Timothy);
David.armor = 50;
cout << "\n\n";
print(David);
cout << endl;
print(Timothy);
}
patient initializePatient()
{
patient Samuel;
Samuel.gender = 1;
Samuel.armor = 100.0;
Samuel.damage = 5.0;
Samuel.hitpoints = 100;
Samuel.sanity = 120;
return Samuel;
}
void print(patient S)
{
cout << "Gender: " << S.gender << endl;
cout << "Armor: " << S.armor << endl;
cout << "Damage: " << S.damage << endl;
cout << "Hitpoints: " << S.hitpoints << endl;
cout << "Sanity: " << S.sanity << endl;
}
|
And here is my adventure program
[/code]
/* Ethan Dale
* February 17, 2018
* Assignment 04
* Section 02
*/
#include <iostream>
#include <stdlib.h> //This is the library for seeding the srand()
#include <time.h> //This is also for seeding the srand()
using namespace std;
//Function Prototypes
int menu();
void oxygencount();
int room1();
int room2();
int room3();
int room4();
int room5();
int room6();
int gameover();
int gamewin();
//Game-Wide Variables
int oxygen = 120;
//Main Function
int main()
{
oxygen = 120;
menu();
}
//Menu for play or exit
int menu()
{
int menuchoice;
cout << "Welcome to Asylum! Would you like to play?\n1 - Play\n2 - Exit\n";
cin >> menuchoice;
switch (menuchoice)
{
case 1:
room1();
break;
case 2:
return 0;
break;
default:
cout << "Invalid Choice.\n\n";
menu();
}
}
//Oxygen Function
void oxygencount()
{
oxygen = oxygen - 5;
if (oxygen == 0)
gameover();
}
//Functions for rooms. The various text passages are separated into multiple cout statements so that it is easier to read for the programmer.
//Function Room 1
int room1()
{
int room1choice;
oxygencount();
cout << "\n********** Oxygen Remaining: " << oxygen << " **********\n";
cout << "Welcome to the Asylum.\n";
cout << "You see a door on your right and a door on your left.\n";
cout << "The door on your left has a cool breeze blowing from it. The door on your right smells of musty water.\n\n";
cout << "Which door would you like to go through?\n";
cout << "1 - Right\n2 - Left\n";
cin >> room1choice;
switch (room1choice)
{
case 1: room2();
break;
case 2: room3();
break;
default:
cout << "Invalid Choice.\n\n";
room1();
}
return 0;
}
//Function Room 2
int room2()
{
int room2choice;
oxygencount();
cout << "\n********** Oxygen Remaining: " << oxygen << " **********\n";
cout << "You hear a strange voice calling to you...in the corner, there is a box with a red box, yellow triangle, and blue square on it.\n";
cout << "Which one do you press?\n";
cout << "1 - Red Box\n2 - Yellow Triangle\n3 - Blue Square\n";
cin >> room2choice;
switch (room2choice)
{
case 1:
cout << "\nA hidden door opens in the wall, revealing a new path.\n";
room3();
break;
case 2:
cout << "\nA hole in the ceiling opens above you, and a large bucket of acid comes pouring down on your head, killing you instantly.\n";
gameover();
break;
case 3:
cout << "\nA trap door opens beneath you, and you are surprised to find yourself back where you began!\n\n";
room1();
break;
default:
cout << "Invalid Choice.\n\n";
room2();
}
return 0;
}
//Function Room 3
int room3()
{
oxygencount();
cout << "\n********** Oxygen Remaining: " << oxygen << " **********\n";
int room3choice;
cout << "\nThere is a blood spattered hospital bed in the corner of a room. Next to it is a partially open closet door.\n";
cout << "Do you approach the bed, open the closet, or leave through the next door?\n";
cout << "1 - Approach the Bed\n2 - Open the Closet\n3 - Leave\n";
cin >> room3choice;
switch (room3choice)
{
case 1:
cout << "\nA horrendous monster is crouched behind the bed. It slowly stands up and grabs you, tearing you apart, as your screams echo throughout the chambers.";
gameover();
break;
case 2:
cout << "You open the closet door and step through it...\n\n";
room5();
break;
case 3:
room4();
break;
default:
cout << "Invalid Choice.\n\n";
room3();
}
return 0;
}
//Function Room 4
int room4()
{
oxygencount();
cout << "\n********** Oxygen Remaining: " << oxygen << " **********\n";
int room4choice;
cout << "You find yourself in a large courtyard area, with an old, rusty basketball court. You see a ball on the ground.\n";
cout << "Do you go for the slam dunk, the three pointer, or the half court shot?\n";
cout << "1 - Slam Dunk\n2 - Three Pointer\n3 - Half Court\n";
cin >> room4choice;
switch (room4choice)
{
case 1:
cout << "\nYou soar towards the hoop, flying ever so gracefully, with not a care in the world!\n";
cout << "The ball slams through the rim, through the hoop, as you come crashing in, grabbing the rim.\n";
cout << "Out of nowhere, hundreds of NBA recruiters are all over you, desparately wanting you to play for them.\n";
cout << "They take you out of the Asylum, and fly you away.\n";
gamewin();
break;
case 2:
cout << "\nYou shoot from the three point line, but miss horribly. You feel bad about yourself, and leave.";
room5();
break;
case 3:
cout << "\nWhat are you, a middle schooler? That's not cool anymore.\n";
cout << "The ball flies into the rim, bounces back, and hits you in the head, killing you instantly.\n";
gameover();
break;
default:
cout << "Invalid Choice\n\n";
room4();
}
return 0;
}
//Function Room 5
int room5()
{
oxygencount();
cout << "\n********** Oxygen Remaining: " << oxygen << " **********\n";
int room5choice;
cout << "\nYou are now in an existential crisis. There's a computer in front of you with the Asylum game. Do you play it?\n";
cout << "1 - Play\n2 - Leave\n";
cin >> room5choice;
switch (room5choice)
{
int x;
case 1:
srand(time(NULL));
x = rand() % 100;
if (x < 50)
{
cout << "You begin playing.\n\n";
room1();
}
if (x >= 50)
{
cout << "You realize how futile it is to play a game that you're already in, and die instantly.\n\n";
gameover();
}
break;
case 2:
room3();
default:
cout << "Invalid Choice\n\n";
room5();
}
return 0;
}
//Function Room 6
int room6()
{
oxygencount();
cout << "\n********** Oxygen Remaining: " << oxygen << " **********\n";
int room6choice;
cout << "You're outside now, by a large gate. It has spikes on the top, and it is locked tight.\n";
cout << "You notice a rocket launcher nearby. Do you shoot the rocket launcher at the gate, attempt to jump it, or head to the courtyard?\n";
cout << "1 - Shoot the rocket launcher\n2 - Jump the gate\n3 - Head to the courtyard\n";
cin >> room6choice;
switch (room6choice)
{
case 1:
cout << "You pull the trigger, but the launcher malfunctions, exploding and killing you instantly.\n";
gameover();
break;
case 2:
cout << "You attempt to jump over the gate, but you aren't as athletic as you thought. You are impaled and die instantly.\n";
gameover();
break;
case 3:
room4();
break;
default:
cout << "Invalid Choice\n\n";
room6();
}
return 0;
}
//Function Game Over
int gameover()
{
int gameoverchoice;
cout << "\nGame Over\n\n";
cout << "\nWould you like to play again?\n";
cout << "\n1 - Play Again\n2 - Exit\n";
cin >> gameoverchoice;
switch (gameoverchoice)
{
case 1:
oxygen = 120;
room1();
break;
case 2:
return 0;
break;
default:
cout << "Invalid Choice\n\n";
gameover();
}
return 0;
}
//Function Game Win
int gamewin()
{
int gamewinchoice;
cout << "Congratulations! You escaped!\n";
cout << "\nWould you like to play again?";
cout << "\n1 - Play Again\n2 - Exit\n";
cin >> gamewinchoice;
switch (gamewinchoice)
{
case 1: room1();
break;
case 2:
return 0;
break;
default:
cout << "Invalid Choice\n\n";
gamewin();
}
}
[/code]