如何写出骑士可以执行的动作?
我编写了一个程序,用于检查棋盘上的工具对其他工具的威胁。
我想写出骑士能做出的动作。我考虑过用两种选项来检查数学绝对值中的距离——两个水平块和一个垂直块,或者两个垂直块和一个水平块。 我知道他在棋盘上有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您发布的代码,我假设您有一个 2D 板表示。一种更简洁的方法是循环遍历骑士从其当前位置可以做出的所有可能的动作,例如,整数列表的列表。马有 8 种可能的走法,每一种都是水平两个方格和垂直方格的组合,或者垂直方向两个方格和水平方格的组合。
我对 java 不太熟悉,所以这里有一些伪/C# 代码。
然后,您还必须检查国王是否处于检查状态,这可以在生成移动函数本身中完成,也可以在稍后的阶段完成,具体取决于您的实现。
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.
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.