Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Small helper class for 2d binary island map generation  (Read 1213 times)
0 Members and 1 Guest are viewing this topic.
Offline dah01

Junior Devvie


Medals: 7



« Posted 2011-12-23 06:25:04 »

My Helper Version:        http://pastebin.com/VCjU3NnK
Original:                      http://pastebin.com/n4b21FWg
Reddit Thread:             http://www.reddit.com/r/gamedev/comments/nn8hb/java_2d_island_generator_first_real_project_since/

A guy over at reddit.com/r/gamedev posted the source for an island generator:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
package polytester;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Polygon;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Color;
import java.util.Random;
import java.awt.Font;
import java.util.Scanner;


/*
 * # # # # #
 * # # 1 0 0
 * # # # # #
 * # # # # #
 * # # # # #
 */

 
public class hola extends BasicGame {
   
   Polygon[][] rand = new Polygon[25][25];
   int[][][] grid = new int[5][25][25];
   int tempax = 0, tempay = 0;
   Random r = new Random();
   int time;
   Scanner input = new Scanner(System.in);
   Font font = new Font("Helvetica", Font.BOLD, 30);
   int x = 20, y = 20, number = 100, phase = 1;
   int xc = 32, yc = 24;
   String print;
   boolean next;
   int z, p, tempx, tempy;
   int timer;
   int checksum;
   int chance;
   int cursorx = 200, cursory = 200, cx = 1, cy = 1;
   int sex = 0;
   int countd=0;
   

 
   public hola() {
      super("polygontest");
   }
   public void init(GameContainer container) throws SlickException {  
     
      container.setVSync(true);
     
      for(int z = 0; z < 25; z++){
         for(int p = 0; p < 25; p++){

            rand[z][p] = new Polygon(new float[]{
               0,0,
               0,y,
               x,y,
               x,0
            });
           
            chance = r.nextInt(2);
            grid[0][z][p] = chance;
            if(chance == 1){
               rand[z][p].setLocation(z*x, p*y);
            }else{
               rand[z][p].setLocation(900, 900);
            }
            tempx++;
         }
     
         tempy++;
         tempx=0;
      }
     
     
     
      for(int dope = 0; dope < 4; dope++){
         for(int mattb = 0; mattb < 25; mattb++){
            for(int mattx = 0; mattx < 25; mattx++){
               if(mattb-1 >= 0 && mattb+1 <=24 && mattx-1 >= 0 && mattx+1 <= 24){
                  checksum=grid[dope][mattb-1][mattx-1]+grid[dope][mattb-1][mattx]+grid[dope][mattb-1][mattx+1]+
                        grid[dope][mattb][mattx-1]+grid[dope][mattb][mattx+1]+grid[dope][mattb+1][mattx-1]+
                        grid[dope][mattb+1][mattx]+grid[dope][mattb+1][mattx+1];
     
                  if(checksum >= 4 && grid[dope][mattb][mattx] == 1){
                     grid[dope+1][mattb][mattx] = 1;
                  }
                 
                  if(checksum >= 5){
                     grid[dope+1][mattb][mattx] = 1;
                  }                  
               }
            }
         }
      }

     
      for(int ax = 0; ax < 25; ax++){
         for(int ay = 0; ay < 25; ay++){
            if(grid[4][ax][ay] == 1){
               rand[ax][ay].setLocation(ax*x, ay*y);
               tempax++;
            }
         }
         tempay++;
         tempax=0;
      }
   }

   
   public void update(GameContainer container, int delta) {
      timer--;
      if(container.getInput().isKeyDown(Input.KEY_SPACE) && timer < 0){
         timer = 30;
         countd++;
         if(countd>4)countd=0;
         for(int ax = 0; ax < 25; ax++){
            for(int ay = 0; ay < 25; ay++){
               if(grid[countd][ax][ay] == 1){
                  rand[ax][ay].setLocation(ax*x, ay*y);
                  tempax++;
               }
            }
            tempay++;
            tempax=0;
         }
         
      }
   }
   
   public void render(GameContainer container, Graphics g)  {

     
      for(int d = 0; d < 25; d++){
         for(int o = 0; o < 25; o++){
   //         g.draw(rand[o][d]);
            if(grid[countd][d][o]==1){
               g.drawString("#", d*x, o*y);
               System.out.print("#");
            }else{
               g.drawString(" ", d*x, o*y);
               System.out.print(" ");
            }
         }
         System.out.println();
      }
      System.out.println("BREAKBREAKBREAK");
   }
   
   public static void main(String[] argv) throws SlickException {
      AppGameContainer container = new AppGameContainer(new hola(), 500, 500, false);
      container.setShowFPS(false);
      container.start();
   }
}






I attempted to make it a little easier to use but I have no idea what witchcraft was used in it's creation so I'm not sure how to tidy up the generation section but at least it's easy to use now.

If you're just looking for a 2d island map you want to call get2dClarifiedMap(width,height)

http://pastebin.com/VCjU3NnK
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
package com.hadesvine.tiledgame.examples;

import org.newdawn.slick.geom.Polygon;
import java.util.Random;

public class MapHelper {

    public static final int DEFAULTWIDTH = 32;
    public static final int DEFAULTHEIGHT = 25;
    public static final int CLARIFICATION = 4;

    public MapHelper() {
    }

    public static int[][][] getNewMap(int width, int height, int clarification) {
        Polygon[][] rand = new Polygon[width][height];
        int[][][] grid;
        int tempax = 0, tempay = 0;
        Random r = new Random();
        int x = 20;
        int y = 20;
        int tempx = 0;
        int tempy = 0;
        int checksum = 0;
        int chance = 0;
        grid = new int[5][width][height];
        for (int z = 0; z < width; z++) {
            for (int p = 0; p < height; p++) {
                rand[z][p] = new Polygon(new float[]{
                            0, 0,
                            0, y,
                            x, y,
                            x, 0
                        });
                chance = r.nextInt(2);
                grid[0][z][p] = chance;
                if (chance == 1) {
                    rand[z][p].setLocation(z * x, p * y);
                } else {
                    rand[z][p].setLocation(900, 900);
                }
                tempx++;
            }
            tempy++;
            tempx = 0;
        }
        for (int i = 0; i < clarification; i++) {
            for (int mattb = 0; mattb < width; mattb++) {
                for (int mattx = 0; mattx < height; mattx++) {
                    if (mattb - 1 >= 0 && mattb + 1 < width && mattx - 1 >= 0 && mattx + 1 < height) {
                        checksum = grid[i][mattb - 1][mattx - 1] + grid[i][mattb - 1][mattx] + grid[i][mattb - 1][mattx + 1]
                                + grid[i][mattb][mattx - 1] + grid[i][mattb][mattx + 1] + grid[i][mattb + 1][mattx - 1]
                                + grid[i][mattb + 1][mattx] + grid[i][mattb + 1][mattx + 1];

                        if (checksum >= clarification && grid[i][mattb][mattx] == 1) {
                            grid[i + 1][mattb][mattx] = 1;
                        }

                        if (checksum >= 5) {
                            grid[i + 1][mattb][mattx] = 1;
                        }
                    }
                }
            }
        }
        for (int ax = 0; ax < width; ax++) {
            for (int ay = 0; ay < height; ay++) {
                if (grid[CLARIFICATION][ax][ay] == 1) {
                    rand[ax][ay].setLocation(ax * x, ay * y);
                    tempax++;
                }
            }
            tempay++;
            tempax = 0;
        }
        return grid ;
    }
   
    public static void main(String[] args) {
        //int [][][]testMap = getNewMap(32,24);
        printRandomMapToConsole(800,600,4);
    }
   
    public static void printRandomMapToConsole(int width, int height, int clarification){
        int [][][]grid = getNewMap(width, height,clarification);
                for (int d = 0; d < height; d++) {
            for (int o = 0; o < width; o++) {
                if (grid[clarification][d][o] == 1) {
                    System.out.print("#");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
    public static void printRandomMapToConsole(int width, int height){
        int [][][]grid = getNewMap(width, height,CLARIFICATION);
                for (int d = 0; d < height; d++) {
            for (int o = 0; o < width; o++) {
                if (grid[CLARIFICATION][d][o] == 1) {
                    System.out.print("#");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
   
    public static int[][] getClarified2DIslandMap(int width, int height){
        int [][][] gridWithClarification = getNewMap(width,height,4);
        int [][] clarifiedGrid = gridWithClarification[4];
        return clarifiedGrid;
    }
}


As a note, this works much better on smaller maps.
Offline Mike

« JGO Spiffy Duke »


Medals: 87
Projects: 1
Exp: 6 years


Java guru wanabee


« Reply #1 - Posted 2011-12-23 10:19:51 »

To give an idea as to what it creates:
http://pastebin.com/LFxC1vSV

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (37 views)
2014-12-15 09:26:44

Mr.CodeIt (31 views)
2014-12-14 19:50:38

BurntPizza (62 views)
2014-12-09 22:41:13

BurntPizza (99 views)
2014-12-08 04:46:31

JscottyBieshaar (60 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (77 views)
2014-11-29 21:32:03

toopeicgaming1999 (138 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (38 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!