Skip to content

Commit

Permalink
Fix when borders are wrong after nonorientation
Browse files Browse the repository at this point in the history
  • Loading branch information
RheingoldRiver committed Oct 4, 2023
1 parent a7a0efa commit fe9eae5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
10 changes: 5 additions & 5 deletions src/components/Board/Board.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
ChevronLeftIcon,
ChevronRightIcon,
} from "@heroicons/react/24/outline";
import { surfaceOrientations } from "../../constants";
import { Orientation, surfaceOrientations } from "../../constants";

export const Board = ({ gridArea }: { gridArea: string }) => {
const { grid, surface, clickBoard } = useContext(GameStateContext);
Expand All @@ -23,14 +23,14 @@ export const Board = ({ gridArea }: { gridArea: string }) => {
}}
>
<div className="flex flex-row justify-center h-6" style={{ gridArea: "topOrientation" }}>
{surfaceOrientations[surface] ? <ChevronRightIcon width={20} /> : ""}
{surfaceOrientations[surface].w !== Orientation.None ? <ChevronRightIcon width={20} /> : ""}
</div>
<div className="flex flex-col justify-center w-6" style={{ gridArea: "leftOrientation" }}>
{surfaceOrientations[surface] ? <ChevronDoubleDownIcon width={20} /> : ""}
{surfaceOrientations[surface].h !== Orientation.None ? <ChevronDoubleDownIcon width={20} /> : ""}
</div>
<div className="flex flex-col justify-center w-6" style={{ gridArea: "rightOrientation" }}>
{surfaceOrientations[surface] ? (
surfaceOrientations[surface].w === true ? (
surfaceOrientations[surface].h === Orientation.Nonorientable ? (
<ChevronDoubleUpIcon width={20} />
) : (
<ChevronDoubleDownIcon width={20} />
Expand All @@ -41,7 +41,7 @@ export const Board = ({ gridArea }: { gridArea: string }) => {
</div>
<div className="flex flex-row justify-center h-6" style={{ gridArea: "botOrientation" }}>
{surfaceOrientations[surface] ? (
surfaceOrientations[surface].h === true ? (
surfaceOrientations[surface].w === Orientation.Orientable ? (
<ChevronLeftIcon width={20} />
) : (
<ChevronRightIcon width={20} />
Expand Down
38 changes: 28 additions & 10 deletions src/components/Grid/paintGrid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { range } from "lodash";
import { PENTOMINOES } from "../../pentominoes";
import { EMPTY_PENTOMINO, PaintedCell, PlacedPentomino, Surface } from "../../constants";
import {
EMPTY_PENTOMINO,
Orientation,
PaintedCell,
PlacedPentomino,
Surface,
surfaceOrientations,
} from "../../constants";

interface NewCoordinates {
newX: number;
Expand Down Expand Up @@ -30,7 +37,9 @@ export function getPaintedBoard(grid: PlacedPentomino[][], surface: Surface): Pa
if (val === 0) return; // the pentomino isn't taking up this square of its grid, return
const rawX = x + px - orientation.center.x;
const rawY = y + py - orientation.center.y;
const { newX, newY } = getCoordinatesToPaint(surface, grid.length, grid[0].length, rawX, rawY);
const height = grid.length;
const width = grid[0].length;
const { newX, newY } = getCoordinatesToPaint(surface, height, width, rawX, rawY);

if (checkOutOfBounds(grid, paintedGrid, newX, newY)) return;

Expand All @@ -40,11 +49,20 @@ export function getPaintedBoard(grid: PlacedPentomino[][], surface: Surface): Pa
cellToPaint.conflict = true;
}
cellToPaint.pentomino = p;
if (px === 0 || orientation.shape[px - 1][py] === 0) cellToPaint.borderTop = true;
if (py === 0 || orientation.shape[px][py - 1] === 0) cellToPaint.borderLeft = true;
if (px === orientation.shape.length - 1 || orientation.shape[px + 1][py] === 0) cellToPaint.borderBot = true;
if (py === orientation.shape[0].length - 1 || orientation.shape[px][py + 1] === 0)
cellToPaint.borderRight = true;
const flipX = outOfBounds(rawY, width) && surfaceOrientations[surface].h === Orientation.Nonorientable;
const flipY = outOfBounds(rawX, height) && surfaceOrientations[surface].w === Orientation.Nonorientable;
if (px === 0 || orientation.shape[px - 1][py] === 0) {
cellToPaint[flipX ? "borderBot" : "borderTop"] = true;
}
if (py === 0 || orientation.shape[px][py - 1] === 0) {
cellToPaint[flipY ? "borderRight" : "borderLeft"] = true;
}
if (px === orientation.shape.length - 1 || orientation.shape[px + 1][py] === 0) {
cellToPaint[flipX ? "borderTop" : "borderBot"] = true;
}
if (py === orientation.shape[0].length - 1 || orientation.shape[px][py + 1] === 0) {
cellToPaint[flipY ? "borderLeft" : "borderRight"] = true;
}
})
);
})
Expand All @@ -67,13 +85,13 @@ export function getCoordinatesToPaint(
};
case Surface.KleinBottle:
return {
newX: outOfBounds(rawY, height) ? orient(wrap(rawX, height), height) : wrap(rawX, height),
newX: outOfBounds(rawY, width) ? orient(wrap(rawX, height), height) : wrap(rawX, height),
newY: wrap(rawY, width),
};
case Surface.ProjectivePlane:
return {
newX: outOfBounds(rawY, height) ? orient(wrap(rawX, height), height) : wrap(rawX, height),
newY: outOfBounds(rawX, width) ? orient(wrap(rawY, width), width) : wrap(rawY, width),
newX: outOfBounds(rawY, width) ? orient(wrap(rawX, height), height) : wrap(rawX, height),
newY: outOfBounds(rawX, height) ? orient(wrap(rawY, width), width) : wrap(rawY, width),
};
case Surface.Torus:
return {
Expand Down
15 changes: 11 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,21 @@ export enum Surface {
KleinBottle, // klein bottle
}

export enum Orientation {
Orientable,
Nonorientable,
None,
}

interface SurfaceOrientations {
[key: number]: { h: boolean; w: boolean };
[key: number]: { h: Orientation; w: Orientation };
}

export const surfaceOrientations: SurfaceOrientations = {
[Surface.Torus]: { h: false, w: false },
[Surface.ProjectivePlane]: { h: true, w: true },
[Surface.KleinBottle]: { h: false, w: true },
[Surface.Rectangle]: { w: Orientation.None, h: Orientation.None },
[Surface.Torus]: { w: Orientation.Orientable, h: Orientation.Orientable },
[Surface.ProjectivePlane]: { w: Orientation.Nonorientable, h: Orientation.Nonorientable },
[Surface.KleinBottle]: { w: Orientation.Orientable, h: Orientation.Nonorientable },
};

export type UrlConfig = {
Expand Down

0 comments on commit fe9eae5

Please sign in to comment.