aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-03-12 15:47:58 +0300
committereug-vs <eugene@eug-vs.xyz>2022-03-12 15:47:58 +0300
commit668985e6ed78add10e3a0eb94eff283d59b3971f (patch)
treef3a379f4fba55817460f6bed00ba17dee0ffeff1
parent26b15f6ef72ca983e5fb114fb36bf0c0860a91ea (diff)
downloadcarcassonne-engine-ts-668985e6ed78add10e3a0eb94eff283d59b3971f.tar.gz
refactor: rename Feature.Emtpy -> Grass
-rw-r--r--src/Board/Board.test.ts6
-rw-r--r--src/Board/Board.ts4
-rw-r--r--src/Tile/Tile.ts2
-rw-r--r--src/Tile/TileOnBoard.test.ts16
-rw-r--r--src/Tile/TileOnBoard.ts8
5 files changed, 21 insertions, 15 deletions
diff --git a/src/Board/Board.test.ts b/src/Board/Board.test.ts
index 1370a87..7e99581 100644
--- a/src/Board/Board.test.ts
+++ b/src/Board/Board.test.ts
@@ -3,14 +3,14 @@ import Tile, { Feature } from '../Tile/Tile';
import TileOnBoard from '../Tile/TileOnBoard';
import Board from './Board';
-const { Road, Town, Empty } = Feature;
+const { Road, Town, Grass } = Feature;
describe('Board', () => {
describe('constructor', () => {
it('Should initialize empty board with a starting tile', () => {
const board = new Board();
assert.strictEqual(board.tiles.length, 1);
- assert.deepStrictEqual(board.tiles[0], new TileOnBoard(Road, [Town, Road, Empty, Road]));
+ assert.deepStrictEqual(board.tiles[0], new TileOnBoard(Road, [Town, Road, Grass, Road]));
});
});
@@ -18,7 +18,7 @@ describe('Board', () => {
it('Should correctly determine legal moves for 1-tile board', () => {
const board = new Board();
const attachTo = board.tiles[0];
- const tile = new Tile(Town, [Empty, Town, Empty, Town]);
+ const tile = new Tile(Town, [Grass, Town, Grass, Town]);
const legalMoves = board.getAttachments(tile);
assert.strictEqual(legalMoves.length, 4);
diff --git a/src/Board/Board.ts b/src/Board/Board.ts
index 59afd31..2c4a215 100644
--- a/src/Board/Board.ts
+++ b/src/Board/Board.ts
@@ -2,14 +2,14 @@ import _ from 'lodash';
import Tile, { Feature } from '../Tile/Tile';
import TileOnBoard from '../Tile/TileOnBoard';
-const { Road, Town, Empty } = Feature;
+const { Road, Town, Grass } = Feature;
export default class Board {
tiles: TileOnBoard[];
constructor(tiles?: TileOnBoard[]) {
if (tiles) this.tiles = tiles;
- else this.tiles = [new TileOnBoard(Road, [Town, Road, Empty, Road])]
+ else this.tiles = [new TileOnBoard(Road, [Town, Road, Grass, Road])]
}
print() {
diff --git a/src/Tile/Tile.ts b/src/Tile/Tile.ts
index 4dd6ecb..3a2d350 100644
--- a/src/Tile/Tile.ts
+++ b/src/Tile/Tile.ts
@@ -6,7 +6,7 @@ export enum Direction {
}
export enum Feature {
- Empty = ' ',
+ Grass = 'G',
Road = 'R',
Town = 'T',
River = 'I',
diff --git a/src/Tile/TileOnBoard.test.ts b/src/Tile/TileOnBoard.test.ts
index 7448435..70fc5a1 100644
--- a/src/Tile/TileOnBoard.test.ts
+++ b/src/Tile/TileOnBoard.test.ts
@@ -3,44 +3,44 @@ import TileOnBoard from './TileOnBoard';
import { Direction, Feature } from './Tile';
const { North, East, South, West } = Direction;
-const { Road, Town, Empty, River } = Feature;
+const { Road, Town, Grass, River } = Feature;
describe('TileOnBoard', () => {
describe('getSide', () => {
it('Should get North, East, South and West sides correctly', () => {
- const tile = new TileOnBoard(Empty, [Road, Town, Empty, River]);
+ const tile = new TileOnBoard(Grass, [Road, Town, Grass, River]);
assert.strictEqual(tile.getSide(North), Road);
assert.strictEqual(tile.getSide(East), Town);
- assert.strictEqual(tile.getSide(South), Empty);
+ assert.strictEqual(tile.getSide(South), Grass);
assert.strictEqual(tile.getSide(West), River);
});
it('Should respect tile orientation', () => {
- const tile = new TileOnBoard(Empty, [Road, Town, Empty, River]);
+ const tile = new TileOnBoard(Grass, [Road, Town, Grass, River]);
tile.rotate(5);
assert.strictEqual(tile.getSide(North), River);
assert.strictEqual(tile.getSide(East), Road);
assert.strictEqual(tile.getSide(South), Town);
- assert.strictEqual(tile.getSide(West), Empty);
+ assert.strictEqual(tile.getSide(West), Grass);
});
it('Should work with negative orientation', () => {
- const tile = new TileOnBoard(Empty, [Road, Town, Empty, River]);
+ const tile = new TileOnBoard(Grass, [Road, Town, Grass, River]);
tile.rotate(-7);
assert.strictEqual(tile.getSide(North), River);
assert.strictEqual(tile.getSide(East), Road);
assert.strictEqual(tile.getSide(South), Town);
- assert.strictEqual(tile.getSide(West), Empty);
+ assert.strictEqual(tile.getSide(West), Grass);
});
});
describe('getAttachments', () => {
it('Should correclty list legal attachments', () => {
const attachTo = new TileOnBoard(Town, [Road, Town, Town, Road])
- const tile = new TileOnBoard(Road, [Empty, Road, Road, Empty])
+ const tile = new TileOnBoard(Road, [Grass, Road, Road, Grass])
tile.print();
attachTo.print();
diff --git a/src/Tile/TileOnBoard.ts b/src/Tile/TileOnBoard.ts
index b15fcf4..fb0817c 100644
--- a/src/Tile/TileOnBoard.ts
+++ b/src/Tile/TileOnBoard.ts
@@ -10,7 +10,11 @@ export interface Attachment {
export default class TileOnBoard extends Tile {
neighbors: [Tile, Tile, Tile, Tile];
- orientation: number // amount of 90-degree counter-clockwise rotations from original orientation
+ orientation: number; // amount of 90-degree counter-clockwise rotations from original orientation
+ position: {
+ x: number;
+ y: number;
+ }
constructor(center: Feature, sides: [Feature, Feature, Feature, Feature], shield = false, orientation = 0) {
super(center, sides, shield);
@@ -22,6 +26,8 @@ export default class TileOnBoard extends Tile {
}
rotate(rotation = 1) {
+ // We want to rotate clockwise, but orientation stores counter-clockwise rotations,
+ // therefore use the difference, not sum.
this.orientation = this.orientation - rotation;
}