1

I'm trying to figure out how to randomize the object selected as a parameter in a method. So I created two Pokemon classes below (rattata and pidgey)

class WildPokemon {

private static int randomHealth(int min, int max) {
    int range = (max - min) + 1;
    return (int)(Math.random() * range) + min;
}
private static int randomAttack(int min, int max) {
    int range = (max - min) + 1;
    return (int)(Math.random() * range) + min;
}
private static int randomSpeed(int min, int max) {
    int range = (max - min) + 1;
    return (int)(Math.random() * range) + min;
}


static Pokemon rattata = new Pokemon("Rattata",randomHealth(15,20),randomAttack(2,5),randomSpeed(2,6));
static Pokemon pidgey = new Pokemon("Pidgey",randomHealth(10,17),randomAttack(3,4),randomSpeed(3,5));
}

Below I am able to call rattata in the method Pokemon.battle() and it functions as expected. Is there a way I could randomize my second parameter to where it could be either rattata or pidgey selected at random?

public class PokemonTester{

    public static void main(String[] args){
        Pokemon.battle(starter, WildPokemon.rattata);
    }
}
Sean
  • 17
  • 5

2 Answers2

2

Important remark : using static methods and static fields for model is generally not advised.
Instead you should create an instance of WildPokemon and call method on it.

Do it in the same way you have already done to calculate random values.
You should use a List of pokemon rather than doing the compute with two hard coded values.

Try this :

public class WildPokemon{
    ...
    private Random rand = new Random();
    private List<Pokemon> pokemonList;
    ...
    public WildPokemon(){
      pokemonList = new ArrayList();
      Pokemon rattata = new Pokemon("Rattata",randomHealth(15,20),randomAttack(2,5),randomSpeed(2,6));
      pokemonList.add(rattata);
      Pokemon pidgey = new Pokemon("Pidgey",randomHealth(10,17),randomAttack(3,4),randomSpeed(3,5));
      pokemonList.add(pidgey);
      ...
    }

    private Pokemon getRandomPokemon() {
        int n = rand.nextInt(pokemonList.size());
        return pokemonList.get(n);
    }
     ...
}

And call it :

 WildPokemon wildPokemon = new WildPokemon();
 Pokemon.battle(starter, wildPokemon.getRandomPokemon());
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • You forgot `list.size()` and `list.get(n)`. – M. Prokhorov Apr 21 '17 at 18:55
  • @M. Prokhorov Indeed and thank you. I was disturbed by all these static methods and fields that are really not advised that I would change too fast :) – davidxxx Apr 21 '17 at 18:58
  • @davidxxx Sorry about all the static nonsense. I am using this project to teach myself, so I'm mostly just auto-filling what IntelliJ recommends. I also hadn't learned lists yet, so I clearly have a long ways to go. Thank you for your help! – Sean Apr 21 '17 at 19:06
  • @davidxxx static state is actually fine for small applications. Advising to avoid it without explaining why is worse in my eyes: it breeds too much misunderstanding. – M. Prokhorov Apr 21 '17 at 19:12
  • @M. Prokhorov I disagree. Using static everywhere means designed your app with global variables. The little application of today may become the medium or the big of tomorrow. Writing not maintainable and harder to test code for an app is never fine. – davidxxx Apr 21 '17 at 19:20
  • @Sean To understand better when use the static modifier, I advise you read upvoted answers to this question : this http://stackoverflow.com/questions/1530353/in-what-situations-is-static-method-a-good-practice – davidxxx Apr 21 '17 at 19:27
  • @davidxxx I will definitely check that thread out. Should be helpful, thanks again! – Sean Apr 21 '17 at 20:23
0

Use an array (or list) of objects and randomly generate an index value.

public class PokemonTester{

    public static void main(String[] args){
        WildPokemon[] pokemons = { rattata, pidgey };
        Pokemon.battle(starter, pokemons[ (int)(Math.random()*pokemons.length) ] );
    }
}
markspace
  • 10,621
  • 3
  • 25
  • 39