Advertisement
Guest User

SquarePrinter

a guest
Oct 12th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. import java.awt.Point;
  2.  
  3. /**
  4.  * Oh, you thought creating squares is easy?!
  5.  * Not here, no way!
  6.  *
  7.  */
  8. public class Challenge1 {
  9.    
  10.     public static void main(String[] args) {
  11.         // I'm too lazy to check if arguments are formatted correctly
  12.         // do that yourself!
  13.         int size = Integer.parseInt(args[0]);
  14.         if(size % 2 == 0 || size < 0) {
  15.             System.err.println("Input must be an odd positive number");
  16.             System.exit(1);
  17.         }
  18.        
  19.         printSquare(size);
  20.     }
  21.    
  22.    
  23.     /**
  24.      * Print sweet squares such as this
  25.      *
  26.      * 111 112 113 114 115 116 117 118 119 120 121
  27.      * 110  73  74  75  76  77  78  79  80  81  82
  28.      * 109  72  43  44  45  46  47  48  49  50  83
  29.      * 108  71  42  21  22  23  24  25  26  51  84
  30.      * 107  70  41  20   7   8   9  10  27  52  85
  31.      * 106  69  40  19   6   1   2  11  28  53  86
  32.      * 105  68  39  18   5   4   3  12  29  54  87
  33.      * 104  67  38  17  16  15  14  13  30  55  88
  34.      * 103  66  37  36  35  34  33  32  31  56  89
  35.      * 102  65  64  63  62  61  60  59  58  57  90
  36.      * 101 100  99  98  97  96  95  94  93  92  91
  37.      *
  38.      * whaaaaat how did he do that? will this get me all the ladies? let's wait and see!
  39.      *
  40.      * @param size
  41.      */
  42.     private static void printSquare(int size) {
  43.        
  44.         // initialize arrays
  45.         int[][] square = new int[size][];
  46.        
  47.         for(int line = 0; line < size; line++) {
  48.             square[line] = new int[size];
  49.         }
  50.        
  51.         // current position, direction to walk to
  52.         Point pos = new Point(size / 2, size / 2);
  53.         Point dir = new Point(1, 0);
  54.        
  55.         // change direction based on step count
  56.         // first two times turn right on every single step
  57.         // then two times on every second step (done through incrementing stepReset by 2)
  58.         // then two times on every third step...
  59.         int stepReset = 2;
  60.         int stepCount = stepReset;
  61.        
  62.         for(int num = 1; num <= size * size; num++) {
  63.             square[pos.y][pos.x] = num;
  64.             pos.translate(dir.x, dir.y);
  65.            
  66.             // if step count is half of stepReset or 0, turn right
  67.             stepCount--;
  68.             if(stepCount % (stepReset / 2) == 0) {
  69.                 int tmp = dir.x;
  70.                 dir.x = -dir.y;
  71.                 dir.y = tmp;
  72.                
  73.                 // turned right twice with same length
  74.                 // add 1 to the amount of steps it takes to turn right again
  75.                 if(stepCount == 0) {
  76.                     stepReset += 2;
  77.                     stepCount = stepReset;
  78.                 }
  79.             }
  80.         }
  81.        
  82.         // right align numbers. if longest number has three digits, 1 will be displayed as "  1"
  83.         String digits = "%" + ((int) Math.log10(size * size) + 1) + "d ";
  84.         for(int[] y : square) {
  85.             for(int x : y) {
  86.                 System.out.printf(digits, x);
  87.             }
  88.            
  89.             System.out.println();
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement