ok so this is the little turn based game i was working on...and i just got it working...kinda lol but i was curious as to how you guys would make this thing work. im sure you have ways to do the same thing with less code and im interested in learning how to make mine alittle better/cleaner
here goes
Code:
//Written By : Zero_X
//"Turn Based Battle System"
//"Works but it is a mess..."
#include<iostream>
#include<string>
#define cls system("cls");
#define pause system("pause");
using namespace std;
int turn =1;
int main();
class player1
{
public:
string name;
int classSel;
string type;
int health;
void player1::clSel();
};
class player2 : public player1{};
void inStats(player1& play1, player2& play2)
{
play1.health = 100;
play2.health = 100;
}
void dead(player1& play1,player2& play2)
{
string loser,winner;
if(play1.health <= 0){
loser = play1.name;
winner = play2.name;}
else if(play2.health <=0){
loser = play2.name;
winner = play1.name;}
cls
cout<<"....As The Dust Settles....\n";
cout<<"\t"<<loser<<" Has Fallen...\n";
cout<<"\t\t"<<winner<<" You Are Victorious!!!\n\n";
cout<<"Thanks For Playing!"<<endl;
pause;
main();
}
void battle(player1& play1, player2& play2)
{
cls
string activeName;
string activeType;
int activeHealth;
string enemyName;
string enemyType;
int enemyHealth;
if(play1.health <= 0){
dead(play1, play2);}
if(play2.health <= 0){
dead(play1, play2);}
if(turn == 1){
activeName = play1.name;
activeType = play1.type;
activeHealth = play1.health;
enemyName = play2.name;
enemyType = play2.type;
enemyHealth = play2.health;}
if(turn == 2){
activeName = play2.name;
activeType = play2.type;
activeHealth = play2.health;
enemyName = play1.name;
enemyType = play1.type;
enemyHealth = play1.health;}
cout<<"\t\t"<<activeName<<"'s Health: "<<activeHealth;
cout<<"\n\t\t Enemy's Health: "<<enemyHealth<<endl<<endl;
if(activeType == "Knight")
{
cout<<"1)Stab\n";
cout<<"2)Slash\n";
cout<<"3)Power Slash\n";
cout<<"Please Select Attack: ";
int atkSel =0;
cin>>atkSel;
switch(atkSel){
case 1:{
enemyHealth -= 20;
break;}
case 2:{
enemyHealth -= 30;
break;}
case 3:{
enemyHealth -= 35;
break;}
}
if(turn == 1){
play2.health = enemyHealth;
turn = 2;}
else if(turn == 2){
play1.health = enemyHealth;
turn = 1;}
battle(play1, play2);
}
if(activeType == "Mage")
{
cout<<"1)Magic Arrow\n";
cout<<"2)FireBall\n";
cout<<"3)Falling Stars\n";
cout<<"Please Select Attack: ";
int atkSel =0;
cin>>atkSel;
switch(atkSel){
case 1:{
enemyHealth -= 25;
break;}
case 2:{
enemyHealth -= 30;
break;}
case 3:{
enemyHealth -= 40;
break;}
}
if(turn == 1){
play2.health = enemyHealth;
turn = 2;}
else if(turn == 2){
play1.health = enemyHealth;
turn = 1;}
battle(play1, play2);
}
pause
}
int main()
{
cls
player1 play1;
player2 play2;
inStats(play1, play2);
cout<<"\n\n\t*------------------------*\n";
cout<<"\t| TURN-BASED |\n";
cout<<"\t| BATTLE SYSTEM |\n";
cout<<"\t| |\n";
cout<<"\t| By: Zero_X |\n";
cout<<"\t*------------------------*\n\n\t\t";
pause
cls
cout<<"Please enter player 1's name: ";
cin>>play1.name;
play1.clSel();
cls
cout<<"Please enter player 2's name: ";
cin>>play2.name;
play2.clSel();
cls
cout<<play1.name<<" The "<<play1.type<<" VS "<<play2.name<<" The "<<play2.type<<"!\n\n";
pause
battle(play1,play2);
}
void player1::clSel()
{
cls
cout<<name<<" Please Select a Class...\n";
cout<<"1)Knight\n";
cout<<"2)Mage\n";
cin>>classSel;
switch(classSel)
{
case 1:{
cls
type = "Knight";
cout<<name<<" Has Selected to Be A "<<type<<endl;
pause
break;
}
case 2:{
cls
type = "Mage";
cout<<name<<" Has Selected to Be A "<<type<<endl;
pause
break;
}
default:{
cout<<"ERROR\n";
classSel = 0;
clSel();
}
}
}
thanks