我仍然处于编写这个程序的早期阶段(BreakThrough游戏),但是当试图运行我已经得到的代码时,似乎在一个循环完成之前do while循环中重复执行步骤。看来它在完成循环之前运行了obj.userMove(row, col, move);
三次。while循环在完成循环之前似乎是重复步骤?
import java.util.Scanner;
import java.util.Random;
class BreakThroughGame {
char board[][];
Random rand = new Random();
BreakThroughGame() {
board = new char[][]{{' ','1','2','3','4','5','6','7','8', ' '},
{'1','w','w','w','w','w','w','w','w','1'},
{'2','w','w','w','w','w','w','w','w','2'},
{'3','_', '_','_', '_','_', '_','_', '_','3'},
{'4','_', '_','_', '_','_', '_','_', '_','4'},
{'5','_', '_','_', '_','_', '_','_', '_','5'},
{'6','_', '_','_', '_','_', '_','_', '_','6'},
{'7','b', 'b','b', 'b','b', 'b','b', 'b','7'},
{'8','b', 'b','b', 'b','b', 'b','b', 'b','8'},
{' ','1','2','3','4','5','6','7','8',' '}};
public boolean userMove(int row, int col, String move) {
System.out.println("pos = "+board[row][col]);
if (board[row][col] == 'b') {
if(move.charAt(0) == 'f' && board[row+1][col] == 'w') {
System.out.println("Can't move there, try again");
return false;
}
switch (move.charAt(0)){
case 'f':
board[row-1][col] = 'b';
board[row][col] = '_';
return true;
case 'r':
board[row-1][col+1] = 'b';
board[row][col] = '_';
return true;
case 'l':
board[row-1][col-1] = 'b';
board[row][col] = '_';
return true;
}
}
else {
System.out.println("Invalid position, try again");
return false;
}
return false;
}
public void computerMove() {
int rm = rand.nextInt(8)+1;
int cm = rand.nextInt(8)+1;
int dm = rand.nextInt(2);
//code goes here
}
public boolean gameOver() {
//code goes here
return false;
}
public void printBoard() {
for (int row = 0; row <= 9; row++){
for (int column = 0; column <= 9; column++) {
System.out.print(board[row][column]+" ");
}
System.out.println();
}
}
}
public class game {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
BreakThroughGame obj = new BreakThroughGame();
do {
obj.printBoard();
System.out.println("Enter row position");
int row = in.nextInt();
System.out.println("Enter column position");
int col = in.nextInt();
System.out.println("Enter move direction");
String move = in.next();
obj.userMove(row, col, move);
System.out.println(obj.userMove(row, col, move));
if(obj.userMove(row, col, move) == true)
obj.computerMove();
}while (obj.gameOver() == false);
}
}
这里是完成第一循环之后的输出:
1 2 3 4 5 6 7 8
1 w w w w w w w w 1
2 w w w w w w w w 2
3 _ _ _ _ _ _ _ _ 3
4 _ _ _ _ _ _ _ _ 4
5 _ _ _ _ _ _ _ _ 5
6 _ _ _ _ _ _ _ _ 6
7 b b b b b b b b 7
8 b b b b b b b b 8
1 2 3 4 5 6 7 8
Enter row position
7
Enter column position
5
Enter move direction
f
pos = b
pos = _
Invalid position, try again
false
pos = _
Invalid position, try again
现在的循环似乎终于写完了,然后从顶部再次
1 2 3 4 5 6 7 8
1 w w w w w w w w 1
2 w w w w w w w w 2
3 _ _ _ _ _ _ _ _ 3
4 _ _ _ _ _ _ _ _ 4
5 _ _ _ _ _ _ _ _ 5
6 _ _ _ _ b _ _ _ 6
7 b b b b _ b b b 7
8 b b b b b b b b 8
1 2 3 4 5 6 7 8
Enter row position
是的,但在第一次之后它只调用打印它的返回值,第三次只是为了确保用户实际移动。打印返回值是否会导致整个方法再次运行? if语句也一样?对于调用类仍然很新颖。 – ToonLink
如果你实际上称它为3次(和你一样),它将运行3次。如果您只调用一次,然后仅在其他地方使用返回的值,则它只会运行一次。每次将'obj.userMove(...)'放入代码中时,都会调用该方法。它不在'System.out.println'里面。 –