2014-02-14 41 views
0

我仍然处于编写这个程序的早期阶段(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 

回答

2

您所呼叫的方法3次循环:

// 1 
obj.userMove(row, col, move); 

// 2 
System.out.println(obj.userMove(row, col, move)); 

// 3 
if(obj.userMove(row, col, move) == true) 

我假设你想调用它一次,并在所有3个地方使用了相同的结果。将结果分配给一个变量,然后在这3个地方使用该变量。

boolean moveResult = obj.userMove(row, col, move); 

System.out.println(moveResult); 

if(moveResult) 

现在它只被调用一次。

+0

是的,但在第一次之后它只调用打印它的返回值,第三次只是为了确保用户实际移动。打印返回值是否会导致整个方法再次运行? if语句也一样?对于调用类仍然很新颖。 – ToonLink

+1

如果你实际上称它为3次(和你一样),它将运行3次。如果您只调用一次,然后仅在其他地方使用返回的值,则它只会运行一次。每次将'obj.userMove(...)'放入代码中时,都会调用该方法。它不在'System.out.println'里面。 –

0

要调用启动该方法三次:

obj.userMove(row, col, move); //1 
    System.out.println(obj.userMove(row, col, move)); //2 

    if(obj.userMove(row, col, move) == true) //3 
0

您的循环运行三次,因为它在您的循环中三次运行obj.userMove(row, col, move)。如果你只希望它运行一次,结果分配到一个新的变量:

boolean userMoveResult = obj.userMove(row, col, move); 
System.out.println(userMoveResult); 
if (userMoveResult == true) 
    obj.computerMove(); 

顺便说一句,你不需要布尔值在ifwhile比较truefalse。你可以像下面这样使用它们:

if (userMoveResult) { 
    .... 
} 
while (!obj.gameOver()) { // Note: ! is the negation operator (i.e. NOT) 
    .... 
}