aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-03-11 15:14:36 +0300
committereug-vs <eugene@eug-vs.xyz>2022-03-11 15:14:36 +0300
commitc8341b265e69d5a08f0110558297a7a36b8a5394 (patch)
tree6a7a2ac078caa8cfa96417d27d334fef35ba90c6 /src
parent03623a7e835dde461c44acc77fe670574dfe2c4b (diff)
downloadcarcassonne-engine-ts-c8341b265e69d5a08f0110558297a7a36b8a5394.tar.gz
chore: add tests for Cell
Diffstat (limited to 'src')
-rw-r--r--src/Cell/Cell.test.ts51
-rw-r--r--src/Cell/Cell.ts55
2 files changed, 106 insertions, 0 deletions
diff --git a/src/Cell/Cell.test.ts b/src/Cell/Cell.test.ts
new file mode 100644
index 0000000..56f227f
--- /dev/null
+++ b/src/Cell/Cell.test.ts
@@ -0,0 +1,51 @@
+import assert from 'assert';
+import Cell, { Direction, Item } from './Cell';
+
+const { North, East, South, West } = Direction;
+const { Road, Town, Empty, River, Church } = Item;
+
+describe('Cell', () => {
+ describe('getSide', () => {
+ it('Should get North, East, South and West sides correctly', () => {
+ const cell = new Cell(Empty, [Road, Town, Empty, River]);
+
+ assert.strictEqual(cell.getSide(North), Road);
+ assert.strictEqual(cell.getSide(East), Town);
+ assert.strictEqual(cell.getSide(South), Empty);
+ assert.strictEqual(cell.getSide(West), River);
+ });
+
+ it('Should respect cell orientation', () => {
+ const cell = new Cell(Empty, [Road, Town, Empty, River]);
+ cell.rotate(5);
+
+ assert.strictEqual(cell.getSide(North), River);
+ assert.strictEqual(cell.getSide(East), Road);
+ assert.strictEqual(cell.getSide(South), Town);
+ assert.strictEqual(cell.getSide(West), Empty);
+ });
+
+ it('Should work with negative orientation', () => {
+ const cell = new Cell(Empty, [Road, Town, Empty, River]);
+ cell.rotate(-7);
+
+ assert.strictEqual(cell.getSide(North), River);
+ assert.strictEqual(cell.getSide(East), Road);
+ assert.strictEqual(cell.getSide(South), Town);
+ assert.strictEqual(cell.getSide(West), Empty);
+ });
+ });
+
+ 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);
+ });
+ });
+});
+
diff --git a/src/Cell/Cell.ts b/src/Cell/Cell.ts
new file mode 100644
index 0000000..23fdc9e
--- /dev/null
+++ b/src/Cell/Cell.ts
@@ -0,0 +1,55 @@
+import Debug, { Debugger } from 'debug';
+
+export enum Direction {
+ North,
+ East,
+ South,
+ West
+}
+
+export enum Item {
+ Empty = " ",
+ Road = "R",
+ Town = "T",
+ River = "I",
+ Church = "C",
+}
+
+export default class Cell {
+ center: Item;
+ private sides: [Item, Item, Item, Item];
+ private orientation: number // amount of 90-degree counter-clockwise rotations from original orientation
+ shield?: boolean;
+
+ debug: Debugger;
+
+ public constructor(center: Item, sides: [Item, Item, Item, Item], orientation = 0, shield = false) {
+ this.center = center;
+ this.sides = sides;
+ this.shield = shield;
+ this.orientation = orientation;
+
+ this.debug = Debug('cell');
+ }
+
+ toString() {
+ return;
+ }
+
+ print() {
+ console.log( ` ${this.getSide(Direction.North)} \n${this.getSide(Direction.West)}${this.center}${this.getSide(Direction.East)}\n ${this.getSide(Direction.South)} `);
+ }
+
+ getSide(direction: Direction) {
+ return this.sides[(((this.orientation + direction) % 4) + 4) % 4];
+ }
+
+ rotate(rotation = 1) {
+ this.debug(`Rotating ${rotation} clockwise`)
+ this.orientation = this.orientation - rotation;
+ }
+
+ isAttachable(cell: Cell, side: Direction) {
+ return (this.getSide(side) === cell.getSide(side + 2));
+ }
+}