From 9809f6e226d593c1e717b8d6ab576d986da9cbe3 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 22 Feb 2016 14:45:17 +0100 Subject: Implement the Cell class --- src/ch/epfl/xblast/Cell.java | 196 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 src/ch/epfl/xblast/Cell.java (limited to 'src/ch/epfl') diff --git a/src/ch/epfl/xblast/Cell.java b/src/ch/epfl/xblast/Cell.java new file mode 100644 index 0000000..d5b15e7 --- /dev/null +++ b/src/ch/epfl/xblast/Cell.java @@ -0,0 +1,196 @@ +package ch.epfl.xblast; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * A Cell. + * + * @author Pacien TRAN-GIRARD (261948) + */ +public final class Cell { + + /** + * The width of the board (number of columns). + */ + public static final int COLUMNS = 15; + + /** + * The height of the board (number of rows). + */ + public static final int ROWS = 13; + + /** + * The total number of Cell-s of the board. + */ + public static final int COUNT = COLUMNS * ROWS; + + /** + * The list of the board's Cell's, major-ordered. + */ + public static final List ROW_MAJOR_ORDER = Collections.unmodifiableList(rowMajorOrder()); + + /** + * The list of the board's Cell-s, spiral-ordered. + */ + public static final List SPIRAL_ORDER = Collections.unmodifiableList(spiralOrder()); + + /** + * Builds a major-ordered list of Cell-s. + * + * @return the list of Cell-s + */ + private static ArrayList rowMajorOrder() { + ArrayList list = new ArrayList<>(COUNT); + + for (int row = 0; row < ROWS; ++row) + for (int col = 0; col < COLUMNS; ++col) + list.add(new Cell(col, row)); + + return list; + } + + /** + * Builds a spiral-ordered list of Cell-s. + * + * @return the list of Cell-s + */ + private static ArrayList spiralOrder() { + ArrayList list = new ArrayList<>(COUNT); + ArrayList ix = range(0, COLUMNS, 1); + ArrayList iy = range(0, ROWS, 1); + boolean horizontal = true; + + while (!ix.isEmpty() && !iy.isEmpty()) { + ArrayList i1 = horizontal ? ix : iy; + ArrayList i2 = horizontal ? iy : ix; + int c2 = i2.remove(0); + for (int c1 : i1) + list.add(horizontal ? new Cell(c1, c2) : new Cell(c2, c1)); + + Collections.reverse(i1); + horizontal = !horizontal; + } + + return list; + } + + /** + * Creates a list containing an arithmetic progression. + * + * @param from the starting value + * @param to the boundary value + * @param step the arithmetic step + * @return the arithmetic progression + */ + private static ArrayList range(int from, int to, int step) { + int n = (to - from) / step; + ArrayList list = new ArrayList<>(n); + + for (int i = 0; i < n; ++i) + list.add(from + (i * step)); + + return list; + + } + + /** + * Normalizes the given number (using the integer floor modulus). + * + * @param max the maximum (the divisor) + * @param n the number to normalize (the dividend) + * @return the normalized value + */ + private static int normalize(int max, int n) { + return Math.floorMod(n, max); + } + + /** + * The coordinates of the Cell. + */ + private final int x, y; + + /** + * Instantiates a new Cell with the given coordinates. + * + * @param x the x-coordinate + * @param y the y-coordinate + */ + public Cell(int x, int y) { + this.x = normalize(COLUMNS, x); + this.y = normalize(ROWS, y); + } + + /** + * Returns the normalized x-coordinate of the Cell. + * + * @return the x-coordinate + */ + public int x() { + return this.x; + } + + /** + * Returns the normalized y-coordinate of the Cell. + * + * @return the y-coordinate + */ + public int y() { + return this.y; + } + + /** + * Returns the index of the Cell (major ordered). + * + * @return the index of the Cell + */ + public int rowMajorIndex() { + return this.x * ROWS + this.y; + } + + /** + * Returns the neighboring Cell at the given Direction. + * + * @param dir the Direction + * @return the neighboring Cell + */ + public Cell neighbor(Direction dir) { + switch (dir) { + case N: + return new Cell(this.x, this.y - 1); + case S: + return new Cell(this.x, this.y + 1); + case E: + return new Cell(this.x - 1, this.y); + case W: + return new Cell(this.x + 1, this.y); + default: + return null; + } + } + + /** + * Returns T(the given Object is equal to this Cell (have the same coordinates)). + * + * @param that the Object to compare against + * @return T(the given Object is equal to this Cell) + */ + @Override + public boolean equals(Object that) { + if (!super.equals(that)) return false; + if (!(that instanceof Cell)) return false; + return (((Cell) that).x == this.x && ((Cell) that).y == this.y); + } + + /** + * Returns a String representation of the coordinates of the Cell. + * + * @return a String representation of the coordinates of the Cell. + */ + @Override + public String toString() { + return String.format("(%d,%d)", this.x, this.y); + } + +} -- cgit v1.2.3