aboutsummaryrefslogtreecommitdiff
path: root/src/Board/Board.ts
blob: 1525c6ca427cc69b1aec0e36e31c849499481571 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import _ from 'lodash';
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[];

  constructor(tiles?: TileOnBoard[]) {
    if (tiles) this.tiles = tiles;
    else this.tiles = [new TileOnBoard(Road, [Town, Road, Grass, Road])]
  }

  print() {
    const maxY = _.maxBy(this.tiles, 'position.y').position.y;
    const minY = _.minBy(this.tiles, 'position.y').position.y;

    _.range(maxY, minY - 1, -1)
      .map(y => {
        const rowTiles = _.filter(this.tiles, { position: { y } });
        // console.log({ rowTiles, y });
        console.log(rowTiles.map(tile => ` ${tile.sides[North]} `).join());
        console.log(rowTiles.map(tile => `${tile.sides[West]}${tile.center}${tile.sides[East]}`).join());
        console.log(rowTiles.map(tile => ` ${tile.sides[South]} `).join());
      });
  }

  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);
  }
}