From 40b90b3647225c4446ef9db523cd474368ef26dd Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 22 Feb 2016 16:23:57 +0100 Subject: Implement the SubCell class --- src/ch/epfl/xblast/Cell.java | 2 +- src/ch/epfl/xblast/SubCell.java | 159 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 src/ch/epfl/xblast/SubCell.java diff --git a/src/ch/epfl/xblast/Cell.java b/src/ch/epfl/xblast/Cell.java index 813bd5f..ef5ff60 100644 --- a/src/ch/epfl/xblast/Cell.java +++ b/src/ch/epfl/xblast/Cell.java @@ -102,7 +102,7 @@ public final class Cell { * @param n the number to normalize (the dividend) * @return the normalized value */ - private static int normalize(int max, int n) { + protected static int normalize(int max, int n) { return Math.floorMod(n, max); } diff --git a/src/ch/epfl/xblast/SubCell.java b/src/ch/epfl/xblast/SubCell.java new file mode 100644 index 0000000..2731029 --- /dev/null +++ b/src/ch/epfl/xblast/SubCell.java @@ -0,0 +1,159 @@ +package ch.epfl.xblast; + +/** + * A SubCell. + * + * @author Pacien TRAN-GIRARD (261948) + */ +public final class SubCell { + + /** + * The number of x-subdivisions of each Cell. + */ + public static final int SUB_COL_DIVISIONS = 16; + + /** + * The number of y-subdivisions of each Cell. + */ + public static final int SUB_ROW_DIVISIONS = 16; + + /** + * The width of the board (total of sub-columns). + */ + public static final int SUB_COLUMNS = SUB_COL_DIVISIONS * Cell.COLUMNS; + + /** + * The height of the board (total of sub-rows). + */ + public static final int SUB_ROWS = SUB_ROW_DIVISIONS * Cell.ROWS; + + /** + * Returns the central SubCell of the given Cell. + * + * @param cell the reference Cell + * @return the central SubCell + */ + public static SubCell centralSubCellOf(Cell cell) { + return new SubCell( + cell.x() * SUB_COL_DIVISIONS + (SUB_COL_DIVISIONS / 2), + cell.y() * SUB_ROW_DIVISIONS + (SUB_ROW_DIVISIONS / 2) + ); + } + + /** + * The coordinates of the SubCell. + */ + private int x, y; + + /** + * Instantiates a new SubCell with the given coordinates. + * + * @param x the x-coordinate + * @param y the y-coordinate + */ + public SubCell(int x, int y) { + this.x = Cell.normalize(SUB_COLUMNS, x); + this.y = Cell.normalize(SUB_ROWS, y); + } + + /** + * Returns the normalized x-coordinate of the SubCell. + * + * @return the x-coordinate + */ + public int x() { + return this.x; + } + + /** + * Returns the normalized y-coordinate of the SubCell. + * + * @return the y-coordinate + */ + public int y() { + return this.y; + } + + /** + * Computes the Manhattan distance to the given SubCell. + * + * @param subCell the other SubCell + * @return the distance + */ + private int distanceTo(SubCell subCell) { + return Math.abs(this.x - subCell.x) + Math.abs(this.y - subCell.y); + } + + /** + * Returns the Manhattan distance to the closest central SubCell. + * + * @return the distance + */ + public int distanceToCentral() { + return this.distanceTo(centralSubCellOf(this.containingCell())); + } + + /** + * Returns T(this SubCell is central). + * + * @return T(this SubCell is central) + */ + public boolean isCentral() { + return this.distanceToCentral() == 0; + } + + /** + * Returns the neighboring SubCell at the given Direction. + * + * @param dir the Direction + * @return the neighboring SubCell + */ + public SubCell neighbor(Direction dir) { + switch (dir) { + case N: + return new SubCell(this.x, this.y - 1); + case S: + return new SubCell(this.x, this.y + 1); + case E: + return new SubCell(this.x + 1, this.y); + case W: + return new SubCell(this.x - 1, this.y); + default: + return null; + } + } + + /** + * Returns the Cell containing this SubCell. + * + * @return the containing Cell + */ + public Cell containingCell() { + return new Cell(this.x / SUB_COL_DIVISIONS, this.y / SUB_ROW_DIVISIONS); + } + + /** + * Returns T(the given Object is equal to this SubCell (have the same coordinates)). + * + * @param that the Object to compare against + * @return T(the given Object is equal to this SubCell) + */ + @Override + public boolean equals(Object that) { + if (that == null) return false; + if (that == this) return true; + if (!(that instanceof SubCell)) return false; + return (((SubCell) that).x == this.x && ((SubCell) that).y == this.y); + } + + /** + * Returns a String representation of the coordinates of the SubCell. + * + * @return a String representation of the coordinates of the SubCell. + */ + @Override + public String toString() { + return String.format("(%d,%d)", this.x, this.y); + } + +} -- cgit v1.2.3