From d64c44c432ccb5574909edd77b8b882e8543ef1b Mon Sep 17 00:00:00 2001
From: eug-vs <eugene@eug-vs.xyz>
Date: Mon, 14 Mar 2022 20:52:08 +0300
Subject: refactor!: change internal edge representation

---
 src/Board/Board.ts | 50 +++++++++++++++++++++++---------------------------
 1 file changed, 23 insertions(+), 27 deletions(-)

(limited to 'src/Board/Board.ts')

diff --git a/src/Board/Board.ts b/src/Board/Board.ts
index b9a2136..65777ad 100644
--- a/src/Board/Board.ts
+++ b/src/Board/Board.ts
@@ -1,16 +1,14 @@
 import _ from 'lodash';
-import Tile, { Feature, Direction } from '../Tile/Tile';
-import TileOnBoard, { Attachment } from '../Tile/TileOnBoard';
+import Tile, { Attachment, Direction } from '../Tile/Tile';
 
-const { Road, Town, Grass } = Feature;
 const { North, East, South, West } = Direction;
 
 export default class Board {
-  tiles: TileOnBoard[];
+  tiles: Tile[];
 
-  constructor(tiles?: TileOnBoard[]) {
+  constructor(tiles?: Tile[]) {
     if (tiles) this.tiles = tiles;
-    else this.tiles = [new TileOnBoard(Road, [Town, Road, Grass, Road])]
+    else this.tiles = [new Tile('R', 'CRFR')]
   }
 
   print() {
@@ -29,7 +27,7 @@ export default class Board {
           .map((x: number) => {
             const tile = _.find(rowTiles, { position: { x, y } });
             if (!tile) return '   ';
-            return  ` ${tile.getSide(North)} `;
+            return  ` ${tile.edges[North]} `;
           })
           .join('|')
         );
@@ -39,7 +37,7 @@ export default class Board {
           .map((x: number) => {
             const tile = _.find(rowTiles, { position: { x, y } });
             if (!tile) return '   ';
-            return  `${tile.getSide(West)}${tile.center}${tile.getSide(East)}`;
+            return  `${tile.edges[West]}${tile.center}${tile.edges[East]}`;
           })
           .join('|')
         );
@@ -49,7 +47,7 @@ export default class Board {
           .map((x: number) => {
             const tile = _.find(rowTiles, { position: { x, y } });
             if (!tile) return '   ';
-            return  ` ${tile.getSide(South)} `;
+            return  ` ${tile.edges[South]} `;
           })
           .join('|')
         );
@@ -69,27 +67,27 @@ export default class Board {
     previewBoard.print();
   }
 
-  getTileNeighbors(position: TileOnBoard['position']): TileOnBoard[] {
+  getTileNeighbors(position: Tile['position']): Tile[] {
     // Do not change the order!
     const neighborIncrements = [
-      { x: 0, y: -1 },
-      { x: -1, y: 0 },
       { x: 0, y: 1 },
       { x: 1, y: 0 },
+      { x: 0, y: -1 },
+      { x: -1, y: 0 },
     ];
 
-    return _.filter(_.map(neighborIncrements, ({ x, y }) => {
+    return _.map(neighborIncrements, ({ x, y }) => {
       return _.find(this.tiles, {
         position: {
           x: position.x + x,
           y: position.y + y,
         }
       });
-    }));
+    });
   }
 
-  calculateNewTilePosition(attachment: Attachment): TileOnBoard['position'] {
-    const { attachTo: { position }, side } = attachment
+  calculateNewTilePosition(attachment: Attachment): Tile['position'] {
+    const { attachTo: { position }, edge } = attachment
 
     const xIncrement = {
       [East]: 1,
@@ -102,13 +100,13 @@ export default class Board {
     };
 
     return {
-      x: position.x + (xIncrement[side] || 0),
-      y: position.y + (yIncrement[side] || 0),
+      x: position.x + (xIncrement[edge] || 0),
+      y: position.y + (yIncrement[edge] || 0),
     };
   }
 
   isLegalAttachment(attachment: Attachment) {
-    const { tile, side, orientation } = attachment;
+    const { tile } = attachment;
 
     const position = this.calculateNewTilePosition(attachment);
 
@@ -116,12 +114,10 @@ export default class Board {
     if (isBusy) return false;
 
     const neighbors = this.getTileNeighbors(position);
-
-    return _.every(_.map(neighbors, (neighbor: TileOnBoard) => {
-      if (!neighbor) return true;
-      if (neighbor.getSide(side) !== tile.getSide(side - orientation + 2)) return false;
-      return true;
-    }));
+    const neighborsRegex = _.map(neighbors, (neighbor: Tile, edge: number) => neighbor?.edges[edge] || '.').join('');
+    const str = [tile.edges, tile.edges, tile.edges].join('');
+    console.log(neighborsRegex, str);
+    return new RegExp(neighborsRegex).test(str);
   }
 
   attach(attachment: Attachment) {
@@ -129,9 +125,9 @@ export default class Board {
 
     const position = this.calculateNewTilePosition(attachment);
 
-    const tileOnBoard = new TileOnBoard(
+    const tileOnBoard = new Tile(
       tile.center,
-      tile.sides,
+      tile.edges,
       tile.shield,
       position,
       orientation,
-- 
cgit v1.2.3