簡體   English   中英

無限二維數組搜索

[英]Infinite 2D array search

我正在嘗試實現一種方法,該方法不允許國際象棋棋子跳過另一個棋子。 IntelliJ通知我該方法總是返回反向,但是我看不到問題。 誰能幫我嗎? 我在棋盤上使用2D陣列。 xFrom yFrom是要移動的工件的坐標,xTo yTo是要移動的工件的坐標

 public boolean pieceJumps(char[][] boardIn, int xFrom, int yFrom, int xTo, int yTo) {
        for (int i = 0; i < boardIn.length; i++) {
            for (int j = 0; j < boardIn[i].length; j++) {
                if (boardIn[i][j] != FREE && (Math.sqrt(Math.pow((i - xFrom), 2) + Math.pow((j - yFrom), 2)) < (Math.sqrt(Math.pow((xTo - xFrom), 2) + Math.pow((yTo - yFrom), 2))))) {
                    //IF THE DISTANCE MAGNITUDE OF A POINT FROM THE PIECE TO BE MOVED LANDS ON A SPACE WITH ANOTHER PIECE BUT IS SMALLER IN MAGNITUDE THAN THE DISTANCE TO VE MOVED, RETURNS TRUE THAT PIECE WOULD JUMP


                    return true;
                } else {
                    return false;
                }

            }
        }
        return false;
    }
}

您的循環最多只能進行一次迭代,原因是由於return語句。

您的條件語句也有一些問題。 您怎么知道要檢查哪個廣場? 使用當前的回路結構,您將需要計算零件需要行進的向量,並檢查沿該路徑落下的向量。

return語句將退出循環並退出該函數。 else return false是可以的,如果您要占用空間,則希望它失敗。 您的陳述是否需要更多條件。 僅在到達目的地時才返回true,如果第一個插槽空閑則返回true。

沒有驗證邏輯,這也不是最有效的方法,但這是朝正確方向邁出的一步。 我基於類職責分解了代碼,該類職責基於復合設計。 https://www.geeksforgeeks.org/composite-design-pattern/ 基本上從任務的概念出發分解邏輯,其中每個類都有要實現的特定目標。 這使調試,理解和擴展項目變得更加容易。

public class Board {
    public boolean pieceJumps(char[][] boardIn, int xFrom, int yFrom, int xTo, int yTo) {
        Line line = new Line(xFrom, yFrom, xTo, yTo);

        for (int i = 0; i < boardIn.length; i++) {
            for (int j = 0; j < boardIn[i].length; j++) {
                // If Point exists on line then validate position
                if (line.existsOnLine(i, j)) {
                    // If the point we are on is already occupied fail
                    if (boardIn[i][j] != 'o') {
                        return false;
                    } else {
                        // If we are where we want to be
                        if (i == xTo && j == yTo) {
                            boardIn[i][j] = 'x';
                            return true;
                        }

                        // continue to next if we haven't reach destination
                    }
                }
            }
        }
        return false;
    }
}

/**
 * Defines basic line properties and calculations 
 */
public class Line {
    private int b;
    private int slope;

    /**
     * Constructor which accepts two points 
     * @param x1
     * @param y1
     * @param x2
     * @param y2
     */
    public Line(int x1, int y1, int x2, int y2) {
        calculateSlope(x1, y1, x2, y2);
        calculateB(x1, y1);
    }

    /**
     * Does point exist on line 
     * @param x
     * @param y
     * @return true if on line else false
     */
    public boolean existsOnLine(int x, int y) {
        return y == (slope * x) + b;
    }

    ///
    //  PRIVATE HELPER METHODS
    ///

    /**
     * Determine line slope
     * @return slope of line
     */
    private void calculateSlope(int x1, int y1, int x2, int y2) {
        slope =  (x2 - x1) / (y2 - y1);
    }

    /**
     * Calculate y-intercept for line
     * @param x
     * @param y
     */
    private void calculateB(int x, int y) {
            b = y - slope * x;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM