11 Replies - 948 Views - Last Post: 24 September 2012 - 10:44 PM Rate Topic: ***** 1 Votes

#1 jetball89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-September 12

Random Number with Multiple Classes Calculation Error

Posted 24 September 2012 - 07:42 PM

I'm working on a problem where I need to create two classes that can be used in a simple game. Each class represents a character in the game and they are called Ogre and Giant.

They have an integer representing their strength and health. They will both be able to attack the each other by decreasing the other character's health by the intensity of attack. (intensity = a random number between 0 and the characters strength)

Then I have loops that randomly determine the attacker and attackee which prints the type of attack and the health of the character.

I have the classes made, but I am having a problem with my output. I think it may have to do with either a mistake in my calculation or misuse of the Random class.

Below is my Ogre Class:

import java.util.Random;

 public class Ogre
  {
    private int strength;
    private int health;

     public Ogre()
     {
        strength = 30;
        health = 50;

     }
     
     public int getHealth()
     {
         return health;
         // orge attacks  
     }
         public void setHealth(int s1)
         {
             health = health - s1;

     }
         public int getAttackValue()
         {
            Random intensity = new Random();
           Random critical = new Random(100);
            int AttackValue;
            
            int num = critical.nextInt();
            AttackValue = intensity.nextInt(strength);

     if(num > 80)

         return (AttackValue*2);

     else

         return AttackValue;
          
      }


     }






And Giant

import java.util.Random;
 public class Giant
  {
    private int strength;
    private int health;

     public Giant()
     {
        strength = 15;
        health = 100;

     }
     
     public int getHealth()
     {
         return health;
         // orge attacks  
     }
         public void setHealth(int s1)
         {
             health = health - s1;

     }
         public int getAttackValue()
         {
           Random intensity = new Random();
           Random critical = new Random(100);
           int AttackValue;
           int num = critical.nextInt();
           AttackValue = intensity.nextInt(strength);

     if(num > 80)

         return (AttackValue*2);

     else

         return AttackValue;
          
      }


     }






Finally, the class where they are fighting (where I think the error is)


import java.util.Random;
public class fight
{
    public static void main(String args[])
    {
    Ogre OgreMain = new Ogre();
    Giant GiantMain = new Giant();
    Random Attacker = new Random();

double Attack;

    while ((OgreMain.getHealth() > 1) && (GiantMain.getHealth() > 1))
    {

        Attack  = Attacker.nextDouble();

        //Determines Attacker true  Ogre first /false giant first
        if(Attack >=.5)
        {
        int x  = (GiantMain.getHealth()) - (OgreMain.getAttackValue()) ;
                GiantMain.setHealth(x);
        System.out.println("Ogre Attacks Giant and the Giant now has "+ x + " health");
        }
        else

{
        int x  = (OgreMain.getHealth()) - (GiantMain.getAttackValue()) ;
        OgreMain.setHealth(x);
        System.out.println("Giant Attacks Ogre and the ogre now has "+ x + " health");   

}
       
       
       

}   


    }
   
    
}
    


The output I receive is generally something along the lines of:

Ogre Attacks Giant and the Giant now has 80 health
Ogre Attacks Giant and the Giant now has 18 health
Giant Attacks Ogre and the ogre now has 48 health
Giant Attacks Ogre and the ogre now has -6 health
Giant Attacks Ogre and the ogre now has 2 health
Giant Attacks Ogre and the ogre now has 0 health
Ogre Attacks Giant and the Giant now has -8 health
Giant Attacks Ogre and the ogre now has -3 health
Ogre Attacks Giant and the Giant now has 5 health
Giant Attacks Ogre and the ogre now has 9 health

Is This A Good Question/Topic? 0
  • +

Replies To: Random Number with Multiple Classes Calculation Error

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Random Number with Multiple Classes Calculation Error

Posted 24 September 2012 - 09:17 PM

The problem is with your setHealth method. Shouldn't you just assign s1 to health rather than subtracting health and s1 before assigning it to s1 again? What is your motive when you did that?

Also, you can try creating an abstract class so your code would be more object oriented, and if you want to divert on a method you can just override it. Here's your code rewritten to accommodate abstract class inheritance:

Abstract class
import java.util.Random;

abstract class Monster {
	protected int strength;
	protected int health;

	public int getHealth() {
		return health;
	}

	public void setHealth(int s1) {
		health = s1;
	}

	public int getAttackValue() {
		Random intensity = new Random();
		Random critical = new Random(100);
		int AttackValue;
		int num = critical.nextInt();
		AttackValue = intensity.nextInt(strength);

		if (num > 80) {
			return (AttackValue * 2);
		} else {

			return AttackValue;
		}

	}
}



Ogre Class
 public class Ogre extends Monster{
	 public Ogre(){
        strength = 30;
        health = 50;
     }
}



Giant Class
public class Giant extends Monster
  {
     public Giant(){
        strength = 15;
        health = 100;
     }
     
  }



Output:
Giant Attacks Ogre and the ogre now has 50 health
Ogre Attacks Giant and the Giant now has 94 health
Giant Attacks Ogre and the ogre now has 48 health
Ogre Attacks Giant and the Giant now has 71 health
Giant Attacks Ogre and the ogre now has 35 health
Giant Attacks Ogre and the ogre now has 26 health
Giant Attacks Ogre and the ogre now has 16 health
Giant Attacks Ogre and the ogre now has 2 health
Giant Attacks Ogre and the ogre now has 0 health



You can do more enhancements on your code as you go along. ^^

Also recommended reading:
Abstract Methods and Classes
Interfaces and Inheritance

And from this site:
Abstract Classes Vs. Interfaces All About Abstract Classes, Abstract Methods And Interfaces
Was This Post Helpful? 1
  • +
  • -

#3 jetball89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-September 12

Re: Random Number with Multiple Classes Calculation Error

Posted 24 September 2012 - 09:37 PM

I didn't use an abstract method because the assignment has specific instructions and my teacher is strict :/ I can see how this way is easier and more effective though. However, my calculations are still off with the s1 change. I think the root problem is in my while output.
Was This Post Helpful? 0
  • +
  • -

#4 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Random Number with Multiple Classes Calculation Error

Posted 24 September 2012 - 09:40 PM

What is your expected output? :)
Was This Post Helpful? 1
  • +
  • -

#5 jetball89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-September 12

Re: Random Number with Multiple Classes Calculation Error

Posted 24 September 2012 - 09:48 PM

My expected output is for the fight to end at 0 hit points. They keep continuing past negative, but I can't see the error in my while method, could the crits be messing with it?
Was This Post Helpful? 0
  • +
  • -

#6 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Random Number with Multiple Classes Calculation Error

Posted 24 September 2012 - 09:52 PM

Whoa whoa whoa. I believe I covered your problem in my first paragraph. Look at the new output I posted. Is that what you wanted?
Was This Post Helpful? 1
  • +
  • -

#7 jetball89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-September 12

Re: Random Number with Multiple Classes Calculation Error

Posted 24 September 2012 - 09:55 PM

Yes it is, but how can I output using an abstract class, I believe my fight driver is still causing the issue with the calculation. My professor is forcing me to use a program called BlueJay, which is also probably adding to the confusion.
Was This Post Helpful? 0
  • +
  • -

#8 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Random Number with Multiple Classes Calculation Error

Posted 24 September 2012 - 10:02 PM

Nope, you don't need to follow my abstract class suggestion. You can just edit the setHealth method as I did for both Ogre and Giant and you will get the results you're looking for. Also, it is worth noting that by using Random you will never always get the correct subtrahend so I suggest before printing your output, do a check if the hp is < 0 and then set it to 0. Try it.
Was This Post Helpful? 1
  • +
  • -

#9 jetball89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-September 12

Re: Random Number with Multiple Classes Calculation Error

Posted 24 September 2012 - 10:25 PM

Still having trouble, how can I test if the hp is < 0 while also having the information needed for it to know what x is?
Was This Post Helpful? 0
  • +
  • -

#10 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Random Number with Multiple Classes Calculation Error

Posted 24 September 2012 - 10:34 PM

Well, you only need to add this before you set the health of the monster and before you print the message:
if(x<0){
x = 0;
}


That's it. The Ogre or Giant's dead, the program ends there, and you'll print that the monster got zero therefore it's dead. ^^
Was This Post Helpful? 0
  • +
  • -

#11 jetball89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-September 12

Re: Random Number with Multiple Classes Calculation Error

Posted 24 September 2012 - 10:38 PM

Ok, I figured out a way to make it work for the Giant, but now the Ogre is tricky


    double Attack;
 
    while ((OgreMain.getHealth() > 1)&& GiantMain.getHealth() > 1)
    {
 
        Attack  = Attacker.nextDouble();
 
        //Determines Attacker true  Ogre first /false giant first
        if(Attack >=.5)
        {
        int x  = (GiantMain.getHealth()) - (OgreMain.getAttackValue()) ;
                GiantMain.setHealth(x);
        System.out.println("Ogre Attacks Giant and the Giant now has "+ x + " health");
        }
        else
{
        int x  = (OgreMain.getHealth()) - (GiantMain.getAttackValue()) ;

        OgreMain.setHealth(x);
        
        System.out.println("Giant Attacks Ogre and the ogre now has "+ x + " health");  
        if (x<0)
        System.out.println("Giant Attacks Ogre and the ogre is now dead"); 
       
}




Got it. Thank you for the guidance, I'm reading my Syllabus now and we're scheduled to
"learn" inheritance and abstract classes next month, I'm definitely going to check out those resources so I have some insight and am prepared for that chapter. Time for bed, that last code I posted I almost used [ogre] [/ogre] to highlight my code.
Was This Post Helpful? 0
  • +
  • -

#12 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Random Number with Multiple Classes Calculation Error

Posted 24 September 2012 - 10:44 PM

Well, you could do that. Dude, the logic for Ogre and Giant is exactly the same:

Monster attacks Enemy Monster, assign result of attack to x
x is assigned to Enemy Monster's health
print the attack message and how much health Enemy Monster has left



Building up on this and your desired output, we can do it like this:
Monster attacks Enemy Monster, assign result of attack to x
If x is less than 0
    x is set to 0
    x is assigned to Enemy Monster's health
    print the attack message and pronounce Enemy Monster is dead
Else
    x is assigned to Enemy Monster's health
    print the attack message and how much health Enemy Monster has left



That's just the modification you need to do for both Ogre and Giant's code blocks.

Edit:
Oh you got it then? Good job and good luck. ^^
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1