-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support random boards & improve new board UX (#70)
- Loading branch information
1 parent
7920966
commit e7672b0
Showing
16 changed files
with
735 additions
and
137 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { expect, test } from "vitest"; | ||
import { EMPTY_GRID } from "../../constants"; | ||
import { PENTOMINOES } from "../../pentominoes"; | ||
import { getAdjacentArea, invalidSolve } from "./bottomToolbar.utils"; | ||
|
||
test("negative control", () => { | ||
const grid = EMPTY_GRID(8, 8); | ||
expect(getAdjacentArea(grid)).toBe(64); | ||
}); | ||
|
||
test("works with some random tiles", () => { | ||
const grid = EMPTY_GRID(8, 8); | ||
grid[0][1].pentomino = PENTOMINOES.R; | ||
grid[5][6].pentomino = PENTOMINOES.R; | ||
grid[2][3].pentomino = PENTOMINOES.R; | ||
grid[7][7].pentomino = PENTOMINOES.R; | ||
expect(getAdjacentArea(grid)).toBe(60); | ||
expect(invalidSolve(grid)).toBe(false); | ||
}); | ||
|
||
test("validating a corner properly: trivial case", () => { | ||
const grid = EMPTY_GRID(8, 8); | ||
grid[0][1].pentomino = PENTOMINOES.R; | ||
grid[1][0].pentomino = PENTOMINOES.R; | ||
expect(invalidSolve(grid)).toBe(true); | ||
}); | ||
|
||
test("validating a corner properly: trivial case", () => { | ||
const grid = EMPTY_GRID(8, 8); | ||
grid[6][0].pentomino = PENTOMINOES.R; | ||
grid[7][1].pentomino = PENTOMINOES.R; | ||
expect(invalidSolve(grid)).toBe(true); | ||
}); | ||
|
||
test("validating a corner properly: rectangle case", () => { | ||
const grid = EMPTY_GRID(8, 8); | ||
grid[0][1].pentomino = PENTOMINOES.R; | ||
grid[1][1].pentomino = PENTOMINOES.R; | ||
grid[2][0].pentomino = PENTOMINOES.R; | ||
expect(invalidSolve(grid)).toBe(true); | ||
}); | ||
|
||
test("validating a corner properly: rectangle case 2", () => { | ||
const grid = EMPTY_GRID(8, 8); | ||
grid[1][0].pentomino = PENTOMINOES.R; | ||
grid[1][1].pentomino = PENTOMINOES.R; | ||
grid[1][2].pentomino = PENTOMINOES.R; | ||
grid[0][3].pentomino = PENTOMINOES.R; | ||
expect(invalidSolve(grid)).toBe(true); | ||
}); | ||
|
||
test("validating a diagonal corner properly", () => { | ||
const grid = EMPTY_GRID(8, 8); | ||
grid[0][2].pentomino = PENTOMINOES.R; | ||
grid[1][1].pentomino = PENTOMINOES.R; | ||
grid[2][0].pentomino = PENTOMINOES.R; | ||
expect(invalidSolve(grid)).toBe(true); | ||
|
||
const grid2 = EMPTY_GRID(8, 8); | ||
grid2[3][0].pentomino = PENTOMINOES.R; | ||
grid2[2][1].pentomino = PENTOMINOES.R; | ||
grid2[1][2].pentomino = PENTOMINOES.R; | ||
grid2[0][3].pentomino = PENTOMINOES.R; | ||
expect(invalidSolve(grid2)).toBe(true); | ||
}); | ||
|
||
test("validating a center properly", () => { | ||
const grid = EMPTY_GRID(8, 8); | ||
grid[4][5].pentomino = PENTOMINOES.R; | ||
grid[4][3].pentomino = PENTOMINOES.R; | ||
grid[3][4].pentomino = PENTOMINOES.R; | ||
grid[5][4].pentomino = PENTOMINOES.R; | ||
expect(invalidSolve(grid)).toBe(true); | ||
}); | ||
|
||
test("validating a center properly", () => { | ||
const grid = EMPTY_GRID(8, 8); | ||
grid[0][3].pentomino = PENTOMINOES.R; | ||
grid[0][1].pentomino = PENTOMINOES.R; | ||
grid[1][2].pentomino = PENTOMINOES.R; | ||
grid[7][7].pentomino = PENTOMINOES.R; | ||
expect(invalidSolve(grid)).toBe(true); | ||
}); | ||
|
||
test("validating a corner 2x2 square", () => { | ||
const grid = EMPTY_GRID(8, 8); | ||
grid[0][2].pentomino = PENTOMINOES.R; | ||
grid[1][2].pentomino = PENTOMINOES.R; | ||
grid[2][0].pentomino = PENTOMINOES.R; | ||
grid[2][1].pentomino = PENTOMINOES.R; | ||
expect(invalidSolve(grid)).toBe(true); | ||
|
||
const grid2 = EMPTY_GRID(8, 8); | ||
grid2[0][5].pentomino = PENTOMINOES.R; | ||
grid2[1][5].pentomino = PENTOMINOES.R; | ||
grid2[2][7].pentomino = PENTOMINOES.R; | ||
grid2[2][6].pentomino = PENTOMINOES.R; | ||
expect(invalidSolve(grid2)).toBe(true); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { cloneDeep } from "lodash"; | ||
import { Dimensions, PlacedPentomino } from "../../constants"; | ||
import { Coordinates, PENTOMINOES } from "../../pentominoes"; | ||
|
||
export const PRESET_SIZES: Dimensions[] = [ | ||
{ | ||
height: 8, | ||
width: 8, | ||
}, | ||
{ | ||
height: 5, | ||
width: 12, | ||
}, | ||
{ | ||
height: 6, | ||
width: 10, | ||
}, | ||
{ | ||
height: 12, | ||
width: 5, | ||
}, | ||
{ | ||
height: 10, | ||
width: 6, | ||
}, | ||
]; | ||
|
||
export const RANDOM_TERRAIN_ALLOWED: Dimensions = { | ||
height: 8, | ||
width: 8, | ||
}; | ||
|
||
const adjacentCells: Coordinates[] = [ | ||
{ x: -1, y: 0 }, | ||
{ x: 1, y: 0 }, | ||
{ x: 0, y: 1 }, | ||
{ x: 0, y: -1 }, | ||
]; | ||
|
||
export function invalidSolve(gridToCheck: PlacedPentomino[][]) { | ||
const area = getAdjacentArea(gridToCheck); | ||
return area % 5 !== 0; | ||
} | ||
|
||
export function getAdjacentArea(gridToCheck: PlacedPentomino[][]) { | ||
const grid = cloneDeep(gridToCheck); | ||
const tileQueue: PlacedPentomino[] = []; | ||
let x = 0; | ||
let firstCell: PlacedPentomino; | ||
do { | ||
firstCell = grid[x][0]; | ||
x++; | ||
} while (firstCell.pentomino.name !== PENTOMINOES.None.name); | ||
tileQueue.push(firstCell); | ||
let area = 0; | ||
firstCell.pentomino = PENTOMINOES.Found; | ||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
const curCell = tileQueue.pop(); | ||
if (curCell === undefined) break; | ||
area++; | ||
adjacentCells.forEach(({ x, y }) => { | ||
const curX = curCell.coordinates.x + x; | ||
const curY = curCell.coordinates.y + y; | ||
if (curX < 0 || curX >= grid.length) return; | ||
if (curY < 0 || curY >= grid[0].length) return; | ||
const nextCell = grid[curX][curY]; | ||
if (nextCell.pentomino.name !== PENTOMINOES.None.name) return; | ||
nextCell.pentomino = PENTOMINOES.Found; | ||
tileQueue.push(nextCell); | ||
}); | ||
} | ||
return area; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.