Reputation: 27
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();
}
}
Upvotes: 0
Views: 370
Reputation: 121
"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.
Upvotes: 1