aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl/xblast/SubCell.java
blob: fd9db0b364fa83e70d6376a102c556050169e335 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package ch.epfl.xblast;

/**
 * A SubCell.
 *
 * @author Pacien TRAN-GIRARD (261948)
 * @author Timothée FLOURE (257420)
 */
public final class SubCell implements Comparable<SubCell> {

    /**
     * The number of x-subdivisions of each Cell.
     */
    private static final int SUB_COL_DIVISIONS = 16;

    /**
     * The number of y-subdivisions of each Cell.
     */
    private static final int SUB_ROW_DIVISIONS = 16;

    /**
     * The width of the board (total of sub-columns).
     */
    private static final int SUB_COLUMNS = SUB_COL_DIVISIONS * Cell.COLUMNS;

    /**
     * The height of the board (total of sub-rows).
     */
    private static final int SUB_ROWS = SUB_ROW_DIVISIONS * Cell.ROWS;

    /**
     * The coordinates of the SubCell.
     */
    private final 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;
    }

    /**
     * Returns T(this SubCell is central).
     *
     * @return T(this SubCell is central)
     */
    public boolean isCentral() {
        return this.distanceToCentral() == 0;
    }

    /**
     * Returns the Manhattan distance to the closest central SubCell.
     *
     * @return the distance
     */
    public int distanceToCentral() {
        return this.distanceTo(centralSubCellOf(this.containingCell()));
    }

    /**
     * Computes the Manhattan distance to the given SubCell.
     *
     * @param subCell the other SubCell
     * @return the distance
     */
    public int distanceTo(SubCell subCell) {
        return Math.abs(this.x - subCell.x) + Math.abs(this.y - subCell.y);
    }

    /**
     * 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)
        );
    }

    /**
     * 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 the neighboring SubCell at the given Direction.
     *
     * @param dir the Direction
     * @return the neighboring SubCell
     */
    public SubCell neighbor(Direction dir) {
        return new SubCell(this.x + dir.xVector(), this.y + dir.yVector());
    }

    /**
     * Returns the hash code of this SubCell, given by its row-major index.
     *
     * @return the hash code
     */
    @Override
    public int hashCode() {
        return this.rowMajorIndex();
    }

    /**
     * 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).rowMajorIndex() == this.rowMajorIndex());
    }

    /**
     * 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);
    }

    /**
     * Returns the index of the SubCell (major ordered).
     *
     * @return the index of the SubCell
     */
    private int rowMajorIndex() {
        return this.y * SUB_COLUMNS + this.x;
    }

    /**
     * Compares two SubCells relatively to their row major indexes.
     *
     * @param subCell the other SubCell to compare
     * @return the comparison result
     */
    @Override
    public int compareTo(SubCell subCell) {
        return Integer.compare(this.rowMajorIndex(), subCell.rowMajorIndex());
    }

}