Skip to content

Commit

Permalink
Initial hexgrid
Browse files Browse the repository at this point in the history
  • Loading branch information
butzopower committed Dec 31, 2021
1 parent 5aea100 commit a8ed257
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 0 deletions.
141 changes: 141 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"author": "",
"license": "ISC",
"devDependencies": {
"@types/chai": "^4.3.0",
"@types/mocha": "^9.0.0",
"chai": "^4.3.4",
"mocha": "^9.1.3",
"ts-node": "^10.4.0",
"typescript": "^4.5.4"
Expand Down
61 changes: 61 additions & 0 deletions tests/hexgrid.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { describe, it } from "mocha";
import { expect } from "chai";

class Hex {
private readonly parent: HexGrid;
private readonly x: number;
private readonly y: number;

constructor(parent: HexGrid, x: number) {
this.parent = parent;
this.x = x;
this.y = 0;
}

get neighbors(): Hex[] {
return this.parent.neighborsOf(this.x, this.y);
}
}

class HexGrid {
private readonly hexArray: Hex[]

constructor(width: number, height: number) {
const size = width * height;
this.hexArray = Array.from(new Array(size)).map((_, x) => new Hex(this, x));
}

at(x: number, y: number): Hex {
return this.hexArray[x];
}

neighborsOf(x: number, y: number): Hex[] {
const left = x - 1;
const right = x + 1;

return [this.hexArray[left], this.hexArray[right]]
.filter(hex => hex !== undefined);
}
}

describe('a hex grid', () => {
describe('with a single hex', () => {
it('has no neighbors', () => {
const grid = new HexGrid(1, 1);
const hex = grid.at(0,0);
expect(hex.neighbors).to.be.empty;
});
});

describe('a completely horizontal map', () => {
it('has neighbors left and right of tiles', () => {
const grid = new HexGrid(5, 1);

expect(grid.at(0,0).neighbors).to.have.members([grid.at(1,0)]);
expect(grid.at(1,0).neighbors).to.have.members([grid.at(0,0), grid.at(2,0)]);
expect(grid.at(2,0).neighbors).to.have.members([grid.at(1,0), grid.at(3,0)]);
expect(grid.at(3,0).neighbors).to.have.members([grid.at(2,0), grid.at(4,0)]);
expect(grid.at(4,0).neighbors).to.have.members([grid.at(3,0)]);
});
});
});

0 comments on commit a8ed257

Please sign in to comment.