0

I am trying to create this class called Terrain for a board game I am trying to build in Java, but I will try and make this post as generic as possible so it can be explained in any OOP language, in pseudo code.

This game has 36 hex shaped tiles, each Tile is a different Terrain. Terrain has 8 types, like Swamp, Forest, Mountain etc. Each terrain holds how many moves you can make, it can also hold Creatures and Buildings (which are two different classes), or at least have Creatures and Buildings associated with a specific terrain.

I am not sure I want an enum to represent the 8 types of terrains, or if it is even necessary.

Essentially what I want to be able to do with this class is say something like:

Terrain t0 = new Swamp();
Terrain t1 = new Forest();

And then I want to be able to associate the neighbour's of each terrain with that specific terrain, so something like:

//terrain 0, neighbour 0
t0.n0 = t1;
//terrain 0, neighbour 1
t0.n1 = t7;
//terrain 0, neighbour 2
t0.n2 = t8;
//terrain 0, neighbour 3
t0.n3 = t2;
//terrain 0, neighbour 4
t0.n4 = t23;
//terrain 0, neighbour 5
t0.n5 = t22;

etc.

I think it should be something like:

public class Terrain{
    public int moves = 0;  
    public Terrain(int m){
        moves = m;
    }
}

I am really not sure what to do with the types of Terrains as I said...

I have worked out by hand what all of the connections for each tiles for the game board should be, but I am not sure how to translate this into code, or into a class for Terrain. If anyone has any suggestions, I would really appreciate it.

Sarah
  • 2,713
  • 13
  • 31
  • 45
  • For starters I would reccomend making the classes' atrributes private and implement get and set methods like [this](http://stackoverflow.com/questions/2036970/tutorial-on-getters-and-setters). – André Silva Jan 20 '14 at 17:45

4 Answers4

1

You can create an interface Terrain which will contain variables and methods which are common to all terrains. Then, you can create classes that implement the Terrain interface and provide a definition for abstract methods in the interface.

Something along the lines:

public interface Terrain{
    int getMoves();
}

public class Forest implements Terrain{

    final String NAME = "FOREST";
    private int someMovesLikeTheJagger = -1;

    public Forest(int moves){
        someMovesLikeTheJagger = moves;
    }

    @Override
    public void getMoves(){
        return someMovesLikeTheJagger;
    }

}
An SO User
  • 24,612
  • 35
  • 133
  • 221
1

I think your instinct about using sub-classes for each terrain type is a good one, however I can't say for sure if it's necessary. I would be asking yourself this question: Does the behavior (in code) change depending on the terrain type? If so, you'd definitely want to use a different sub-class for each terrain type (the terrain base class would be an abstract class) as you'd be able to make certain methods virtual, and thus behave differently at runtime. This is known as polymorphism. If not, you'd be better using an enum to store the terrain type as it keeps the code simpler.

As for keeping track of neighbors, I wouldn't do this within the individual terrain class. I would have some sort of array or external data structure that basically holds a collection of terrain instances (which can, of course, be any sub-class) and their position. This could probably be implemented as a graph, or a linked list that has references to the terrain pieces on the left, right, top, bottom, etc. Or, perhaps just a 2D array.

This same data structure can also potentially hold the positions of other game objects, such as creatures, buildings, etc. Hope this helps!

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
  • So have an enum in Terrain that holds all of the types of terrains, and then in a separate class can I say Terrain t0 = new Swamp()? I don't really understand how Enum's work, I've only worked with very basic examples in C++ – Sarah Jan 20 '14 at 18:03
  • An `enum` is just a variable that has a constrained set of possible values. Don't think of an `enum` any differently than you'd think of other types, such as `integer` or `string` or `double`. – Mike Christensen Jan 20 '14 at 18:07
1

if the attributes of the subclasses are the same polymorphism might be an overkill you can do something like this:

public class Terrain {

  private List<Terrain> neighbors = new ArrayList<Terrain>;      

  private List<Creature> creatures = new ArrayList<Creature>();

  private List<Building> buildings = new ArrayList<Building>();

  //you can replace this with ENUM style coding but this is simpler 
  private String type;

  public Terrain(String type){
    this.type=type;
  }

  public addTerrain(Terrain t) {
    this.neighbors.add(t); 
  }

  public static void main(String [] args) {
    Terrain forest = new Terrain("forest");
    Terrain swamp  = new Terrain("swamp");
    swamp.addNeighbor(forest);
  }
}

one advantage of this method over the polymorphism is that every time you create a new terrain, you don't have to add/code a new class

nightograph
  • 2,179
  • 5
  • 31
  • 49
  • This is almost exactly along the lines of what I am looking for, you have definitely given me some excellent ideas, thank you! – Sarah Jan 20 '14 at 22:59
1

Using enum to define terrain (in Java):

enum Terrain {    
    Forest(12), Mountain(9);         // And other Terrain types...
    private int move;    
    private Terrain(int mo) {
        move=mo;
    }
    public int getMove(){
        return move;
    }
}

With the Terrain enum you can do:

    Terrain t0= Terrain.Forest;
    Terrain t2= Terrain.Mountain;
    Terrain t6= .....

Tiles can again be an enum and contain other methods and a Terrain array/or some other data structure type

boxed__l
  • 1,334
  • 1
  • 10
  • 24
  • 1
    Thanks, enum's are very powerful, but very confusing still for me, now that I understand what is happening here, this is very close to how I am proceeding with my project. – Sarah Jan 24 '14 at 01:16