我是Java新手,从事简单的任务-TicTacToe控制台游戏。今天我遇到了一个问题,在完成while
循环后,我无法继续使用主方法。该项目有两个类——Main
和Field
。Field对所有游戏场地更新都有响应。我在main方法的do while循环中调用Field
方法,但当结果在循环中时,循环必须完成,main方法必须继续(它应该询问用户是否想再次播放)。不幸的是,程序在执行while
循环后停止。
这是我的代码:
import java.io.IOException;
import java.util.Scanner;
public class Main {
private static char playerSym, compSym;
private static int Sym;
public static int playerChoice;
public static boolean result;
public static void main(String args[]) throws IOException {
Field field = new Field();
// start of the game
System.out.println("Let`s play");
System.out.println("Choose your symbol please");
System.out.println("0='O', 1='X'");
Scanner sc = new Scanner(System.in);
Sym = sc.nextInt();
while (Sym != 0 & Sym != 1) {
System.out
.println("The symbol you entered is incorrect, please repeat");
System.out.println("0='O', 1='X'");
Sym = sc.nextInt();
}
// setting player character
if (Sym == 1) {
playerSym = 'X';
compSym = 'O';
} else if (Sym == 0) {
playerSym = 'O';
compSym = 'Х';
}
System.out.println("There is a game field");
System.out.println("Please choose the cell number you`d like to fill with " + playerSym);
field.firstShowFields();
do {
playerChoice = (Integer) sc.nextInt();
field.updateFields(playerChoice, playerSym);
field.showFields(field.fields);
} while (result==false);
System.out.println("Want to play once more? Y-Yes, N-No");
char answer = (char) System.in.read();
switch (answer) {
case 'Y':
System.out.println("Restarting the game");
break;
case 'N':
System.out.println("Thank you! Bye-Bye!");
break;
default:
break;
}
}
}
public class Field {
public static final int FIELD_SIZE = 3;
// private static final char DEFAULT_CHAR=' ';
public char[][] fields;
public boolean result = true;
public char playerSym;
public Field() {
fields = new char[FIELD_SIZE][FIELD_SIZE];
}
public void firstShowFields() {
int cellValue = 1;
for (int i = 0; i < FIELD_SIZE; i++) {
for (int j = 0; j < FIELD_SIZE; j++) {
System.out.print("[" + cellValue + "]");
cellValue++;
}
System.out.println();
}
}
public char[][] updateFields(int choice, char sym) {
playerSym = sym;
int cellValue = 1;
int playerChoice = choice;
do {
for (int i = 0; i < FIELD_SIZE; i++) {
for (int j = 0; j < FIELD_SIZE; j++) {
if (playerChoice == cellValue) {
fields[i][j] = (char) playerSym;
} else if (fields[i][j] == (char) playerSym) {
fields[i][j] = (char) playerSym;
} else {
fields[i][j] = (char) ('0' + cellValue);
}
cellValue++;
}
}
this.checkWin(fields, playerSym);
return fields;
} while (this.checkWin(fields, playerSym) == false);
}
public void showFields(char[][] fields) {
this.fields = fields;
for (int i = 0; i < FIELD_SIZE; i++) {
for (int j = 0; j < FIELD_SIZE; j++) {
System.out.print("[" + fields[i][j] + "]");
}
System.out.println();
}
}
public boolean checkWin(char[][] field, char playerSym) {
char[][] checkField = field;
this.playerSym = playerSym;
// checkline
if (((checkField[0][0] == checkField[0][1]) && (checkField[0][1] == checkField[0][2]))
|| ((checkField[1][0] == checkField[1][1]) && (checkField[1][1] == checkField[1][2]))
|| ((checkField[2][0] == checkField[2][1]) && (checkField[2][1] == checkField[2][2]))) {
System.out.println("The game is over. The winner is player " + playerSym);
return true;
}
// checkraw
else if (((checkField[0][0] == checkField[1][0]) && (checkField[1][0] == checkField[2][0]))
|| ((checkField[0][1] == checkField[1][1]) && (checkField[1][1] == checkField[2][1]))
|| ((checkField[0][2] == checkField[1][2]) && (checkField[1][2] == checkField[2][2]))) {
System.out.println("The game is over. The winner is player " + playerSym);
return result = true;
} // checkdiagonal
else if (((checkField[0][0] == checkField[1][1]) && (checkField[1][1] == checkField[2][2]))
|| ((checkField[0][2] == checkField[1][1]) && (checkField[1][1] == checkField[2][0]))) {
System.out.println("The game is over. The winner is player " + playerSym);
return result = true;
}
return false;
}
}
这里有一个无限循环:
do {
playerChoice = (Integer) sc.nextInt();
field.updateFields(playerChoice, playerSym);
field.showFields(field.fields);
} while (result==false);
CCD_ 6永远不会更新,所以只要第一次是真的,CCD_。你可以尝试修改如下:
do {
playerChoice = (Integer) sc.nextInt();
field.updateFields(playerChoice, playerSym);
field.showFields(field.fields);
result = field.checkWin(field.fields, playerSym);
} while (result==false);
此外,将已经附加到实例的字段传递到实例方法也不是一个好的做法。您可以从checkWin方法中删除参数char[][] field
,并简单地让它对实例变量fields
进行操作。但这并不是循环问题的原因。
正如user506所指出的,它不会通过循环的唯一原因是布尔值result
从未设置为true
。
为什么有两个类来分隔主方法?