2011-12-03 36 views
1

我試着實現吸塵器隨機移動,移動的方向應該在一個數組列表中,所以每次機器應該從列表中隨機選擇3個方向作爲方向示例(N ,N,S)。機器將按照方向不斷移動,直到它到達應再次選擇的邊緣。移動隨機矩陣8 * 8

機器在到達邊緣時不改變方向的問題。

我寫該方法中,負責決定是否機器到達邊緣:

public boolean Wall(int x, int y) { 

    if (choices.contains("N") && y == 7) { 
     return false; 
    } else if (choices.contains("S") && y == 0) { 
     return false; 
    } else if (choices.contains("W") && x == 0) { 
     return false; 
    } else if (choices.contains("E") && x == 7) { 
     return false; 
    } else { 
     return true; 
    } 
} 

和我請壁(),內部移動()。

這是我的所有代碼:

import java.util.*; 

public class test { 

private static String[][] board; 
private static final int ROWS = 8; 
private static final int COLUMNS = 8; 
int moves = 0; 
List<String> orientation = Arrays.asList(new String[]{"N", "E", "W", "S"}); 
List<String> choices = new ArrayList<String>(3); 


public test() { 
    //String state = "d"; 
    board = new String[ROWS][COLUMNS]; 
    for (int i = 0; i < ROWS; i++) { 
     for (int j = 0; j < COLUMNS; j++) { 
      board[i][j] = " "; 

     } 
    } 
    board[4][4] = "d"; 
    board[0][2] = "d"; 
    board[4][7] = "d"; 
    board[1][5] = "d"; 
    board[6][6] = "d"; 
    board[4][0] = "d"; 
} 

public String toString() { 
    String r = ""; 
    for (int i = 0; i < ROWS; i++) { 

     for (int j = 0; j < COLUMNS; j++) { 
      r += "[" + board[i][j] + "]"; 
     } 
     r += "\n"; 
    } 
    return r; 
} 

public int[] gostright(int x, int y) { 
    if (choices.contains("N")) { 
     x--; 
     if (x == -1) { 
      x = 0; 
     } 

    } else if (choices.contains("W")) { 
     y--; 
     if (y == -1) { 
      y = 0; 
     } 

    } else if (choices.contains("S")) { 
     x++; 
     if (x == 8) { 
      x = 7; 
     } 
    } else if (choices.contains("E")) { 
     y++; 
     if (y == 8) { 
      y = 7; 
     } 
    } 
    System.out.println("choise taste equal" + x + ":" + y); 
    return new int[]{x, y}; 
} 

public boolean Wall(int x, int y) { 

    if (choices.contains("N") && y == 7) { 
     return false; 
    } else if (choices.contains("S") && y == 0) { 
     return false; 
    } else if (choices.contains("W") && x == 0) { 
     return false; 
    } else if (choices.contains("E") && x == 7) { 
     return false; 
    } else { 
     return true; 
    } 
} 

public int CountDirt() { 
    int count = 0; 
    for (int i = 0; i < ROWS; i++) { 
     for (int j = 0; j < COLUMNS; j++) { 
      if (board[i][j] == "d") { 
       count++; 
      } 
     } 
    } 
    // System.out.println("the found dirt " + count+" dirts"); 
    return count; 
} 

public void Move() { 
    Collections.shuffle(orientation); 
    int nowX = 4, nowY = 4; 
    int counter = 0; 
    int strightCounter = 1; 
    int wallx = 0; 
    int wally = 0; 
    while (CountDirt() > 0) { 
     for (int i = 0; i < 3; i++) { 
      choices.add(orientation.get(i)); 
      for (int x = 0; x < strightCounter; x++) { 
       for (int y = 0; y < strightCounter; y++) { 
        if(Wall(wallx,wally)) { 
         break; 
        }    

        System.out.println("Wall" + x + ":" + y); 
        board[nowX][nowY] = "1"; 
        int[] pos = gostright(nowX, nowY); 
        nowX = pos[0]; 
        nowY = pos[1]; 
        System.out.println("" + nowX + ":" + nowY); 
        System.out.println("nowX and nowY" + board[nowX][nowY]); 
        board[nowX][nowY] = "#"; 
        moves++; 
        System.out.println(toString()); 
        System.out.println(orientation.get(i)); 
        System.out.println("Choices " + choices); 
        System.out.println("# move" + moves); 
       } 
      } 
      counter++; 
      System.out.println("CountDirt()==" + CountDirt()); 


     } 
     choices.clear(); 

    } 

} 

我認爲這個問題在移動(),但不知道具體在哪裏。

+0

cross-posted here:[www.java-forums.org:help-vacuum-cleaner-code](http://www.java-forums.org/new-java/52123-help-vacuum-cleaner- code.html) –

回答

0

我只看到wallx出現在此代碼中兩次。 int wallx = 0;if(Wall(wallx,wally))。這意味着你總是將0和0傳遞給Wall()。