Small helper class for 2d binary island map generation

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:

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

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.

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