1
\$\begingroup\$

I'm currently making my own little 2d text based game and I've got a 11 / 11 grid where a monster spawns in the middle and when i stand on it's position and it brings me to another 11/11 grid where i fight the monster. I'm going to make it a turn based theme where it starts on my turn and i can only move a certain amount of spaces and the monster can only move a certain amount of spaces and when are abilities are in range we can use them etc etc etc,

I want it so every time i encounter a monster it sees where i am on the grid and see's if its a low level area or a high level area, if it's a low level area it will spawn a Slime , wolf or pig. or high level area it will spawn a dragon , giant etc. Is there a better way to do it than what i'm currently doing it. Right now I've started to low level area.

What i'd like it to do

if monster = pig,wolf,slime , give it a certain amount of stats and health then start the fight

if monster = giant,dragon , give it a certain amount of stats and health then start the fight

Is the current way i'm doing it good or is there a better way?

  public static void monster_encounter() {
  Random gen = new Random();
  final int monster_gen = gen.nextInt(3);
  if( monster_gen == 0) {
  System.out.println("You're fighting a: Wolf");  
  }
  else if( monster_gen == 1) {
  System.out.println("You're fighting a: Slime");  
  }
  else if( monster_gen == 2) {
  System.out.println("You're fighting a: Pig");  
  }



  }
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Long term it might be better to create either monster classes that all inherit from a single monster class that contains all methods you need. Then each class can simply override these methods (make them abstract). That will be more scalable. A second good method is to create just 1 monster class and put the variables that monsters use in there (in this case just a single string with the name of the monster). You can then add in a map which maps various numbers towards various numbers (an encounter table). That would be something like this:

encounterTable Map<Integer,Monster> = new Map<Integer,Monster>;
encounterTable.put(new Integer(0),new Wolf);
encounterTable.put(new Integer(1),new Slime);
encounterTable.put(new Integer(2),new Pig;

then an encounter looks like this:

  Random gen = new Random();
  final int monster_gen = gen.nextInt(3);
  Monster m = encounterTable.get(monster_gen);

where monster looks like this:

public abstract Monster{
   abstract void  printname();
 }

Where pig looks like this:

public Pig extends Monster{
     void printname(){
     System.out.println("You're fighting a: Pig");  
          }
    }

Wolf can look like:

public Wolf extends Monster{
     void printname(){
     System.out.println("You're fighting a: Wolf");  
          }
    }

I will leave slime as an exercise to the reader.

The second method is using just a single Monster class thus we get:

public Monster{
     String name;
    public Monster(String monsterName){
        name=monsterName;
    }
   public void printname{
        System.out.println("you are fighting a: "+ name);
   }
}

And now you set up the encounter table as following:

   encounterTable Map<Integer,Monster> = new Map<Integer,Monster>;
    encounterTable.put(new Integer(0),new Monster("Wolf");
    encounterTable.put(new Integer(1),new Monster("Slime"));
    encounterTable.put(new Integer(2),new Monster("Pig");

Your current method is going to become really long and a bit confusing soon (next you are going to have to give each monster an attack,number of hit points,special powers ext.).

\$\endgroup\$
6
  • \$\begingroup\$ I don't know how to make different monster classes at the moment. Your second idea sounds like what i want to do, but i wouldn't know where to start. \$\endgroup\$ Aug 13, 2014 at 21:43
  • \$\begingroup\$ I hope you are able to use this. If you want to know more look up a tutorial on the various forms of polymorphism or maybe the strategy and factory pattern if you feel more advanced (you need polymorphism first). Or is it that you don't know how to make classes as all? \$\endgroup\$
    – Thijser
    Aug 13, 2014 at 21:49
  • \$\begingroup\$ I don't know how to make classes at all. Currently i'm half way through my second year of learning programming and I just started learning 2D arrays 1 month ago. All of this stuff i can slightly understand but i'm new to all this \$\endgroup\$ Aug 13, 2014 at 22:01
  • \$\begingroup\$ You can take a look here: docs.oracle.com/javase/tutorial/java/javaOO/classes.html . Do note that the details depend on what IDE (the program you use to program in) you use. You can probably find a better tutorial by googling [IDE name] tutorial making new classes. Fundamentally a java class is just an extra file with the name of the class(. java) and then containing for example the Monster class as per my example. \$\endgroup\$
    – Thijser
    Aug 13, 2014 at 22:09
  • \$\begingroup\$ I'm trying to use this code but it's giving me a bunch of errors? \$\endgroup\$ Aug 14, 2014 at 1:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .