Advertisement
Guest User

Java Spiral Code

a guest
Jan 31st, 2014
1,102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.79 KB | None | 0 0
  1. public class Spiral {
  2.  
  3.     private static final int DIRECTION_RIGHT =     0x001;
  4.     private static final int DIRECTION_LEFT =      0x002;
  5.     private static final int DIRECTION_TOP =       0x004;
  6.     private static final int DIRECTION_BOTTOM =    0x008;
  7.    
  8.     private static final int EMPTY_CELL =          0;
  9.     private static final int FIRST_CELL =          1;
  10.    
  11.     private static final int DELAY_BETWEEN_TESTS = 3000;
  12.     private static final int DELAY_BETWEEN_STEPS = 200;
  13.    
  14.     private static final int SPIRAL_SIZE =         15;
  15.    
  16.     public static void main(String... args) {
  17.         Spiral calculator = new Spiral(SPIRAL_SIZE);
  18.         calculator.fastConsoleDemo();
  19.         sleep(DELAY_BETWEEN_TESTS);
  20.         calculator.slowConsoleDemo();
  21.     }
  22.  
  23.     private static void sleep(int ms) {
  24.         try {
  25.             Thread.sleep(ms);
  26.         } catch (InterruptedException e) {
  27.             e.printStackTrace();
  28.         }
  29.     }
  30.  
  31.     private static void fakeConsoleClear(){
  32.         StringBuilder builder = new StringBuilder();
  33.         for (int i=0; i< 100; i++){
  34.             builder.append("\n");
  35.         }
  36.         System.out.print(builder.toString());
  37.     }
  38.    
  39.     private int[][] mCoub;
  40.     private int mDirection, mSize, mCurrentX, mCurrentY, mCurrentNumber;
  41.  
  42.     public Spiral(int size) {
  43.         mSize = size;
  44.     }
  45.  
  46.     public void fastConsoleDemo() {
  47.         init();
  48.         for (int i = 0; i < mSize; ++i) {
  49.             for (int j = 0; j < mSize; ++j) {
  50.                 if (i > j) {
  51.                     if (i + j < mSize) {
  52.                         mCoub[i][j] = (mSize - (j + 1)) * (j + 1) * 4 - (i - (j + 1));
  53.                     } else {
  54.                         mCoub[i][j] = i * (mSize - i) * 4 - (i - (mSize - i)) - (j - mSize + i + 1);
  55.                     }
  56.                 } else {
  57.                     if (i + j < mSize) {
  58.                         mCoub[i][j] = i * (mSize - i) * 4 - (i - (mSize - i)) + i + j - mSize + 1;
  59.                     } else {
  60.                         mCoub[i][j] = (mSize - (mSize - j - 1)) * (mSize - j - 1) * 4 + (j - (mSize - j)) + i + (j - mSize) + 3;
  61.                     }
  62.                 }
  63.             }
  64.         }
  65.         fakeConsoleClear();
  66.         System.out.print("fast...");
  67.         System.out.print(toString());
  68.     }
  69.  
  70.     public void slowConsoleDemo() {
  71.         init();
  72.         while (goAndPut(0)) {
  73.             fakeConsoleClear();
  74.             System.out.print("slow...");
  75.             System.out.print(toString());
  76.             sleep(DELAY_BETWEEN_STEPS);
  77.         }
  78.     }
  79.  
  80.     private void init() {
  81.         mCoub = new int[mSize][mSize];
  82.         mCoub[0][0] = FIRST_CELL;
  83.         mCurrentNumber = FIRST_CELL;
  84.         mDirection = DIRECTION_RIGHT;
  85.     }
  86.  
  87.     private boolean goAndPut(int step) {
  88.         step++;
  89.         if (step > 3) {
  90.             return false;
  91.         }
  92.         int lastX = mCurrentX, lastY = mCurrentY;
  93.         switch (mDirection) {
  94.         case DIRECTION_RIGHT:
  95.             if (mCurrentX < mSize - 1 && mCoub[mCurrentX + 1][mCurrentY] == EMPTY_CELL) {
  96.                 mCoub[++mCurrentX][mCurrentY] = ++mCurrentNumber;
  97.             } else {
  98.                 mDirection = DIRECTION_BOTTOM;
  99.                 return goAndPut(step);
  100.             }
  101.             break;
  102.         case DIRECTION_BOTTOM:
  103.             if (mCurrentY < mSize - 1 && mCoub[mCurrentX][mCurrentY + 1] == EMPTY_CELL) {
  104.                 mCoub[mCurrentX][++mCurrentY] = ++mCurrentNumber;
  105.             } else {
  106.                 mDirection = DIRECTION_LEFT;
  107.                 return goAndPut(step);
  108.             }
  109.             break;
  110.         case DIRECTION_LEFT:
  111.             if (mCurrentX > 0 && mCoub[mCurrentX - 1][mCurrentY] == EMPTY_CELL) {
  112.                 mCoub[--mCurrentX][mCurrentY] = ++mCurrentNumber;
  113.             } else {
  114.                 mDirection = DIRECTION_TOP;
  115.                 return goAndPut(step);
  116.             }
  117.             break;
  118.         case DIRECTION_TOP:
  119.             if (mCurrentY > 0 && mCoub[mCurrentX][mCurrentY - 1] == EMPTY_CELL) {
  120.                 mCoub[mCurrentX][--mCurrentY] = ++mCurrentNumber;
  121.             } else {
  122.                 mDirection = DIRECTION_RIGHT;
  123.                 return goAndPut(step);
  124.             }
  125.             break;
  126.         }
  127.         return !(lastX == mCurrentX && lastY == mCurrentY);
  128.     }
  129.  
  130.     @Override
  131.     public String toString() {
  132.         StringBuilder builder = new StringBuilder();
  133.         builder.append(mCurrentNumber + "\n");
  134.         for (int i = 0; i < mCoub.length; i++) {
  135.             for (int j = 0; j < mCoub[i].length; j++) {
  136.                 builder.append(String.format("%4s", Integer.toString(mCoub[i][j])));
  137.             }
  138.             builder.append("\n");
  139.         }
  140.         return builder.toString();
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement