aboutsummaryrefslogtreecommitdiff
path: root/src/Board/Board.ts
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-03-13 14:48:30 +0300
committereug-vs <eugene@eug-vs.xyz>2022-03-13 14:48:30 +0300
commit555d604d1e67ed6eac8c09a8b99c59d342660bb7 (patch)
tree64b939c610bdf09abe8f30de663f74eefcfa9a9d /src/Board/Board.ts
parente30259bb228ff5011998b1b9dbaf2508b0047425 (diff)
downloadcarcassonne-engine-ts-555d604d1e67ed6eac8c09a8b99c59d342660bb7.tar.gz
feat: implement correct getAttachments for board
Diffstat (limited to 'src/Board/Board.ts')
-rw-r--r--src/Board/Board.ts60
1 files changed, 52 insertions, 8 deletions
diff --git a/src/Board/Board.ts b/src/Board/Board.ts
index ef4b4a5..b114ff1 100644
--- a/src/Board/Board.ts
+++ b/src/Board/Board.ts
@@ -57,7 +57,58 @@ export default class Board {
}
getAttachments(tile: Tile): Attachment[] {
- return _.flatten(this.tiles.map(attachTo => attachTo.getAttachments(tile)));
+ return _
+ .flatten(this.tiles.map(attachTo => attachTo.getAttachments(tile)))
+ .filter((attachment: Attachment) => this.isLegalAttachment(attachment));
+ }
+
+ previewAttachment(attachment: Attachment) {
+ console.log(attachment);
+ const previewBoard = _.cloneDeep(this);
+ previewBoard.attach(attachment);
+ previewBoard.print();
+ }
+
+ isLegalAttachment(attachment: Attachment) {
+ const { tile, side, attachTo, orientation } = attachment;
+
+ const xIncrement = {
+ [East]: 1,
+ [West]: -1,
+ };
+
+ const yIncrement = {
+ [North]: 1,
+ [South]: -1,
+ };
+
+ const position = {
+ x: attachTo.position.x + (xIncrement[side] || 0),
+ y: attachTo.position.y + (yIncrement[side] || 0),
+ };
+
+ 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 },
+ ];
+
+ return _.every(_.map(neighborIncrements, ({ x, y, side }) => {
+ const neighbor = _.find(this.tiles, {
+ position: {
+ x: position.x + x,
+ y: position.y + y,
+ }
+ });
+ if (!neighbor) return true;
+ if (neighbor.getSide(side) !== tile.getSide(side - orientation + 2)) return false;
+ return true;
+ }));
}
attach(attachment: Attachment) {
@@ -86,11 +137,4 @@ export default class Board {
this.tiles.push(tileOnBoard);
}
-
- previewAttachment(attachment: Attachment) {
- console.log(attachment);
- const previewBoard = _.cloneDeep(this);
- previewBoard.attach(attachment);
- previewBoard.print();
- }
}