如何写出骑士可以执行的动作?

发布于 2025-01-16 15:54:10 字数 1796 浏览 0 评论 0原文

我编写了一个程序,用于检查棋盘上的工具对其他工具的威胁。

我想写出骑士能做出的动作。我考虑过用两种选项来检查数学绝对值中的距离——两个水平块和一个垂直块,或者两个垂直块和一个水平块。 我知道他在棋盘上有 8 步棋,但是有没有办法缩短它,或者有其他方法来写他的棋步吗? 我附上了代码,但是问题出在骑士上:

// examines threats of tools on the chessboard  
switch (first)
{
    case 'r':
        if ((row1 == row2) || (col1 == col2)){
            System.out.println("rook threats " + (second == knight ? "knight" : "bishop"));
            foundTreat = true;
        }
        break;
    case 'b':
        if(Math.abs(row1 - row2) == Math.abs(col1 - col2)){
            System.out.println("bishop threats " + (second == rook ? "rook" : "knight"));
            foundTreat = true;
        }
        break;
    case 'k':
        if((Math.abs(row1 + 1) == row2) && (Math.abs(col1 + 2) == col2)){
            System.out.println("knight threats " + (second == rook ? "rook" : "bishop"));
            foundTreat = true;
        }
        break;  
}

switch (second)
{
    case 'r':
        if ((row1 == row2) || (col1 == col2)){
            System.out.println("rook threats " + (first == knight ? "knight" : "bishop"));
            foundTreat = true;
        }
        break;
    case 'b':
        if(Math.abs(row1 - row2) == Math.abs(col1 - col2)){
            System.out.println("bishop threats " + (first == rook ? "rook" : "knight"));
            foundTreat = true;
        }
        break;
    case 'k':
        if((Math.abs(row1 + 1) == row2) && (Math.abs(col1 + 2) == col2)){
            System.out.println("knight threats " + (first == rook ? "rook" : "bishop"));
            foundTreat = true;
        }
        break;
}  

但是我无法正确编写代码,所以我很想获得帮助。

顺便说一句,我是一名 Java 初学者,我只能编写带有以下代码的代码:if、else、Switch 和不带 while、loop 的代码。

谢谢。

I wrote a program that examines threats of tools on a chessboard on other tools.

I want to write the movements that a knight can perform. I thought about checking the distance in Math abs in two options- two Horizontal blocks and one vertical or two vertical blocks and one Horizontal.
I know he has 8 moves on the board, but is there a way to shorten it or is there another way to write his move?
I attached the code, but the problem is with the knight:

// examines threats of tools on the chessboard  
switch (first)
{
    case 'r':
        if ((row1 == row2) || (col1 == col2)){
            System.out.println("rook threats " + (second == knight ? "knight" : "bishop"));
            foundTreat = true;
        }
        break;
    case 'b':
        if(Math.abs(row1 - row2) == Math.abs(col1 - col2)){
            System.out.println("bishop threats " + (second == rook ? "rook" : "knight"));
            foundTreat = true;
        }
        break;
    case 'k':
        if((Math.abs(row1 + 1) == row2) && (Math.abs(col1 + 2) == col2)){
            System.out.println("knight threats " + (second == rook ? "rook" : "bishop"));
            foundTreat = true;
        }
        break;  
}

switch (second)
{
    case 'r':
        if ((row1 == row2) || (col1 == col2)){
            System.out.println("rook threats " + (first == knight ? "knight" : "bishop"));
            foundTreat = true;
        }
        break;
    case 'b':
        if(Math.abs(row1 - row2) == Math.abs(col1 - col2)){
            System.out.println("bishop threats " + (first == rook ? "rook" : "knight"));
            foundTreat = true;
        }
        break;
    case 'k':
        if((Math.abs(row1 + 1) == row2) && (Math.abs(col1 + 2) == col2)){
            System.out.println("knight threats " + (first == rook ? "rook" : "bishop"));
            foundTreat = true;
        }
        break;
}  

But I can't write the code correctly, so I would love to get help.

By the way, I'm a beginner programmer in Java, and I can write the code only with: if, else, Switch and without: while, loop.

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟若柳尘 2025-01-23 15:54:10

根据您发布的代码,我假设您有一个 2D 板表示。一种更简洁的方法是循环遍历骑士从其当前位置可以做出的所有可能的动作,例如,整数列表的列表。马有 8 种可能的走法,每一种都是水平两个方格和垂直方格的组合,或者垂直方向两个方格和水平方格的组合。

我对 java 不太熟悉,所以这里有一些伪/C# 代码。

knightMoves = [ [1, 2], [1, -2], ..., [-2, -1]];  // All 8  possible moves in all dircetions
foreach(List<int> move in knightMoves)
{
    newPos = knightCurrentPosition + move;

    // Check if knight is on board after move which means that the knights
    // new position should be within 0-7.
    if(newPos[0] < 0 || newPos[0] > 7 || newPos[1] < 0 || newPos[1] > 7) continue;

    // Check if piece is not own piece (can't capture own piece)
    if(board[newPos] == ownPiece) continue;

    // If the criterias are fulfilled, add to the possible moves
    possibleMoves.Add(newPos);        
}

然后,您还必须检查国王是否处于检查状态,这可以在生成移动函数本身中完成,也可以在稍后的阶段完成,具体取决于您的实现。

From the code you posted I assume you have a 2D board representation. A more clean way of doing this would be to loop through all possible moves a knight can make from its current position, which would be e.g. a list of list of ints. A knight has 8 possible moves, each being some combination of two squares away horizontally and one square vertically, or two squares vertically and one square horizontally.

I am not super familiar with java, so here is some pseudo/C# code.

knightMoves = [ [1, 2], [1, -2], ..., [-2, -1]];  // All 8  possible moves in all dircetions
foreach(List<int> move in knightMoves)
{
    newPos = knightCurrentPosition + move;

    // Check if knight is on board after move which means that the knights
    // new position should be within 0-7.
    if(newPos[0] < 0 || newPos[0] > 7 || newPos[1] < 0 || newPos[1] > 7) continue;

    // Check if piece is not own piece (can't capture own piece)
    if(board[newPos] == ownPiece) continue;

    // If the criterias are fulfilled, add to the possible moves
    possibleMoves.Add(newPos);        
}

Then you will also have to check if king is left in check, this can be done either in the generate moves function itself, or at some later stage depending on your implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文