女王棋盘运动的算法?

时间:2017-04-29 23:21:18

标签: java algorithm

所以我的棋盘网格与其他网格有点不同。行从下到上开始,所以从底部开始是1到8。列从A到H,基本上是8乘8的网格。现在直截了当地说,我不确定如何制作一个允许女王棋子攻击一个位置的算法,即“X”。所以如果我做这个输入

我的计划的示例输入

. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
. . B X K . . R 
. . N . . . . . 
. Q . . . . . . 
. . . . . . . .

我如何加入阻止?就像虽然有“N”这是骑士的作品。总的来说,基本上我将如何制作一个算法,在网格的任何一点上,女王都可以攻击目标(“X”)?

编辑(更新):我发现了怎么做但是当我尝试时,它只移动一个方格,为什么会这样?

我的QUEEN算法的样本

. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
Q . Q . . . . . 
. . . . . . . . 
Q . Q . . . . . 

消息来源

 import java.util.Scanner;
    class chessMovesz
    {
      //MAIN CODE AT THE VERY BOTTOM OF THE CLASS
      Scanner sc = new Scanner(System.in);

      private String[][] grid = new String[8][8];

      private String king,queen,rook,bishop,knight,target;

      public void getPieces(){


  System.out.println("Hello Guest00129, Welcome to Chess.");
  System.out.println("In order to play this game, input pieces like below(cap;atilaized)");
  System.out.println("Rook at column c and at row 5 then: Rc5");

  System.out.println("Please enter a position for Rook");
  rook = sc.nextLine();
  System.out.println("Please enter a position for King");
  king = sc.nextLine();
  System.out.println("Please enter a position for Queen");
  queen = sc.nextLine();
  System.out.println("Please enter a position for Bishop");
  bishop = sc.nextLine();
  System.out.println("Please enter a position for Knight");
  knight = sc.nextLine();
  System.out.println("Please enter a position for Target(X) to move the peices to that position");
  target = sc.nextLine();
  }
  public void printGrid(){      
        for(int row = 0; row <grid.length; row++){
      for (int column = 0;column <grid[row].length; column++){
        grid[row][column] = ".";
      }
  }    
   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-queen.charAt(2)+49][(int)queen.charAt(1)-97] = "Q";
   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";



        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();
  }

  }   



     public void movePosition(){



    for(int row = 0; row < grid.length; row++){
      for(int column = 0; column < grid[row].length; column++){


        grid[row][column] = ".";

        //south east  diaognal
        grid[7-queen.charAt(2)+49 +1][(int)queen.charAt(1)-97 +1 ] = "Q";

        //North West diagonal
        grid[7-queen.charAt(2)+49 -1][(int)queen.charAt(1)-97 -1 ] = "Q";

        //North East diagonal
        grid[7-queen.charAt(2)+49 -1][(int)queen.charAt(1)-97 +1 ] = "Q";

        //South West Diagonal
        grid[7-queen.charAt(2)+49 +1][(int)queen.charAt(1)-97 -1 ] = "Q";


   System.out.printf("%2s",grid[row][column] + " ");
      }


      System.out.println();

    }
  }





      public void readChessPositions(){
      }
      //the file created from the method above read it print the grid here like printout here and show the possible
      //positons that can attack 
      public void chessOutput(){
      }
      //method that prints the grid with the positiosn showed in the outputfile of chess moves
      //print all empty spaces with dot(.) and the postiions
      public static void main (String[] args){
        chessMovesz test1 = new chessMovesz();
        test1.getPieces();
        test1.printGrid();
        System.out.println("");
        test1.movePosition();
      }
    }

1 个答案:

答案 0 :(得分:0)

如果我正确理解了这个问题(请参阅上面的评论),我的建议是首先计算出Rook / Castle的算法 - 也就是说,它需要与女王在同一行或列上没有任何碎片之间。然后添加另一个类似的规则来处理对角线。

或者,您可以计算出女王能够移动到的所有位置。然后看看X是否是其中之一。

更新(重新评论,下方)

A1 B1 C1 D1 E1 F1 G1 H1
A2 B2 C2 D2 E2 F2 G2 H2
A3 B3 C3 D3 E3 F3 G3 H3
A4 B4 C4 D4 E4 F4 G4 H4
A5 B5 C5 D5 E5 F5 G5 H5
A6 B6 C6 D6 E6 F6 G6 H6
A7 B7 C7 D7 E7 F7 G7 H7
A8 B8 C8 D8 E8 F8 G8 H8