aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Board/Board.ts35
1 files changed, 21 insertions, 14 deletions
diff --git a/src/Board/Board.ts b/src/Board/Board.ts
index b114ff1..5f38088 100644
--- a/src/Board/Board.ts
+++ b/src/Board/Board.ts
@@ -69,6 +69,25 @@ export default class Board {
previewBoard.print();
}
+ getTileNeighbors(position: TileOnBoard['position']): TileOnBoard[] {
+ // Do not change the order!
+ const neighborIncrements = [
+ { x: 0, y: -1 },
+ { x: -1, y: 0 },
+ { x: 0, y: 1 },
+ { x: 1, y: 0 },
+ ];
+
+ return _.filter(_.map(neighborIncrements, ({ x, y }) => {
+ return _.find(this.tiles, {
+ position: {
+ x: position.x + x,
+ y: position.y + y,
+ }
+ });
+ }));
+ }
+
isLegalAttachment(attachment: Attachment) {
const { tile, side, attachTo, orientation } = attachment;
@@ -90,21 +109,9 @@ export default class Board {
const isBusy = _.find(this.tiles, { position });
if (isBusy) return false;
- // TODO: compute this array at runtime
- const neighborIncrements = [
- { x: 0, y: -1, side: 0 },
- { x: -1, y: 0, side: 1 },
- { x: 0, y: 1, side: 2 },
- { x: 1, y: 0, side: 3 },
- ];
+ const neighbors = this.getTileNeighbors(position);
- return _.every(_.map(neighborIncrements, ({ x, y, side }) => {
- const neighbor = _.find(this.tiles, {
- position: {
- x: position.x + x,
- y: position.y + y,
- }
- });
+ return _.every(_.map(neighbors, (neighbor: TileOnBoard) => {
if (!neighbor) return true;
if (neighbor.getSide(side) !== tile.getSide(side - orientation + 2)) return false;
return true;