Hi, so I've been working on several simple text rpgs for a long time, and I think this one is my best yet. For some reason, my battle system only seems to work when I tried doing just a battle in the main method:
package game; import java.util.*; public class Main { public static void main(String[] args) { Battle battle = new Battle(); Player p1 = new Player(); Monster m1 = new Monster(); battle.bMain(p1, m1, 50); } }
Here's what I actually want to have written in main:
package game; import java.util.*; public class Main { public static void main(String[] args) { Player p1 = new Player(); p1.setName(); System.out.println("You're standing in the middle of a dirt road in the country."); Scanner in = new Scanner(System.in); in.nextLine(); System.out.println("You look around and decide to follow it south."); in.nextLine(); System.out.println("You're not entirely sure how you got here, but you've got a strange determination to keep going."); in.nextLine(); System.out.println("After walking for a while, you start to feel the hairs on the back of your neck rise up."); in.nextLine(); System.out.println("Suddenly, something jumps out at you!"); in.nextLine(); Battle b1 = new Battle(); Monster scarecrow = new Monster(); scarecrow.setName("Scarecrow"); b1.bMain(p1, scarecrow, 50); System.out.println("It appears the scarecrow has dropped something..."); Item lhat = new Item(); lhat.hMod(p1, 5); in.nextLine(); System.out.println("It's a leather hat! It adds 5 hp."); in.nextLine(); } }
Here's my battle class:
package game; import java.util.*; public class Battle { public void bMain(Player player, Monster monster, int x) // x = experience gain from defeating monster { Scanner in = new Scanner(System.in); System.out.println("You've encountered a "+monster.mName); player.calcAtk(); // calculate player battle statistics player.calcDef(); player.calcHP(); in.nextLine(); while(player.hp > 0 && monster.hp > 0) { pTurn(player, monster); if(monster.hp <= 0) { System.out.println(monster.mName+" has died!"); monster.setXP(x); player.getXP(monster.xpdeath); System.out.println("You've gained "+monster.xpdeath+" experience!"); break; } in.nextLine(); mTurn(player, monster); if(player.hp <= 0) { System.out.println("You have died!"); player.isAlive = false; break; } in.nextLine(); } } public void pTurn(Player player, Monster monster) { System.out.println("You attack!"); Random rand = new Random(); int roll = rand.nextInt(10); int hchnc = roll + player.atk; if(hchnc > monster.def) { int rolldmg = rand.nextInt(10); System.out.println("You strike "+monster.mName+" for "+rolldmg+" damage"); monster.hp = monster.hp - rolldmg; System.out.println(monster.mName+ " has " +monster.hp+ " hp."); } else if(hchnc <= monster.def) { System.out.println("You miss!"); } } public void mTurn(Player player, Monster monster) { System.out.println(monster.mName + " attacks!"); Random rand = new Random(); int roll = rand.nextInt(10); int hchnc = roll + monster.atk; if(hchnc > player.def) { int rolldmg = rand.nextInt(10) + monster.bossplus; System.out.println(monster.mName+" strikes for "+rolldmg+" damage"); player.hp = player.hp - rolldmg; System.out.println("You have "+player.hp+" hp."); } else if(hchnc <= player.def) { System.out.println(monster.mName+" misses!"); } } }
When I run the actual main method instead of the test one, it just doesn't call the battle method. It skips right to the part where the first Item is created. None of the print lines in the method happen. It's as if there were just two nextline methods from my scanner.