aboutsummaryrefslogtreecommitdiff
path: root/src/Board
diff options
context:
space:
mode:
Diffstat (limited to 'src/Board')
-rw-r--r--src/Board/Board.test.ts30
-rw-r--r--src/Board/Board.ts26
2 files changed, 56 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);
+ }
+}