Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Help with Battle system?

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with Battle system?

    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.
    Last edited by NTWolf1220; February 7th, 2013 at 11:01 AM. Reason: forgot something


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with Battle system?

    What happened when you stepped through this with a debugger, or at least added some more print statements to figure out what's going on?

    Hint: What are you checking in the if statements inside your while loop inside your bMain() function?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with Battle system?

    the loop lets the monster you're fighting attack you one more time after its HP drops below zero
    Can you exit the loop to prevent the extra attack? Look at the break statement.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Battle system?

    Thanks a ton! I read up on the break statement and implemented it with two more if statements, and it works perfectly!

    		while (monster.hp > 0)
    		{
    			wrAttack(player, monster);
    			if (monster.hp < 0)
    			{
    				break;
    			}
    			mAttack(player, monster);
    			if (player.hp < 0)
    			{
    				break;
    			}			
    		}

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with Battle system?

    For what it's worth, I believe the other problem was that you had your if statements reversed: you were checking player health before allowing the monster to attack, which did not check the monster's health.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. 3D Battle Cruiser for Game Development
    By Angryfly in forum Android Development
    Replies: 1
    Last Post: August 12th, 2012, 02:06 AM
  2. The Battle Ships Game
    By Valisek in forum Object Oriented Programming
    Replies: 1
    Last Post: April 19th, 2012, 11:20 AM
  3. banking system
    By preeti in forum Java Theory & Questions
    Replies: 3
    Last Post: August 11th, 2011, 01:25 PM
  4. Replies: 1
    Last Post: May 14th, 2011, 04:57 PM

Tags for this Thread