Advertisement
Guest User

CellVector.java

a guest
May 15th, 2021
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. //  Sokoban state space explorer.
  2. //  Discussion: https://dxdy.ru/topic144781.html
  3.  
  4. public class CellVector {
  5.    
  6.     private static final CellVector [] neighbourShifts = {
  7.             new CellVector (-1,  0),
  8.             new CellVector ( 0, -1),
  9.             new CellVector ( 1,  0),
  10.             new CellVector ( 0,  1),
  11.             new CellVector (-1, -1),
  12.             new CellVector ( 1, -1),
  13.             new CellVector (-1,  1),
  14.             new CellVector ( 1,  1),
  15.     };
  16.    
  17.     private static final char [] moveLetter = {'l', 'u', 'r', 'd'};
  18.    
  19.     private static final int [] rearIndex = {2, 3, 0, 1, 7, 6, 5, 4};
  20.    
  21.     private int x, y;
  22.    
  23.     public CellVector (int ax, int ay) {
  24.         x = ax;
  25.         y = ay;
  26.     }
  27.    
  28.     public CellVector (int [] arg) {
  29.         x = arg [0];
  30.         y = arg [1];
  31.     }
  32.    
  33.     public CellVector sumWith (CellVector arg) {
  34.         return new CellVector (x + arg .x, y + arg .y);
  35.     }
  36.    
  37.     public CellVector neighbour (int index) {
  38.         return sumWith (neighbourShifts [index]);
  39.     }
  40.    
  41.     public CellVector rearNeighbour (int index) {
  42.         return sumWith (neighbourShifts [rearIndex [index]]);
  43.     }
  44.    
  45.     public char moveLetter (int index) {
  46.         return moveLetter [index];
  47.     }
  48.    
  49.     public int getCell (int [] [] array) {
  50.         return array [y] [x];
  51.     }
  52.    
  53.     public void setCell (int [] [] array, int value) {
  54.         array [y] [x] = value;
  55.     }
  56.    
  57.     public short getCell (short [] [] array) {
  58.         return array [y] [x];
  59.     }
  60.    
  61.     public void setCell (short [] [] array, short value) {
  62.         array [y] [x] = value;
  63.     }
  64.    
  65.     public boolean getCell (boolean [] [] array) {
  66.         return array [y] [x];
  67.     }
  68.    
  69.     public void setCell (boolean [] [] array, boolean value) {
  70.         array [y] [x] = value;
  71.     }
  72.    
  73.     public int getX () {
  74.         return x;
  75.     }
  76.    
  77.     public int getY () {
  78.         return y;
  79.     }
  80.    
  81.     public boolean outOfBorder (CellVector border) {
  82.         if (0 > x || 0 > y) {
  83.             return true;
  84.         }
  85.         if (border .x <= x || border .y <= y) {
  86.             return true;
  87.         }
  88.         return false;
  89.     }
  90.    
  91.     public boolean equals (CellVector vector) {
  92.         return x == vector .x && y == vector .y;
  93.     }
  94.    
  95.     public String toString () {
  96.         return "(" + x + ", " + y + ")";
  97.     }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement