aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-03-12 14:36:31 +0300
committereug-vs <eugene@eug-vs.xyz>2022-03-12 14:37:11 +0300
commit8aa2213390e01996e3f9682abfd5910c698df0e2 (patch)
tree86be278563cf285392c58e3938d002a5b288d7a5
parent2b6ba3f395cb62bef07f2cc12a00dab0c1cf2f04 (diff)
downloadcarcassonne-engine-ts-8aa2213390e01996e3f9682abfd5910c698df0e2.tar.gz
feat: add initial Board class
-rw-r--r--src/Board/Board.test.ts30
-rw-r--r--src/Board/Board.ts26
-rw-r--r--src/Cell/Cell.test.ts3
3 files changed, 59 insertions, 0 deletions
diff --git a/src/Board/Board.test.ts b/src/Board/Board.test.ts
new file mode 100644
index 0000000..ad5509f
--- /dev/null
+++ b/src/Board/Board.test.ts
@@ -0,0 +1,30 @@
+import assert from 'assert';
+import Cell, { Item } from '../Cell/Cell';
+import Board from "./Board";
+
+const { Road, Town, Empty } = Item;
+
+describe('Board', () => {
+ describe('constructor', () => {
+ it('Should initialize empty board with a starting cell', () => {
+ const board = new Board();
+ assert.strictEqual(board.cells.length, 1);
+ assert.deepStrictEqual(board.cells[0], new Cell(Road, [Town, Road, Empty, Road]));
+ });
+ });
+
+ describe('getAttachments', () => {
+ it('Should correctly determine legal moves for 1-cell board', () => {
+ const board = new Board();
+ const attachTo = board.cells[0];
+ const cell = new Cell(Town, [Empty, Town, Empty, Town]);
+ const legalMoves = board.getAttachments(cell);
+
+ assert.strictEqual(legalMoves.length, 4);
+ assert.deepStrictEqual(legalMoves[0], { side: 0, rotation: 1, attachTo, cell });
+ assert.deepStrictEqual(legalMoves[1], { side: 0, rotation: 3, attachTo, cell });
+ assert.deepStrictEqual(legalMoves[2], { side: 2, rotation: 0, attachTo, cell });
+ assert.deepStrictEqual(legalMoves[3], { side: 2, rotation: 2, attachTo, cell });
+ });
+ });
+});
diff --git a/src/Board/Board.ts b/src/Board/Board.ts
new file mode 100644
index 0000000..c511e89
--- /dev/null
+++ b/src/Board/Board.ts
@@ -0,0 +1,26 @@
+import _ from 'lodash';
+import Cell, { Item } from "../Cell/Cell";
+
+const { Road, Town, Empty } = Item;
+
+export default class Board {
+ cells: Cell[];
+
+ constructor(cells?: Cell[]) {
+ if (cells) this.cells = cells;
+ else this.cells = [new Cell(Road, [Town, Road, Empty, Road])]
+ }
+
+ print() {
+ this.cells.forEach(cell => cell.print());
+ }
+
+ getAttachments(cell: Cell) {
+ return _.flatten(this.cells.map(attachTo => attachTo.getAttachments(cell)));
+ }
+
+ getLegalMoves(cell: Cell) {
+ const attachments = this.getAttachments(cell);
+ console.log(attachments);
+ }
+}
diff --git a/src/Cell/Cell.test.ts b/src/Cell/Cell.test.ts
index 0931130..be6f475 100644
--- a/src/Cell/Cell.test.ts
+++ b/src/Cell/Cell.test.ts
@@ -41,6 +41,9 @@ describe('Cell', () => {
const attachTo = new Cell(Town, [Road, Town, Town, Road])
const cell = new Cell(Road, [Empty, Road, Road, Empty])
+ cell.print();
+ attachTo.print();
+
const attachments = attachTo.getAttachments(cell);
assert.strictEqual(attachments.length, 4);
assert.deepStrictEqual(attachments[0], { side: 0, rotation: 0, cell, attachTo });