我理解这似乎是我在C ++和python方面遇到的常见问题,但似乎无法找到解决我的java问题的解释。
附件是我的程序代码,相当长,因为我还是很新,并学习如何简化它。
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
boolean gameActive = true;
boolean player1turn = true;
String board[][]= {{" ","|"," ","|"," "},{"-","-","-","-","-",},{" ","|"," ","|"," "},{"-","-","-","-","-",},{" ","|"," ","|"," "}};
commands();
printBoard(board);
while (gameActive == true) {
if (player1turn == true) {
System.out.println("\nPlayer 1, noughts turn. Please choose a position.");
} else {
System.out.println("\nPlayer 2, crosses turn. Please choose a position.");
}
String position = null;
while ("retry".equals(changePosition(position, player1turn, board))) {
Scanner keyboard = new Scanner (System.in);
position = keyboard.nextLine();
if ("help".equalsIgnoreCase(position)) {
commands();
}
else if ("turn".equalsIgnoreCase(position)){
turn(player1turn);
}
else if ("board".equalsIgnoreCase(position)){
printBoard(board);
}
else {
if (!"retry".equalsIgnoreCase(changePosition(position, player1turn, board))) {
System.out.println(changePosition(position, player1turn, board));
if (player1turn == true) {
player1turn = false;
}
else if (player1turn == false) {
player1turn = true;
}
}
}
}
}
}
public static void commands() {
System.out.println("\nPlayer 1 is noughts, O");
System.out.println("Player 2 is crosses, X");
System.out.println("\nThe board uses a grid format");
System.out.println("\"A1\" is the top-left position");
System.out.println("\"A2\" is the top-middle position");
System.out.println("\"A3\" is the top-right position");
System.out.println("\"B1\" is the middle-left position");
System.out.println("\"B2\" is the middle position");
System.out.println("\"B3\" is the middle-right position");
System.out.println("\"C1\" is the bottom-left position");
System.out.println("\"C2\" is the bottom-middle position");
System.out.println("\"C3\" is the bottom-right position");
System.out.println("\n\"help\" will reveal this help message");
System.out.println("\"board\" will reveal the board");
System.out.println("\"turn\" will reveal whos turn it is");
}
public static void turn(boolean player1turn) {
if (player1turn == true) {
System.out.println("\nPlayer 1, noughts turn.");
} else {
System.out.println("\nPlayer 2, crosses turn.");
}
}
public static void printBoard(String[][] board) {
System.out.print("\n");
for (int x=0;x<5;x++) {
for (int y=0;y<5;y++) {
System.out.print(board[x][y]+"");
}
System.out.println();
}
}
public static String changePosition(String position, boolean player1turn, String[][] board) {
if (("A1".equalsIgnoreCase(position)) && (player1turn == true)) {
if (" ".equals(board[0][0])) {
board[0][0] = "O";
return board[0][0];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("B1".equalsIgnoreCase(position)) && (player1turn == true)) {
if (" ".equals(board[2][0])) {
board[2][0] = "O";
return board[2][0];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("C1".equalsIgnoreCase(position)) && (player1turn == true)) {
if (" ".equals(board[4][0])) {
board[4][0] = "O";
return board[4][0];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("A2".equalsIgnoreCase(position)) && (player1turn == true)) {
if (" ".equals(board[0][2])) {
board[0][2] = "O";
return board[0][2];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("B2".equalsIgnoreCase(position)) && (player1turn == true)) {
if (" ".equals(board[2][2])) {
board[2][2] = "O";
return board[2][2];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("C2".equalsIgnoreCase(position)) && (player1turn == true)) {
if (" ".equals(board[4][2])) {
board[4][2] = "O";
return board[4][2];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("A3".equalsIgnoreCase(position)) && (player1turn == true)) {
if (" ".equals(board[0][4])) {
board[0][4] = "O";
return board[0][4];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("B3".equalsIgnoreCase(position)) && (player1turn == true)) {
if (" ".equals(board[2][4])) {
board[2][4] = "O";
return board[2][4];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("C3".equalsIgnoreCase(position)) && (player1turn == true)) {
if (" ".equals(board[4][4])) {
board[4][4] = "O";
return board[4][4];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("A1".equalsIgnoreCase(position)) && (player1turn == false)) {
if (" ".equals(board[0][0])) {
board[0][0] = "X";
return board[0][0];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("B1".equalsIgnoreCase(position)) && (player1turn == false)) {
if (" ".equals(board[2][0])) {
board[2][0] = "X";
return board[2][0];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("C1".equalsIgnoreCase(position)) && (player1turn == false)) {
if (" ".equals(board[4][0])) {
board[4][0] = "X";
return board[4][0];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("A2".equalsIgnoreCase(position)) && (player1turn == false)) {
if (" ".equals(board[0][2])) {
board[0][2] = "X";
return board[0][2];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("B2".equalsIgnoreCase(position)) && (player1turn == false)) {
if (" ".equals(board[2][2])) {
board[2][2] = "X";
return board[2][2];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("C2".equalsIgnoreCase(position)) && (player1turn == false)) {
if (" ".equals(board[4][2])) {
board[4][2] = "X";
return board[4][2];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("A3".equalsIgnoreCase(position)) && (player1turn == false)) {
if (" ".equals(board[0][4])) {
board[0][4] = "X";
return board[0][4];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("B3".equalsIgnoreCase(position)) && (player1turn == false)) {
if (" ".equals(board[2][5])) {
board[2][4] = "X";
return board[2][4];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else if (("C3".equalsIgnoreCase(position)) && (player1turn == false)) {
if (" ".equals(board[4][4])) {
board[4][4] = "X";
return board[4][4];
} else {
System.out.println("That spot is taken please choose another place");
return "retry";
}
}
else {
if ((position == null) || ("help".equalsIgnoreCase(position)) || ("turn".equalsIgnoreCase(position)) || ("board".equalsIgnoreCase(position))) {
return "retry";
} else {
System.out.println("Error please enter a valid position found by entering \"help\" ");
return "retry";
}
}
}
}
从我的长度changePosition()方法可以看出问题的范围。如果我进入控制台&#34; A1&#34;结果产量:
Player 1, noughts turn. Please choose a position.
A1
That spot is taken please choose another place
retry
That spot is taken please choose another place
board
O| |
-----
| |
-----
| |
这让我感到困惑,因为似乎if块实际上被触发并导致board [0] [0]被更改但是由于某种原因必须移动到else块并将消息打印到控制台并返回&#34;重试&#34;
我尝试将if块中的返回值更改为标准字符串,例如&#34; success&#34;但这似乎并没有解决问题。此外,当我将鼠标悬停在返回类型上时,IDE会将这些标识为返回点。我已经确定(我有信心)我正在使用正确的语法。
我希望这是我的冗长代码的一个问题,我相信这对你们来说都很好。
谢谢。
答案 0 :(得分:1)
问题是你在这个循环的每次迭代中多次调用changePosition
方法:
while ("retry".equals(changePosition(position, player1turn, board))) {
Scanner keyboard = new Scanner (System.in);
position = keyboard.nextLine();
if ("help".equalsIgnoreCase(position)) {
commands();
}
else if ("turn".equalsIgnoreCase(position)){
turn(player1turn);
}
else if ("board".equalsIgnoreCase(position)){
printBoard(board);
}
else {
if (!"retry".equalsIgnoreCase(changePosition(position, player1turn, board))) {
System.out.println(changePosition(position, player1turn, board));
if (player1turn == true) {
player1turn = false;
}
else if (player1turn == false) {
player1turn = true;
}
}
}
}
}
首先在while
循环的条件下调用它:
while(“retry”.equals(changePosition(position,player1turn,board)))
但是因为参数位置一开始仍为空,它只会返回"retry"
并在第一次出现时进入循环。
进入职位后,您可以在此处调用方法:
if(!“retry”.equalsIgnoreCase(changePosition(position,player1turn, 板)))
如果该地点是免费的,它将起作用而不返回"retry"
,因此输入if
语句块。这就是你的情况下发生的事情,从那个地方A1实际上已经填满了。
然后,您可以在下一行中再次调用该方法:
System.out.println(changePosition(position,player1turn,board));
但是现在A1被占用了,这次输出的错误是“那个地方请选择另一个地方”并返回你同时打印的"retry"
。
当在下一次迭代开始时再次检查循环条件时,再次调用该方法。由于您现在使用相同的参数第三次调用它,它将再次输出错误并返回"retry"
作为返回值,因此再次进入循环。
解决方案是每次迭代只调用一次函数并保存返回值并使用它。 你还应该使用do-while-loop,因为它应该至少运行一次:
String changePositionResult = null;
do {
final Scanner keyboard = new Scanner(System.in);
position = keyboard.nextLine();
if ("help".equalsIgnoreCase(position)) {
commands();
} else if ("turn".equalsIgnoreCase(position)) {
turn(player1turn);
} else if ("board".equalsIgnoreCase(position)) {
printBoard(board);
} else {
changePositionResult = changePosition(position, player1turn, board);
if (!"retry".equalsIgnoreCase(changePositionResult)) {
System.out.println(changePositionResult);
if (player1turn == true) {
player1turn = false;
} else if (player1turn == false) {
player1turn = true;
}
}
}
} while ("retry".equals(changePositionResult));