Player vs Skeleton v1.0 HELP!!

I've made a little program that simulates a battle between player and skeleton, however 2 problems I'm facing is level up when it's not suppose to level up and how to generalize the monsters (like creating an array of obj1, without too much modification to the program..) thanks for help!!
EDIT:Made some changes
EDIT2:More changes
EDIT3:void main() changed to int main()

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;
}
Last edited on
If you use c++ use int main(), <iostream> // without .h , <cstdio> and so on, if you use c don't use std::cout, std::cin, and no matter what you do it's not good to use clrscr as it is not portable.
EDIT:
One more bug, adding points to defence increments attack
Last edited on
I don't know what you mean since this was the syntax taught to me, anyway ill check things tomorrow as it is late night right now.
I mean you probably use old Borland software wich accepts it. In C++ use int main() 'cuz it returns value whether program execution was successful or not. clrscr() is an old function from Borland's conio.h header file, it won't exist in code::blocks or visual studio. If you decide to use <iostream> remember to use std::cout; // with namespace or just set using namespace std; on the top of file. One more thing, why do you write:
1
2
3
4
5
6
int defend(){
  int u;
  def=def+1;
  u=1;
  return u;
 }

where it could be just:
1
2
3
4
int defend(){
  ++def;
  return 1;
}
Yeah sorry about the defend function I noticed it while thinking about it today in school.The compiler I use is turbo c++ and I know it's very old and I'm using some obsolete techniques but I'm comfortable this way right now.Thanks for the help!

EDIT:About the bug it doesn't increase attack for me when I add points to defence??
Last edited on
Maybe that bug just came out when I was trying to change this code, so that it compiles on my computer.
Site about void main(). I post it 'cuz in c++ it's not a standard. You already know structures with methods so you know quite a lot. I'm suprised your teacher teaches you to use it. http://users.aber.ac.uk/auj/voidmain.cgi
Last edited on
I see...I don't really have any problem with using int main() from now on but even the textbook I've been using used int main() but I didn't pay much attention when I thought you could just use void main()
In your question you ask how to make an array of obj1. If you meant an array of objects of type entity, then C++ makes it for you, the only thing you need to write is:
entity arr[3]; // default constructor, won't work 'cuz you didn't specifie any
or
entity arr[3] = {{"Skeleton", 1, 2, 3, 4, 5}, { "Zombie", 1, 2, 3, 4, 5 }, {"Spider", 1, 2, 3, 4, 5}};
Of course your entity class has to be flexible to make diffrent monsters.

Could you tell me when does this error with levelup() function occur? I've not seen this yet.

EDIT:
With your new code skill points work properly for me
EDIT 2:
Try to use unsigned types when you don't need negative numbers and const keyword in functions when they aren't supposed to change arguments' value, sometimes it helps to find a bug in code.
Last edited on
line 169: Im trying to make it level up as long as it's exp is greater than or equal to 10 times the current level so at level 1 you should require 100exp but it gets levelled up with just 18 xp after beating the monster
EDIT:I also knew how to make an array of object but didn't knew how that worked with a constructor in place
Last edited on
I will try to find it, but for now I realized you forgot in your code that "Skeleton" is equal to {'S', 'k', 'e', 'l', 'e', 't', 'o', 'n', '\0'}. You should write
entity obj1("Skeleton",9,13,3,0,1); and
entity obj0(player,strlen(player) + 1,20,4,1,1); // strlen doesn't count terminating null cheracter
EDIT:
There is no bug, obj0.lvl = 1 so obj0.lvl * 10 = 10 <= 18
Last edited on
Wow so stupid of me ahaha that's why it was levelling up....alright everything is solved then
Topic archived. No new replies allowed.