Skip to content

Instantly share code, notes, and snippets.

@dha-lo-jd
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save dha-lo-jd/11320128 to your computer and use it in GitHub Desktop.

Select an option

Save dha-lo-jd/11320128 to your computer and use it in GitHub Desktop.
import java.util.Iterator;
/**
* Created by orekyuuPC on 14/04/26.
*/
public class Vortex {
private enum Dir {
CENTER(0, 0), //
UP(0, -1), //
DOWN(0, 1), //
LEFT(-1, 0), //
RIGHT(1, 0), //
UP_LEFT(UP.getY(), LEFT.getX()), //
UP_RIGHT(UP.getY(), RIGHT.getX()), //
DOWN_LEFT(DOWN.getY(), LEFT.getX()), //
DOWN_RIGHT(DOWN.getY(), RIGHT.getX()), //
;
final int x;
final int y;
private Dir(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
private static class VortexStep implements Iterable<Dir> {
private class IteratorImpl implements Iterator<Dir> {
private int dir = -1;
private int size = 1;
private int step;
private int remainDirStep = 0;
private int remainSizeStep = 2;
private IteratorImpl(int step) {
this.step = step;
}
@Override
public boolean hasNext() {
return 0 < step;
}
@Override
public Dir next() {
step--;
remainDirStep--;
if (remainDirStep <= 0) {
remainDirStep = size;
dir++;
remainSizeStep--;
if (remainSizeStep <= 0) {
remainSizeStep = 2;
size++;
}
}
dir = dir % dirs.length;
return dirs[dir];
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
private final int end;
private static final Dir[] dirs = new Dir[] {
Dir.LEFT,//
Dir.DOWN,//
Dir.RIGHT,//
Dir.UP,//
};
private VortexStep(int end) {
super();
this.end = end;
}
@Override
public Iterator<Dir> iterator() {
return new IteratorImpl(end);
}
}
private static final int SIZE = 20;
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 25; i++) {
boolean[][] map = new boolean[SIZE][SIZE];
printMap(replace(map, i, 5, 5));
Thread.sleep(500);
}
}
public static boolean[][] replace(boolean[][] map, int n, int cx, int cy) {
if (n <= 0) {
return map;
}
boolean f = true;
map[cx][cy] = f;// 一個目は手動
VortexStep step = new VortexStep(n - 1);
for (Dir dir : step) {
cx += dir.getX();
cy += dir.getY();
if (inIn(cx, cy)) {
map[cy][cx] = f;
}
}
return map;
}
private static boolean inIn(int i) {
return 0 <= i && i < SIZE;
}
private static boolean inIn(int x, int y) {
return inIn(x) && inIn(y);
}
private static void printMap(boolean[][] array) {
for (boolean[] i : array) {
for (boolean k : i) {
System.out.print(k ? "#" : ".");
}
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment