1

I'm thinking about starting to programming an Android game, and my main problem is that I don't know how to generate a random map and/or a ground like "Boom Beach" and all that stuff which follow a logic (pattern ?).

I often read articles about this but it's always the theory (like heightmap), and I don't understand how to implement this (heightmap or not) in Android code.

I would appreciate some examples and references guys.

Thanks a lot.

[Note : I'm a beginner of game development but not in programming, i'm a web developer and also know some Java and Android programming]

Bogdan Bogdanov
  • 1,707
  • 2
  • 20
  • 31
rob-art
  • 517
  • 6
  • 18
  • possible duplicate of [Random 2D Tile-Map Generating Algorithm](http://stackoverflow.com/questions/11968167/random-2d-tile-map-generating-algorithm) – user902383 Aug 28 '15 at 15:16
  • It's really interesting but it's still a lot of theory, no code example, and that's my problem, i really don't see how i can implement that. – rob-art Aug 28 '15 at 15:24

1 Answers1

1

Here you have very simple algorithm to generate you very simple maps

public class Peach {
    private static enum Tile {
        Water("~"), Sand("."), Plain("\""), Forest("#"), Hill("#");

        public String tile;

        Tile(String tile) {
            this.tile = tile;
        }

    }

    Map<Tile, List<Tile>> constrains = new HashMap<>();
    {
        constrains.put(Tile.Water,
                Arrays.asList(Tile.Water, Tile.Sand, Tile.Plain));
        constrains.put(Tile.Sand,
                Arrays.asList(Tile.Water, Tile.Sand, Tile.Plain));
        constrains.put(Tile.Plain,
                Arrays.asList(Tile.Water, Tile.Sand, Tile.Plain, Tile.Forest));
        constrains.put(Tile.Forest, Arrays.asList(Tile.Hill, Tile.Plain));
        constrains.put(Tile.Hill, Arrays.asList(Tile.Hill, Tile.Forest));
    }


    public Tile[][] generate(int x, int y) {

        Random rand = new Random();
        Tile[][] map = new Tile[x][y];

        for (int i = 0; i < x; i++) {
            map[i][0] = Tile.Water;
            map[i][y - 1] = Tile.Water;
        }

        for (int i = 0; i < y; i++) {
            map[0][i] = Tile.Water;
            map[x - 1][i] = Tile.Water;
        }

        for (int i = 1; i < x - 1; i++) {
            for (int j = 1; j < y - 1; j++) {
                List<Tile> con1 = constrains.get(map[i][j-1]);
                List<Tile> con2 = constrains.get(map[i-1][j]);
                List<Tile> con = new ArrayList<Peach.Tile>();
                for (Tile tile : con1){
                    if (con2.contains(tile)){
                        con.add(tile);
                    }
                }
                if (con.isEmpty()){
                    con.add(Tile.Plain);
                }

                map[i][j] = con.get(rand.nextInt(con.size()));

            }
        }

        return map;
    }

    public static void main(String[] args) {
        Peach p = new Peach();
        Tile[][] map = p.generate(20, 10);
        System.out.print("\n");
        for (Tile[] line : map) {
            for (Tile tile : line) {
                System.out.print(tile.tile);
            }
            System.out.print("\n");
        }
    }

}

You have 5 type of tiles, water sand plain forest and hill, now to reduce chaos we define constrain table, defining what type of tiles can be next to each other.

ie water can be next to sand, creating nice beach coast, but sand cant be found in middle of a forest.

Algorithm is simple, and code have some flaws, but i hope it will give you some ideas how to carry your problem forward

user902383
  • 8,420
  • 8
  • 43
  • 63
  • @rob-art no problem, if you will have more problems just ask – user902383 Aug 28 '15 at 17:50
  • Great ! I think you know a lot of things about mobile games, so what's your advice about a cross-plateform framework for mobile ? Or maybe i shouldn't ? Thanks again – rob-art Aug 31 '15 at 10:38
  • @rob-art sorry tbut i'm not able to help you, i dont have much experience in mobile developing. I heard (but i dont have any personal experience with it) xamarin is good. from other hand i think there are ways to be able to run app written in html5 in multiple platform. but again, no personal experience. – user902383 Sep 04 '15 at 13:37