diff options
Diffstat (limited to 'src/ch/epfl')
-rw-r--r-- | src/ch/epfl/xblast/Cell.java | 2 | ||||
-rw-r--r-- | src/ch/epfl/xblast/SubCell.java | 159 |
2 files changed, 160 insertions, 1 deletions
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 { | |||
102 | * @param n the number to normalize (the dividend) | 102 | * @param n the number to normalize (the dividend) |
103 | * @return the normalized value | 103 | * @return the normalized value |
104 | */ | 104 | */ |
105 | private static int normalize(int max, int n) { | 105 | protected static int normalize(int max, int n) { |
106 | return Math.floorMod(n, max); | 106 | return Math.floorMod(n, max); |
107 | } | 107 | } |
108 | 108 | ||
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 @@ | |||
1 | package ch.epfl.xblast; | ||
2 | |||
3 | /** | ||
4 | * A SubCell. | ||
5 | * | ||
6 | * @author Pacien TRAN-GIRARD (261948) | ||
7 | */ | ||
8 | public final class SubCell { | ||
9 | |||
10 | /** | ||
11 | * The number of x-subdivisions of each Cell. | ||
12 | */ | ||
13 | public static final int SUB_COL_DIVISIONS = 16; | ||
14 | |||
15 | /** | ||
16 | * The number of y-subdivisions of each Cell. | ||
17 | */ | ||
18 | public static final int SUB_ROW_DIVISIONS = 16; | ||
19 | |||
20 | /** | ||
21 | * The width of the board (total of sub-columns). | ||
22 | */ | ||
23 | public static final int SUB_COLUMNS = SUB_COL_DIVISIONS * Cell.COLUMNS; | ||
24 | |||
25 | /** | ||
26 | * The height of the board (total of sub-rows). | ||
27 | */ | ||
28 | public static final int SUB_ROWS = SUB_ROW_DIVISIONS * Cell.ROWS; | ||
29 | |||
30 | /** | ||
31 | * Returns the central SubCell of the given Cell. | ||
32 | * | ||
33 | * @param cell the reference Cell | ||
34 | * @return the central SubCell | ||
35 | */ | ||
36 | public static SubCell centralSubCellOf(Cell cell) { | ||
37 | return new SubCell( | ||
38 | cell.x() * SUB_COL_DIVISIONS + (SUB_COL_DIVISIONS / 2), | ||
39 | cell.y() * SUB_ROW_DIVISIONS + (SUB_ROW_DIVISIONS / 2) | ||
40 | ); | ||
41 | } | ||
42 | |||
43 | /** | ||
44 | * The coordinates of the SubCell. | ||
45 | */ | ||
46 | private int x, y; | ||
47 | |||
48 | /** | ||
49 | * Instantiates a new SubCell with the given coordinates. | ||
50 | * | ||
51 | * @param x the x-coordinate | ||
52 | * @param y the y-coordinate | ||
53 | */ | ||
54 | public SubCell(int x, int y) { | ||
55 | this.x = Cell.normalize(SUB_COLUMNS, x); | ||
56 | this.y = Cell.normalize(SUB_ROWS, y); | ||
57 | } | ||
58 | |||
59 | /** | ||
60 | * Returns the normalized x-coordinate of the SubCell. | ||
61 | * | ||
62 | * @return the x-coordinate | ||
63 | */ | ||
64 | public int x() { | ||
65 | return this.x; | ||
66 | } | ||
67 | |||
68 | /** | ||
69 | * Returns the normalized y-coordinate of the SubCell. | ||
70 | * | ||
71 | * @return the y-coordinate | ||
72 | */ | ||
73 | public int y() { | ||
74 | return this.y; | ||
75 | } | ||
76 | |||
77 | /** | ||
78 | * Computes the Manhattan distance to the given SubCell. | ||
79 | * | ||
80 | * @param subCell the other SubCell | ||
81 | * @return the distance | ||
82 | */ | ||
83 | private int distanceTo(SubCell subCell) { | ||
84 | return Math.abs(this.x - subCell.x) + Math.abs(this.y - subCell.y); | ||
85 | } | ||
86 | |||
87 | /** | ||
88 | * Returns the Manhattan distance to the closest central SubCell. | ||
89 | * | ||
90 | * @return the distance | ||
91 | */ | ||
92 | public int distanceToCentral() { | ||
93 | return this.distanceTo(centralSubCellOf(this.containingCell())); | ||
94 | } | ||
95 | |||
96 | /** | ||
97 | * Returns T(this SubCell is central). | ||
98 | * | ||
99 | * @return T(this SubCell is central) | ||
100 | */ | ||
101 | public boolean isCentral() { | ||
102 | return this.distanceToCentral() == 0; | ||
103 | } | ||
104 | |||
105 | /** | ||
106 | * Returns the neighboring SubCell at the given Direction. | ||
107 | * | ||
108 | * @param dir the Direction | ||
109 | * @return the neighboring SubCell | ||
110 | */ | ||
111 | public SubCell neighbor(Direction dir) { | ||
112 | switch (dir) { | ||
113 | case N: | ||
114 | return new SubCell(this.x, this.y - 1); | ||
115 | case S: | ||
116 | return new SubCell(this.x, this.y + 1); | ||
117 | case E: | ||
118 | return new SubCell(this.x + 1, this.y); | ||
119 | case W: | ||
120 | return new SubCell(this.x - 1, this.y); | ||
121 | default: | ||
122 | return null; | ||
123 | } | ||
124 | } | ||
125 | |||
126 | /** | ||
127 | * Returns the Cell containing this SubCell. | ||
128 | * | ||
129 | * @return the containing Cell | ||
130 | */ | ||
131 | public Cell containingCell() { | ||
132 | return new Cell(this.x / SUB_COL_DIVISIONS, this.y / SUB_ROW_DIVISIONS); | ||
133 | } | ||
134 | |||
135 | /** | ||
136 | * Returns T(the given Object is equal to this SubCell (have the same coordinates)). | ||
137 | * | ||
138 | * @param that the Object to compare against | ||
139 | * @return T(the given Object is equal to this SubCell) | ||
140 | */ | ||
141 | @Override | ||
142 | public boolean equals(Object that) { | ||
143 | if (that == null) return false; | ||
144 | if (that == this) return true; | ||
145 | if (!(that instanceof SubCell)) return false; | ||
146 | return (((SubCell) that).x == this.x && ((SubCell) that).y == this.y); | ||
147 | } | ||
148 | |||
149 | /** | ||
150 | * Returns a String representation of the coordinates of the SubCell. | ||
151 | * | ||
152 | * @return a String representation of the coordinates of the SubCell. | ||
153 | */ | ||
154 | @Override | ||
155 | public String toString() { | ||
156 | return String.format("(%d,%d)", this.x, this.y); | ||
157 | } | ||
158 | |||
159 | } | ||