#include <stdio.h>
int a;
int monhp=25;
int xp=0;
void monbattle()
{
while (monhp >0)
{
printf ("The monsters hp is %d!\n",monhp);
printf ("How much do you want to attack for?\n");
scanf ("%d",&a);
monhp-=a;
printf("The kobold's hp is now %d\n",monhp);
}
printf("You have slain the Kobold! You gain 10 xp!\n");
xp+=10;
printf ("You now have %d xp!\n",xp);
monhp+=25;
}
main ()
{
while(xp<50)
{
monbattle();
}
printf("You have levelled to 2!");
scanf("%d",&a);
}
Help consolidating my levelling system into a more efficient form
I'd like to make a levelling system past 2, and I know there is an efficient way to do it. Currently my code looks like this, and while it is completely functional until level 2, I can see that it is not the most efficient way to make a levelling system. Here is my code:
EDIT: I learned how to include a source window, so I'm doing that:
I'd like to put the levelling system into some kind of function, structure, or possibly array. I am currently refreshing my knowledge on pointers because I seem to remember somehow pointers can be used to do this.. it's just fuzzy. Any suggestions?
Quote:Original post by Mr_Derek
I'd like to make a levelling system past 2, and I know there is an efficient way to do it. Currently my code looks like this, and while it is completely functional until level 2, I can see that it is not the most efficient way to make a levelling system. Here is my code:
EDIT: I learned how to include a source window, so I'm doing that:
*** Source Snippet Removed ***
I'd like to put the levelling system into some kind of function, structure, or possibly array. I am currently refreshing my knowledge on pointers because I seem to remember somehow pointers can be used to do this.. it's just fuzzy. Any suggestions?
Instead of
while(xp < 50) { [...] }
You can do
int expLvl = 1; const int MAX_LEVEL = 7; int expNeeded[] = {50, 100, 200, 400, 800, 1600, [...]} while (expLvl < MAX_LEVEL) { while(xp < expNeeded[expLvl - 1]) { [...] } expLvl++; printf (...) }
It could be cleaner to make a function which calculates the experience needed for a particular level. For example, in FallOut:
int ExpNeededForNextLevel(int CurrentLevel){ int ExpNeeded = CurrentLevel * 1000; if (CurrentLevel > 1) ExpNeeded += ExpNeededForNextLevel(CurrentLevel - 1); return ExpNeeded;}int main(){ [...] int expLvl = 1; while (true) { while(xp < ExpNeededForNextLevel(expLvl)) { [...] } expLvl++; [...] } // Infinite loop, never goes out of here}
Hi, this is what I've done so far.
I used the case method for my levelling system, although I am sure there is a more simple way to do this. Any advice?
Specifically, could I form
and
into one function somehow? That would be amazing and save alot of finger soreness.
Ok, I found a way to do it, but it only works for levels past 1, because if it's 1(0 in the array value) it sets the array value to -1, which there is no value for.
If I can get it to work for level one, I've got it!
[Edited by - Mr_Derek on May 3, 2009 1:23:14 PM]
#include <stdio.h> int a; int monhp=25; int xp; int lvl; //monbattle void monbattle() { while (monhp >0) { printf ("The monsters hp is %d!\n",monhp); printf ("How much do you want to attack for?\n"); scanf ("%d",&a); monhp-=a; printf("The kobold's hp is now %d\n",monhp); } printf("You have slain the Kobold! You gain 10 xp!\n"); xp+=10; printf ("You now have %d xp!\n",xp); monhp+=25; }//monbattlestop//lvlscr void lvlscr() { printf("You have levelled to %d!\n",lvl+2);lvl+=1;monbattle(); }//lvlscrstop//enginevoid engine(){ switch(lvl) { case 0: while(xp<50){monbattle();}lvlscr(); case 1: while(xp<150&&xp>50){ monbattle();}lvlscr();}}//enginestop main (){engine();scanf("%d",&a); }
I used the case method for my levelling system, although I am sure there is a more simple way to do this. Any advice?
Specifically, could I form
while(xp<150&&xp>50){ monbattle();}lvlscr();
and
while(xp<50){monbattle();}lvlscr();
into one function somehow? That would be amazing and save alot of finger soreness.
Ok, I found a way to do it, but it only works for levels past 1, because if it's 1(0 in the array value) it sets the array value to -1, which there is no value for.
#include <stdio.h> int a; int monhp=25; int xp; int lvl; int xpneeded[] = {50,100,150,200,250,300}; //monbattle void monbattle() { while (monhp >0) { printf ("The monsters hp is %d!\n",monhp); printf ("How much do you want to attack for?\n"); scanf ("%d",&a); monhp-=a; printf("The kobold's hp is now %d\n",monhp); } printf("You have slain the Kobold! You gain 10 xp!\n"); xp+=10; printf ("You now have %d xp!\n",xp); monhp+=25; }//monbattlestop//lvlscr void lvlscr() { printf("You have levelled to %d!\n",lvl+2);lvl+=1;monbattle(); }//lvlscrstop//enginevoid engine(){while(xp<xpneeded[lvl]&&xp>xpneeded[lvl-1]){monbattle();}lvlscr();}//enginestop main (){engine();scanf("%d",&a); }
If I can get it to work for level one, I've got it!
[Edited by - Mr_Derek on May 3, 2009 1:23:14 PM]
[...] int xpneeded[] = {0, 50,100,150,200,250,300}; [...] while(xp<xpneeded[lvl+1] && xp>=xpneeded[lvl]) [...]
BTW, you should initialize your variables (xp, lvl,...)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement