皇后棋盘运动的算法?

问题描述 投票:0回答:2

所以我的棋盘网格与其他的有点不同。行从下到上,就像从下往上依次为 1 到 8 行。列从 A 到 H,基本上是 8 x 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();
      }
    }
java algorithm
2个回答
0
投票

如果我正确理解了这个问题(请参阅上面的评论),我的建议是首先计算出车/城堡的算法 - 即它需要与女王位于同一行或同一列,中间没有棋子。然后添加另一个类似的规则来处理对角线。

或者,您可以计算出女王能够移动到的所有位置。然后看看 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

0
投票

国际象棋棋盘通常是 64 个方格 [8x8]。简化每个方格的数学数字 [0-63]。 皇后可以向 8 个可能的方向移动,最多 7 个方格。 首先创建所有可能的移动的列表[56]。遍历移动列表并检查每个可能的位置: 是否在棋盘上,丢弃; 占用的颜色是否相同: 计算阴影,丢弃; 是否被对手占用,标记以供进一步处理。

计算所有可能位置的方法: 获得皇后位置(pos) //向前移动 moveList[0] = pos + 1; moveList[1] = pos + 2; ... moveList[9] = pos - 1; moveList[10] = pos- 2;

© www.soinside.com 2019 - 2024. All rights reserved.