package uk.ac.sheffield.aca14js;
public class Chess {
public static void main(String[] arg) {
Board chessBoard = new Board();
Pieces white = new Pieces(chessBoard,1);
Pieces black = new Pieces(chessBoard,0);
Display board = new TextDisplay();
Player one,two = null;
one = new HumanPlayer("John",white,chessBoard,two);
two = new HumanPlayer("opponent",black,chessBoard,one);
one.setOpponent(two);
int x = 0;
while (x == 0){
board.showPiecesOnBoard(chessBoard.getData());
while (one.makeMove() != true){
System.out.println("Choose a valid move");
board.showPiecesOnBoard(chessBoard.getData());
one.makeMove();
}
board.showPiecesOnBoard(chessBoard.getData());
while (two.makeMove() != true){
System.out.println("Choose a valid move");
board.showPiecesOnBoard(chessBoard.getData());
two.makeMove();
}
}
}
}
我想要的是让玩家选择一个动作,如果有效,则进行该动作,如果没有,则重新执行该动作。
如果选择的每一步都是有效的,那么游戏会进行得很好,玩家轮流交替出现。
如果一个玩家做出了无效的动作,那么即使他们之后做出了有效的动作,游戏也会停留在该处,直到他们做出另一次无效的动作。
我的while循环有问题吗?(无限循环目前仅用于测试)。
您打给的电话太多makeMove()
:
while (one.makeMove() != true){
System.out.println("Choose a valid move");
board.showPiecesOnBoard(chessBoard.getData());
one.makeMove(); // remove this
}
board.showPiecesOnBoard(chessBoard.getData());
while (two.makeMove() != true){
System.out.println("Choose a valid move");
board.showPiecesOnBoard(chessBoard.getData());
two.makeMove(); // remove this
}
由于您已经makeMove()
在循环的条件中进行调用,因此无需在循环的主体中再次调用它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句