diff --git a/src/TicTacToe.js b/src/TicTacToe.js index 06df2ff..b583c76 100644 --- a/src/TicTacToe.js +++ b/src/TicTacToe.js @@ -42,7 +42,7 @@ function SquareRow({ rowId, rows, clickHandler }) { function Map({ mapSize }) { // Config Here - const ticTacToeMaxMapSize = 6 + const ticTacToeMaxMapSize = 5 if (mapSize == undefined) { mapSize = 3; @@ -75,12 +75,13 @@ function Map({ mapSize }) { } updateMap(nextMap) setCurrentMove(currentMove + 1) - if (currentMove + 1 >= mapSize * mapSize){ - setNoWinner(true) - } - if (calculateWinner(row, column,nextMap)) { + let hasWinner = calculateWinner(row, column,nextMap) + if (hasWinner) { setWinner(nextMap[row][column]) } + if (currentMove + 1 >= mapSize * mapSize && !hasWinner){ + setNoWinner(true) + } } function calculateWinner(row, column,map) { @@ -156,7 +157,7 @@ function Map({ mapSize }) { } return ( -
+

{mapSize > ticTacToeMaxMapSize ? "五子棋" : "井字棋"}模式

现在是 {xIsNext ? "X" : "O"} 移动

@@ -169,35 +170,66 @@ function Map({ mapSize }) { ←回到上一个
-

{winner != null ? winner + "赢了" : ""}

+

{winner != null ? winner + "赢了" : ""}

{noWinner ? "没有胜利者" : ""}

); } +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(){ const location = useLocation(); const params = new URLSearchParams(location.search); const useSize = params.get('mapSize'); + let realSize = parseInt(useSize) - if (isNaN(realSize) || realSize < 3 || realSize > 20){ + if (isNaN(realSize) || realSize < 3 || realSize > getMaxBoardSize()){ realSize = 3 } return( -
-
- -
-
- - -
-
-
+
+
+ +
+

当前最大地图大小: {getMaxBoardSize()}

+
+ + +
+
+
+ ) }