From 7574a77482936bf2c56ee904f7d91d64571b0b12 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 22 Feb 2016 16:39:45 +0100 Subject: Refactor neighbor computation using directional vectors --- src/ch/epfl/xblast/Cell.java | 13 +------------ src/ch/epfl/xblast/Direction.java | 32 ++++++++++++++++++++++++++++++++ src/ch/epfl/xblast/SubCell.java | 13 +------------ test/ch/epfl/xblast/DirectionTest.java | 26 ++++++++++++++++++-------- 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 { * @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; - } + return new Cell(this.x + dir.xVector(), this.y + dir.yVector()); } /** 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 { return that == this || that == this.opposite(); } + /** + * Returns the x-coordinate of the normed vector representation of the Direction. + * + * @return the x-coordinate + */ + public int xVector() { + switch (this) { + case W: + return -1; + case E: + return +1; + default: + return 0; + } + } + + /** + * Returns the x-coordinate of the normed vector representation of the Direction. + * + * @return the y-coordinate + */ + public int yVector() { + switch (this) { + case N: + return -1; + case S: + return +1; + default: + return 0; + } + } + } 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 { * @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; - } + return new SubCell(this.x + dir.xVector(), this.y + dir.yVector()); } /** 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 @@ package ch.epfl.xblast; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import org.junit.Test; +import static org.junit.Assert.*; + +/** + * @author EPFL + * @author Pacien TRAN-GIRARD (261948) + */ public class DirectionTest { @Test public void oppositeOfOppositeIsIdentity() { - for (Direction d: Direction.values()) + for (Direction d : Direction.values()) assertEquals(d, d.opposite().opposite()); } @Test public void oppositeIsTwoStepsAway() { - for (Direction d: Direction.values()) + for (Direction d : Direction.values()) assertEquals(2, Math.abs(d.ordinal() - d.opposite().ordinal())); } @@ -29,8 +31,8 @@ public class DirectionTest { @Test public void isParallelIsTrueOnlyForOppositeAndSelf() { - for (Direction d1: Direction.values()) { - for (Direction d2: Direction.values()) { + for (Direction d1 : Direction.values()) { + for (Direction d2 : Direction.values()) { if (d1 == d2 || d1 == d2.opposite()) assertTrue(d1.isParallelTo(d2)); else @@ -38,4 +40,12 @@ public class DirectionTest { } } } + + @Test + public void oppositeVectorSumIsNil() { + for (Direction d : Direction.values()) { + assertEquals(0, d.xVector() + d.opposite().xVector()); + assertEquals(0, d.yVector() + d.opposite().yVector()); + } + } } -- cgit v1.2.3