diff --git a/src/models/tiles.test.ts b/src/models/tiles.test.ts new file mode 100644 index 0000000..091048d --- /dev/null +++ b/src/models/tiles.test.ts @@ -0,0 +1,14 @@ +import { tilesInRange } from "./tiles"; + +describe("tilesInRange()", () => { + /** + * [ ] [X] [X] + * [ ] [X] [.] [X] + * [ ] [ ] [X] [X] [ ] + * + */ + it("returns tilesID[] in range 1 from origin tile", () => { + const got = tilesInRange("1,-2", { range: 1 }); + expect(got).toEqual(["1,-3", "2,-3", "0,-2", "2,-2", "1,-1", "2,-1"]); + }); +}); diff --git a/src/models/tiles.ts b/src/models/tiles.ts index 42dfdf9..fdca1fb 100644 --- a/src/models/tiles.ts +++ b/src/models/tiles.ts @@ -1,3 +1,30 @@ +/** + * ``` + * [ ] [ ] [ ] [ ] [ ] + * [ ] [ ] [ ] [ ] [ ] [ ] + * [ ] [ ] [ ] [ ] [ ] [ ] [ ] + * [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] + * [ ] [ ] [ ] [ ] [ ] [ ] [ ] + * [ ] [ ] [ ] [ ] [ ] [ ] + * [ ] [ ] [ ] [ ] [ ] + * + * => + * + * [-2,-3] [-1,-3] [+0,-3] [1,-3] [2,-3] + * + * [-3,-2] [-2,-2] [-1,-2] [+0,-2] [+1,-2] [+2,-2] + * + * [-3,-1] [-2,-1] [-1,-1] [+0,-1] [+1,-1] [+2,-1] [+3,-1] + * + * [-4,+0] [-3,+0] [-2,+0] [-1,+0] [+0,+0] [+1,+0] [+2,+0] [+3,+0] + * + * [-3,+1] [-2,+1] [-1,+1] [+0,+1] [+1,+1] [+2,+1] [+3,+1] + * + * [-3,+2] [-2,+2] [-1,+2] [+0,+2] [+1,+2] [+2,+2] + * + * [-2,+3] [-1,+3] [+0,+3] [1,+3] [2,+3] + * ``` + */ export const tiles = [ // 1st row "-2,-3", @@ -75,7 +102,7 @@ export function row(n: -3 | -2 | -1 | 0 | 1 | 2 | 3): TileID[] { return tiles.filter((id) => coordinates(id).y === n); } -export function tilesInRange(tile: TileID, range = 1): TileID[] { +export function tilesInRange(tile: TileID, { range } = { range: 1 }): TileID[] { const { x, y } = coordinates(tile); // TODO use range param instead of fixed array?