Hey I'm new to programming but I know enough now that I have been trying to program my own game. I works, but I think my code is too complicated. Does someone want to test it and give me some suggestions? Thanks!
kidprogrammer 0 Newbie Poster
The attachment preview is chopped off after the first 10 KB. Please download the entire file.
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <math.h>
#include <windows.h>
#include <fstream>
using namespace std;
//**************************************************************
//***********************Structures*****************************
//**************************************************************
struct hero
{
string name;
int str, acc, intell, health, lvl, exp, adrenaline, weaponeq, armor;
int armorresistance;
};
class creature
{
public:
string name;
int str, acc, intell, health;
};
class armor
{
public:
string desc;
int locale, armorpnts; //1 head, 2 shoulders, 3 chest, 4 hands, 5 legs, 6 feet
};
class weapon
{
public:
string desc;
int damage;
};
/*********************************************************
*********************FUNCTIONS****************************
*********************************************************/
void startscreen();
//battle functions
int battle(struct hero*, struct creature*);
void battle_delay();
void battle_animation();
//storyline
void intro();
void talking_to_hobos();
void background1();
//locations
void parislocale45();
//********************************************************
//*****************Global Variables***********************
//********************************************************
int playerexp = 0;
//********************************************************
//***********************MAIN*****************************
//********************************************************
int main()
{
char name[80];
char savedata[30];
char test[30];
int numsave = 0;
int index = 0;
int inputa, inputb, inputc, inputd; // stores user prompts
int dmgdealtcre = 0; // damage dealt by enemy
int dmgdealthero = 0; // damage dealt by player
int adrenalinespent = 0; // adrenaline (mana) spent
int randoma, randomb, randomc; // locations the random numbers are stored
int totaldmghero = 0; // total damage dealt TO hero
int totaldmgcre = 0; // total damage dealt TO creature
int creature_died = 5; // signifies enemy is alive. When changed to 0 battle loop ends. It changes to 0 when totaldmgcre >= creature.health
int healing = 0; // healing done to self
int progress = 0;
int head = 0, chest = 0, hands = 0, legs = 0, feet = 0; // armor values, changed below
ofstream save;
ifstream load;
int playerlvltemp;
int progresstemp;
int playerstrtemp;
int playerexptemp;
int playeracctemp;
int playerintelltemp;
int playerweapontemp;
int playerarmortemp;
//********************************************************
//**************STRUCTUREDEFINITIONS**********************
//********************************************************
//WEAPONS
weapon old_siphxon_pistol;
old_siphxon_pistol.desc = "An old Siphxon brand pistol. Fit for a five year old. +2 Damage";
old_siphxon_pistol.damage = 3;
weapon old_siphxon_rifle;
old_siphxon_rifle.desc = "An old Siphxon brand rifle. Fit for a five year old. +4 Damage";
old_siphxon_rifle.damage = 6;
// ARMOR
armor civ_cap;
civ_cap.desc = "An old baseball cap. +2 Armor";
civ_cap.locale = 1;
civ_cap.armorpnts = 2;
armor civ_vest;
civ_vest.desc = "An old vest. +4 Armor";
civ_vest.locale = 3;
civ_vest.armorpnts = 4;
armor civ_gloves;
civ_gloves.desc = "A pair of old leather gloves. + 2 Armor";
civ_gloves.locale = 4;
civ_gloves.armorpnts = 2;
armor civ_pants;
civ_pants.desc = "Some old jeans. +3 Armor";
civ_gloves.locale = 5;
civ_pants.armorpnts = 3;
armor civ_shoes;
civ_shoes.desc = "A pair of old rubber-soled boots. +2 Armor";
civ_shoes.locale = 6;
civ_shoes.armorpnts = 2;
//armor equiped
head = civ_cap.armorpnts;
chest = civ_vest.armorpnts;
hands = civ_gloves.armorpnts;
legs = civ_pants.armorpnts;
feet = civ_shoes.armorpnts;
hero player;
player.name = "Xane";
player.lvl = 1;
player.exp = 0;
player.str = 5;
player.acc = 5;
player.intell = 5;
player.health = player.str * 10;
player.adrenaline = player.intell * 10;
player.weaponeq = old_siphxon_pistol.damage; // equiped weapon
player.armor = head + chest + hands + legs + feet;
player.armorresistance = player.armor / 10;
//creatures
creature lvl1bountyhunter;
lvl1bountyhunter.name = "Bounty Hunter";
lvl1bountyhunter.str = 6;
lvl1bountyhunter.acc = 5;
lvl1bountyhunter.intell = 0;
lvl1bountyhunter.health = lvl1bountyhunter.str * 10;
creature lvl2hobo;
lvl2hobo.name = "Angry Hobo";
lvl2hobo.str = 8;
lvl2hobo.acc = 5;
lvl2hobo.intell = 5;
lvl2hobo.health = lvl2hobo.str * 10;
//********************************************************
//***********************GAMESTART************************
//********************************************************
startscreen();
cout << "Would you like to load a previously saved game? (1 for yes or 2 for no)" << endl;
cin >> inputa;
switch(inputa)
{
case 1:
{
cout << "Loading" << endl;
load.open("save.txt", ios::in);
if(!load)
{
cout << "Error loading file." << endl;
}
while (!load.eof())
{
// STORING SAVE DATA IN TEMPORARY SPOTS
load >> progresstemp;
load >> playerlvltemp;
load >> playerexptemp;
load >> playerstrtemp;
load >> playeracctemp;
load >> playerintelltemp;
load >> playerweapontemp;
load >> playerarmortemp;
load.close();
numsave++;
}
// ASSIGNING SAVE DATA TO CORRESPONDING VARIABLESs
progress = progresstemp;
player.lvl = playerlvltemp;
player.exp = playerexptemp;
player.str = playerstrtemp;
player.acc = playeracctemp;
player.intell = playerintelltemp;
player.weaponeq = playerweapontemp;
player.armor = playerarmortemp;
break;
}
case 2:
{
cout << "OK" << endl << endl;
Sleep(2000);
system("CLS");
break;
}
}
if (progress == 0) {
intro();
Sleep(3000);
system("PAUSE");
system("CLS");
battle_animation();
battle(&player, &lvl1bountyhunter);
playerexp = 50;
system("CLS");
if(playerexp >= 50)
{
player.lvl++;
player.str++;
player.acc = player.acc + 0;
player.intell++;
player.adrenaline = player.intell * 10;
player.health = player.str * 10;
cout << "Congratulations, you gained a level! You have gained\n";
cout << "1 Strength\n";
cout << "0 Accuracy\n";
cout << "1 Intelligence\n" << endl;
cout << "Your new stats are: " << endl;
cout << "Level: " << player.lvl << endl;
cout << "Strength: " << player.str << endl;
cout << "Accuracy: " << player.acc << endl;
cout << "Intelligence: " << player.intell << endl;
cout << "Health: " << player.health << endl;
cout << "Adrenaline: " << player.adrenaline << endl << endl;
system("PAUSE");
}
talking_to_hobos();
player.weaponeq = old_siphxon_rifle.damage;
system("CLS");
battle_animation();
battle(&player, &lvl2hobo);
playerexp = playerexp + 60;
progress = 1;
cout << "Would you like to save your game? (1 for yes and 2 for no)" << endl;
cin >> inputa;
switch(inputa)
{
case 1:
{
save.open("save.txt", ios::out);
if(!save)
{
cout << "Error saving." << endl;
break;
}
save << progress << endl;
save << player.lvl << endl;
save << playerexp << endl;
save << player.str << endl;
save << player.acc << endl;
save << player.intell << endl;
save << player.weaponeq << endl;
save << player.armor << endl;
system("CLS");
}
}
}
if (progress == 1)
{
background1();
}
system("PAUSE");
return 0;
}
//*****************************************************************************************************
//*************************************FUNCTIONS*******************************************************
//*****************************************************************************************************
int battle(struct hero *play, struct creature *foe)
{
int inputc, inputd, inputa, inputb;
int dmgdealtcre = 0;
int dmgdealthero = 0;
int adrenalinespent = 0;
int randoma, randomb, randomc;
int totaldmghero = 0;
int totaldmgcre = 0;
int creature_died = 5;
int healing = 0;
int hits = 0; // rapid shot total hits
int rsdamage = 0; // rapid shot damage
int menu_open = 1;
//WEAPONS
hero player;
player.name = play->name; player.lvl = play->lvl;
player.exp = play->exp;
player.str = play->str;
player.acc = play->acc;
player.intell = play->intell;
player.health = play->health;
player.adrenaline = play->adrenaline;
player.weaponeq = play->weaponeq;
player.armor = play->armor;
player.armorresistance = play->armorresistance;
//creatures
creature enemy;
enemy.name = foe->name;
enemy.str = foe->str;
enemy.acc = foe->acc;
enemy.intell = foe->intell;
enemy.health = foe->health;
do
{
battle_delay();
system("CLS");
cout << "Xane" << ": " << player.health - totaldmghero + healing << " HP ~ " << player.adrenaline - adrenalinespent << " MP" <
Suetan 1 Junior Poster in Training
I'll check it out, and see what I can come up with to help you out. I'll try debugging it, as well. Please note that my knowledge in C++ is limited, but I'm starting college on March 3rd for video game programming. I'll let you know if I come up with anything.
kidprogrammer 0 Newbie Poster
Cool thanks!
Suetan 1 Junior Poster in Training
Here's something that I already found after reading just a few lines.
line #254
player.health = player.str * 10;
You already have that declared on line 158, so you're not going to have that declared again.
lines #247 - #267
if(playerexp >= 50)
{
player.lvl++;
player.str++;
player.acc = player.acc + 0;
player.intell++;
player.adrenaline = player.intell * 10;
player.health = player.str * 10;
cout << "Congratulations, you gained a level! You have gained\n";
cout << "1 Strength\n";
cout << "0 Accuracy\n";
cout << "1 Intelligence\n" << endl;
cout << "Your new stats are: " << endl;
cout << "Level: " << player.lvl << endl;
cout << "Strength: " << player.str << endl;
cout << "Accuracy: " << player.acc << endl;
cout << "Intelligence: " << player.intell << endl;
cout << "Health: " << player.health << endl;
cout << "Adrenaline: " << player.adrenaline << endl << endl;
system("PAUSE");
}
I'd just have that be in a module that you'd call at the beginning of the file. It could be in something like functions.cpp. Once you have the module created, you'd replace that code with the following
if neededexp = 0
{
levelup();
}
else
{
neededexp = neededexp - expgained;
}
The repeated battle animations can be cut from this file, and put into a function that would be in functions.cpp
lines #319 - #795
Cut them out of this file entirely, and make them a part of functions.cpp and include them at the beginning of this file. It'll save you a lot of time that way.
With your comments, just have them be like this:
// whatever
and the at the end of the code that is commented
// end whatever
This will make it so that the computer has to read less code, which will optimize loading times.
That's just what I've found just by skimming over your file quickly. #include <functions.cpp>;
should make it so that your load times will be smaller, and it'll also make it so that you don't have to keep repeating the same code over and over.
I hope that I helped you out.
Good luck, man (^_^)
kidprogrammer 0 Newbie Poster
Ya, I modularized a bunch of the code today, including the leveling part you were explaining. I'll go through the battle function and look for repeated/unnecessary code. I didn't know you could include your own functions written in other files, so I'll expirement with that too.
Thanks for the tips!
Suetan 1 Junior Poster in Training
Hey, no problem. I'm just glad that I was able to help.
I don't know if you're old enough to go to college (or even in USA), but if you are, you might want to think about going to DeVry for their online game and simulation programming courses. I'm doing that starting in March.
Thanks for letting me help you out.
kidprogrammer 0 Newbie Poster
Ok, I tried putting the battle function in a new file and #including it into the main file, but it tells me that there is no such file or directory when I try to compile. The name of the function file is bfunction.cpp and in the main file I am including it like so: #include <bfunction.cpp>
What am I doing wrong?
Nick Evan 4,005 Industrious Poster Team Colleague Featured Poster
You should change the "< >" to quotes ( " " ): #include "bfunction.cpp"
This means that the file is in de project-folder
If you want to use multiple files I would recommend that you use .h files. For example:
You have a function void foo(int number) { }
You put de declaration in the header file (foo.h): int foo (int);
Next you put the implementation in the foo.cpp file:
int foo (int number)
{
return number*2;
}
Now you put #include "foo.h"
in your main.cpp and your done!
Suetan 1 Junior Poster in Training
I'm assuming that this is going to be on a web page.
I know that in PHP you can do things like this
<?php
$phpbb_root_path = './../';
include($phpbb_root_path . 'includes/functions.php');
?>
Or you can also do it like phpBB does it:
<?php
$phpbb_root_path = './../';
include($phpbb_root_path . 'includes/functions.' .$phpEx);
?>
You will have to declare the root directory before you're able to do any including in PHP.
Since PHP is based off of C++, you might be able to do it something like this:
#include "functions/bfunction.cpp";
Like I said, I have limited knowledge of C++ right now, so I'm not guarenteeing that anything will work exactly how you want it to, or at all for that matter.
MattEvans 473 Veteran Poster Team Colleague Featured Poster
@Suetan, since the original poster's code is C++, I sincerly doubt that the means for performing a PHP include will be much help... PHP isn't a subset/superset of C++ : PHP is parsed by and interpretted by a C++ runtime engine; if that's what you mean by 'based off of C++'.
@kidprogrammer, it looks good to me. Perhaps some of the stuff in main could be moved out into other functions; but only if it looks like you'll gain any benefit, and not have many costs ( i.e. having to pass more than about 8 parameters to a 10 line function, or a function that's only called from one place is too many costs ).
Also, a minor; you use the windows.h header only for the 'Sleep' function.. That means your program will only compile on Windows machines, even though most of the code is platform-neutral. If you're only targeting Windows, that's fine. There are other ways to do a sleep( n ) though [ i.e, you should be able to use a call to system( "sleep n" ) and thus cut out the dependancy on windows.h ].
Suetan 1 Junior Poster in Training
@MattEvans
What I was doing was using my knowledge with PHP to be able to hopefully implement it into C++. You're right about PHP's parsing by and interpretation on a C++ runtime engine.
I was merely trying to show how things are done in PHP and then work that into C++. I showed an example in a language that I know, and tried to work it into C++. That's all I was doing.
I'll keep it in mind not to refrence other languages in the future though.
chaosprime 8 Light Poster
Hope you'll excuse the thread necromancy, but how did this turn out? kidprogrammer, if you see this, are you interested in posting your updated code for further critique?
kidprogrammer 0 Newbie Poster
Hope you'll excuse the thread necromancy, but how did this turn out? kidprogrammer, if you see this, are you interested in posting your updated code for further critique?
I've made a lot of progress since the last code I uploaded. I completely started over and modularized the battle function, and I have added many new features. Right now I am not able to access the the actual code (on a different computer), so I will post the code tomarrow. Thanks for your interest in the game and wanting to help!
kidprogrammer 0 Newbie Poster
Ok, here are all the files needed to compile and run my game. What I want to do now is be able to pass an entire array or matrix to a function. It would be easy to program an inventory this way. I just can't figure out how to do it. More later, thanks!
Cap 010 1 0 1 1
E.i. 012 2 0 1 1
Shirt 010 3 0 1 1
Gloves 006 4 0 1 1
Belt 006 5 0 1 1
Jeans 012 6 0 1 1
Shoes 008 7 0 1 1
Guard 15 15 15 8 10 25
Turret 13 13 16 9 16 25
Prisoner 10 10 10 6 5 15
Warden 17 17 17 17 17 50
#ifndef exp_h
#define exp_h
int lvlUp(int exp, int lvl) {
if(exp >= 050 && lvl == 1) { system("CLS"); cout << "You have gained a level!\n\n";
system("PAUSE"); return 1; }
if(exp >= 150 && lvl == 2) { system("CLS"); cout << "You have gained a level!\n\n";
system("PAUSE"); return 1; }
if(exp >= 450 && lvl == 3) { system("CLS"); cout << "You have gained a level!\n\n";
system("PAUSE"); return 1; }
else { return 0; }
}
void assignPnts(struct hero *stats) {
int inp;
for(int i = 0; i < 3; i++) {
system("CLS");
cout << "STAT POINTS ASSIGNMENT\n"
<< "\nSTR - " << stats->str
<< "\nAGI - " << stats->agi
<< "\nINT - " << stats->intel
<< "\nVIT - " << stats->vit
<< "\nDEX - " << stats->dex
<< "\n\nPick a stat to be raised (1 - STR, 2 - ACC, 3 - INT, 4 - VIT, 5 - DEX): ";
cin >> inp;
switch(inp) {
case 1: stats->str++; break;
case 2: stats->agi++; break;
case 3: stats->intel++; break;
case 4: stats->vit++; break;
case 5: stats->dex++; break;
default: i--; break;
}
}
system("CLS");
cout << "STAT POINTS ASSIGNMENT\n"
<< "\nSTR - " << stats->str
<< "\nAGI - " << stats->agi
<< "\nINT - " << stats->intel
<< "\nVIT - " << stats->vit
<< "\nDEX - " << stats->dex;
Sleep(3000);
system("CLS");
}
#endif
Karth 15 15 15 15 15 1 0 500
//495 lines total as of july 28 'o8 ya!
//617 lines total as of july 30 '08 big progress being made
//669 lines july 31 when will i get to 1000?
#include <iostream>
#include <string.h>
#include <fstream>
#include <windows.h>
#include "revisedbattle.h"
#include "prisoncamp.h"
#include "prisonStory.h"
#include "exp.h"
using namespace std;
void assignPnts(struct hero *stats);
int prisonCamp(struct hero *player, struct foe guard, struct foe psycho, struct foe turret,
struct foe boss);
int main() {
ifstream creatureData, heroData, armorData;
foe creatureArray[50];
armor armorArray[7][50];
hero player;
//loading data
heroData.open("hero.txt", ios::in);
creatureData.open("creatures.txt", ios::in);
armorData.open("armor.txt", ios::in);
if(!heroData || !creatureData || !armorData) {
cout << "Problem loading data. Fix it!";
system("PAUSE");
return 1;
}
for(int i = 0; i < 50; i++) {
heroData >> player.name;
heroData >> player.str;
heroData >> player.agi;
heroData >> player.intel;
heroData >> player.vit;
heroData >> player.dex;
heroData >> player.lvl;
heroData >> player.exp;
heroData >> player.battleSpeed;
creatureData >> creatureArray[i].name;
creatureData >> creatureArray[i].str;
creatureData >> creatureArray[i].agi;
creatureData >> creatureArray[i].intel;
creatureData >> creatureArray[i].vit;
creatureData >> creatureArray[i].dex;
creatureData >> creatureArray[i].exp;
for(int j = 0; j < 7; j++) {
armorData >> armorArray[j][i].name;
armorData >> armorArray[j][i].points;
armorData >> armorArray[j][i].location;
armorData >> armorArray[j][i].isArmor;
armorData >> armorArray[j][i].isItem;
armorData >> armorArray[j][i].isWeapon;
}
}
creatureData.close();
heroData.close();
armorData.close();
//armor equip and name assignments
player.head = armorArray[0][0].points;
player.shoulders = armorArray[1][0].points;
player.chest = armorArray[2][0].points;
player.hands = armorArray[3][0].points;
player.waist = armorArray[4][0].points;
player.legs = armorArray[5][0].points;
player.feet = armorArray[6][0].points;
player.totalArmor = player.head + player.shoulders + player.chest +
player.hands + player.waist + player.legs + player.feet;
player.armorValue = player.totalArmor / 7;
player.headCE = armorArray[0][0].name;
player.shouldersCE = armorArray[1][0].name;
player.chestCE = armorArray[2][0].name;
player.handsCE = armorArray[3][0].name;
player.waistCE = armorArray[4][0].name;
player.legsCE = armorArray[5][0].name;
player.feetCE = armorArray[6][0].name;
//game start
mainScreen();
intro();
explanation();
controls();
prisonEntry();
if(prisonCamp(&player, creatureArray[0], creatureArray[2], creatureArray[1],
creatureArray[3]) == 1) { return 1; }
return 0;
}
int prisonCamp(struct hero *player, struct foe guard, struct foe psycho,
struct foe turret, struct foe boss) {
int locale = 1, battleWon1 = 1, battleWon2 = 1, battleWon3 = 1, gameOver = 0, lastLvl = player->lvl,
battleWon4 = 1, bW5 = 1, doorUnlocked = 1;
do{
locale = prisonMove(locale, player, doorUnlocked);
if(locale == 3 && battleWon1 != 0) {
guardBattle();
gameOver = battle(*player, guard, 1);
if(gameOver == 1) { return 1; }
battleWon1 = 0;
player->exp = player->exp + guard.exp;
player->lvl = player->lvl + lvlUp(player->exp, player->lvl);
if(player->lvl > lastLvl) { assignPnts(player); lastLvl = player->lvl; }
postGuard();
}
if(locale == 4 && battleWon2 != 0) {
prisonerBattle(); Sleep(1500);
gameOver = battle(*player, psycho, 1);
if(gameOver == 1) { return 1; }
battleWon2 = 0;
player->exp = player->exp + psycho.exp;
player->lvl = player->lvl + lvlUp(player->exp, player->lvl);
if(player->lvl > lastLvl) { assignPnts(player); lastLvl = player->lvl; }
}
if(locale == 5 && battleWon3 != 0) {
turretBattle();
gameOver = battle(*player, turret, 1);
if(gameOver == 1) { return 1; }
battleWon3 = 0;
player->exp = player->exp + turret.exp;
player->lvl = player->lvl + lvlUp(player->exp, player->lvl);
if(player->lvl > lastLvl) { assignPnts(player); lastLvl = player->lvl; }
}
if(locale == 6 && battleWon4 != 0) {
guardBattle2(); Sleep(3000);
gameOver = battle(*player, guard, 2);
if(gameOver == 1) { return 1; }
battleWon4 = 0;
player->exp = player->exp + guard.exp * 2;
player->lvl = player->lvl + lvlUp(player->exp, player->lvl);
if(player->lvl > lastLvl) { assignPnts(player); lastLvl = player->lvl; }
}
if(locale == 9 && bW5 != 0) {
bossFight();
gameOver = battle(*player, boss, 1);
if(gameOver == 1) { return 1; }
bW5 = 0;
doorUnlocked = 0;
player->exp = player->exp + boss.exp;
player->lvl = player->lvl + lvlUp(player->exp, player->lvl);
if(player->lvl > lastLvl) { assignPnts(player); lastLvl = player->lvl; }
postBoss();
}
} while(locale != 10);
return 0;
}
#ifndef _prisoncamp_h
#define _prisoncamp_h
int charInfo(struct hero *player);
void invalidEntry();
int keyPad();
int prisonMove(int location, struct hero *player, int doorUnlocked) {
char inp;
//your cell
if(location == 1) {
cout << "\nYou are in a cell. The dead gaurd is on the floor. There is a"
<< "\nlocker against the wall. The exit is to the north. ";
cin >> inp;
switch(inp) {
case 'c': charInfo(player); return location;
case 'n': return 2; break;
default: invalidEntry(); return location;
}
}
//top of stairs
if(location == 2) {
cout << "\nYou are in a hallway. There is a locked cell to the north."
<< "\nThere is a staircase leading down to the east. Your cell"
<< "\nis to the south. ";
cin >> inp;
switch(inp) {
case 'c': charInfo(player); return location;
case 'n': return 4; break;
case 's': return 1; break;
case 'e': return 3; break;
default: invalidEntry(); return location;
}
}
//bottom of stairs
if(location == 3) {
cout << "\nYou are on the first floor. There is a staircase to the west.\n"
<< "A mess hall is to the north. ";
cin >> inp;
switch(inp) {
case 'c': charInfo(player); return location;
case 'w': return 2; break;
case 'n': return 5; break;
default: invalidEntry(); return location;
}
}
//crazy prisoner
if(location == 4) {
cout << "\nThere is a dead prisoner on the floor. Other than that the\n"
<< "room is empty. The exit is to the south. ";
cin >> inp;
switch(inp) {
case 'c': charInfo(player); return location;
case 's': return 2; break;
default: invalidEntry(); return location;
}
}
//mess hall
if(location == 5) {
cout << "\nYou are in an empty mess hall. The turret you destroyed\n"
<< "is shooting sparks. A hallway is to the south. An armory\n"
<< "is to the east. ";
cin >> inp;
switch(inp) {
case 'c': charInfo(player); return location;
case 's': return 3; break;
case 'e': cout << "\nThis door is locked. There is a keypad next to it.\n\n";
system("PAUSE");
if(keyPad() == 0) { return 6; break; }
else { cout << "\nThis door is locked.\n"; return location; }
default: invalidEntry(); return location;
}
}
//armory
if(location == 6) {
cout << "\nYou are in an Armory. Three dead guards are on the floor.\n"
<< "An exit sign is to the north. A hallway leads to the south.\n"
<< "The mess hall is to the west. ";
cin >> inp;
switch(inp) {
case 'c': charInfo(player); return location;
case 'n': return 7; break;
case 's': return 8; break;
case 'w': return 5; break;
default: invalidEntry(); return location;
}
}
//exit
if(location == 7) {
cout << "\nYou are at the end of a hallway. A door that leads outside is\n"
<< "to the north. The armory is to the south. ";
cin >> inp;
switch(inp) {
case 'c': charInfo(player); return location;
case 's': return 6; break;
case 'n': if(doorUnlocked == 0) { return 10; break; }
else { cout << "\nThis door is locked.\n"; return 7; }
default: invalidEntry(); return location;
}
}
//hallway to warden
if(location == 8) {
cout << "\nYou are in a hallway. The door to the south is ajar and smoke\n"
<< "is pouring out of it. You can hear the obvious sound of fire.\n"
<< "The armory is to the north. ";
cin >> inp;
switch(inp) {
case 's': return 9; break;
case 'n': return 6; break;
default: invalidEntry(); return location;
}
}
//wardens room
if(location == 9) {
cout << "\nThe dead warden is on the floor. The smoke is causing you to\n"
<< "to cough uncontrolably. You need to get out this room fast. The\n"
<< "exit is to the north. ";
cin >> inp;
switch(inp) {
case 'n': return 8; break;
default: invalidEntry(); return location;
}
}
}
int charInfo(struct hero *player) {
char inp;
do {
system("CLS");
cout << "CHARACTER INFORMATION AND OPTIONS\n\n"
<< "s - Stats \na - Armor\no - Options\ne - Exit Menu\n\n";
cin >> inp;
switch(inp) {
case 's':
cout << "\n\nCHARACTER STATS"
<< "\n\n" << player->name
<< "\nLevel " << player->lvl << endl
<< player->str << " Strength\n"
<< player->agi << " Agility\n"
<< player->intel << " Intelligence\n"
<< player->vit << " Vitality\n"
<< player->dex << " Dexterity\n\n";
system("PAUSE");
break;
case 'a':
cout << "\n\nEQUIPPED ARMOR"
<< "\n\n"
<< player->headCE << " " << player->head << " -- Head\n"
<< player->shouldersCE << " " << player->shoulders<< " -- Shoulders\n"
<< player->chestCE << " " << player->chest << " -- Chest\n"
<< player->handsCE << " " << player->hands << " -- Hands\n"
<< player->waistCE << " " << player->waist << " -- Waiste\n"
<< player->legsCE << " " << player->legs << " -- Legs\n"
<< player->feetCE << " " << player->feet << " -- Feet\n"
<< player->totalArmor << " -- Total Armor Points\n"
<< player->armorValue << " -- Damage Reduced (%)\n\n";
system("PAUSE");
break;
case 'o':
char inpO;
cout << "\nOPTIONS\n\n"
<< "c - Change battle text speed.\n"
<< "e - Exit Menu ";
cin >> inpO;
switch(inpO) {
case 'c':
char inpOO;
cout << "\ns - Slow (Two second break to read text)\n"
<< "m - Medium (One second break)\n"
<< "f - Fast (1/2 second) ";
cin >> inpOO;
switch(inpOO) {
case 's':
player->battleSpeed = 2000;
cout << "\nText speed changed to SLOW.\n\n";
Sleep(2000);
break;
case 'm':
player->battleSpeed = 1000;
cout << "\nText speed changed to MEDIUM.\n\n";
Sleep(2000);
break;
case 'f':
player->battleSpeed = 500;
cout << "\nText speed changed to FAST.\n\n";
Sleep(2000);
break;
}
break;
case 'e': break;
default: invalidEntry(); break;
}
case 'e': break;
default: invalidEntry(); break;
}
}while(inp != 'e');
}
void invalidEntry() {
cout << "\nYou cant do that.\n";
}
int keyPad() {
int keypadInput;
system("CLS");
cout << "\nYour standard 10-key keypad. Enter a code: ";
cin >> keypadInput;
if(keypadInput == 3845) { cout << "\nYou hear a click\n"; return 0; }
else { cout << "\nNothing happens.\n"; return 1; }
}
#endif
#ifndef prisonStory_h
#define prisonStory_h
void controls() {
system("CLS");
cout << "CONTROLS"
<< "\nc - Character Info. n - North. s - South. e - East. w - West.\n";
}
void intro() {
int inp;
system("CLS");
do {
cout << "1 - 'Where am I?'\n2 - 'Who am I?'\n3 - 'Who are you?'\n4 - 'I'm tired now' ";
cin >> inp;
switch(inp) {
case 1: cout << "\nThis is a US Army M*A*S*H* located on the outskirts of New York City.\n\n";
break;
case 2: cout << "\nYour name is Karth. The reason you can't remember your identity\n"
<< "is because the Canadians put a bullet in your head the size of your \n"
<< "thumb. You have been givin a drug that will help the amnesia pass,\n"
<< "but it will take a long time before you remember everything.\n\n";
break;
case 3: cout << "\nMy name is Dr. B; I'm the head of psychiatric and cerebral studies for\n"
<< "US Army headquarters. I'm here to assist in your transportation back\n"
<< "to Washington. If you are not handled properly your spine will not\n"
<< "heal and paralysis will occur. Once we get to Washington you will\n"
<< "undergoe a series of tests to deem whether you are capable of \n"
<< "resuming...work.\n\n";
break;
case 4: cout << "\nOK, I'll leave you alone now.\n\n";
break;
}
}while(inp != 4);
}
void explanation() {
cout << "\nHey...hey! Wake up! Welcome to U.S. Army headquarters. The time has come\n"
<< "for you to start your training. I know you are in no physical condition to\n"
<< "even walk, but this training will be done using a virtual reality training\n"
<< "program. You will be completely immersed in a virtual prison armed with \n"
<< "nothing but your sharp wit. Once you escape the prison the program will \n"
<< "terminate and you will come back to reality. Ready? OK, see you soon.\n\n";
system("PAUSE");
}
void prisonEntry() {
cout << "\nYou are in the virtual prison chained to a wall. A guard walks in and says\n"
<< "it is time for your execution. He unlocks your shackles. You wrap your arm\n"
<< "around his neck and choke him to death. You take his keys and gun.\n";
}
void bossFight() {
cout << "\nYou walk into the smoke filled room and find a warden standing next to a \n"
<< "pile of burning clothes. He turns around, looks you over, and says, "
<< "\nYou shouldn't have tried to escape. The only way to get out of this prison is\n"
<< "if I unlock the door, and since I have the key, there is no way you are \n"
<< "escaping. Prepare to die!\n\n";
system("PAUSE");
}
void postBoss() {
cout << "\nYou walk over to the warden's body and search his person. You find the key\n"
<< "to the prison door as well as a note. It reads, 'Karth, when you get back to\n"
<< "reality, there is someone you must meet. Floor level 3, room 7, code to open\n"
<< "door is 49090491. 2300 hours. dont tell anyone. covert man, u caught = dead'\n\n";
system("PAUSE");
}
void guardBattle() {
cout << "\nYou run into a guard and shoot at him. You miss and he ducks under "
<< "\na table.\n\n";
system("PAUSE");
}
void postGuard() {
cout << "\nAs you walk past the guard you notice a note laying next to him. It reads,\n"
<< "'armory code: 3845'. Maybe you should remember that.\n\n";
}
void guardBattle2() {
cout << "\nYou walk into the armory and find 3 guards deeply engaged in conversation.\n"
<< "You take carefull aim and shoot one in the back of the head, killing\n"
<< "him instantly. The other two open fire, but you are already under cover.\n\n";
system("PAUSE");
}
void prisonerBattle() {
cout << "\nYou walk in the cell and find a psychotic prisoner babbling to himself.\n"
<< "He looks at you and screams. You can't risk being caught.\n\n";
system("PAUSE");
}
void turretBattle() {
cout << "\nAn automatic turret pops out of the ceiling. You duck for cover.\n\n";
system("PAUSE");
}
void mainScreen() {
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
<< " "
<< "The Adventures of Text Man\n\n"
<< " "
<< "A text-based RPG by John Walker";
Sleep(7000);
}
#endif
#ifndef revisedbattle_h
#define revisedbattle_h
using namespace std;
struct hero {
string name;
int str, agi, intel, vit, dex, lvl, exp, dodge, hit, hp, ap;
//armor pnts
int head/*1*/, shoulders/*2*/, chest/*3*/, hands/*4*/,
waist/*5*/, legs/*6*/, feet/*7*/, totalArmor, armorValue;
//armor names -- CE = curently equipped
string headCE/*1*/, shouldersCE/*2*/, chestCE/*3*/, handsCE/*4*/,
waistCE/*5*/, legsCE/*6*/, feetCE;/*7*/
int battleSpeed;
};
struct foe {
string name;
int str, agi, intel, vit, dex, exp, dodge, hit, hp;
};
struct armor {
string name;
int points, location, isArmor, isItem, isWeapon;
};
void initPlayer(struct hero *player);
void initOpponent(struct foe *enemy);
void drawProgress(struct hero *player, struct foe *enemy, int speed);
int pickMove();
int playerTurn(int choice, struct hero *player, struct foe *enemy);
int compTurn(struct hero *player, struct foe *enemy);
int checkLose(struct hero *player, struct foe *enemy);
int checkWin(struct hero *player, struct foe *enemy);
int battle(struct hero player, struct foe enemy, int opponentNumber) {
int turn = 1, reInitOpp = 1, reInitPlayer = 1, numDefeated = 0;
int battleSpeed = player.battleSpeed;
do {
if (turn == 1 || (numDefeated >= 1 && reInitOpp == 1)) {
initOpponent(&enemy);
if(turn != 1) { reInitOpp--; }
if (reInitPlayer == 1) {
initPlayer(&player);
reInitPlayer = 0;
}
}
drawProgress(&player, &enemy, battleSpeed);
enemy.hp = enemy.hp - playerTurn(pickMove(), &player, &enemy);
player.hp = player.hp - compTurn(&player, &enemy);
numDefeated = numDefeated + checkWin(&player, &enemy);
if(numDefeated == opponentNumber) {
return 0;
}
if(checkLose(&player, &enemy) == 0) { return 1; }
turn++;
} while(checkLose(&player, &enemy) != 0);
}
void initPlayer(struct hero *player) {
player->hp = player->vit * 10;
player->ap = player->intel * 2;
player->hit = (player->agi + player->dex) / 2;
player->dodge = (player->dex + player->agi) / 2;
}
void initOpponent(struct foe *enemy) {
enemy->hit = (enemy->agi + enemy->dex) / 2;
enemy->dodge = (enemy->dex + enemy->agi) / 2;
enemy->hp = enemy->vit * 10;
}
void drawProgress(struct hero *player, struct foe *enemy, int speed) {
Sleep(speed);
system("CLS");
cout << player->name << ": " << player->hp << " HP - " << player->ap << " AP\n"
<< enemy->name << ": " << enemy->hp << " HP\n\n";
}
int pickMove() {
char inp;
cout << "a - Attack\nu - INSTANT DEATH!!!\n\n";
do {
cin >> inp;
switch(inp) {
case 'a': { return 1; break; }
case 'u': { return 2; break; }
default: { cout << "\nInvalid Entry\n\n"; break; }
}
}while(inp != 'a' && inp != 'u');
}
int playerTurn(int choice, struct hero *player, struct foe *enemy) {
if(choice == 1) {
int d20 = rand() % 20 + 1;
if(d20 < player->hit && d20 > enemy->dodge / 2) {
int dmg = rand() % player->str + 1;
cout << "\nYou hit the enemy for " << dmg << " damage.\n";
return dmg;
}
else {
cout << "\nYour attack was unsuccesful.\n";
return 0;
}
}
if(choice == 2) {
}
}
int compTurn(struct hero *player, struct foe *enemy) {
int d20 = rand() % 20 + 1;
if(d20 < enemy->hit && d20 > player->dodge / 2) {
int dmg = rand() % enemy->str + 1;
int armorReduction = dmg - (dmg / player->armorValue);
cout << "\nYour enemy hit you for " << armorReduction << " damage.\n";
return armorReduction;
}
else {
cout << "\nYour enemy's attack was unsuccesful.\n";
return 0;
}
}
int checkLose(struct hero *player, struct foe *enemy) {
if(player->hp <= 0) { cout << "You died.\n\n"; system("PAUSE"); return 0; }
else { return 1; }
}
int checkWin(struct hero *player, struct foe *enemy) {
if(enemy->hp <= 0) { cout << "\nYour enemy has died.\n\n";
system("PAUSE");
return 1;
}
else {
return 0;
}
}
#endif
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.