From fe9eae5acceb19aa575b08771d7f4a32530b766b Mon Sep 17 00:00:00 2001 From: RheingoldRiver <18037011+RheingoldRiver@users.noreply.github.com> Date: Tue, 3 Oct 2023 19:14:20 -0500 Subject: [PATCH] Fix when borders are wrong after nonorientation --- src/components/Board/Board.tsx | 10 ++++----- src/components/Grid/paintGrid.ts | 38 +++++++++++++++++++++++--------- src/constants.ts | 15 +++++++++---- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/src/components/Board/Board.tsx b/src/components/Board/Board.tsx index d8f6387..60fd69c 100644 --- a/src/components/Board/Board.tsx +++ b/src/components/Board/Board.tsx @@ -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); @@ -23,14 +23,14 @@ export const Board = ({ gridArea }: { gridArea: string }) => { }} >
- {surfaceOrientations[surface] ? : ""} + {surfaceOrientations[surface].w !== Orientation.None ? : ""}
- {surfaceOrientations[surface] ? : ""} + {surfaceOrientations[surface].h !== Orientation.None ? : ""}
{surfaceOrientations[surface] ? ( - surfaceOrientations[surface].w === true ? ( + surfaceOrientations[surface].h === Orientation.Nonorientable ? ( ) : ( @@ -41,7 +41,7 @@ export const Board = ({ gridArea }: { gridArea: string }) => {
{surfaceOrientations[surface] ? ( - surfaceOrientations[surface].h === true ? ( + surfaceOrientations[surface].w === Orientation.Orientable ? ( ) : ( diff --git a/src/components/Grid/paintGrid.ts b/src/components/Grid/paintGrid.ts index 6297cf7..00b1a26 100644 --- a/src/components/Grid/paintGrid.ts +++ b/src/components/Grid/paintGrid.ts @@ -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; @@ -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; @@ -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; + } }) ); }) @@ -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 { diff --git a/src/constants.ts b/src/constants.ts index 95dc497..a8f8a42 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -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 = {