【问题标题】:how can i limit the 2D array user input and output the rest of values "."?我如何限制二维数组用户输入并输出其余值“。”?
【发布时间】:2017-04-27 10:55:23
【问题描述】:

我要做的是给用户 5 个输入(棋子),Rook,Bishop,King,Queen,Knight。并将它们输出到网格中,我不确定如何将用户输入限制为 5 次并用“。”打印网格的其余部分

我的输出结果现在是下面的网格,但是我如何获取 Qb2(Queen,在第 2 列,在第 2 行?))的用户输入并将其放入网格中?等等碎片?

Output of the result I get vs the output of the sample given

注意:列从“A”到“H”,行从底部开始,因此从“第 1 行”到“第 8 行”。

. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
. . . . . . . . 
. . . . . . . .

我的代码:

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

          private String[][] grid = new String[8][8];
          //target is x, so if x is at location in the grid, your program should determine if
          // any of the pieces can move to that position.
          private String king,queen,rook,bishop,knight,target;

          public void chessPieces(){

            //remember you're only using the peices in scanner for a string just for a test
        //switch them to 2D Array so that i can as
      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] = ".";
            System.out.printf("%2s",grid[row][column] + " ");
          }
          System.out.println();
      }
      }
      //get the userinput working first then make a file that gets the information for that userinput and outputs here
      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){
        chessMoves test1 = new chessMoves();
        test1.chessPieces();
        test1.printGrid();
      }
    }

【问题讨论】:

  • 怎么样:用“.”填充网格然后将每个棋子一个接一个地放入网格中(并在5后停止)?
  • 你跟踪棋子的位置,问他们要移动哪一个,然后移动它。不要问他们每一件应该在哪里。提示:不要使用数组。
  • @RC 请写一个示例代码或者什么我不明白你的意思
  • @jnbbendewr 为什么不呢?我需要一个二维数组,以便可以将结果放入二维数组中。基本上我正在使用数组制作一个文本国际象棋游戏。
  • 是但不是。如果您不了解具体内容,请询问,我(或其他人)可能会解释。

标签: java arrays


【解决方案1】:

“我的输出结果现在是下面的网格,但是我怎样才能获得 Qb2(女王,在第 2 列,在第 2 行?))的用户输入并将其放入网格中?等等碎片 ?” 用户将始终输入包含三个字符的字符串。 使用 charAt(int index)。 编辑1:(阅读评论后) 这是代码,运行它并告诉我这是否是您希望程序执行的操作。

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

      private String[][] grid = new String[8][8];
      //target is x, so if x is at location in the grid, your program should determine if
      // any of the pieces can move to that position.
      private String king,queen,rook,bishop,knight,target;

      public void chessPieces(){

        //remember you're only using the peices in scanner for a string just for a test
    //switch them to 2D Array so that i can as
  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] = "i";
   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();
  }

  }   
  //get the userinput working first then make a file that gets the information for that userinput and outputs here
  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){
    chessMoves test1 = new chessMoves();
    test1.chessPieces();
    test1.printGrid();
  }
}

为了解释我在做什么,让我解释一下 charAt() 返回一个字符。当类型转换为整数时,字符 a、b、c、d 将给出值 97、98、99...等等。类似的字符 1, 2,3,4... 将给出 47,48,49。 现在,当用户输入 rc7(比如说)时,代码采用对应于 c 和 7.c 对应于 99 的 2d 矩阵位置,您需要将其设为 2,因此减去 97(您可以在代码中看到)。同样,您可以看到 7 将如何变为 1。 如果您不明白,我建议您阅读有关 ASCII 和 Unicode 的内容。基本上 ecah 字符被分配了一个数字代码,仅此而已。 我把你的字符输入转换成整数放到网格上。

现在,让你的作品到达目标。为此定义另一个方法 boolean inScope(int[][] grid,string piece),在函数中定义一个二维数组(局部)让它成为8X8 并使其为int类型并将所有元素初始化为零。使数组可以到达的元素为1。如果目标位于1的位置。您的棋子可以到达那里,返回true。

【讨论】:

  • 你能解释一下这段代码是做什么的吗?比如为什么只有“r”我还想知道那个车“r”在国际象棋的哪一列和哪一行?
  • 嘿,非常感谢,但是有一个错误它不能完全工作,看看我上面上传的图片,编辑了主题,它显示了我得到的结果与样本的结果输入
  • 另外我想知道,如果我这样做的话,我以后可以使用这些位置来利用算法来确定哪个棋子可以攻击该位置基本上移动到这个项目中的位置)因为它只是字符串而不是二维字符串
  • 再次嘿嘿,非常感谢它的工作,但我现在的问题是在我继续调整程序之前,因为 peices 是一个字符串并且我有一个二维数组表,我可以稍后使用二维数组表将碎片移动到不同的位置?