Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //header
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <cstdlib>
- #include <ctime>
- #include <conio.h>
- using namespace std;
- //function init
- int getWhatTheyWant();
- void displayItems(int);
- void addItem(string,double);
- string getHighScore();
- void setHighScore(int);
- //main function
- int main(){
- int whatTheyWant;
- whatTheyWant = getWhatTheyWant();
- while(whatTheyWant!=9){
- switch(whatTheyWant) {
- case 1:
- displayItems(1);
- break;
- case 2:
- displayItems(2);
- break;
- case 3:
- displayItems(3);
- break;
- case 4:
- {
- string itemName;
- double itemPower;
- cout << "Enter item name" << endl;
- cin >> itemName;
- cout << "Enter item power" << endl;
- cin >> itemPower;
- addItem(itemName,itemPower);
- }
- break;
- case 5:
- //the actual game
- {
- srand(time(0));
- int health=100;
- string fightName;
- double fightPow;
- int score=0;
- ifstream objectFile("objects.txt");
- string objName;
- double objPow;
- int objNum=0;
- while(objectFile >> objName >> objPow) {
- objNum++;
- }
- if (objNum==0) {
- cout << endl;
- cout << "You really want to fight in an empty dungeon?" << endl;
- cout << endl;
- break;
- }
- cout << "Monsters in dungeon: " << objNum << endl;
- getch();
- while(health>0){
- cout<<"Health: " << health<<endl;
- cout<<"Score: " << score<<endl;
- getch();
- int monster=1+(rand()%objNum);
- int monster2=0;
- ifstream objectFile("objects.txt");
- while(objectFile >> objName >> objPow) {
- monster2++;
- if (monster2==monster){
- fightName=objName;
- fightPow=objPow;
- }
- }
- cout << "a(n) " << fightName << " is in the next dungeon room."<<endl;
- if(fightPow==0){
- cout << "the " << fightName << " does nothing."<<endl;
- }
- if(fightPow>0){
- cout << "the " << fightName << " heals you for "<<fightPow<<endl;
- health+=fightPow;
- }
- if(fightPow<0){
- cout << "the " << fightName << " hits you for "<<fightPow<<endl;
- health+=fightPow;
- }
- score+=1;
- getch();
- }
- cout << "Game over" << endl;
- getch();
- cout << "Final Score: " << score << endl;
- setHighScore(score);
- }
- //end of actual game
- break;
- case 6:
- {
- cout << endl;
- ofstream objectFile("objects.txt");
- objectFile << "";
- objectFile.close();
- cout << "Monsters cleared!" << endl;
- }
- break;
- case 7:
- cout <<endl;
- cout <<"Highscore: " << getHighScore() << endl;
- cout <<endl;
- break;
- case 8:
- {
- ofstream highWrite("highscore.txt");
- highWrite << 0;
- highWrite.close();
- cout << "\nHighscore cleared!\n" << endl;
- }
- break;
- default:
- cout << "Please enter a valid input!" << endl;
- break;
- }
- whatTheyWant = getWhatTheyWant();
- }
- cout << endl << "Thanks for playing!" <<endl;
- return 0;
- }
- //getwhattheywant function
- int getWhatTheyWant(){
- int choice;
- cout << endl;
- cout << "Enter what you want: " << endl;
- cout << "1- Plain Monsters" << endl;
- cout << "2- Helpful Monsters" << endl;
- cout << "3- Harmful Monsters" << endl;
- cout << "4- Add Monster" << endl;
- cout << "5- Play Game" << endl;
- cout << "6- Clear Monsters" << endl;
- cout << "7- Show Highscore" << endl;
- cout << "8- Clear Highscore" << endl;
- cout << "9- Quit Game" << endl;
- cout << endl;
- cin >> choice;
- return choice;
- }
- //displayitems function
- void displayItems(int x){
- cout << endl;
- ifstream objectFile("objects.txt");
- string name;
- double power;
- switch(x){
- case 1:
- while(objectFile >> name >> power) {
- if(power==0) {
- cout << name << " " << power << endl;
- }
- }
- break;
- case 2:
- while(objectFile >> name >> power) {
- if(power>0) {
- cout << name << " " << power << endl;
- }
- }
- break;
- case 3:
- while(objectFile >> name >> power) {
- if(power<0) {
- cout << name << " " << power << endl;
- }
- }
- }
- }
- //additem function
- void addItem(string n, double x) {
- ofstream objectAddFile("objects.txt",ios::app);
- objectAddFile << n << " " << x << endl;
- objectAddFile.close();
- }
- //gethighscore function
- string getHighScore() {
- ifstream highFile("highscore.txt");
- string highScore;
- getline(highFile,highScore);
- return highScore;
- highFile.close();
- }
- //sethighscore function
- void setHighScore(int x) {
- ifstream highFile("highscore.txt");
- string highScore;
- getline(highFile,highScore);
- highFile.close();
- int value = atoi(highScore.c_str());
- if (x>value){
- ofstream highWrite("highscore.txt");
- highWrite << x;
- highWrite.close();
- cout << "You made the new highscore!" << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement