Brian
Brian

Reputation: 43

Simple Java Pokemon Fight Simulator

I have written a class to create and battle pokemon but I cannot figure out how to call the battle method in the tester class in order to test the class I wrote.

My assaignment is to write and test a simulation that models a battle between two Pokemon. Each Pokemon has a health value, a strength value, and a speed value. The health, strength, and speed values are passed in to the constructor as arguments. These values must be between 1 and 300 initially and should be nonzero initially. The overall idea for the finished game is that two Pokemon will “battle” one another in the simulation, with the pokemon taking turns attacking. (The one with the highest speed value goes first each round) The attacking Pokemon’s strength will be subtracted from the “attackee’s” health.

public class Pokemon{
  private int health;
  private int strength;
  private int speed;

/**
* Constructs the pokemon
* @Require:
*    health is an integer greater than or equal to 1 but less than or equal to 300
*    strength is and integer greater than or equal to 1 but less than or equal to 300
*    speed is an integer greater than or equal to 1 but less than or equal to 300
*/
public Pokemon(int health, int strength, int speed){
  assert health >= 1;
  assert health <= 300;
  assert strength >= 1;
  assert strength <= 300;
  assert speed >= 1;
  assert speed <= 300;

  this.health = health;
  this.strength = strength;
  this.speed = speed;
}

public void battle(Pokemon pokemon1, Pokemon pokemon2){
  do{
    System.out.println(pokemon1+" begins the fight against "+pokemon2);
    pokemon2.health = pokemon2.health - pokemon1.strength;

    System.out.println(pokemon1 +" does "+ pokemon1.strength +" damage to "+
    pokemon2 +" and "+ pokemon2 +" has "+ pokemon2.health +" left.");

    pokemon1.health = pokemon1.health - pokemon2.strength;

    System.out.println(pokemon2 +" does "+ pokemon2.strength +" damage to "+ 
    pokemon1 +" and "+ pokemon1 +" has "+ pokemon1.health +" left.");

  }while(pokemon1.health >= 1 || pokemon2.health >= 1);
  if(pokemon1.health < 1)
    System.out.println(pokemon1 +" has lost the fight");
  else
    System.out.println(pokemon2 +" has lost the fight");
  }
}

Pokemon Tester

public class PokemonTester{
  private Pokemon charizard;
  private Pokemon blastoise;
  private Pokemon venusaur;

public PokemonTester(){
   charizard = new Pokemon(100,50,50);
   blastoise = new Pokemon(150,25,150);
   venusaur = new Pokemon(300,10,100);
 }

public static void main(String[] args){
  Pokemon.battle(charizard, blastoise); //will not compile
 }
}

I do realize I have not implemented the speed aspect in taking turns yet as I'm trying to just make it work.

Upvotes: 3

Views: 32801

Answers (3)

Riley Jones
Riley Jones

Reputation: 43

Aside from making the battle function static, I would recommend the following changes. When I run your code, I get:

Pokemon@6228a17f begins the fight against Pokemon@c9be777
Pokemon@6228a17f does 50 damage to Pokemon@c9be777 and Pokemon@c9be777 has 100 left.
Pokemon@c9be777 does 25 damage to Pokemon@6228a17f and Pokemon@6228a17f has 75 left.
Pokemon@6228a17f begins the fight against Pokemon@c9be777
Pokemon@6228a17f does 50 damage to Pokemon@c9be777 and Pokemon@c9be777 has 50 left.
Pokemon@c9be777 does 25 damage to Pokemon@6228a17f and Pokemon@6228a17f has 50 left.
Pokemon@6228a17f begins the fight against Pokemon@c9be777
Pokemon@6228a17f does 50 damage to Pokemon@c9be777 and Pokemon@c9be777 has 0 left.
Pokemon@c9be777 does 25 damage to Pokemon@6228a17f and Pokemon@6228a17f has 25 left.
Pokemon@6228a17f begins the fight against Pokemon@c9be777
Pokemon@6228a17f does 50 damage to Pokemon@c9be777 and Pokemon@c9be777 has -50 left.
Pokemon@c9be777 does 25 damage to Pokemon@6228a17f and Pokemon@6228a17f has 0 left.
Pokemon@6228a17f has lost the fight

Names @6228a17f and @6228a17f are not good names for pokemon. To change this, add a String-type name field to the Constructor and use it accordingly. Battle The simulation for the battle is obviously wrong as (a) The battle "begins" each turn (b) The battle does not stop after a pokemon's health has fallen to 0 or below. To solve this, I suggest an entry-controlled while loop instead of do-while and inserting a break statement after the first turn. Applying these changes, your program goes: Pokemon:

public class Pokemon{
    private int health;
    private int strength;
    private int speed;
    private String name;
    public Pokemon(String name, int health, int strength, int speed){
        assert health >= 1;
        assert health <= 300;
        assert strength >= 1;
        assert strength <= 300;
        assert speed >= 1;
        assert speed <= 300;
        this.health = health;
        this.strength = strength;
        this.speed = speed;
        this.name = name;
    }

    public static void battle(Pokemon pokemon1, Pokemon pokemon2)
    {
        System.out.println(pokemon1.name+" begins the fight against "+pokemon2.name);
        while
        (pokemon1.health >= 1 || pokemon2.health >= 1)
        {

            pokemon2.health = pokemon2.health - pokemon1.strength;

            System.out.println(pokemon1.name +" does "+ pokemon1.strength +" damage to "+
                pokemon2.name +" and "+ pokemon2.name +" has "+ pokemon2.health +" health left.");

            if
            (pokemon2.health <= 0)
            break;

            pokemon1.health = pokemon1.health - pokemon2.strength;

            System.out.println(pokemon2.name +" does "+ pokemon2.strength +" damage to "+ 
                pokemon1.name +" and "+ pokemon1.name +" has "+ pokemon1.health +" health left.");

        }

        if
        (pokemon1.health < 1)
            System.out.println(pokemon1.name +" has lost the fight");
        else
            System.out.println(pokemon2.name +" has lost the fight");
    }
}

Upvotes: 0

Jonathan Toomer
Jonathan Toomer

Reputation: 11

It works fine for me. The only thing I'd say is wrong with the code is that your battle method isn't static.

public static void battle(Pokemon pokemon1, Pokemon pokemon2)

Upvotes: 1

Piotr Praszmo
Piotr Praszmo

Reputation: 18330

Add static to battle function, just like in main.

Also, you cannot use charizard and blastoise in main. Non-static variables cannot be used in static functions. You need to create local variables in `main

public static void main(String[] args){
    Pokemon charizard = new Pokemon(100,50,50);
    Pokemon blastoise = new Pokemon(150,25,150);
    Pokemon.battle(charizard, blastoise);
}

You can also create new PokemonTester and use it's variables:

public static void main(String[] args){
    PokemonTester tester=new PokemonTester();
    Pokemon.battle(tester.charizard, tester.blastoise);
}

You can learn more about static members here

Upvotes: 6

Related Questions