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");
}
}
}
System.out.println()
without it's semicolon. My IDE complains. \$\endgroup\$