BaR5uk

CellVector.java

Dec 4th, 2021 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1. //      Sokoban puzzles auto generator
  2. //      https://dxdy.ru/post1541620.html#p1541620
  3. //      2021 dec B@R5uk
  4.  
  5. public class CellVector {
  6.    
  7.     private static final CellVector [] neighbourShifts = {
  8.             new CellVector (-1,  0),
  9.             new CellVector ( 0, -1),
  10.             new CellVector ( 1,  0),
  11.             new CellVector ( 0,  1),
  12.             new CellVector (-1, -1),
  13.             new CellVector ( 1, -1),
  14.             new CellVector (-1,  1),
  15.             new CellVector ( 1,  1),
  16.     };
  17.    
  18.     private static final char [] moveLetter = {'l', 'u', 'r', 'd'};
  19.     private static final char [] moveUpperLetter = {'L', 'U', 'R', 'D'};
  20.    
  21.     private static final int [] rearIndex = {2, 3, 0, 1, 7, 6, 5, 4};
  22.    
  23.     private int x, y;
  24.    
  25.     public CellVector (int ax, int ay) {
  26.         x = ax;
  27.         y = ay;
  28.     }
  29.    
  30.     public CellVector (int [] arg) {
  31.         x = arg [0];
  32.         y = arg [1];
  33.     }
  34.    
  35.     public CellVector sumWith (CellVector arg) {
  36.         return new CellVector (x + arg .x, y + arg .y);
  37.     }
  38.    
  39.     public CellVector neighbour (int index) {
  40.         return sumWith (neighbourShifts [index]);
  41.     }
  42.    
  43.     public CellVector rearNeighbour (int index) {
  44.         return sumWith (neighbourShifts [rearIndex [index]]);
  45.     }
  46.    
  47.     public static char moveLetter (int index) {
  48.         return moveLetter [index];
  49.     }
  50.    
  51.     public static char moveUpperLetter (int index) {
  52.         return moveUpperLetter [index];
  53.     }
  54.    
  55.     public static int reverseMove (int index) {
  56.         return rearIndex [index];
  57.     }
  58.    
  59.     public static char reverseMoveLetter (int index) {
  60.         return moveLetter [rearIndex [index]];
  61.     }
  62.    
  63.     public static char reverseMoveUpperLetter (int index) {
  64.         return moveUpperLetter [rearIndex [index]];
  65.     }
  66.    
  67.     public int getCell (int [] [] array) {
  68.         return array [y] [x];
  69.     }
  70.    
  71.     public void setCell (int [] [] array, int value) {
  72.         array [y] [x] = value;
  73.     }
  74.    
  75.     public short getCell (short [] [] array) {
  76.         return array [y] [x];
  77.     }
  78.    
  79.     public void setCell (short [] [] array, short value) {
  80.         array [y] [x] = value;
  81.     }
  82.    
  83.     public boolean getCell (boolean [] [] array) {
  84.         return array [y] [x];
  85.     }
  86.    
  87.     public void setCell (boolean [] [] array, boolean value) {
  88.         array [y] [x] = value;
  89.     }
  90.    
  91.     public char getCell (char [] [] array) {
  92.         return array [y] [x];
  93.     }
  94.    
  95.     public void setCell (char [] [] array, char value) {
  96.         array [y] [x] = value;
  97.     }
  98.    
  99.     public int getX () {
  100.         return x;
  101.     }
  102.    
  103.     public int getY () {
  104.         return y;
  105.     }
  106.    
  107.     public boolean outOfBorder (CellVector border) {
  108.         if (0 > x || 0 > y) {
  109.             return true;
  110.         }
  111.         if (border .x <= x || border .y <= y) {
  112.             return true;
  113.         }
  114.         return false;
  115.     }
  116.    
  117.     public boolean equals (CellVector vector) {
  118.         return x == vector .x && y == vector .y;
  119.     }
  120.    
  121.     @Override
  122.     public String toString () {
  123.         return "(" + x + ", " + y + ")";
  124.     }
  125. }
  126.  
Add Comment
Please, Sign In to add comment