2017-05-04 54 views
0

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

回答

2

https://stackoverflow.com/questions/43740616/how-can-i-check-if-queen-is-able-to-attack-the-x-position-but-not-moving-it-t/43740955#43740955

多少帳戶你有以及你要問幾次相同的問題

private boolean inBoard(int row, int col) { 
     return (row <= 8) 
       && (row >= 1) 
       && (col <= 8) 
       && (col >= 1); 
    } 

我敢肯定,這個函數是錯誤的,在Java數組中開始爲0,並且表的長度爲8,所以它包含在0和7之間。

必須是:

private boolean inBoard(int row, int col) { 
      return (row <= 7) 
        && (row >= 0) 
        && (col <= 7) 
        && (col >= 0); 
     } 
+0

忽略我做的評論,我認爲你是對的我會做 –

+0

嘗試修復你的inBoard功能並告訴我們它是否解決了你的問題。 – rilent

+0

是的!非常感謝你我的朋友,我很抱歉如果我困擾你,但是我最終因爲你而感到工作上的問題,謝謝<3。另外對不起,如果我很煩,我是初學者,並在11年級 –

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; 
+0

但是我的程序忽略了阻塞,所以它沒有什麼問題。我的moveRow或moveColumn有問題嗎?或循環多少次呢? –

1

您內側算法是關閉的一個。你的主板陣列是0-7,而不是1-8

+0

是啊,謝謝,它的工作如同沉默說早些時候 –