-
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.
Loading status checks…
more refactoring by o1
Showing
4 changed files
with
130 additions
and
111 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
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,47 @@ | ||
import React from 'react'; | ||
import { Board } from '../Board'; | ||
|
||
type GridProps = { | ||
board: Board; | ||
previewBoard?: Board; | ||
currentCellWeights?: number[][]; | ||
showAi?: boolean; | ||
onCellRender?: (i: number, j: number) => React.ReactNode; | ||
startRow?: number; | ||
endRow?: number; | ||
}; | ||
|
||
const Grid: React.FC<GridProps> = ({ | ||
board, | ||
previewBoard, | ||
currentCellWeights, | ||
showAi, | ||
onCellRender, | ||
startRow = 5, | ||
endRow = 20, | ||
}) => { | ||
const cells = []; | ||
for (let i = startRow; i < endRow; i++) { | ||
for (let j = 0; j < 10; j++) { | ||
const isFilled = board.get(i, j); | ||
const weight = currentCellWeights ? currentCellWeights[i][j] : 0; | ||
const isPreview = !isFilled && previewBoard && previewBoard.get(i, j); | ||
|
||
const className = isFilled | ||
? 'cell filled' | ||
: isPreview | ||
? 'cell preview' | ||
: 'cell'; | ||
|
||
cells.push( | ||
<div key={`${i}-${j}`} className={className}> | ||
{showAi && <div className="ai-dot" style={{ opacity: weight }}></div>} | ||
{onCellRender && onCellRender(i, j)} | ||
</div> | ||
); | ||
} | ||
} | ||
return <div className="grid">{cells}</div>; | ||
}; | ||
|
||
export default Grid; |
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,20 @@ | ||
import { useState, useRef } from 'react'; | ||
|
||
export function useMousePosition() { | ||
const [mousePos, setMousePos] = useState<{ i: number; j: number }>(); | ||
const gameAreaRef = useRef<HTMLDivElement>(null); | ||
|
||
const handleMouseMove = (event: React.MouseEvent) => { | ||
if (!gameAreaRef.current) return; | ||
const rect = gameAreaRef.current.getBoundingClientRect(); | ||
const cellWidth = rect.width / 10; | ||
const cellHeight = rect.height / 15; | ||
|
||
setMousePos({ | ||
i: (event.clientY - rect.top) / cellHeight + 5, | ||
j: (event.clientX - rect.left) / cellWidth, | ||
}); | ||
}; | ||
|
||
return { mousePos, gameAreaRef, handleMouseMove }; | ||
} |