2017-05-04 57 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

是啊,谢谢,它的工作如同沉默说早些时候 –