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 +------------ 3 files changed, 34 insertions(+), 24 deletions(-) (limited to 'src/ch') 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()); } /** -- cgit v1.2.3