Poly.java (2504B)
1 package blocks; 2 3 import java.awt.Color; 4 5 public abstract class Poly implements Tileable { 6 private int posX, posY; 7 private boolean occupied; 8 9 private Color color; 10 11 /** 12 * Instantiates a new poly at given coordinates. 13 * @param x spawn position. 14 * @param y spawn position. 15 */ 16 public Poly(int x, int y) { 17 this.posX = x; 18 this.posY = y; 19 occupied = false; 20 } 21 22 /** 23 * Rotates a given matrix clockwise. 24 * Note: all rows and columns must be of the same cardinality. 25 * @param shape, the matrix to rotate 26 * @return the rotated matrix 27 */ 28 protected int[][] rotateClockwise(int[][] shape) { 29 int h = shape.length; 30 int w = shape[0].length; 31 int[][] rotatedShape = new int[w][h]; 32 for (int y = 0; y < h; y++) { 33 for (int x = 0; x < w; x++) { 34 rotatedShape[x][h - 1 - y] = shape[y][x]; 35 } 36 } 37 return rotatedShape; 38 } 39 40 /** 41 * Rotates a given matrix counter clockwise. 42 * Note: all rows and columns must be of the same cardinality. 43 * @param shape, the matrix to rotate 44 * @return the rotated matrix 45 */ 46 protected int[][] rotateCounterClockwise(int[][] shape) { 47 int h = shape.length; 48 int w = shape[0].length; 49 int[][] rotatedShape = new int[w][h]; 50 for (int y = 0; y < h; y++) { 51 for (int x = 0; x < w; x++) { 52 rotatedShape[w - 1 - x][y] = shape[y][x]; 53 } 54 } 55 return rotatedShape; 56 } 57 58 /** 59 * Abstract rotate function used by the subclasses of Poly. 60 */ 61 public abstract void rotateRight(); 62 63 /** 64 * Abstract rotate function used by the subclasses of Poly. 65 */ 66 public abstract void rotateLeft(); 67 68 /** 69 * Moves the poly in the desired direction. 70 * @param distance to move in x direction. 71 * @param distance to move in y direction. 72 */ 73 public void move(int x, int y) { 74 this.posX += x; 75 this.posY += y; 76 } 77 78 /** 79 * Get the position of an instantiated poly. 80 * @return 81 */ 82 public int[] getPos() { 83 int[] pos = { posX, posY }; 84 return pos; 85 } 86 87 /** 88 * Get the shape of a Poly, only used by subclasses. 89 * @return 90 */ 91 public abstract int[][] getShape(); 92 93 /** 94 * Set the color of the poly. 95 */ 96 public void setColor(Color color) { 97 this.color = color; 98 } 99 100 /** 101 * Get the color of the poly. 102 */ 103 public Color getColor() { 104 // if no other value is set, red will be the default color. 105 return color; 106 } 107 108 /** 109 * @return is it occupied. 110 */ 111 public boolean isOccupied() { 112 return occupied; 113 } 114 115 /** 116 * Set whether or not the Square is occupied. 117 */ 118 public void setOccupied(boolean occupied) { 119 this.occupied = occupied; 120 } 121 }