首先简单介绍一下我的程序,它是一个国际象棋棋盘。 8乘8格,从底行开始到顶行和左列 到右栏,A到H.
SAMPLE INPUT:TRIAL 1
============
Test.
Qh8 is attacking the target on Xb2
. . . . . . . Q
. . . . . . . .
. . . . . . . .
N . . . . . . .
. . B . K . . R
. . . . . . . .
. X . . . . . .
. . . . . . . .
在这种情况下,女王能够攻击,因为X不在网格的最左下方。但问题是我无法达到网格左下角的最后一个值,对于其他情况,请参阅它们以帮助您更好地了解我遇到的情况。
SAMPLE INPUT:TRIAL 2
============
Test.
. . . . . . . Q
. . . . . . . .
. . . . . . . .
. . . . . . . .
N . B . K . . R
. . . . . . . .
. . . . . . . .
X . . . . . . .
这就是我的意思,正如我上面所说,女王无法达到X值
这只是要解释我的意思
案例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;
}
答案 0 :(得分:2)
您有多少帐户以及您要求同样问题的次数
private boolean inBoard(int row, int col) {
return (row <= 8)
&& (row >= 1)
&& (col <= 8)
&& (col >= 1);
}
我很确定这个函数是错的,在Java Arrays中从0开始,你的表长度是8,所以它包含在0到7之间。
必须:
private boolean inBoard(int row, int col) {
return (row <= 7)
&& (row >= 0)
&& (col <= 7)
&& (col >= 0);
}
答案 1 :(得分:1)
您的inBoard算法已被关闭。您的电路板阵列是0-7,而不是1-8
答案 2 :(得分:0)
我没有时间了解你的算法,但我会这样做:
if(Dame_row == Q_row){
//now go through the distance in a loop, if nothing is in the way, return true
}else if(Dame_col == Q_col){
//now go through the distance in a loop, if nothing is in the way, return true
}else if(Math.abs(Dame_row - Q_row) == Math.abs(Dame_col - Q_col)){
//now go through the distance in a loop, if nothing is in the way, return true
}
return false;