TicTacToe: Update

This commit is contained in:
Kagura 2024-11-22 18:48:03 +08:00
parent 4fefa48079
commit 1b465ee07a

View file

@ -42,7 +42,7 @@ function SquareRow({ rowId, rows, clickHandler }) {
function Map({ mapSize }) { function Map({ mapSize }) {
// Config Here // Config Here
const ticTacToeMaxMapSize = 6 const ticTacToeMaxMapSize = 5
if (mapSize == undefined) { if (mapSize == undefined) {
mapSize = 3; mapSize = 3;
@ -75,12 +75,13 @@ function Map({ mapSize }) {
} }
updateMap(nextMap) updateMap(nextMap)
setCurrentMove(currentMove + 1) setCurrentMove(currentMove + 1)
if (currentMove + 1 >= mapSize * mapSize){ let hasWinner = calculateWinner(row, column,nextMap)
setNoWinner(true) if (hasWinner) {
}
if (calculateWinner(row, column,nextMap)) {
setWinner(nextMap[row][column]) setWinner(nextMap[row][column])
} }
if (currentMove + 1 >= mapSize * mapSize && !hasWinner){
setNoWinner(true)
}
} }
function calculateWinner(row, column,map) { function calculateWinner(row, column,map) {
@ -156,7 +157,7 @@ function Map({ mapSize }) {
} }
return ( return (
<div className="rounded-xl bg-sky-50 shadow-xl table items-center space-y-2 container mx-auto content-center"> <div className="rounded-xl bg-sky-50 shadow-xl flex flex-col items-center space-y-2 container mx-auto content-center gap-1">
<p className="font-sans text-2xl text-center ">{mapSize > ticTacToeMaxMapSize ? "五子棋" : "井字棋"}模式</p> <p className="font-sans text-2xl text-center ">{mapSize > ticTacToeMaxMapSize ? "五子棋" : "井字棋"}模式</p>
<p className="text-xl text-center ">现在是 {xIsNext ? "X" : "O"} 移动</p> <p className="text-xl text-center ">现在是 {xIsNext ? "X" : "O"} 移动</p>
<div className="flex flex-row justify-center items-center"> <div className="flex flex-row justify-center items-center">
@ -169,35 +170,66 @@ function Map({ mapSize }) {
&#8592;回到上一个 &#8592;回到上一个
</button> </button>
</div> </div>
<p className="text-xl text-center">{winner != null ? winner + "赢了" : ""} </p> <p className="text-2xl text-center">{winner != null ? winner + "赢了" : ""} </p>
<p className="text-xl text-center">{noWinner ? "没有胜利者" : ""} </p> <p className="text-xl text-center">{noWinner ? "没有胜利者" : ""} </p>
</div> </div>
); );
} }
function getMaxBoardSize() {
const screenWidth = window.innerWidth;
const screenHeight = window.innerHeight;
const squareSize = 34;
const otherWidthUse = 48*2;
const squaresHorizontally = Math.floor((screenWidth - otherWidthUse) / squareSize);
const squaresVertically = Math.floor(screenHeight / squareSize);
if (squaresHorizontally > 20 && squaresVertically > 20){
return 20
}
return squaresHorizontally < squaresVertically ? squaresHorizontally : squaresVertically;
}
export default function Game(){ export default function Game(){
const location = useLocation(); const location = useLocation();
const params = new URLSearchParams(location.search); const params = new URLSearchParams(location.search);
const useSize = params.get('mapSize'); const useSize = params.get('mapSize');
let realSize = parseInt(useSize) let realSize = parseInt(useSize)
if (isNaN(realSize) || realSize < 3 || realSize > 20){ if (isNaN(realSize) || realSize < 3 || realSize > getMaxBoardSize()){
realSize = 3 realSize = 3
} }
return( return(
<div className="min-h-svh flex flex-col items-center justify-center bg-slate-50 p-2 sm:p-4"> <div className="min-h-screen flex flex-col items-center justify-center bg-gradient-to-br from-cyan-200 to-pink-200 via-white p-6 sm:p-10">
<div className="table items-center p-10"> <div className="w-full max-w-4xl bg-white rounded-xl shadow-xl p-6 sm:p-12 space-y-6">
<Map mapSize={realSize}/> <Map mapSize={realSize} />
<div className="rounded-xl bg-green-50 shadow-md flex space-x-2 container mx-auto"> <div className="rounded-xl bg-green-50 shadow-lg flex flex-col items-center sm:space-y-4 p-6 sm:p-8 space-y-3 w-full">
<form className="flex gap-x-3" > <p>当前最大地图大小 {getMaxBoardSize()}</p>
<label className="bg-green-50"> <form className="flex flex-col sm:flex-row gap-4 sm:gap-8 items-center sm:text-center w-full">
地图大小 <label className="text-lg sm:text-xl bg-green-50 border border-green-200 p-2 sm:p-4 rounded-md w-full sm:w-auto">
<input name="mapSize" defaultValue={realSize} type="number" /> 地图大小
</label> <input
<button type="submit" className="bg-cyan-200 rounded-md text-sm">确认</button> className="sm:w-20 sm:h-10 mt-2 sm:mt-0 border border-sky-300 rounded-md p-2 w-full"
</form> name="mapSize"
</div> defaultValue={realSize}
</div> type="number"
/>
</label>
<button
type="submit"
className="bg-cyan-300 hover:bg-cyan-400 text-white rounded-md px-4 py-2 sm:px-6 sm:py-3 text-sm sm:text-lg mt-4 sm:mt-0"
>
确认
</button>
</form>
</div> </div>
</div>
</div>
) )
} }