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循环有问题吗? (无限循环仅用于测试)。
答案 0 :(得分:2)
您拨打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()
,因此无需再次在循环播放器中调用它。