0
\$\begingroup\$

I'm creating a text-based RPG game. In my game, a player has five different stats, which might decrease over time as the player does certain things.

public static int HP = 100;         //CHARACTER STARTING VITAL HIT POINTS
public static int food = 100;       //CHARACTER STARTING FOOD
public static int water = 100;      //CHARACTER STARTING WATER
public static int sanity = 100;     //CHARACTER MENTAL SANITY
public static int stamina = 100;    //CHARACTER STARTING STAMINA

I'm not sure how I can implement the logic to end the game when any of those stats reaches zero. My main game loop looks like this:

public static void main(String args[]){
    Map mainMap = new Map(10, 10);
    Character hero = new Character(mainMap);
    Main.start();

    while (true) {
        printmap(hero, mainMap);

        // Ask the player what direction to move, move the player.
        Character.moving();

        // ...
    }
}

The Character type has instances of all the players stats (as well as methods to increase or decrease them). How can I accomplish ending the game when a stat hits zero?

\$\endgroup\$
  • 2
    \$\begingroup\$ Hi, welcome to the site. Your question was far too lengthy and contained far too much extra, unnecessary information. Please take some time to read over the help center, specifically the sections on asking on-topic and focused questions. I've edited your question to significantly clarify it based on what seemed to be the crux of your concern. \$\endgroup\$ – user1430 Jan 13 '15 at 19:43
  • 1
    \$\begingroup\$ deleted 14891 characters in body -- I'm scared to know what those ~15k characters were. \$\endgroup\$ – Fund Monica's Lawsuit Jan 13 '15 at 19:48
  • \$\begingroup\$ Damn, thank you Josh. My bad, i'm still new here. I'll look at the help center. Anyways i explained how a lot of code worked in those characters :P \$\endgroup\$ – Filipe Madureira Jan 13 '15 at 19:58
2
\$\begingroup\$

You already have a game loop that (currently) runs "forever:"

while (true) {
  ... do stuff ...
}

What you want to do instead is guard this loop with a variable that says if the game is done yet:

bool done = false;
while (!done ) {
  ... do stuff ...
}

Then, every time through the loop after you've checked for player input, drawn the map, et cetera, you can check to see if you ending conditions (winning or losing) have been met. For example, if Character has a method to check individual player stats, you could do:

bool done = false;
while (!done ) {
  ... do stuff ...

  if (Character.HP <= 0 || Character.food <= 0 || ... ) {
    done = true;
  }
}

Alternatively, you could encapsulate the logic slightly more inside the Character class with a method like:

public bool IsDead () {
  return HP <= 0 || food <= 0 || ...
}

So your loop can check just check if the character is dead:

bool done = false;
while (!done ) {
  ... do stuff ...

  done = Character.IsDead();
}

You can easily extend this to support victory conditions as well.

\$\endgroup\$
  • \$\begingroup\$ Thank you. I used your method and added "If" conditions which change the boolean variable running the while loop! It works perfectly and i added several different endings depending on which stat reached the value 0! :D \$\endgroup\$ – Filipe Madureira Jan 13 '15 at 22:00
1
\$\begingroup\$

I would recommend having something like this in your loop:

if (<death check>) break;

<death check> would probably best be suited to a method (to steal from Josh Petrie, Character.IsDead()) which does the actual checking and returns a boolean true if they are dead.

What the break keyword does is immediately, unhesitatingly break out of the innermost loop it's in. It doesn't perform any other statements in the loop -- as soon as you hit that statement, you're out. If you have some last thing you need to do when the character is dead but, for any reason, it has to be inside the loop, you can do something like this:

while (true) {
    //Game logic
    if (<death check>) {
        //Whatever logic must be executed
        break;
    }
}

This is good if you want to check mid-turn or multiple times per turn, and break immediately.

Alternatively, if you want to put it at the beginning of every loop, you can do this:

while (!<death check>) {
    //Game logic
}

When it finishes the last line of code, it will wrap back around, check the statement, and if that's false, it won't go into the loop again.

This is good for if you want the entire turn to be completed before ending the turn. However, it may cause some bugs if the death check should return false, but due to something else happening between the actual "death" and the check at the beginning of the turn, it returns true. One way to solve this would be to keep running track, with a boolean:

boolean stillAlive = true;
while (stillAlive) {
    //Game logic
    if (<death check>) stillAlive = false;
}

This way, you can have whatever logic you want happen, but the player still dies at the end of the turn.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.