简体   繁体   中英

Infinite 2D array search

I'm trying to implement a method which doesn't allow a chess piece to jump over another. IntelliJ informs me that this method always returns inverted, but I can't see the issue. Can anyone help me out? I'm using a 2D array for the chessboard. xFrom yFrom are the co ordinates of piece wanting to move, xTo yTo are those where the piece wants to move to

 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;
    }
}

Your loop will have at most one iteration, and the reason is because of the return statements.

Your conditional statement also has some issues. How to you know which square to check? With the current loop structure you will need to calculate the vector the piece needs to travel, and check those which fall along that path.

The return statement will exit the loop and stepout of the function. The else return false is fine, you want it to fail if space is occupied. The statement in you if condition will need more. You should only return true if you have reached your destination, you return true if the first slot is free.

Have not validated logic, this also isn't the most efficient way of doing this, but this is a step in the right direction. I broke up the code based on classes responsibility, which based off of composite based design. https://www.geeksforgeeks.org/composite-design-pattern/ . Basically break up logic based off of the idea of tasks, where each class has a specific goal it is meant to accomplish. This makes it easier debug, understand, and scale projects.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM