我想要做的是给用户5个输入(国际象棋棋子),鲁克,主教,国王,女王,骑士。并将它们输出到网格中,我不确定如何将用户输入限制为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();
}
}
答案 0 :(得分:1)
“我的输出结果现在是下面的网格,但是如何获得Qb2的用户输入(Queen,第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 ... so.Similarly characters 1, 2,3,4 ......将给出47,48,49。 现在,当用户输入rc7(比方说)时,代码采用2d矩阵位置,相互反应c和7.c相互反应为99,你需要使它2减去97(你可以在代码中看到)。同样你可以看出7将如何变为1。 如果您不明白,我建议阅读有关ASCII和Unicode的内容。基本上,ecah字符是一个数字代码,就是这样。 我接受了你的角色输入并将它们转换成整数以将它们放在网格上。
现在,让你的作品转到目标。为此,定义另一种方法 boolean inScope(int [] [] grid,string piece),在函数中定义一个2d数组(本地)让它成为8X8 并且让它为int类型并将所有元素初始化为零。使得一个片段的元素可以被片段1到达。如果目标站在一个1.你的片段可以到达的位置,则返回true。 / p>