aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-02-22 16:39:45 +0100
committerPacien TRAN-GIRARD2016-02-22 16:39:45 +0100
commit7574a77482936bf2c56ee904f7d91d64571b0b12 (patch)
tree4d133ac5e5d4e79f9e51450fe3187f9ae78f3173
parent40b90b3647225c4446ef9db523cd474368ef26dd (diff)
downloadxblast-7574a77482936bf2c56ee904f7d91d64571b0b12.tar.gz
Refactor neighbor computation using directional vectors
-rw-r--r--src/ch/epfl/xblast/Cell.java13
-rw-r--r--src/ch/epfl/xblast/Direction.java32
-rw-r--r--src/ch/epfl/xblast/SubCell.java13
-rw-r--r--test/ch/epfl/xblast/DirectionTest.java26
4 files changed, 52 insertions, 32 deletions
diff --git a/src/ch/epfl/xblast/Cell.java b/src/ch/epfl/xblast/Cell.java
index ef5ff60..9a17f6a 100644
--- a/src/ch/epfl/xblast/Cell.java
+++ b/src/ch/epfl/xblast/Cell.java
@@ -156,18 +156,7 @@ public final class Cell {
156 * @return the neighboring Cell 156 * @return the neighboring Cell
157 */ 157 */
158 public Cell neighbor(Direction dir) { 158 public Cell neighbor(Direction dir) {
159 switch (dir) { 159 return new Cell(this.x + dir.xVector(), this.y + dir.yVector());
160 case N:
161 return new Cell(this.x, this.y - 1);
162 case S:
163 return new Cell(this.x, this.y + 1);
164 case E:
165 return new Cell(this.x + 1, this.y);
166 case W:
167 return new Cell(this.x - 1, this.y);
168 default:
169 return null;
170 }
171 } 160 }
172 161
173 /** 162 /**
diff --git a/src/ch/epfl/xblast/Direction.java b/src/ch/epfl/xblast/Direction.java
index cd4822e..e9d5961 100644
--- a/src/ch/epfl/xblast/Direction.java
+++ b/src/ch/epfl/xblast/Direction.java
@@ -66,4 +66,36 @@ public enum Direction {
66 return that == this || that == this.opposite(); 66 return that == this || that == this.opposite();
67 } 67 }
68 68
69 /**
70 * Returns the x-coordinate of the normed vector representation of the Direction.
71 *
72 * @return the x-coordinate
73 */
74 public int xVector() {
75 switch (this) {
76 case W:
77 return -1;
78 case E:
79 return +1;
80 default:
81 return 0;
82 }
83 }
84
85 /**
86 * Returns the x-coordinate of the normed vector representation of the Direction.
87 *
88 * @return the y-coordinate
89 */
90 public int yVector() {
91 switch (this) {
92 case N:
93 return -1;
94 case S:
95 return +1;
96 default:
97 return 0;
98 }
99 }
100
69} 101}
diff --git a/src/ch/epfl/xblast/SubCell.java b/src/ch/epfl/xblast/SubCell.java
index 2731029..b4f9c91 100644
--- a/src/ch/epfl/xblast/SubCell.java
+++ b/src/ch/epfl/xblast/SubCell.java
@@ -109,18 +109,7 @@ public final class SubCell {
109 * @return the neighboring SubCell 109 * @return the neighboring SubCell
110 */ 110 */
111 public SubCell neighbor(Direction dir) { 111 public SubCell neighbor(Direction dir) {
112 switch (dir) { 112 return new SubCell(this.x + dir.xVector(), this.y + dir.yVector());
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 } 113 }
125 114
126 /** 115 /**
diff --git a/test/ch/epfl/xblast/DirectionTest.java b/test/ch/epfl/xblast/DirectionTest.java
index a2ac9cd..0ebb3b7 100644
--- a/test/ch/epfl/xblast/DirectionTest.java
+++ b/test/ch/epfl/xblast/DirectionTest.java
@@ -1,21 +1,23 @@
1package ch.epfl.xblast; 1package ch.epfl.xblast;
2 2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertFalse;
5import static org.junit.Assert.assertTrue;
6
7import org.junit.Test; 3import org.junit.Test;
8 4
5import static org.junit.Assert.*;
6
7/**
8 * @author EPFL
9 * @author Pacien TRAN-GIRARD (261948)
10 */
9public class DirectionTest { 11public class DirectionTest {
10 @Test 12 @Test
11 public void oppositeOfOppositeIsIdentity() { 13 public void oppositeOfOppositeIsIdentity() {
12 for (Direction d: Direction.values()) 14 for (Direction d : Direction.values())
13 assertEquals(d, d.opposite().opposite()); 15 assertEquals(d, d.opposite().opposite());
14 } 16 }
15 17
16 @Test 18 @Test
17 public void oppositeIsTwoStepsAway() { 19 public void oppositeIsTwoStepsAway() {
18 for (Direction d: Direction.values()) 20 for (Direction d : Direction.values())
19 assertEquals(2, Math.abs(d.ordinal() - d.opposite().ordinal())); 21 assertEquals(2, Math.abs(d.ordinal() - d.opposite().ordinal()));
20 } 22 }
21 23
@@ -29,8 +31,8 @@ public class DirectionTest {
29 31
30 @Test 32 @Test
31 public void isParallelIsTrueOnlyForOppositeAndSelf() { 33 public void isParallelIsTrueOnlyForOppositeAndSelf() {
32 for (Direction d1: Direction.values()) { 34 for (Direction d1 : Direction.values()) {
33 for (Direction d2: Direction.values()) { 35 for (Direction d2 : Direction.values()) {
34 if (d1 == d2 || d1 == d2.opposite()) 36 if (d1 == d2 || d1 == d2.opposite())
35 assertTrue(d1.isParallelTo(d2)); 37 assertTrue(d1.isParallelTo(d2));
36 else 38 else
@@ -38,4 +40,12 @@ public class DirectionTest {
38 } 40 }
39 } 41 }
40 } 42 }
43
44 @Test
45 public void oppositeVectorSumIsNil() {
46 for (Direction d : Direction.values()) {
47 assertEquals(0, d.xVector() + d.opposite().xVector());
48 assertEquals(0, d.yVector() + d.opposite().yVector());
49 }
50 }
41} 51}