import java.util.LinkedList; import java.util.Queue; import java.util.Random; import java.awt.Point; public class NeutralGroundGenerator { private final double ExtendThreshold = 0.4; public NeutralGroundGenerator() { // TODO Auto-generated constructor stub } /** * Usage java NeutralGroundGenerator width weight * @param args */ public static void main(String[] args) { int w = Integer.parseInt(args[0]); int h = Integer.parseInt(args[1]); new NeutralGroundGenerator().generate(w,h); } private void generate(int w, int h) { System.out.println("" + w + " " + h); char[][] map = new char[h][w]; Random rand = new Random(); for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { int k = rand.nextInt(10); char c = (char)((int)'0' + k); map[y][x] = c; } } for (int seeds = 0; seeds < 4; ++seeds) { char seed = (seeds % 2 == 0) ? 'A' : 'B'; boolean done = false; while (!done) { int x = rand.nextInt(w); int y = rand.nextInt(h); if (map[y][x] < 'A') { done = true; map[y][x] = seed; Queue q = new LinkedList(); q.add(new Point(x,y)); while (!q.isEmpty()) { Point p = q.remove(); for (int dy = -1; dy <= 1; ++dy) for (int dx = -1; dx <= 1; ++dx) if (dx ==0 || dy == 0) { Point p2 = new Point(p.x+dx, p.y+dy); if (p2.x >= 0 && p2.x < w && p2.y >= 0 && p2.y < h) { char old = map[p2.y][p2.x]; if (old < 'A') { // Randomly extend the seeded region if (rand.nextDouble() < ExtendThreshold ) { map[p2.y][p2.x] = seed; q.add(p2); } } } } } } } } for (int y = 0; y < h; ++y) { for (int x = 1; x < w; ++x) { char cx = map[y][x]; char cx1 = map[y][x-1]; if ((cx == 'A' && cx1 == 'B') || (cx == 'B' && cx1 == 'A')) { int k = rand.nextInt(10); char c = (char)((int)'0' + k); map[y][x] = c; } } if (y > 0) { for (int x = 1; x < w; ++x) { char cy = map[y][x]; char cy1 = map[y-1][x]; if ((cy == 'A' && cy1 == 'B') || (cy == 'B' && cy1 == 'A')) { int k = rand.nextInt(10); char c = (char)((int)'0' + k); map[y][x] = c; } } } } for (int y = 0; y < h; ++y) { System.out.println(map[y]); } } }