如何使用 JAVA 根据当前位置找到可能的棋子(马)位置?

How to find possible chess figure (Knight) positions by it's current position using JAVA?

我正在尝试解决此练习。它应该采用两个坐标参数,并显示与输入的位置坐标相关的可能的下一个位置。目前我能够获得输入位置的棋盘,但如何计算骑士棋子的下一个可能位置?感谢您提供任何线索/建议。 我的主文件:

package lt.chess;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Board board = new Board();
        Scanner scanner = new Scanner(System.in);
        System.out.println("");
        System.out.print("Enter board number: ");
        String digit = scanner.next();

        System.out.print("Enter board letter: ");
        String letter = scanner.next();

        Knight knight = new Knight("Knight");
        board.setFigure(knight, digit, letter);
        board.showFigures();
    }
}

董事会class:

package lt.chess;

public class Board {
    private String letters = "ABCDEFGH";
    private String digits = "12345678";

    private Figure[][] arrayFigure = null;

    public Board() {
        arrayFigure = new Figure[letters.length()][digits.length()];
        createBoard();
    }

    private void createBoard() {
        for (int row = digits.length() - 1; row >= 0; row--) {
            System.out.print(digits.charAt(row));

            for (int x = 0; x < letters.length(); x++) {
                System.out.print("  ");
                if (arrayFigure[row][x] != null)
                    System.out.print("X");
            }

            System.out.println("");
            if (row == 0) {
                System.out.print("   ");

                for (int col = 0; col < letters.length(); col++) {
                    System.out.print(letters.charAt(col) + " ");
                }
            }
        }
    }

    public void setFigure(Knight knight,
                          String digit,
                          String letter) {
        int y = digits.indexOf(digit);
        int x = letters.indexOf(letter);
        arrayFigure[y][x] = knight;
    }

    public void showFigures() {
        createBoard();
    }

}

图class:

package lt.chess;

public class Figure {
    private String[] figureColor = {"Black", "White"};
    private String figureName;

    Figure(String figureName) {
        this.figureName = figureName;
    }

    public String getName() {
        return this.figureName;
    }

}

骑士class:

package lt.chess;

public class Knight extends Figure {

    Knight(String name) {
        super(name);
    }

}

你可以像这样创建两个数组

int[] x = {-2, -1, 1, 2, 2, 1, -1, -2};
int[] y = {1, 2, 2, 1, -1, -2, -2, -1};

这可以帮助您计算您的骑士基地在当前位置最多 8 个不同的位置。 之后,每当你需要计算可能的马跳跃时,就循环一遍

for(int i = 0; i < 8; i++) {
        System.out.println(String.format("Possible knight location: %d, %d", current_x + x[i], current_y + y[i]);
}

注意:要考虑板边以及位置已经有其他图

要在 Minh 的回答中添加对棋盘边缘和使用字段的控制,我会提供一种方法来创建下一个可能的位置(点)

 private LinkedList<Point> getNextPositions(int x, int y){
    int[] xOffsets = {-2, -1, 1, 2, 2, 1, -1, -2};
    int[] yOffsets = {1, 2, 2, 1, -1, -2, -2, -1};
    Point nextPosition;
    LinkedList<Point>  = new LinkedList<Point>();
    for(int i = 0; i < xOffsets.length; i++) {
        nextPosition = new Point(x+xOffsets(i),y+yOffsets(i));
        if (isWithinBoardAndFree(nextPosition))
        {
            erg.add(nextPosition);
        }
    }
}

为了检查这些点,我使用了第二种方法:

private boolean isWithinBoardAndFree(Point point, Figure[][] board){
    if (point.x >= 0 && point.x >= 0)
    {
    if (board[point.x][point.y] == null)
        {
            return true;    
        }
    }
        return false;
}

我不确定你的练习,但你可以考虑释放一个骑士来自的领域。