我有數字的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]]
在正方形二維數組中找到任意點的對角線的最簡單方法是什麼?此外,如何簡化我的代碼的建議將不勝感激。
謝謝!
你的索引符號是怪異。你說(3,2)的點值爲7,但是'multi [3] [2]'的值爲2.它是'multi [2] [3]',它的值爲7.這也是我的方式數據直接。如果您使用Java符號,或者至少一個與Java符號中的順序一致的索引順序,也許我們都會變得更好。 –
你是對的。 java使用的符號對我來說並不完全直觀。我仍然習慣它。我會編輯我的帖子,以便更清楚。 –
注意左邊對角線上的第一個座標以0結束的方式。在右邊它們以5結尾。您可以通過做'while((firstCoord!= 0)||(secondCoord! = 0)){firstCoord--; secondCoord--; }這只是一個減法或增加的問題,以獲得任何一個極端的正確座標。我會通過你的數組和座標到一些方法來獲得結果值 – robotlos