Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Spiral {
- private static final int DIRECTION_RIGHT = 0x001;
- private static final int DIRECTION_LEFT = 0x002;
- private static final int DIRECTION_TOP = 0x004;
- private static final int DIRECTION_BOTTOM = 0x008;
- private static final int EMPTY_CELL = 0;
- private static final int FIRST_CELL = 1;
- private static final int DELAY_BETWEEN_TESTS = 3000;
- private static final int DELAY_BETWEEN_STEPS = 200;
- private static final int SPIRAL_SIZE = 15;
- public static void main(String... args) {
- Spiral calculator = new Spiral(SPIRAL_SIZE);
- calculator.fastConsoleDemo();
- sleep(DELAY_BETWEEN_TESTS);
- calculator.slowConsoleDemo();
- }
- private static void sleep(int ms) {
- try {
- Thread.sleep(ms);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- private static void fakeConsoleClear(){
- StringBuilder builder = new StringBuilder();
- for (int i=0; i< 100; i++){
- builder.append("\n");
- }
- System.out.print(builder.toString());
- }
- private int[][] mCoub;
- private int mDirection, mSize, mCurrentX, mCurrentY, mCurrentNumber;
- public Spiral(int size) {
- mSize = size;
- }
- public void fastConsoleDemo() {
- init();
- for (int i = 0; i < mSize; ++i) {
- for (int j = 0; j < mSize; ++j) {
- if (i > j) {
- if (i + j < mSize) {
- mCoub[i][j] = (mSize - (j + 1)) * (j + 1) * 4 - (i - (j + 1));
- } else {
- mCoub[i][j] = i * (mSize - i) * 4 - (i - (mSize - i)) - (j - mSize + i + 1);
- }
- } else {
- if (i + j < mSize) {
- mCoub[i][j] = i * (mSize - i) * 4 - (i - (mSize - i)) + i + j - mSize + 1;
- } else {
- mCoub[i][j] = (mSize - (mSize - j - 1)) * (mSize - j - 1) * 4 + (j - (mSize - j)) + i + (j - mSize) + 3;
- }
- }
- }
- }
- fakeConsoleClear();
- System.out.print("fast...");
- System.out.print(toString());
- }
- public void slowConsoleDemo() {
- init();
- while (goAndPut(0)) {
- fakeConsoleClear();
- System.out.print("slow...");
- System.out.print(toString());
- sleep(DELAY_BETWEEN_STEPS);
- }
- }
- private void init() {
- mCoub = new int[mSize][mSize];
- mCoub[0][0] = FIRST_CELL;
- mCurrentNumber = FIRST_CELL;
- mDirection = DIRECTION_RIGHT;
- }
- private boolean goAndPut(int step) {
- step++;
- if (step > 3) {
- return false;
- }
- int lastX = mCurrentX, lastY = mCurrentY;
- switch (mDirection) {
- case DIRECTION_RIGHT:
- if (mCurrentX < mSize - 1 && mCoub[mCurrentX + 1][mCurrentY] == EMPTY_CELL) {
- mCoub[++mCurrentX][mCurrentY] = ++mCurrentNumber;
- } else {
- mDirection = DIRECTION_BOTTOM;
- return goAndPut(step);
- }
- break;
- case DIRECTION_BOTTOM:
- if (mCurrentY < mSize - 1 && mCoub[mCurrentX][mCurrentY + 1] == EMPTY_CELL) {
- mCoub[mCurrentX][++mCurrentY] = ++mCurrentNumber;
- } else {
- mDirection = DIRECTION_LEFT;
- return goAndPut(step);
- }
- break;
- case DIRECTION_LEFT:
- if (mCurrentX > 0 && mCoub[mCurrentX - 1][mCurrentY] == EMPTY_CELL) {
- mCoub[--mCurrentX][mCurrentY] = ++mCurrentNumber;
- } else {
- mDirection = DIRECTION_TOP;
- return goAndPut(step);
- }
- break;
- case DIRECTION_TOP:
- if (mCurrentY > 0 && mCoub[mCurrentX][mCurrentY - 1] == EMPTY_CELL) {
- mCoub[mCurrentX][--mCurrentY] = ++mCurrentNumber;
- } else {
- mDirection = DIRECTION_RIGHT;
- return goAndPut(step);
- }
- break;
- }
- return !(lastX == mCurrentX && lastY == mCurrentY);
- }
- @Override
- public String toString() {
- StringBuilder builder = new StringBuilder();
- builder.append(mCurrentNumber + "\n");
- for (int i = 0; i < mCoub.length; i++) {
- for (int j = 0; j < mCoub[i].length; j++) {
- builder.append(String.format("%4s", Integer.toString(mCoub[i][j])));
- }
- builder.append("\n");
- }
- return builder.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement