第一個只是簡單介紹一下我的程序,它的棋盤。 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年級 –