From 8aa2213390e01996e3f9682abfd5910c698df0e2 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sat, 12 Mar 2022 14:36:31 +0300 Subject: feat: add initial Board class --- src/Board/Board.test.ts | 30 ++++++++++++++++++++++++++++++ src/Board/Board.ts | 26 ++++++++++++++++++++++++++ src/Cell/Cell.test.ts | 3 +++ 3 files changed, 59 insertions(+) create mode 100644 src/Board/Board.test.ts create mode 100644 src/Board/Board.ts 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 }); -- cgit v1.2.3