aboutsummaryrefslogtreecommitdiff
path: root/src/Board
diff options
context:
space:
mode:
Diffstat (limited to 'src/Board')
-rw-r--r--src/Board/Board.test.ts28
-rw-r--r--src/Board/Board.ts33
2 files changed, 57 insertions, 4 deletions
diff --git a/src/Board/Board.test.ts b/src/Board/Board.test.ts
index 7e99581..349a4aa 100644
--- a/src/Board/Board.test.ts
+++ b/src/Board/Board.test.ts
@@ -1,9 +1,10 @@
import assert from 'assert';
-import Tile, { Feature } from '../Tile/Tile';
+import Tile, { Feature, Direction } from '../Tile/Tile';
import TileOnBoard from '../Tile/TileOnBoard';
import Board from './Board';
const { Road, Town, Grass } = Feature;
+const { North } = Direction;
describe('Board', () => {
describe('constructor', () => {
@@ -28,4 +29,29 @@ describe('Board', () => {
assert.deepStrictEqual(legalMoves[3], { side: 2, rotation: 2, attachTo, tile });
});
});
+
+ describe('attach', () => {
+ it('Should push new tile to the list and assign correct coordinates', () => {
+ const board = new Board();
+
+ const attachment = {
+ tile: new Tile(Town, [Grass, Town, Grass, Town]),
+ attachTo: board.tiles[0],
+ orientation: 0,
+ side: North,
+ };
+
+ const expectedTileOnBoard = new TileOnBoard(
+ Town,
+ [Grass, Town, Grass, Town],
+ false,
+ { x: 0, y: 1 },
+ 0
+ );
+
+ board.attach(attachment);
+ assert.strictEqual(board.tiles.length, 2);
+ assert.deepStrictEqual(board.tiles[1], expectedTileOnBoard);
+ });
+ });
});
diff --git a/src/Board/Board.ts b/src/Board/Board.ts
index 2c4a215..4ba7a4c 100644
--- a/src/Board/Board.ts
+++ b/src/Board/Board.ts
@@ -1,8 +1,9 @@
import _ from 'lodash';
-import Tile, { Feature } from '../Tile/Tile';
-import TileOnBoard from '../Tile/TileOnBoard';
+import Tile, { Feature, Direction } from '../Tile/Tile';
+import TileOnBoard, { Attachment } from '../Tile/TileOnBoard';
const { Road, Town, Grass } = Feature;
+const { North, East, South, West } = Direction;
export default class Board {
tiles: TileOnBoard[];
@@ -16,10 +17,36 @@ export default class Board {
this.tiles.forEach(tile => tile.print());
}
- getAttachments(tile: Tile) {
+ getAttachments(tile: Tile): Attachment[] {
return _.flatten(this.tiles.map(attachTo => attachTo.getAttachments(tile)));
}
+ attach(attachment: Attachment) {
+ const { tile, attachTo, side } = attachment;
+
+ const xIncrement = {
+ [East]: 1,
+ [West]: -1,
+ };
+
+ const yIncrement = {
+ [North]: 1,
+ [South]: -1,
+ };
+
+ const tileOnBoard = new TileOnBoard(
+ tile.center,
+ tile.sides,
+ tile.shield,
+ {
+ x: attachTo.position.x + (xIncrement[side] || 0),
+ y: attachTo.position.y + (yIncrement[side] || 0),
+ },
+ );
+
+ this.tiles.push(tileOnBoard);
+ }
+
getLegalMoves(tile: Tile) {
const attachments = this.getAttachments(tile);
console.log(attachments);