0
\$\begingroup\$

I'm trying to make an RPG game and I would like to make the code a lot shorter.

This is only the first floor but there will be 5 floors. To advance to the next level, you have to defeat the boss of that floor. The only difference between the floors are the enemies and their strength. The game ends when you die or when you defeat the final boss.

My game is supposed to have a battle system with options to attack, defend, and use skills. I would like to add a leveling and skill system. The code will be too long unless I use methods (I don't know how).

import java.util.Scanner;
import java.util.Random;

public class Program
{
    public static void main(String[] args){
        Random rng = new Random();
        Scanner get = new Scanner(System.in);

        int day = 0;
        int action;

        int playerMaxHealth = 10; int playerHealth = 10; int playerAttack = 5; int playerDefense = 5;
        int bossFinalHealth = 99999; int bossFinalAttack = 1000; int bossFinalDefense = 1000;
        int boss1Health = 5000; int boss1Attack = 100; int boss1Defense = 100;
        int slimeMaxHealth = 100; int slimeHealth = 100; int slimeAttack = 20; int slimeDefense = 20;
        int goblinMaxHealth = 80; int goblinHealth = 80; int goblinAttack = 40; int goblinDefense = 10;

        int choice;
        int train = 0;
        String playerName;

        System.out.print("What is your name? ");
        playerName = get.next();
        System.out.println("...Welcome " + playerName + " ");

        while(playerHealth > 0 && boss1Health > 0){
            ++day;

            System.out.println("\nDay " + day);
            System.out.println("\nWhat will you do today?\n");
            System.out.println("(1) Fight");
            System.out.println("(2) Fight Boss");
            System.out.println("(3) Train");
            System.out.println("(4) Rest");
            System.out.println("(5) Stats");
            while(playerHealth > 0){
                choice = get.nextInt();
                if(choice == 1){
                    int encounter = rng.nextInt(2);
                    if(encounter == 0){
                       goblinHealth = goblinMaxHealth;
                        while(goblinHealth > 0 && playerHealth > 0){
                            System.out.println("What will you do?\n(1)Attack\n(2)Defend\n(3)Skills\n(4)Run");
                            action = get.nextInt();
                            if(action == 1){
                                int goblinHit = rng.nextInt(5);
                                int playerHit = rng.nextInt(5);
                                if(goblinHit > 1){
                                    int goblinDamage = Math.max(0, goblinAttack - playerDefense);
                                    playerHealth -= goblinDamage;
                                    System.out.println("The goblin dealt " + goblinDamage + " damage!\n" );
                                }
                                else{
                                   System.out.println("The goblin missed!\n");
                                }
                                if(playerHit > 1){
                                    int playerDamage = Math.max(0, playerAttack - goblinDefense);
                                    goblinHealth -= playerDamage;
                                    System.out.println("You dealt " + playerDamage + " damage!\n" );
                                }
                                else{
                                   System.out.println("You missed!\n");
                                }
                            }
                            if(action == 2){
                                int goblinHit = rng.nextInt(5);
                                if(goblinHit > 1){
                                    int goblinDamage = Math.max(0, goblinAttack - (playerDefense*2));
                                    playerHealth -= goblinDamage;
                                    System.out.println("The goblin dealt " + goblinDamage + " damage!\n" );
                                }
                                else{
                                   System.out.println("The goblin missed!\n");
                                }
                            }
                            if(action == 3){
                                System.out.println()
                                int goblinHit = rng.nextInt(5);
                                int playerHit = rng.nextInt(5);
                                if(goblinHit > 1){
                                    int goblinDamage = Math.max(0, goblinAttack - playerDefense);
                                    playerHealth -= goblinDamage;
                                    System.out.println("The goblin dealt " + goblinDamage + " damage!\n" );
                                }
                                else{
                                   System.out.println("The goblin missed!\n");
                                }
                                if(playerHit > 1){
                                    int playerDamage = Math.max(0, playerAttack - goblinDefense);
                                    goblinHealth -= playerDamage;
                                    System.out.println("You dealt " + playerDamage + " damage!\n" );
                                }
                                else{
                                   System.out.println("You missed!\n");
                                }
                            }
                        }
                        if(playerHealth > 0){
                           System.out.println("You have slain a goblin.\n");
                        }
                    }
                    if(encounter == 1){
                       slimeHealth = slimeMaxHealth;
                        while(slimeHealth > 0 && playerHealth > 0){
                            System.out.println("What will you do?\n(1)Attack\n(2)Defend\n(3)Skills\n(4)Run");
                            action = get.nextInt();

                            int slimeHit = rng.nextInt(5);
                            int playerHit = rng.nextInt(5);
                            if(slimeHit > 1){
                                int slimeDamage = Math.max(0, slimeAttack - playerDefense);
                                playerHealth -= slimeDamage;
                                System.out.println("The slime dealt " + slimeDamage + " damage!\n" );
                            }
                            else{
                               System.out.println("The slime missed!\n");
                            }
                            if(playerHit > 1){
                                int playerDamage = Math.max(0, playerAttack - slimeDefense);
                                slimeHealth -= playerDamage;
                                System.out.println("You dealt " + playerDamage + " damage!\n" );
                            }
                            else{
                               System.out.println("You missed!\n");
                            }
                        }
                        if(playerHealth > 0){
                           System.out.println("You have slain a slime.\n");
                        }
                    }
                }
                if(choice == 2){
                   while(boss1Health > 0 && playerHealth > 0){
                        int boss1Hit = rng.nextInt(5);
                        int playerHit = rng.nextInt(5);
                        if(boss1Hit > 1){
                            int boss1Damage = Math.max(0, boss1Attack - playerDefense);
                            playerHealth -= boss1Damage;
                            System.out.println("boss1 dealt " + boss1Damage + " damage!\n" );
                        }
                        else{
                           System.out.println("boss1 missed!\n");
                        }
                        if(playerHit > 1){
                            int playerDamage = Math.max(0, playerAttack - boss1Defense);
                            boss1Health -= playerDamage;
                            System.out.println("You dealt " + playerDamage + " damage!\n" );
                        }
                        else{
                           System.out.println("You missed!\n");
                        }
                    }
                    if(playerHealth > 0){
                       System.out.println("You have slain boss1.\n");
                    }
                    break;
                }
                if(choice == 3){
                   System.out.println("\nYou train.");
                   playerMaxHealth += 10; playerHealth  += 10; playerAttack += 5; playerDefense += 5;
                   train++;
                   break; 
                }
                if(choice == 4){
                   System.out.println("\nYou rest.");
                   playerHealth = playerMaxHealth;
                   break;
                }
                if(choice == 5){
                   System.out.println("\nHealth: " + playerHealth);
                   System.out.println("Attack: " + playerAttack);
                   System.out.println("Defense: " + playerDefense);
                   System.out.println("\nWhat will you do today?");
                }
            }
        }
        if(playerHealth > 0){
            System.out.println("You have completed the first floor. Please exit the program.");
        }
        if(playerHealth <= 0){
            System.out.println("You have died");
        }
    }
}
\$\endgroup\$
5
  • 1
    \$\begingroup\$ Do you know about classes? \$\endgroup\$
    – User319
    Feb 5, 2017 at 13:52
  • 3
    \$\begingroup\$ I'm assuming that the downvote on the question is because there is missing the intention and purpose of your code. Also you mention incomplete. Could you elaborate on the assignment and what pieces you are missing? \$\endgroup\$ Feb 5, 2017 at 22:49
  • \$\begingroup\$ @User319 I know about them but I do not know how to use them properly. We were taught very little about Java in school (since it's just an elective for HS) so I had to learn about objects, classes, and methods by myself. \$\endgroup\$ Feb 6, 2017 at 9:23
  • 1
    \$\begingroup\$ Instant-help: You're missing a semicolon on line 78 (with two import-statements added at the top). There is a System.out.println() without it's semicolon. My IDE complains. \$\endgroup\$
    – JFBM
    Feb 6, 2017 at 10:17
  • \$\begingroup\$ Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. \$\endgroup\$
    – Peilonrayz
    Feb 11, 2017 at 17:19

1 Answer 1

2
\$\begingroup\$

Methods

Probably the easiest thing to lookup and learn how to do, but also comes with a few caveats. I'll try to break it down and apply it to your code. The greatest reason for writing methods is for clarity when reading your code. As someone who reviews your code I want to know from a high level what your code is doing and make a decision along the way which method I want to dive into. So for instance in your code. I'm looking at a variable called choice In order for me to understand what each of the choice numbers mean I have to read the code. Well what if your code looked like this example code:

int choice = getPlayersChoice();
if (choice == 1)
    startFight();
else if (choice == 2)
    slayBoss1();
else if (choice == 3)
    trainPlayer();
else if (choice == 4)
    restPlayer();
else if (choice = 5)
    showPlayerStats();

Now imagine that something is wrong with your fighting logic. Where do you start looking? Well obviously the startFight() method which narrows down where the problem is by a lot of lines of code.

Things to keep in mind

  • For the most part methods can only return 1 object or when nothing is to be returned the method returns void.
  • methods should be short and specific.
  • methods should not contain the word and because this means it is probably doing to much.
  • avoid putting fields in your code just to pass a variable from method to method.
  • avoid having methods with a huge list of parameters.

A simple example

    //line 27
    while(twoPeopleAreAlive(playerHealth, boss1Health)) {

    //line 43
    while(twoPeopleAreAlive(goblinHealth, playerHealth)){

    //line 105
    while(twoPeopleAreAlive(slimeHealth, playerHealth)){

    //line 134
    while(twoPeopleAreAlive(boss1Health, playerHealth)){

private static boolean twoPeopleAreAlive(int player1Health, int player2Health) {
    return player1Health > 0 && player2Health > 0;
}

Normally I would write a lot more on the points and so forth but I think just doing methods will keep you busy for a long time.

\$\endgroup\$
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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