-
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.
Merge pull request #7 from philfreshman/feature-maze-grid
Feature maze grid
- Loading branch information
Showing
8 changed files
with
102 additions
and
15 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
{ | ||
"printWidth": 180, | ||
"semi": false | ||
"semi": false, | ||
"plugins": ["prettier-plugin-organize-attributes", "prettier-plugin-organize-imports", "prettier-plugin-tailwindcss"], | ||
"tailwindFunctions": ["tw", "clsx"] | ||
} |
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 |
---|---|---|
|
@@ -93,4 +93,11 @@ | |
} | ||
|
||
|
||
.checked{ | ||
background-color: gray; | ||
} | ||
|
||
.wall{ | ||
background-color: black; | ||
} | ||
|
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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import Board from "@/components/board" | ||
import React, { memo } from "react" | ||
|
||
const MemoizedBoard = memo(Board) // Memoize the Board component | ||
|
||
export default function Home() { | ||
return ( | ||
<div className="w-full h-full flex justify-center items-center p-7 min-w-[900px] "> | ||
<div className="w-full h-full overflow-scroll "> | ||
<Board /> | ||
</div> | ||
<div className="flex h-full w-full items-center justify-center overflow-scroll p-7"> | ||
<MemoizedBoard /> | ||
</div> | ||
) | ||
} |
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 |
---|---|---|
@@ -1,3 +1,24 @@ | ||
"use client" | ||
|
||
import Box from "@/components/ui/box" | ||
import { useBoxes } from "@/lib/maze/maze" | ||
|
||
export default function Board() { | ||
return <div>Board</div> | ||
const { matrix, toggleBox } = useBoxes() | ||
|
||
return ( | ||
<div className={"flex rotate-90 transform justify-center md:rotate-0"}> | ||
<table> | ||
<tbody> | ||
{matrix.map((row, i) => ( | ||
<tr key={i}> | ||
{row.map((value, j) => ( | ||
<Box id={`B${i}:${j}`} key={j} isToggled={value} toggleBox={() => toggleBox(i, j)} /> | ||
))} | ||
</tr> | ||
))} | ||
</tbody> | ||
</table> | ||
</div> | ||
) | ||
} |
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,23 @@ | ||
import { memo } from "react" | ||
|
||
interface BoxProps { | ||
id: string | ||
isToggled: boolean | ||
toggleBox: () => void | ||
} | ||
|
||
const Box = memo<BoxProps>(({ id, isToggled, toggleBox }) => { | ||
const handleMouseDown = () => toggleBox() | ||
const handleMouseOver = (e: any) => e.buttons === 1 && toggleBox() | ||
|
||
return ( | ||
<td | ||
id={id} | ||
className={`md-min-w-[22px] h-[18px] min-w-[18px] border border-sky-500 hover:cursor-pointer sm:h-[20px] sm:min-w-[20px] md:h-[22px] lg:h-[24px] lg:min-w-[24px] ${isToggled ? "bg-gray-600" : "bg-white"}`} | ||
onMouseDown={handleMouseDown} | ||
onMouseOver={handleMouseOver} | ||
/> | ||
) | ||
}) | ||
|
||
export default Box |
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,31 @@ | ||
"use client" | ||
|
||
import { useState } from "react" | ||
|
||
export function useBoxes() { | ||
// config | ||
const width = 40 | ||
const height = 30 | ||
|
||
// Initialize matrix as a two-dimensional array filled with zeros | ||
const [matrix, setMatrix] = useState(() => { | ||
return Array(height) | ||
.fill(0) | ||
.map(() => Array(width).fill(0)) | ||
}) | ||
|
||
// handlers | ||
const toggleBox = (i: number, j: number) => { | ||
let box = document.querySelector(`#B${i}\\:${j}`) | ||
if (box && !box.classList.contains("wall")) { | ||
box!.classList.toggle("checked") | ||
} | ||
setMatrix((prevMatrix) => { | ||
const newMatrix = prevMatrix.map((row) => [...row]) | ||
newMatrix[i][j] = newMatrix[i][j] === 0 ? 1 : 0 | ||
return newMatrix | ||
}) | ||
} | ||
|
||
return { width, height, matrix, toggleBox } | ||
} |