我有以下代码
//Querying for move int playerMove = currentPlayer.PlayCard(myBoard); //Making move try { playMove(playerMove, currentPlayer); } catch (IndexOutOfBoundsException e) { System.out.println("Sorry, I don't think you can do that..."); }
播放器所做的移动需要与ArrayList中的索引相关联.现在,我的代码偏向于正确进行无效移动的玩家的例外,但我想知道如何修改它以便让玩家继续被移动直到他们成为有效的移动.
谢谢!:)
使用while循环
while(!IsMoveValid) { int playerMove = currentPlayer.PlayCard(myboard); IsMoveValid = CheckMoveValidity(playerMove, myBoard); } playMove(playerMove, currentPlayer);
和
public bool CheckMoveValidity(int move, Board board) { if (move > 0) && (move < board.Length) { return true; } else { return false; } // you could make this helper method shorter by doing // return (move > 0) && (move < board.Length); }
注意这不会在逻辑中使用异常:)