Advertisement
Guest User

CellVector.java

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