aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-03-13 14:05:22 +0300
committereug-vs <eugene@eug-vs.xyz>2022-03-13 14:05:22 +0300
commite30259bb228ff5011998b1b9dbaf2508b0047425 (patch)
treeff516f889a697b8e464b0aa11629eaf22f730781
parent1d6519f9f97e1d6d4046e2caf21b81dfe277992d (diff)
downloadcarcassonne-engine-ts-e30259bb228ff5011998b1b9dbaf2508b0047425.tar.gz
feat: add test to manually verify attachments
-rw-r--r--src/Board/Board.test.ts35
-rw-r--r--src/Board/Board.ts50
2 files changed, 71 insertions, 14 deletions
diff --git a/src/Board/Board.test.ts b/src/Board/Board.test.ts
index 349a4aa..5023f0b 100644
--- a/src/Board/Board.test.ts
+++ b/src/Board/Board.test.ts
@@ -1,10 +1,11 @@
+import _ from 'lodash';
import assert from 'assert';
import Tile, { Feature, Direction } from '../Tile/Tile';
import TileOnBoard from '../Tile/TileOnBoard';
import Board from './Board';
const { Road, Town, Grass } = Feature;
-const { North } = Direction;
+const { North, East } = Direction;
describe('Board', () => {
describe('constructor', () => {
@@ -23,10 +24,33 @@ describe('Board', () => {
const legalMoves = board.getAttachments(tile);
assert.strictEqual(legalMoves.length, 4);
- assert.deepStrictEqual(legalMoves[0], { side: 0, rotation: 1, attachTo, tile });
- assert.deepStrictEqual(legalMoves[1], { side: 0, rotation: 3, attachTo, tile });
- assert.deepStrictEqual(legalMoves[2], { side: 2, rotation: 0, attachTo, tile });
- assert.deepStrictEqual(legalMoves[3], { side: 2, rotation: 2, attachTo, tile });
+ 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 });
+ });
+
+ it.skip('Have a look at my nice attachments manually bro', () => {
+ const board = new Board();
+
+ board.attach({
+ tile: new Tile(Town, [Town, Grass, Town, Grass]),
+ attachTo: board.tiles[0],
+ orientation: 0,
+ side: North,
+ });
+
+ board.attach({
+ tile: new Tile(Town, [Road, Town, Town, Road]),
+ attachTo: board.tiles[0],
+ orientation: 0,
+ side: East,
+ });
+
+ const tile = new Tile(Grass, [Town, Grass, Grass, Grass]);
+
+ const attachments = board.getAttachments(tile);
+ attachments.forEach(attachment => board.previewAttachment(attachment));
});
});
@@ -50,6 +74,7 @@ describe('Board', () => {
);
board.attach(attachment);
+
assert.strictEqual(board.tiles.length, 2);
assert.deepStrictEqual(board.tiles[1], expectedTileOnBoard);
});
diff --git a/src/Board/Board.ts b/src/Board/Board.ts
index 1525c6c..ef4b4a5 100644
--- a/src/Board/Board.ts
+++ b/src/Board/Board.ts
@@ -17,13 +17,42 @@ export default class Board {
const maxY = _.maxBy(this.tiles, 'position.y').position.y;
const minY = _.minBy(this.tiles, 'position.y').position.y;
+ const maxX = _.maxBy(this.tiles, 'position.x').position.x;
+ const minX = _.minBy(this.tiles, 'position.x').position.x;
+
_.range(maxY, minY - 1, -1)
- .map(y => {
+ .map((y: number) => {
const rowTiles = _.filter(this.tiles, { position: { y } });
- // console.log({ rowTiles, y });
- console.log(rowTiles.map(tile => ` ${tile.sides[North]} `).join());
- console.log(rowTiles.map(tile => `${tile.sides[West]}${tile.center}${tile.sides[East]}`).join());
- console.log(rowTiles.map(tile => ` ${tile.sides[South]} `).join());
+
+ console.log(
+ _.range(minX, maxX + 1)
+ .map((x: number) => {
+ const tile = _.find(rowTiles, { position: { x, y } });
+ if (!tile) return ' ';
+ return ` ${tile.getSide(North)} `;
+ })
+ .join('|')
+ );
+
+ console.log(
+ _.range(minX, maxX + 1)
+ .map((x: number) => {
+ const tile = _.find(rowTiles, { position: { x, y } });
+ if (!tile) return ' ';
+ return `${tile.getSide(West)}${tile.center}${tile.getSide(East)}`;
+ })
+ .join('|')
+ );
+
+ console.log(
+ _.range(minX, maxX + 1)
+ .map((x: number) => {
+ const tile = _.find(rowTiles, { position: { x, y } });
+ if (!tile) return ' ';
+ return ` ${tile.getSide(South)} `;
+ })
+ .join('|')
+ );
});
}
@@ -32,7 +61,7 @@ export default class Board {
}
attach(attachment: Attachment) {
- const { tile, attachTo, side } = attachment;
+ const { tile, attachTo, side, orientation } = attachment;
const xIncrement = {
[East]: 1,
@@ -52,13 +81,16 @@ export default class Board {
x: attachTo.position.x + (xIncrement[side] || 0),
y: attachTo.position.y + (yIncrement[side] || 0),
},
+ orientation,
);
this.tiles.push(tileOnBoard);
}
- getLegalMoves(tile: Tile) {
- const attachments = this.getAttachments(tile);
- console.log(attachments);
+ previewAttachment(attachment: Attachment) {
+ console.log(attachment);
+ const previewBoard = _.cloneDeep(this);
+ previewBoard.attach(attachment);
+ previewBoard.print();
}
}