I've been working on a text-based RPG game with a simple turn based battle system. After a lot of work, I've managed to get the battle system working fine. However, the loop lets the monster you're fighting attack you one more time after its HP drops below zero.
Is there some way I can fix this?
Here's my battle class:
package rpgtxt; import java.util.*; public class Battle { public void bMain(Player player, Monster monster) { System.out.println("A " +monster.mName+ " has attacked!"); player.hp = player.getHP(); monster.hp = monster.getHP(player); while (monster.hp > 0) { if (monster.hp > 0) { wrAttack(player, monster); } if (player.hp > 0) { mAttack(player, monster); } } } public void wrAttack(Player player, Monster monster) { Scanner keyIn = new Scanner(System.in); System.out.println("You attack!"); Random roll = new Random(); int rvalue = roll.nextInt( 21 ); int mBlock = monster.getBlock(player); System.out.println(monster.mName+" has "+monster.hp+" hp."); keyIn.nextLine(); if (rvalue == 20) { System.out.println("Critical hit!"); monster.hp = monster.hp - (player.dmg * 2); System.out.println(monster.mName+" has "+monster.hp+" hp."); player.xp = player.xp + 1000; keyIn.nextLine(); } else if (rvalue > mBlock) { System.out.println("You strike!"); monster.hp = monster.hp - player.dmg; System.out.println(monster.mName+" has "+monster.hp+" hp."); player.xp = player.xp + 500; keyIn.nextLine(); } else if (rvalue < mBlock) { System.out.println("You miss!"); keyIn.nextLine(); } } public void mAttack(Player player, Monster monster) { Scanner keyIn = new Scanner(System.in); System.out.println(monster.mName+" attacks!"); Random roll = new Random(); int rvalue = roll.nextInt( 21 ); int block = player.getBlock(); System.out.println("You have "+player.hp+" hp."); monster.dmg = monster.getDMG(player); keyIn.nextLine(); if (rvalue == 20) { System.out.println("Critical hit!"); player.hp = player.hp - (monster.dmg * 2); System.out.println("You have "+player.hp+" hp."); keyIn.nextLine(); } else if (rvalue > block) { System.out.println(monster.mName+" strikes!"); player.hp = player.hp - monster.dmg; System.out.println("You have "+player.hp+" hp."); keyIn.nextLine(); } else if (rvalue < block) { System.out.println(monster.mName+" misses!"); keyIn.nextLine(); } } }
Edit:
Here's the output for my battle, to show you what's wrong.
A Zombie has attacked!
You attack!
Zombie has 20 hp.
You miss!
Zombie attacks!
You have 25 hp.
Zombie strikes!
You have 23 hp.
You attack!
Zombie has 20 hp.
You miss!
Zombie attacks!
You have 23 hp.
Zombie misses!
You attack!
Zombie has 20 hp.
You strike!
Zombie has 3 hp.
Zombie attacks!
You have 23 hp.
Zombie misses!
You attack!
Zombie has 3 hp.
You strike!
Zombie has -14 hp.
Zombie attacks!
You have 23 hp.
Zombie strikes!
You have 21 hp.