1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
struct entity
{
char name[20];
int hp, atk, def,len,lvl,hpmax,defmax;
entity(char a[],int q,int x, int y, int z,int e)
{
len=q;
for(int i=0;i<len;i++)
name[i]=a[i];
hp=x;
atk=y;
def=z;
lvl=e;
hpmax=hp;
defmax=def;
}
void output()
{
cout<<"\nName of entity : ";
cout.write(name,len);
cout<<"\nHealth : "<<hp<<"/"<<hpmax;
cout<<"\nAttack : "<<atk;
cout<<"\nDefence : "<<def<<"/"<<defmax;
}
void attack(int a,char b[],int k)
{
int g;
if(a<=def)
g=1;
else g=a-def;
clrscr();
cout<<endl;
cout.write(b,k);
cout<<" attcked ";
cout.write(name,len);
cout<<" !"<<endl;
cout.write(name,len);
cout<<" received "<<g;
hp=hp-g;
if(hp<0)
hp=0;
cout<<" damage and has health: "<<hp<<" now remaining";
cout<<"\nPress any key to continue...";
getch();
}
int defend()
{
def=def+1;
return 1;
}
void levelup()
{
cout<<"\n Congratulations ";
cout.write(name,len);
cout<<"!!, You have levelled up!!";
lvl++;
int h,v;
h=1;
cout<<"\n Select 3 stats to be increased!!";
cout<<"\n Press 1 for Health, 2 for Attack and 3 for Defence";
cout<<"\n Choose wisely as any mistake will not be reverted!!";
while(h<4)
{
cin>>v;
while(v<1||v>3)
{
cout<<"\nPlease enter valid choice!!";
cin>>v;
}
if(v==1)
{
hpmax+=5;
hp+=5;
}
if(v==2)
atk+=3;
if(v==3)
{
defmax+=2;
def+=2;
}
h++;
}
cout<<"\n Your stats are now.."<<endl;
cout<<"\n Health :"<<hp<<"/"<<hpmax<<" Attack :"<<atk<<" Defence :"<<def<<"/"<<defmax;
}
};
int main()
{
clrscr();
char player[20];
cout<<"Welcome to Player vs Skeleton V 1.0"<<endl;
cout<<"Enter Your Name, Player"<<endl;
gets(player);
entity obj0(player,strlen(player),20,4,1,1);
entity obj1("Skeleton",8,13,3,0,1);
int n,o,xp,xpdiff,win;
n=o=xp=0;
do
{
switch(n)
{
case 0:clrscr();
cout<<"Press 1 to view your stats"<<endl;
cout<<"Press 2 to view skeleton stats"<<endl;
cout<<"Press 3 to Attack"<<endl;
cout<<"Press 4 to Defend"<<endl;
cin>>n;
while(n<1||n>4)
{
cout<<"\nPlease enter a valid choice";
cin>>n;
}
break;
case 1:clrscr();
n=0;
cout<<"Player Stats:"<<endl;
obj0.output();
cout<<"\nPress any key to go back..."<<endl;
getch();
break;
case 2:clrscr();
n=0;
cout<<"Skeleton Stats:"<<endl;
obj1.output();
cout<<"\nPress any key to go back..."<<endl;
getch();
break;
case 3:obj1.attack(obj0.atk,obj0.name,obj0.len);
if(obj1.hp==0)
{
n=6;
win=1;
}
else
n=5;
break;
case 4:o=obj0.defend();
if(obj0.hp==0)
{
n=7;
}
else
n=5;
break;
case 5:obj0.attack(obj1.atk,obj1.name,obj1.len);
if(o==1)
{
obj0.def=obj0.defmax;
o=0;
}
if(obj0.hp==0)
{
n=7;
}
else n=0;
break;
case 6:cout<<endl;
cout.write(obj1.name,obj1.len);
cout<<" has been defeated!!"<<endl;
xpdiff=(((obj1.lvl/obj0.lvl)+5)*3);
xp=xp+xpdiff;
cout.write(obj0.name,obj0.len);
cout<<" has gained exp "<<xpdiff<<" and has "<<xp<<" exp now"<<endl;
while(xp>=((obj0.lvl)*10))
obj0.levelup();
win=1;
n=8;
break;
case 7:cout<<"\nYou were defeated...";
cout<<"\nYour journey ends here.";
cout<<"\nThanks for playing!";
n=8;
break;
default:cout<<"Program error!!"<<endl;
break;
}
}while(n<8);
if(win==1)
cout<<"\nCongratulations! you have won! Unfortunately this is as far as the game goes...";
getch();
return 0;
}
|