0

I am trying to create a deck of cards That I am going to be choosing from to make a map. I have 16 cards, that are of two different types.

I first have my class with an enum :

package Game;
public class Terrain {
    public static enum Ter{
        DESERT, MOUNTAIN
    }
} 

And now I have a class Map:

package Game;
import Game.Terrain.Ter;

public class Map {

    Ter mapPieces[] =  {Ter.DESERT, Ter.DESERT, Ter.DESERT, Ter.DESERT,
                        Ter.DESERT, Ter.DESERT, Ter.DESERT, Ter.DESERT,
                        Ter.MOUNTAIN, Ter.MOUNTAIN, Ter.MOUNTAIN, Ter.MOUNTAIN,
                        Ter.MOUNTAIN, Ter.MOUNTAIN, Ter.MOUNTAIN, Ter.MOUNTAIN};

}

Is this how I make my array? Is there a better way to do this? It just looks horrible....

Sarah
  • 2,713
  • 13
  • 31
  • 45

2 Answers2

2

You need to create a set as follows:

Set<Terrain> terrains = EnumSet.allOf(Terrain.class);

and convert that to an array. You could also use the values() method. Details on that are provided here on SO.

Community
  • 1
  • 1
Chthonic Project
  • 8,216
  • 1
  • 43
  • 92
1

Assuming that you want to create a map that represents tiles each of which is one of your Terrain enums, a common practice is to have a String or even XML that is parsed into the List. For example you could have "DDDDDDDDMMMMMMMM" and then for each D add a Ter.DESERT to your list, etc.

For your simple situation, a loop makes sense as well. Just add 8 Ter.DESERT and 8 Ter.MOUNTAIN to your list, and convert it to an array. If you don't want to use a List, then you can just iterate through your array and make all the even elements DESERT and the odd ones MOUNTAIN.

Lots of different ways to do it. The way you give should work, but I agree that it looks horrible.

Teresa Carrigan
  • 668
  • 8
  • 14
  • Thank you for all of the suggestions, I am sure this is not how I am going to end up with things but it's still nice to learn the proper way to do things. – Sarah Jan 17 '14 at 00:20