Unity中基于整数值校验玩家回合有效移动的问题求助
解决游戏回合的无效点击校验问题
嘿,我太懂你熬了20小时卡在最后一步的煎熬了——这种差一点就完成的问题最让人挠头!咱们直接把你想要的校验逻辑嵌进代码里,搞定这个无效点击的拦截。
你提到的核心校验逻辑我理清楚了:
对于X玩家:如果
lastPlayedSquareValueX >= boardSquareValue或者boardSquareValue == 0,允许移动;否则忽略这次点击,不切换回合、不更新状态,就像玩家没点过一样。
O玩家的逻辑也是同理,对应lastPlayedSquareValueO。
接下来咱们直接修改你的GameController代码,把校验逻辑放在PlaySquareButton方法的最开头,这样无效点击会被直接拦截:
using UnityEngine; using UnityEngine.UI; public class GameController : MonoBehaviour { public int playerTurn; //0 = X && 1 = O public int turnCountX; //Tally Player X Turns public int turnCountO; //Tally Player O Turns public GameObject[] bigTurnIcons; //Player Icons Array public Sprite[] playerGamePieces; //GamePieces Array public Button[] gameBoardSquares; //GameBoard Array public int lastPlayedSquareValueX; //Store Value of Last Clicked Square public int lastPlayedSquareValueO; // "" public bool gameOver; // End Game public bool isValidMove; // Check For Valid Move public int boardSquareValue; //Value of Each Square from Value Script public int[] markedSquare; //Win Check Array void Awake() { GameSetup(); } public void GameSetup() { isValidMove = true; playerTurn = 0; //Set Player Turn To X Goes First turnCountX = 0; //Set X Turn Count to 0 turnCountO = 0; //Set O Turn COunt to 0 bigTurnIcons[0].SetActive(true); //Set X Turn Icon to Active bigTurnIcons[1].SetActive(false); //Set O Turn Icon to Inactive // 初始化lastPlayed的值,确保第一回合玩家能点击符合规则的方块 lastPlayedSquareValueX = int.MaxValue; lastPlayedSquareValueO = int.MaxValue; for (int i = 0; i < gameBoardSquares.Length; i++) //Initialize Squares (GameBoard) Array { gameBoardSquares[i].interactable = true; //Make All Squares Clickable gameBoardSquares[i].GetComponent<Image>().sprite = null; //Set TurnIcons To Off } for (int j = 0; j < markedSquare.Length; j++) //Initialize MarkedSquare Array { markedSquare[j] = -100; //Set Value To Wacky Number So It Doesn't Interfere With Formula FOr Win Check } } public void PlaySquareButton(int WhichNumber) //OnClick Function { // 先获取当前点击方块的value boardSquareValue = gameBoardSquares[WhichNumber].GetComponent<Value>().boardSquareValue; // 核心校验逻辑:判断当前点击是否有效 bool isMoveValid = false; if (playerTurn == 0) // X玩家的回合 { // X的有效条件:上次点击的value >= 当前方块value,或者当前方块是0 isMoveValid = (lastPlayedSquareValueX >= boardSquareValue) || (boardSquareValue == 0); } else // O玩家的回合 { // O的有效条件同理,对应lastPlayedSquareValueO isMoveValid = (lastPlayedSquareValueO >= boardSquareValue) || (boardSquareValue == 0); } // 如果移动无效,直接返回,不执行后续操作 if (!isMoveValid) { Debug.Log("无效点击!请选择符合规则的方块"); return; } // 下面是原来的有效移动逻辑,只有校验通过才会执行 markedSquare[WhichNumber] = playerTurn + 1; //OnClick Store Which PLayer Clicked Which Square gameBoardSquares[WhichNumber].image.sprite = playerGamePieces[playerTurn]; //OnClick Check To Place Correct Symbol (GamePiece) gameBoardSquares[WhichNumber].interactable = false; //OnClick Set Button.Interactable To False So It Cannot Be CLicked Again if (turnCountO > 3 || turnCountX > 3) //Check For Wins After 3 Turns { CheckForWinner(); //Iterate Through The MarkedSpaces Array To Check For A Winner } if (playerTurn == 0) { playerTurn = 1; //Player O Turn bigTurnIcons[0].SetActive(false); bigTurnIcons[1].SetActive(true); Debug.Log(boardSquareValue); //Show Value of Clicked Square in Console lastPlayedSquareValueX = boardSquareValue; //Set Last CLicked Value to Current Clicked Square turnCountX++; //Increment Player Turn Count } else { playerTurn = 0; bigTurnIcons[0].SetActive(true); bigTurnIcons[1].SetActive(false); Debug.Log(boardSquareValue); lastPlayedSquareValueO = boardSquareValue; turnCountO++; } } // 保留你原来的CheckForWinner方法逻辑 private void CheckForWinner() { // 你的获胜判断代码 } }
几个关键修改说明:
- 在
GameSetup里初始化lastPlayedSquareValueX和lastPlayedSquareValueO为int.MaxValue,确保第一回合的玩家可以自由点击符合规则的方块。 - 把校验逻辑放在方法最开头,先判断点击有效性,无效则直接终止方法,不会触发任何状态变更。
- 有效移动才会执行后续的放置棋子、切换回合等逻辑,完全符合你想要的“就像玩家没点击过”的效果。
赶紧试试这个修改,应该能完美解决你的问题!
内容的提问来源于stack exchange,提问作者Micah Yarborough
相关产品推荐
相关产品推荐

