@khazh/tic-tac-toe-react
Version:
A customizable Tic Tac Toe game component for React with AI opponent, configurable board size, and win conditions
30 lines • 1.06 kB
JavaScript
import { useState } from "react";
export const useWinLength = (initialBoardSize = 3, initialWinLength) => {
const [winLength, setWinLength] = useState(initialWinLength || Math.min(4, initialBoardSize));
const changeWinLength = (newWinLength, boardSize) => {
// Win length cannot exceed board size and must be at least 3
if (newWinLength >= 3 && newWinLength <= boardSize) {
setWinLength(newWinLength);
}
};
const getAvailableWinLengths = (boardSize) => {
const lengths = [];
for (let i = 3; i <= boardSize; i++) {
lengths.push(i);
}
return lengths;
};
const getDefaultWinLength = (boardSize) => {
// Default to 4 in a row for most board sizes, but adapt for smaller boards
if (boardSize >= 4)
return 4;
return boardSize; // For 3x3, use 3 in a row
};
return {
winLength,
changeWinLength,
getAvailableWinLengths,
getDefaultWinLength,
};
};
//# sourceMappingURL=useWinLength.js.map