2014-02-14 74 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) 
    .... 
}