Sign in to follow this  

Returning to loops.

This topic is 3146 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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! :-)

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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. */
}
}


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
How about something like this:



void monbattle(Player player){

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

player.experience += 10;

printf ("You now have %d xp!\n",player.experience);
monhp+=25;
}

bool checkLevel(Player player){

int experiencePerLevel = 100;

if(player.experience / experiencePerLevel > player.level){

player.level++;
return true;
}

return false;

}

struct Player{

int experience;
int level;

};

int main(){

bool quit = false

While(!quit){

monBattle();

if(checkLevel(player))
printf ("You have increased a level! ");


// check input if player wants to quit..
// if so, quit = true, else, play on...

}
}


Share this post


Link to post
Share on other sites

This topic is 3146 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this