aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-03-11 17:47:17 +0300
committereug-vs <eugene@eug-vs.xyz>2022-03-11 17:47:17 +0300
commit2b6ba3f395cb62bef07f2cc12a00dab0c1cf2f04 (patch)
treed8638206050862356d12d8051dd4b51b8b774461
parent9168bba092b15b20b0599a0b2551e8fefc1ad803 (diff)
downloadcarcassonne-engine-ts-2b6ba3f395cb62bef07f2cc12a00dab0c1cf2f04.tar.gz
feat: implement getAttachments
-rw-r--r--src/Cell/Cell.test.ts22
-rw-r--r--src/Cell/Cell.ts39
2 files changed, 42 insertions, 19 deletions
diff --git a/src/Cell/Cell.test.ts b/src/Cell/Cell.test.ts
index 56f227f..0931130 100644
--- a/src/Cell/Cell.test.ts
+++ b/src/Cell/Cell.test.ts
@@ -2,7 +2,7 @@ import assert from 'assert';
import Cell, { Direction, Item } from './Cell';
const { North, East, South, West } = Direction;
-const { Road, Town, Empty, River, Church } = Item;
+const { Road, Town, Empty, River } = Item;
describe('Cell', () => {
describe('getSide', () => {
@@ -36,15 +36,17 @@ describe('Cell', () => {
});
});
- describe('isAttachable', () => {
- it('Should correctly test if cell is attachable', () => {
- const cell = new Cell(Church, [Road, Town, Empty, River]);
- const other = new Cell(Empty, [Road, Road, Town, Road], 3);
-
- assert.strictEqual(cell.isAttachable(other, North), true);
- assert.strictEqual(cell.isAttachable(other, East), true);
- assert.strictEqual(cell.isAttachable(other, South), false);
- assert.strictEqual(cell.isAttachable(other, West), false);
+ describe('getAttachments', () => {
+ it('Should correclty list legal attachments', () => {
+ const attachTo = new Cell(Town, [Road, Town, Town, Road])
+ const cell = new Cell(Road, [Empty, Road, Road, Empty])
+
+ const attachments = attachTo.getAttachments(cell);
+ assert.strictEqual(attachments.length, 4);
+ assert.deepStrictEqual(attachments[0], { side: 0, rotation: 0, cell, attachTo });
+ assert.deepStrictEqual(attachments[1], { side: 0, rotation: 1, cell, attachTo });
+ assert.deepStrictEqual(attachments[2], { side: 3, rotation: 0, cell, attachTo });
+ assert.deepStrictEqual(attachments[3], { side: 3, rotation: 3, cell, attachTo });
});
});
});
diff --git a/src/Cell/Cell.ts b/src/Cell/Cell.ts
index 23fdc9e..70bb792 100644
--- a/src/Cell/Cell.ts
+++ b/src/Cell/Cell.ts
@@ -1,5 +1,8 @@
+import _ from 'lodash';
import Debug, { Debugger } from 'debug';
+const debug = Debug('cell');
+
export enum Direction {
North,
East,
@@ -15,9 +18,18 @@ export enum Item {
Church = "C",
}
+export interface Attachment {
+ attachTo: Cell;
+ side: Direction;
+ cell: Cell;
+ rotation: number; // Clockwise rotation of a cell
+}
+
+
export default class Cell {
center: Item;
private sides: [Item, Item, Item, Item];
+ neighbors: [Cell, Cell, Cell, Cell];
private orientation: number // amount of 90-degree counter-clockwise rotations from original orientation
shield?: boolean;
@@ -28,12 +40,6 @@ export default class Cell {
this.sides = sides;
this.shield = shield;
this.orientation = orientation;
-
- this.debug = Debug('cell');
- }
-
- toString() {
- return;
}
print() {
@@ -45,11 +51,26 @@ export default class Cell {
}
rotate(rotation = 1) {
- this.debug(`Rotating ${rotation} clockwise`)
+ debug(`Rotating ${rotation} clockwise`)
this.orientation = this.orientation - rotation;
}
- isAttachable(cell: Cell, side: Direction) {
- return (this.getSide(side) === cell.getSide(side + 2));
+ getAttachments(cell: Cell) {
+ return _.flatten([0, 1, 2, 3].map(side => {
+ const item = this.getSide(side);
+ return [0, 1, 2, 3]
+ .filter(rotation => cell.getSide(side - rotation + 2) === item)
+ .map(rotation => ({
+ cell,
+ rotation,
+ side,
+ attachTo: this as Cell
+ }))
+ }));
+ }
+
+ attach(cell: Cell, side: Direction) {
+ if (this.neighbors[side]) throw new Error('There is something already attached to this side!');
+ this.neighbors[side] = cell;
}
}