2016-07-19 36 views
0

我有數字的6x6的陣列:發現二維數組相對於任何一個位置的對角線邊緣 - Java的

 int[][] multi = new int[][]{ 
     {4, 2, 3, 2, 5, 1}, 
     {2, 5, 5, 4, 1, 1}, 
     {2, 4, 6, 7, 2, 4}, 
     {2, 1, 2, 3, 4, 3}, 
     {3, 5, 1, 4, 5, 2}, 
     {1, 2, 1, 4, 1, 2} 
    }; 

如果我的起始位置是multi[2][3]。我怎樣才能找到相對於該值的數組的對角線邊緣?例如,在點multi[2][3]處,值爲7.對角線應爲點multi[0][1],multi[0][5],multi[4][5]multi[5][0]。這裏是我的代碼目前所做的:

if (LocationValue == 7) {//find possible moves 
      //There should be 4 potential moves 

      ArrayList<Point> Moves = new ArrayList<Point>(); 
      Point DMove; 

      for (int i = 0; i < multi.length; i++) { 
       DMove = new Point(x + i, y + i); 
       Moves.add(new Point(DMove)); 
      } 
      for (int i = 0; i < multi.length; i++) { 
       DMove = new Point(x - i, y + i); 
       Moves.add(new Point(DMove)); 
      } 
      for (int i = 0; i < multi.length; i++) { 
       DMove = new Point(x - i, y - i); 
       Moves.add(new Point(DMove)); 
      } 
      for (int i = 0; i < multi.length; i++) { 
       DMove = new Point(x + i, y - i); 
       Moves.add(new Point(DMove)); 
      } 

      ArrayList<Point> AlmostFinalMoves = FindPossibleMoves(Moves); //eliminate impossible moves 
      ArrayList<Point> FinalMoves = FindSideMoves(AlmostFinalMoves, x, y); //Get bishop moves 
      System.out.println("Possible Moves: " + FinalMoves); 

      }//End of IF 

然後,此方法消除了不可能值:

public static ArrayList<Point> FindPossibleMoves(ArrayList<Point> AllMoves) { 

    ArrayList<Point> FinalMoves = new ArrayList<Point>(); 

    for (int i = 0; i < AllMoves.size(); i++) { 

     if (AllMoves.get(i).getX() >= 0 && AllMoves.get(i).getX() <= 5 && AllMoves.get(i).getY() >= 0 && AllMoves.get(i).getY() <= 5) { 
      FinalMoves.add(AllMoves.get(i)); 
     } 
    } 

    return FinalMoves; 
} 

最後,這種方法消除了所有不在陣列的邊緣移動。

public static ArrayList<Point> FindSideMoves(ArrayList<Point> AllPossibleMoves, int xloc, int yloc) { 

    ArrayList<Point> AlmostFinalSideMoves = new ArrayList<Point>(); 
    ArrayList<Point> FinalSideMoves = new ArrayList<Point>(); 

    for (int i = 0; i < AllPossibleMoves.size(); i++) { 

     if (AllPossibleMoves.get(i).getX() == 0) { 
      if (AllPossibleMoves.get(i).getY() == 0 || AllPossibleMoves.get(i).getY() == 1 || AllPossibleMoves.get(i).getY() == 2 || AllPossibleMoves.get(i).getY() == 3 || AllPossibleMoves.get(i).getY() == 4 || AllPossibleMoves.get(i).getY() == 5) { 
       AlmostFinalSideMoves.add(AllPossibleMoves.get(i)); 
      } 
     } 
     if (AllPossibleMoves.get(i).getX() == 5) { 
      if (AllPossibleMoves.get(i).getY() == 0 || AllPossibleMoves.get(i).getY() == 1 || AllPossibleMoves.get(i).getY() == 2 || AllPossibleMoves.get(i).getY() == 3 || AllPossibleMoves.get(i).getY() == 4 || AllPossibleMoves.get(i).getY() == 5) { 
       AlmostFinalSideMoves.add(AllPossibleMoves.get(i)); 
      } 
     } 
     if (AllPossibleMoves.get(i).getX() == 1) { 
      if (AllPossibleMoves.get(i).getY() == 0 || AllPossibleMoves.get(i).getY() == 5) { 
       AlmostFinalSideMoves.add(AllPossibleMoves.get(i)); 
      } 
     } 
     if (AllPossibleMoves.get(i).getX() == 2) { 
      if (AllPossibleMoves.get(i).getY() == 0 || AllPossibleMoves.get(i).getY() == 5) { 
       AlmostFinalSideMoves.add(AllPossibleMoves.get(i)); 
      } 
     } 
     if (AllPossibleMoves.get(i).getX() == 3) { 
      if (AllPossibleMoves.get(i).getY() == 0 || AllPossibleMoves.get(i).getY() == 5) { 
       AlmostFinalSideMoves.add(AllPossibleMoves.get(i)); 
      } 
     } 
     if (AllPossibleMoves.get(i).getX() == 4) { 
      if (AllPossibleMoves.get(i).getY() == 0 || AllPossibleMoves.get(i).getY() == 5) { 
       AlmostFinalSideMoves.add(AllPossibleMoves.get(i)); 
      } 
     } 
    } 

    for (int i = 0; i < AlmostFinalSideMoves.size(); i++) {//Check to see if any possible moves match the original location. If so, do not include in list 
     if (AlmostFinalSideMoves.get(i).getX() == xloc && AlmostFinalSideMoves.get(i).getY() == yloc) { 
      //Do Nothing! 
     } else { 
      FinalSideMoves.add(AlmostFinalSideMoves.get(i)); 
     } 
    } 

    return FinalSideMoves; 
} 

運行此程序會導致以下錯誤。

Possible Moves: [java.awt.Point[x=0,y=3], java.awt.Point[x=0,y=5], java.awt.Point[x=2,y=5], java.awt.Point[x=4,y=5], java.awt.Point[x=5,y=3], java.awt.Point[x=5,y=0], java.awt.Point[x=2,y=0], java.awt.Point[x=0,y=1]] 

在正方形二維數組中找到任意點的對角線的最簡單方法是什麼?此外,如何簡化我的代碼的建議將不勝感激。

謝謝!

+1

你的索引符號是怪異。你說(3,2)的點值爲7,但是'multi [3] [2]'的值爲2.它是'multi [2] [3]',它的值爲7.這也是我的方式數據直接。如果您使用Java符號,或者至少一個與Java符號中的順序一致的索引順序,也許我們都會變得更好。 –

+0

你是對的。 java使用的符號對我來說並不完全直觀。我仍然習慣它。我會編輯我的帖子,以便更清楚。 –

+0

注意左邊對角線上的第一個座標以0結束的方式。在右邊它們以5結尾。您可以通過做'while((firstCoord!= 0)||(secondCoord! = 0)){firstCoord--; secondCoord--; }這只是一個減法或增加的問題,以獲得任何一個極端的正確座標。我會通過你的數組和座標到一些方法來獲得結果值 – robotlos

回答

1

如果其中一個索引等於0或數組的長度/高度,則數字位於正方形的邊緣上。假設數組已經是一個正方形(你可以做的檢查自己):

int length = grid.length; 
int height = grid[0].length; 

假設你有X和原點的y座標:

List<Point> findPossibleMoves(int x, int y) { 
    int length = grid.length; 
    int height = grid[0].length; 
    int originalX = x; 
    int originalY = y; 
    //add (1, 1), (-1, 1), (-1, -1), (1, -1) to the original position until you reach an edge 
} 

別急,我們真的需要一個循環?如果我們直接向x和y點添加特定值以在1步中達到邊緣,該怎麼辦?我們如何做到這一點? (2,3)例如(你的數組中的值= 7)。要找到(0,0)的角落,我們使用這個邏輯:

  • X = 2是越接近0比y爲接近於0,所以我們得到(X - X,Y - X)=(0, 1)
  • 這適用於如果y爲接近於0比X:(X - Y,Y - Y)=(X - Y,0)

取本和應用說,(長度,高度)角:

  • (y = 3)比(x = 2)到(長度= 5)更接近(高度= 5),所以我們得到(x +(height - y) (高度 - y))=(4,5)

所有4個角轉換爲代碼如下:

List<Point> findPossibleMoves(int x, int y) { 
    List<Point> pointList = new ArrayList<Point>(); 
    int length = grid.length; 
    int height = grid[0].length; 

    pointList.add(new Point(x - Math.min(x, y), y - Math.min(x, y))); 
    pointList.add(new Point(x + Math.min(length - x, y), y - Math.min(length - x, y))); 
    pointList.add(new Point(x - Math.min(x, height - y), y + Math.min(x, height - y))); 
    pointList.add(new Point(x + Math.min(length - x, height - y), y + Math.min(length - x, height - y))); 

    return pointList; 
} 

哪些可以清理到:

List<Point> findPossibleMoves(int x, int y) { 
    List<Point> pointList = new ArrayList<Point>(); 
    int length = grid.length; 
    int height = grid[0].length; 

    int to00 = Math.min(x, y); 
    int toL0 = Math.min(length - x, y); 
    int to0H = Math.min(x, height - y); 
    int toLH = Math.min(length - x, height - y); 
    pointList.add(new Point(x - to00, y - to00)); 
    pointList.add(new Point(x + toL0, y - toL0)); 
    pointList.add(new Point(x - to0H, y + to0H)); 
    pointList.add(new Point(x + toLH, y + toLH)); 

    return pointList; 
} 
0

我會更直接地做。只要我們認爲multi是經常性的,而不是襤褸的,我們甚至不需要事先知道它的尺寸是多少。它甚至不需要是方形的。

例如,這種方法是一種更爲簡潔的版本你的:

if (LocationValue == 7) { 
    int maxRow = multi.length - 1; 
    int maxColumn = multi[0].length - 1; 
    int[][] directions = { { 1, 1 }, { 1, -1 }, { -1, 1}, { -1, -1 } }; 
    ArrayList<Point> finalMoves = new ArrayList<>(); 

    for (int[] direction : directions) { 
     int x1 = x; 
     int y1 = y; 

     // Proceed in the current direction until we reach an edge 
     while (true) { 
      int x2 = x1 + direction[0]; 
      int y2 = y1 + direction[1]; 

      if ((x2 < 0) || (x2 > numColumns) 
        || (y2 < 0) || (y2 > numRows)) { 
       // Moving in farther would take the piece off the board 
       break; 
      } else { 
       x1 = x2; 
       y1 = y2; 
      } 
     } 

     finalMoves.add(new Point(x1, y1)); 
    } 

    // ... 
} 

請特別注意,我們可以知道哪些點上的優勢很容易(如圖所示)。如果您願意,您可以將給定的條件包裝在方法中,但沒有太多。無論哪種方式,沒有特別的需要創建我們知道不在邊緣的Point,或者進行兩輪過濾或任何這些東西。

請注意,我已經通過引入directions數組來幹掉了您的代碼。您執行的處理每個方向的操作是相同的,但對於兩個軸的增量(+1或-1)。 direction的元素捕獲了這些差異,消除了對重複代碼的需求。

最後,請注意,如果初始位置在邊緣上,那麼計算出的「移動」中的兩個將使該塊位於其初始位置,並且如果初始位置位於角落,則三個移動將離開它在同一個地方。如果願意,您可以通過在向finalMoves添加移動之前測試x1和/或y1來消除這些內容。當然,如果你這樣做,那麼結果有時候會少於四次。

相關問題