diff options
author | eug-vs <eugene@eug-vs.xyz> | 2022-03-13 15:38:45 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2022-03-13 15:38:45 +0300 |
commit | 1c9b62aa3b5bbfdc0fde7a2b343066fda66441b3 (patch) | |
tree | c9cff58ee7ad3614114eb5f7fb8dbfc8b6669530 /src/Board/Board.ts | |
parent | 555d604d1e67ed6eac8c09a8b99c59d342660bb7 (diff) | |
download | carcassonne-engine-ts-1c9b62aa3b5bbfdc0fde7a2b343066fda66441b3.tar.gz |
refactor: separete getTileNeighbors method
Diffstat (limited to 'src/Board/Board.ts')
-rw-r--r-- | src/Board/Board.ts | 35 |
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; |