第一个只是简单介绍一下我的程序,它的棋盘。 8×8的网格,从底部行开始到顶部行并从左列到 右列,A至H.这个算法运行不正常吗?(棋盘皇后运动)
输入样例:TRIAL 1
============
Test.
Qh8 is attacking the target on Xb2
. . . . . . . Q
. . . . . . . .
. . . . . . . .
N . . . . . . .
. . B . K . . R
. . . . . . . .
. X . . . . . .
. . . . . . . .
在这种情况下是女王能够攻击,因为X不在网格的左下方。但问题是我无法达到网格左下角的最后一个值,对于其他情况,请参阅它们以帮助您更好地理解所遇到的情况。
输入样例:TRIAL 2
============
Test.
. . . . . . . Q
. . . . . . . .
. . . . . . . .
. . . . . . . .
N . B . K . . R
. . . . . . . .
. . . . . . . .
X . . . . . . .
这就是我的意思,我说上面女王不能达到X值
这只是为了说明我的意思
CASE 1:
============
Test.
Qa8 is attacking the target on Xh1
Q . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . B . K . . R
. . . . . . . .
. . . . . . . .
N . . . . . . X
作品!女王能够攻击X
注意:只有在这种情况下,它在我的算法中工作不知道为什么,但其他人不。
案例2:
============
Test.
. . . . . . . X
. . . . . . . .
. . . . . . . .
. . . . . . . .
N . B . K . . R
. . . . . . . .
. . . . . . . .
Q . . . . . . .
不行的,女王着攻击X
案例3:
Test.
X . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . B . K . . R
. . . . . . . .
. . . . . . . .
N . . . . . . Q
这也不管用,女王着攻击X?
源代码
public void ableToAttack(){
for(int row = 0; row < grid.length; row++){
for(int column = 0; column < grid[row].length; column++){
grid[row][column] = ".";
grid[7-queen.charAt(2)+49][(int)queen.charAt(1)-97] = "Q";
grid[7-rook.charAt(2)+49][(int)rook.charAt(1)-97] = "R";
grid[7-bishop.charAt(2)+49][(int)bishop.charAt(1)-97] = "B";
grid[7-king.charAt(2)+49][(int)king.charAt(1)-97] = "K";
grid[7-knight.charAt(2)+49][(int)knight.charAt(1)-97] = "N";
grid[7-target.charAt(2)+49][(int)target.charAt(1)-97] = "X";
}
}
//HELLO FRIENDS THIS IS WHERE IM STUCK ON THIS METHOD
int moveRow = 0;
int moveColumn = 0;
for(int takeSteps = 0; takeSteps < 8; takeSteps++){
moveRow++;
moveColumn++;
//South east Diaognal Algorithm
if (inBoard(convRow(target),convCol(target))) {
if ((convRow(target) == convRow(queen)+moveRow) && (convCol(target) == convCol(queen)+moveColumn)) {
System.out.println(queen + " is attacking the target on "+target);
}
}
//North West diagonal Algorithm
if (inBoard(convRow(target),convCol(target))) {
if ((convRow(target) == convRow(queen)-moveRow) && (convCol(target) == convCol(queen)-moveColumn)) {
System.out.println(queen + " is attacking the target on "+target);
}
}
// North East diaognal Algorithm
if (inBoard(convRow(target),convCol(target))) {
if ((convRow(target) == convRow(queen)-moveRow) && (convCol(target) == convCol(queen)+moveColumn)) {
System.out.println(queen + " is attacking the target on "+target);
}
}
// South West diagonal Algorithm
if (inBoard(convRow(target),convCol(target))) {
if ((convRow(target) == convRow(queen)+moveRow) && (convCol(target) == convCol(queen)-moveColumn)) {
System.out.println(queen + " is attacking the target on "+target);
}
}
}
for(int row = 0; row <grid.length; row++){
for(int column = 0; column <grid[row].length; column++){
System.out.printf("%2s",grid[row][column] + " ");
}
System.out.println();
}
}
private boolean inBoard(int row, int col) {
return (row <= 8)
&& (row >= 1)
&& (col <= 8)
&& (col >= 1);
}
private int convRow(String rowz) {
return 7-rowz.charAt(2)+49;
}
private int convCol(String columnz) {
return columnz.charAt(1)-97;
}
忽略我做的评论,我认为你是对的我会做 –
尝试修复你的inBoard功能并告诉我们它是否解决了你的问题。 – rilent
是的!非常感谢你我的朋友,我很抱歉如果我困扰你,但是我最终因为你而感到工作上的问题,谢谢<3。另外对不起,如果我很烦,我是初学者,并在11年级 –