aboutsummaryrefslogtreecommitdiff
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
parente30259bb228ff5011998b1b9dbaf2508b0047425 (diff)
downloadcarcassonne-engine-ts-555d604d1e67ed6eac8c09a8b99c59d342660bb7.tar.gz
feat: implement correct getAttachments for board
-rw-r--r--src/Board/Board.test.ts30
-rw-r--r--src/Board/Board.ts60
2 files changed, 73 insertions, 17 deletions
diff --git a/src/Board/Board.test.ts b/src/Board/Board.test.ts
index 5023f0b..4bd6182 100644
--- a/src/Board/Board.test.ts
+++ b/src/Board/Board.test.ts
@@ -17,20 +17,20 @@ describe('Board', () => {
});
describe('getAttachments', () => {
- it('Should correctly determine legal moves for 1-tile board', () => {
+ it('Should correctly determine legal attachments for 1-tile board', () => {
const board = new Board();
const attachTo = board.tiles[0];
const tile = new Tile(Town, [Grass, Town, Grass, Town]);
- const legalMoves = board.getAttachments(tile);
+ const attachments = board.getAttachments(tile);
- assert.strictEqual(legalMoves.length, 4);
- assert.deepStrictEqual(legalMoves[0], { side: 0, orientation: 1, attachTo, tile });
- assert.deepStrictEqual(legalMoves[1], { side: 0, orientation: 3, attachTo, tile });
- assert.deepStrictEqual(legalMoves[2], { side: 2, orientation: 0, attachTo, tile });
- assert.deepStrictEqual(legalMoves[3], { side: 2, orientation: 2, attachTo, tile });
+ assert.strictEqual(attachments.length, 4);
+ assert.deepStrictEqual(attachments[0], { side: 0, orientation: 1, attachTo, tile });
+ assert.deepStrictEqual(attachments[1], { side: 0, orientation: 3, attachTo, tile });
+ assert.deepStrictEqual(attachments[2], { side: 2, orientation: 0, attachTo, tile });
+ assert.deepStrictEqual(attachments[3], { side: 2, orientation: 2, attachTo, tile });
});
- it.skip('Have a look at my nice attachments manually bro', () => {
+ it('Should correctly return legal attachments for complex board', () => {
const board = new Board();
board.attach({
@@ -50,7 +50,19 @@ describe('Board', () => {
const tile = new Tile(Grass, [Town, Grass, Grass, Grass]);
const attachments = board.getAttachments(tile);
- attachments.forEach(attachment => board.previewAttachment(attachment));
+
+ // attachments.forEach(attachment => board.previewAttachment(attachment));
+
+ assert.strictEqual(attachments.length, 9);
+ assert.deepStrictEqual(attachments[0], { side: 2, orientation: 1, tile, attachTo: board.tiles[0] });
+ assert.deepStrictEqual(attachments[1], { side: 2, orientation: 2, tile, attachTo: board.tiles[0] });
+ assert.deepStrictEqual(attachments[2], { side: 2, orientation: 3, tile, attachTo: board.tiles[0] });
+ assert.deepStrictEqual(attachments[3], { side: 0, orientation: 2, tile, attachTo: board.tiles[1] });
+ assert.deepStrictEqual(attachments[4], { side: 3, orientation: 0, tile, attachTo: board.tiles[1] });
+ assert.deepStrictEqual(attachments[5], { side: 3, orientation: 2, tile, attachTo: board.tiles[1] });
+ assert.deepStrictEqual(attachments[6], { side: 3, orientation: 3, tile, attachTo: board.tiles[1] });
+ assert.deepStrictEqual(attachments[7], { side: 1, orientation: 3, tile, attachTo: board.tiles[2] });
+ assert.deepStrictEqual(attachments[8], { side: 2, orientation: 0, tile, attachTo: board.tiles[2] });
});
});
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();
- }
}