所以我正在制作一个井字游戏程序,我正在开发一种能够检测玩家是否赢了的方法。
游戏编程如下:游戏板是一个包含9个整数的int数组。玩家1将能够使用" 1"填充阵列的指定部分,而玩家2使用" 2"填充阵列的指定部分。
现在,在我检查胜利的方法中,我使用的if / else语句非常冗长且复杂,但最终没有编译错误并且程序运行正常,但是当确定时空格以一种触发胜利条件的方式填满,我的方法不是打印它应该的线。
例如:
run:
Player 1, please enter the number of the square that you want to mark (1 - 9)
1
[1, 0, 0, 0, 0, 0, 0, 0, 0]
Player 2, please enter the number of the square that you want to mark (1 - 9)
6
[1, 0, 0, 0, 0, 2, 0, 0, 0]
Player 1, please enter the number of the square that you want to mark (1 - 9)
2
[1, 1, 0, 0, 0, 2, 0, 0, 0]
Player 2, please enter the number of the square that you want to mark (1 - 9)
8
[1, 1, 0, 0, 0, 2, 0, 2, 0]
Player 1, please enter the number of the square that you want to mark (1 - 9)
3
[1, 1, 1, 0, 0, 2, 0, 2, 0]
Player 2, please enter the number of the square that you want to mark (1 - 9)
正如你在最后一行中看到的那样,数组中的前3个整数已被第一个玩家填满,这应该触发了一条消息,表明玩家1已赢了,但没有出现这样的消息。
这是应该获得胜利条件的方法:
public void checkWin() {
int row1 = board[0] + board[1] + board[2];
int row2 = board[3] + board[4] + board[5];
int row3 = board[6] + board[7] + board[8];
int column1 = board[0] + board[3] + board[6];
int column2 = board[1] + board[4] + board[7];
int column3 = board[2] + board[5] + board[8];
int cross1 = board[0] + board[4] + board[8];
int cross2 = board[2] + board[4] + board[6];
int square0 = board[0];
int square1 = board[1];
int square2 = board[2];
int square3 = board[3];
int square4 = board[4];
int square5 = board[5];
int square6 = board[6];
int square7 = board[7];
int square8 = board[8];
if (row1 == 3 | row1 == 6 | row2 == 3 | row2 == 6 | row3 == 3 | row3 == 6|
column1 == 3 | column1 == 6 | column2 == 3 | column2 == 6 | column3 == 3 | column3 == 6|
cross1 == 3 | cross1 == 6 | cross2 == 3 | cross2 == 6) {
} else if ((square0 == 1 && square1 == 1 && square2 == 1) ||
(square3 == 1 && square4 == 1 && square5 == 1) ||
(square6 == 1 && square7 == 1 && square8 == 1) ||
(square0 == 1 && square3 == 1 && square6 == 1) ||
(square1 == 1 && square4 == 1 && square7 == 1) ||
(square2 == 1 && square5 == 1 && square8 == 1) ||
(square0 == 1 && square4 == 1 && square8 == 1) ||
(square2 == 1 && square4 == 1 && square6 == 1)) {
System.out.println("Player 1 has won this game!");
} else if ((square0 == 2 && square1 == 2 && square2 == 2) ||
(square3 == 2 && square4 == 2 && square5 == 2) ||
(square6 == 2 && square7 == 2 && square8 == 2) ||
(square0 == 2 && square3 == 2 && square6 == 2) ||
(square1 == 2 && square4 == 2 && square7 == 2) ||
(square2 == 2 && square5 == 2 && square8 == 2) ||
(square0 == 2 && square4 == 2 && square8 == 2) ||
(square2 == 2 && square4 == 2 && square6 == 2)) {
System.out.println("Player 2 has won this game!");
}
}
我知道它很复杂,但我还没有找到一种方法来缩短它,同时仍然说明该方法应该如何工作。
如果有人能指出我错过或做错了什么,我真的很感激。
谢谢你们!
答案 0 :(得分:1)
这些家伙回答你失败的原因,但我认为你可以用更好的方式写出来。
public void checkWin() {
List<ResultChecker> resultCheckerList = new ArrayList<ResultChecker>();
int row1 = board[0] + board[1] + board[2];
resultCheckerList.add(new ResultChecker(row1/3,row1%3));
int row2 = board[3] + board[4] + board[5];
resultCheckerList.add(new ResultChecker(row2/3,row2%3));
int row3 = board[6] + board[7] + board[8];
resultCheckerList.add(new ResultChecker(row3/3,row3%3));
int column1 = board[0] + board[3] + board[6];
resultCheckerList.add(new ResultChecker(column1/3,column1%3));
int column2 = board[1] + board[4] + board[7];
resultCheckerList.add(new ResultChecker(column2/3,column2%3));
int column3 = board[2] + board[5] + board[8];
resultCheckerList.add(new ResultChecker(column3/3,column3%3));
int cross1 = board[0] + board[4] + board[8];
resultCheckerList.add(new ResultChecker(cross1/3,cross1%3));
int cross2 = board[2] + board[4] + board[6];
resultCheckerList.add(new ResultChecker(cross2/3,cross2%3));
for(ResultChecker rc:resultCheckerList) {
if(rc.isWin()) {
if(rc.isFirstPlayer()) {
System.out.println("Player 1 has won this game!");
} else {
System.out.println("Player 2 has won this game!");
}
break;
}
}
}
private Class ResultChecker {
private int divResult;
private int modResult;
public ResultChecker(int divResult,int modResult) {
this.divResult = divResult;
this.modResult = modResult;
}
public boolean isWin() {
return this.modResult == 0;
}
public boolean isFirstPlayer() {
return this.divResult == 1;
}
}
答案 1 :(得分:0)
if A {
// does nothing
} else if B {
// Player 1 won
} else if C {
// Player 2 won
}
其中A检查是否有获胜组合,而B和C检查哪个玩家实际获胜。 在我看来,它应该是:
if A {
// Winning game, checks winner
if B {
// Player 1 won
} else {
// Player 2 won
}
}