Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Text-Based-game/main code/newHealth.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
92 lines (80 sloc)
3.04 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <stdlib.h> | |
#include <time.h> | |
#include <windows.h> | |
using namespace std; | |
int main() | |
{ | |
string health = "Health: "; // string health to measure health of protagonist. | |
int hp = 3; | |
cout << health << hp << " "; // integer hp to keep tally of users health. | |
string fight; // fight string to simulate the fight sequence | |
cin >> fight; | |
int randomMax = 10; //random function so to make the fighting simulation random. - used for reference:https://www.tutorialspoint.com/cplusplus-program-to-generate-random-number | |
srand(time(0)); | |
if (fight == "hit") // if input is hit it will trigger the random function and if below 5, will reduce HP of user by. and other responses for above 5. | |
{ | |
if (rand()%randomMax <= 5) | |
{ | |
cout << "oh no! you've missed and lost a life :(" << endl; | |
cout << "Health: " << hp - 1 << endl; | |
} | |
if (rand()%randomMax >= 6) | |
{ | |
cout << "nice, direct hit!" << endl; | |
cout << " Health: " << hp << " " << endl; | |
} | |
}; | |
cin >> fight; | |
if(fight == "again") // same simulation again but with the word 'again' so that the function knows how many lives the user may have this has more if statements. | |
{ | |
if (rand()%randomMax <= 5) | |
{ | |
cout << "oh no! you've missed and lost a life :(" << endl; | |
cout << "Health: " << hp - 2 << endl; | |
} | |
if (rand()%randomMax >= 6) | |
{ | |
if ( hp == 2 ) | |
{ | |
cout << "nice one champ" << endl; | |
cout << " Health: " << hp - 1 << " " << endl; | |
} | |
else | |
{ | |
cout << "nice one champ" << endl; | |
cout << " Health: " << hp << " " << endl; | |
} | |
} | |
}; | |
cin >> fight; | |
if (fight == "fire") | |
{ | |
if(rand()%randomMax <=5) | |
{ | |
cout << "Dang it you've died :(" << endl << "Health: " << hp - 3 << endl; | |
} | |
if (rand()%randomMax >=6) | |
{ | |
if (hp == 3) | |
{ | |
cout << "That was close!" << endl; | |
cout << "Health: " << hp << endl; | |
} | |
if (hp == 2) | |
{ | |
cout << "nice dodge" << endl << "Health: " << hp - 1 << endl; | |
} | |
else | |
{ | |
cout << "nice dodge!" << endl << "Health: " << hp - 2 << endl; | |
} | |
} | |
}; | |
if (hp == 0) // if health is 0 then the game ends. | |
{ | |
cout << "Game over" << endl << "You have Died :(" << endl << "To play again press r " << endl; | |
terminate; | |
} | |
} |