简体   繁体   English

无限二维数组搜索

[英]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. IntelliJ通知我该方法总是返回反向,但是我看不到问题。 Can anyone help me out? 谁能帮我吗? I'm using a 2D array for the chessboard. 我在棋盘上使用2D阵列。 xFrom yFrom are the co ordinates of piece wanting to move, xTo yTo are those where the piece wants to move to 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;
    }
}

Your loop will have at most one iteration, and the reason is because of the return statements. 您的循环最多只能进行一次迭代,原因是由于return语句。

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. return语句将退出循环并退出该函数。 The else return false is fine, you want it to fail if space is occupied. else return false是可以的,如果您要占用空间,则希望它失败。 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. 仅在到达目的地时才返回true,如果第一个插槽空闲则返回true。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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