0

What I'm suppose to do is give the user 5 inputs(pieces of chess), Rook,Bishop,King,Queen,Knight. and output them in a grid, I'm not sure how I would limit the user input to 5 times and print the rest of the grid with "."

my output results right now the grid below, but how can i get the userinput for Qb2(Queen, at column 2, at row 2?)) and out put it in the grid?and so on for the rest of the pieces ?

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

Note: columns are from "A" to "H" and rows are starting from the bottom, so from "row 1" to row "8".

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

My code:

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
  • How about this: fill the grid with "." and then put each chess piece one after one inside the grid (and stop after 5)? Commented Apr 26, 2017 at 15:43
  • You track the chess pieces position, ask them which one they want to move, then move it. Don't ask them where EVERY SINGLE PIECE should be. Hint: Don't use an array. Commented Apr 26, 2017 at 15:43
  • @RC please write a sample code or something I dont understand what you mean Commented Apr 26, 2017 at 18:59
  • @jnbbendewr why not ? I need an 2D array so that I could out put the results in a 2D array. basically im making a text chess game using arrays. Commented Apr 26, 2017 at 19:01
  • Yes but no. If you don't understand something specific, then ask and I (or someone else) will probably explain. Commented Apr 26, 2017 at 20:21

1 Answer 1

1

"my output results right now the grid below, but how can I get the userinput for Qb2(Queen, at column 2, at row 2?)) and out put it in the grid?and so on for the rest of the pieces ?" The user will always enter a string with three characters. Use charAt(int index). Edit 1 : (After reading the comment) Here is the code , run it and tell me if this is what you want the program to do.

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

To explain what I am doing let me explain that charAt() returns a character.Character a,b,c,d when typecasted into integers will give values 97,98,99...so on.Similarly characters 1,2,3,4... will give 47,48,49. Now when the user inputs rc7(say) the code takes the 2d-matrix position that corrosponds to c and 7.c corrosponds to 99, you need to make it 2 so substract 97(you can see that in the code).Similarly you can see how 7 will becomes 1. If you don't understand , I recommending reading about ASCII and Unicode.basically ecah character is alloted a numeric code,that is all it is. I took your character inputs and converted them into integers to put them on the grid.

Now,making your pieces go to the target.To do so define another method boolean inScope(int[][] grid,string piece),define a 2d array (local) in the funtion let it be 8X8 and let it be of type int and initialize all elements to zero.Make the elements of the array that can be reached by a piece 1.If the target stands on a position that is 1.your piece can reach there,Return true.

Sign up to request clarification or add additional context in comments.

4 Comments

can u explain what that code does plesae? like why only "r" I want to know also where that rook "r" is at which column and which row of the chess ?
Hey thank you so much, but there is an error it doesn't work fully, look at the image i uploaded above, Edited the topic and it shows the results i get vs the results of the sample input
Also I was wondering, if i do it that way Can i later on use these locations to make use of algorithms for determining which piece can attack the position basically move to position in this project) since its only a string and not a 2D string
Heey again, thank you soo much it worked, but my question now is before i move on forward adjust the program, since the peices are a string and i have a 2D array table, Could i later on move the pieces to different locations using the 2D array table ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.