rendered paste body#include <iostream>
#include <fstream>
#include <math.h>
#include <string>
using namespace std;
class Game {
public:
int menu();
int newGame();
int load();
int save();
int changePlace();
int town();
int save1();
int newGame1();
int house();
int training();
int save2();
int save3();
int choice, place;
string name;
int hp;
int atk;
int str;
int def;
};
int Game::menu() {
system("cls");
choice = 0;
place = 0;
cout <<" WELCOME TO TEST GAME\n\n";
cout <<" 1 - NEW GAME\n";
cout <<" 2 - LOAD GAME\n";
cout <<"> ";
cin >>choice;
cin.ignore();
switch (choice) {
case 1:
newGame();
break;
case 2:
load();
break;
default:
menu();
}
}
int Game::newGame() {
system("cls");
choice = 0;
place = 1;
cout <<"Are you sure you want to start a new game?\n\n";
cout <<" 1 - YES\n";
cout <<" 2 - NO\n";
cout <<"> ";
cin >>choice;
cin.ignore();
switch (choice) {
case 1:
newGame1();
break;
case 2:
menu();
break;
default:
newGame();
}
}
int Game::newGame1() {
hp = 100;
atk = 5;
str = 5;
def = 1;
cout <<"You have started a new game. What is your name?\n";
cout <<"> ";
cin >>name;
cin.ignore();
ofstream data("save.txt");
data <<hp <<endl;
data <<atk <<endl;
data <<str <<endl;
data <<def <<endl;
data <<name <<endl;
data <<place;
data.close();
cout <<"Data saved successfully!\n";
cin.get();
changePlace();
}
int Game::save() {
system("cls");
choice = 0;
place = 1; // goes back to new game
cout <<"Are you sure you want to save?\n\n";
cout <<" 1 - YES\n";
cout <<" 2 - NO\n";
cout <<"> ";
cin >>choice;
cin.ignore();
switch (choice) {
case 1:
save1();
break;
case 2:
changePlace();
break;
default:
save();
}
}
int Game::save1() {
system("cls");
place = 1; // same
ofstream data("save.txt");
data <<hp <<endl;
data <<atk <<endl;
data <<str <<endl;
data <<def <<endl;
data <<name <<endl;
data <<place;
data.close();
cout <<"Data saved successfully!\n";
cin.get();
changePlace();
}
int Game::save2() {
system("cls");
place = 3; // goes back to house
choice = 0;
cout <<"Are you sure you want to save?\n\n";
cout <<" 1 - YES\n";
cout <<" 2 - NO\n";
cout <<"> ";
cin >>choice;
cin.ignore();
switch (choice) {
case 1:
save3();
break;
case 2:
changePlace();
break;
default:
save2();
}
}
int Game::save3() {
system("cls");
place = 3; // same
ofstream data("save.txt");
data <<hp <<endl;
data <<atk <<endl;
data <<str <<endl;
data <<def <<endl;
data <<name <<endl;
data <<place;
data.close();
cout <<"Data saved successfully!\n";
cin.get();
changePlace();
}
int Game::load() {
system("cls");
choice = 0;
place = 1;
cout <<"Are you sure to you want to load?\n\n";
cout <<" 1 - YES\n";
cout <<" 2 - NO\n";
cout <<"> ";
cin >>choice;
cin.ignore();
switch (choice) {
case 1:
break;
case 2:
menu();
break;
default:
load();
}
ifstream data("save.txt");
if (data.is_open()) {
data >>hp;
data >>atk;
data >>str;
data >>def;
data >>name;
data >>place;
} else {
cout <<"Error occured!\n";
cin.get();
menu();
}
cout <<"Data loaded successfully!";
cin.get();
changePlace();
}
int Game::changePlace() {
switch (place) {
case 1:
town();
break;
case 3:
house();
break;
default:
changePlace();
break;
}
}
int Game::town() {
system("cls");
place = 2;
choice = 0;
cout <<"Welcome to the Town.\n";
cout <<"What would you like to do?\n\n";
cout <<" 1 - ENTER HOUSE\n";
cout <<" 2 - ENTER TRAINING\n";
cout <<" 3 - MENU\n";
cout <<"> ";
cin >>choice;
cin.ignore();
switch (choice) {
case 1:
house();
break;
case 2:
training();
break;
case 3:
menu();
break;
default:
town();
}
}
int Game::house() {
system("cls");
choice = 0;
cout <<"HP " <<hp <<" - LVL " <<floor((atk + str + def)/3) <<"\n\n\n\n";
cout <<"Welcome home " <<name <<"!\n";
cout <<"What would you like to do?\n\n";
cout <<" 1 - REST\n";
cout <<" 2 - SAVE\n";
cout <<" 3 - LEAVE\n";
cout <<"> ";
cin >>choice;
cin.ignore();
switch (choice) {
case 1:
hp = 100;
cout <<"\nYour heath is fully restored.";
cin.get();
house();
break;
case 2:
save2();
break;
case 3:
town();
break;
}
}
int Game::training() {
system("cls");
choice = 0;
place = 1;
cout <<"HP " <<hp <<" - LVL " <<floor((atk + str + def)/3) <<"\n\n\n\n";
cout <<"Welcome to training " <<name <<"!\n";
cout <<"What would you like to do?\n\n";
cout <<" 1 - LIFT WEIGHTS\n";
cout <<" 2 - PRACTICE WEAPONARY\n";
cout <<" 3 - ENDURE BEATING\n";
cout <<" 4 - LEAVE\n";
cout <<"> ";
cin >>choice;
cin.ignore();
switch (choice) {
case 1:
cout <<"You lifted weights!\n";
cout <<"Your strength went from "<<str <<" to " <<str + rand() % 5 + 1<<"!";
str += rand() % 5 + 1;
training();
break;
case 2:
cout <<"You practiced weaponary!\n";
cout <<"Your attack went from "<<atk << " to " <<atk + rand() % 5 + 1 <<"!";
atk += rand() % 5 + 1;
training();
break;
case 3:
cout <<"You endure a beating!\n";
cout <<"Your defense went from "<<def << " to " <<def + rand() % 3 + 1 <<"!";
def += rand() % 3 + 1;
training();
break;
case 4:
changePlace();
break;
default:
training();
}
}
int main() {
Game game;
game.menu();
}