Skip to content

Commit

Permalink
revert edge detection fix attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
RheingoldRiver committed Oct 25, 2023
1 parent 2e0df15 commit 5417d60
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
5 changes: 2 additions & 3 deletions src/components/Cell/Cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ export const Cell = ({
return `1px solid ${borderColor}`;
}
function backgroundColor() {
if (cell.conflict === cell.pentomino.pentomino.name && hasPentomino === true)
return { class: "bg-red-700", style: "" };
if (cell.conflict && hasPentomino) return { class: "bg-red-700", style: "" };
if (cell.pentomino.pentomino.name === PENTOMINOES.R.name)
return { class: "bg-gray-500 dark:bg-gray-600", style: "" };
if (hasPentomino === true)
if (hasPentomino)
return { class: "", style: appPreferences.displayColors[pentominoColors[cell.pentomino.pentomino.name]] };
return { class: "bg-gray-300 dark:bg-gray-800", style: "" };
}
Expand Down
18 changes: 17 additions & 1 deletion src/components/GameStateProvider/paintGrid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ test("no incorrect conflicts where 2 pieces touch", () => {
grid,
false
);
paintCell(
paintedGrid,
{
x: 4,
y: 1,
pentomino: PENTOMINOES.V,
rotation: 0,
reflection: 0,
},
SURFACES.Rectangle,
grid,
false
);
paintCell(
paintedGrid,
{
Expand All @@ -75,5 +88,8 @@ test("no incorrect conflicts where 2 pieces touch", () => {
grid,
false
);
expect(paintedGrid[0][1].conflict).toBe(false);
// expect(paintedGrid[0][1].conflict).toBe(false);
expect(paintedGrid[0][0].conflict).toBe(true);
expect(paintedGrid[4][1].conflict).toBe(true);
expect(paintedGrid[3][1].conflict).toBe(true);
});
21 changes: 10 additions & 11 deletions src/components/GameStateProvider/paintGrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ export function getPaintedBoard(
return paintedGrid;
}

export const emptyPaintedGrid = (h: number, w: number): PaintedCell[][] => {
export const emptyPaintedGrid = (h: number, w: number) => {
return range(h).map((x) =>
range(w).map((y) => {
return {
pentomino: EMPTY_PENTOMINO(x, y),
conflict: PENTOMINOES.None.name,
conflict: false,
center: false,
borders: {
borderTop: false,
Expand Down Expand Up @@ -69,14 +69,14 @@ export const paintCell = (
const width = grid[0].length;
const { newX, newY } = getCoordinatesToPaint(surface, height, width, rawX, rawY);

if (checkOutOfBounds(grid, paintedGrid, newX, newY, p.pentomino.name)) return;
if (checkOutOfBounds(grid, paintedGrid, newX, newY)) return;

// ok should be a valid placement now
const cellToPaint = paintedGrid[newX][newY];
cellToPaint.hovered = hovered;
cellToPaint.center = px === orientation.center.x && py === orientation.center.y;
if (cellToPaint.pentomino.pentomino.name !== PENTOMINOES.None.name) {
cellToPaint.conflict = cellToPaint.pentomino.pentomino.name;
cellToPaint.conflict = true;
}
cellToPaint.pentomino = p;
const flipX = outOfBounds(rawY, width) && surface.orientation.h === Orientation.Nonorientable;
Expand Down Expand Up @@ -207,8 +207,7 @@ function checkOutOfBounds(
grid: PlacedPentomino[][],
paintedGrid: PaintedCell[][],
newX: number,
newY: number,
name: string
newY: number
): boolean {
const height = grid.length;
const width = grid[0].length;
Expand All @@ -217,20 +216,20 @@ function checkOutOfBounds(
if (newX < 0 || newX > height - 1) {
const correctedX = newX < 0 ? 0 : height - 1;
if (newY < 0) {
paintedGrid[correctedX][0].conflict = name;
paintedGrid[correctedX][0].conflict = true;
} else if (newY > width - 1) {
paintedGrid[correctedX][width - 1].conflict = name;
paintedGrid[correctedX][width - 1].conflict = true;
} else {
paintedGrid[correctedX][newY].conflict = name;
paintedGrid[correctedX][newY].conflict = true;
}
return true;
}
if (newY < 0) {
paintedGrid[newX][0].conflict = name;
paintedGrid[newX][0].conflict = true;
return true;
}
if (newY > width - 1) {
paintedGrid[newX][width - 1].conflict = name;
paintedGrid[newX][width - 1].conflict = true;
return true;
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface Borders {

export interface PaintedCell {
pentomino: PlacedPentomino;
conflict: string;
conflict: boolean;
borders: Borders;
center: boolean;
hovered: boolean;
Expand Down

0 comments on commit 5417d60

Please sign in to comment.