Returning to loops.

Started by
9 comments, last by DaveMS 14 years, 11 months ago
I took a class on C programming about 2 years ago. I am now attempting to make a simple RPG for my own pleasure, and to unrust myself in this language. My question is: how do I return to a loop in code? I'd like to return to the loop I have written for a simple monster battle. Here is my code so far: #include <stdio.h> main () { int a; int monhp=25; int xp=0; int lvl=1; while (monhp >0) { 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!"); xp+=10; } At the point beyond xp+=10; I'd like to have it return to the loop simply to repeat the monster battle. I have not contemplated how to advance the level variable. I don't need help with that; I'd like to learn that aspect myself. How do I return to the loop at any point in the code?
Advertisement
To visit to the battle loop from any point in the program you could stick the loop in a function and call that function.

To re-visit the battle loop repeatedly, from just after xp += 10, you could nest it all in another loop.
Nevermind, I just put the loop inside of a function as follows:
#include <stdio.h>
int a;
int monhp=25;
int xp=0;
void monbattle()
{
while (monhp >0)
{
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!");
xp+=10;
}
main ()
{
monbattle();


}



I hope now to introduce a levelling system and create a random attack! :-)
Oh, I hadn't seen your reply! Looks like you beat me to it! This is looking to be a very active community.
Quote:Original post by Mr_Derek
Oh, I hadn't seen your reply! Looks like you beat me to it! This is looking to be a very active community.
Indeed, it stays fairly heavily active 24 hours a day (unless the server breaks, then we have to find other things to do).

By the way, in C, functions need to have return types (main has an int return type) and for any function that takes zero arguments you need to specify void between the parenthesis:

int main(void) { }
void monbattle(void) { }

Also, don't be afraid to pass parameters to functions rather than use global variables. If you find that some variables have a natural togetherness (a monster's name, species, hp, etc) then group them up in a struct. Finally, don't forget to refactor mercilessly, that is never repeat a piece of code more than one - always refactor it, for example say you find yourself asking for an integer a lot:

printf("some text here\n");
scanf("%d", &a);

then refactor them to a function:

int ask_int(const char *str){    printf(str);    printf("\n");    int a;    scanf("%d", &a);    return a;}

It then becomes usable anywhere you want to ask the user for an integer:

int damage = ask_int("How much do you want to attack with?");

That's just a simple example, but it applies to everything [smile]
Quote:Original post by Mr_Derek
My question is: how do I return to a loop in code?


One common option is to just put another loop around it. :)

Oh, and you should get in the habit of scoping variables as tightly as possible.

In your case, this looks like:

#include <stdio.h>main() {  /* The xp and level go outside, because that information needs to be kept     between monsters. */  int xp = 0;  int lvl = 1;  while (lvl < 10) { /* just for example. */    /* Monster hp goes here, because each new monster has its own hp. */    int monhp = 25;    while (monhp > 0) {      /* Attack amount goes here, because it's reset for every attack. */      int a;      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!");    xp += 10;    /* Do something here to calculate level. */  }}

Quote:Original post by dmatter
By the way, in C, functions need to have return types (main has an int return type)


They have return types, but you don't have to specify them explicitly in all cases. Not in C, anyway.

Quote:and for any function that takes zero arguments you need to specify void between the parenthesis:


If you want to make sure the function is only called with zero arguments, then yes. But it's legal C to write () for the parameter list, it just doesn't do quite the same thing.

Quote:
Also, don't be afraid to pass parameters to functions rather than use global variables. If you find that some variables have a natural togetherness (a monster's name, species, hp, etc) then group them up in a struct. Finally, don't forget to refactor mercilessly, that is never repeat a piece of code more than one - always refactor it, for example say you find yourself asking for an integer a lot:

printf("some text here\n");
scanf("%d", &a);

then refactor them to a function:

int ask_int(const char *str){    printf(str);    printf("\n");    int a;    scanf("%d", &a);    return a;}

It then becomes usable anywhere you want to ask the user for an integer:

int damage = ask_int("How much do you want to attack with?");

That's just a simple example, but it applies to everything [smile]


Excellent advice.
I'm sorry for polluting with my off-topic commets but...

Quote:Original post by Zahlman
They have return types, but you don't have to specify them explicitly in all cases. Not in C, anyway.

IIRC, C99 requires to specify all return types explicitly, altough I don't know of any compiler which enforces it by default.

http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/func_type_specifier.htm
Quote:Original post by Zahlman
Quote:Original post by dmatter
By the way, in C, functions need to have return types (main has an int return type)


They have return types, but you don't have to specify them explicitly in all cases. Not in C, anyway.

Quote:and for any function that takes zero arguments you need to specify void between the parenthesis:


If you want to make sure the function is only called with zero arguments, then yes. But it's legal C to write () for the parameter list, it just doesn't do quite the same thing.
To be honest it's been so long since I've done any C that I forget what's legal and what's not [rolleyes]. I don't suppose you could explain those points? I think the original K&R C was more forgiving but I thought modern C was strict on those things.

Edit: Are empty parameter lists used for function prototypes with an unspecified number of arguments? Also deprecated?

[Edited by - dmatter on May 3, 2009 11:47:52 AM]
Thank you very much. While I don't yet completely understand your comment on refactoring, DMatter, I'm working on building a strong understanding of it. I'll try to figure it out myself. :-).

In response to your talk of structures, I am currently researching them to form my monsters. I plan to put all of the monsters information inside of seperate structures, but if there is a way to consolidate that into something even smaller, I'd love to try it.

I'm going to start a new post regarding my levelling system. But if you wish to respond to this one, here is my problem:

#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);

}

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?

This topic is closed to new replies.

Advertisement