我有功课来制作自定义版本的6x6的国际象棋,棋子以特定的方式移动。但是现在,我试图弄清楚如何选择一块并将其放置在我已设置的2d数组内的另一个位置并交换字符的位置。现在,我有:
public class Chess {
static final int rows = 7;
static final int columns = 7;
public static void main(String[] args) {
String[][] board = new String[rows][columns];
fillBoard(board);
enterMove();
}
public static void fillBoard(String[][] board){
for (int row = 0; row < 7; row++) {
for (int col = 0; col < 7; col++) {
board[row][col] = " X ";
outerShell(board);
pawns(board);
System.out.print(board[row][col]);
}
System.out.println();
}
}
public static void outerShell(String[][] board){
board[0][0] = " ";
board[0][1] = " A ";
board[0][2] = " B ";
board[0][3] = " C ";
board[0][4] = " D ";
board[0][5] = " E ";
board[0][6] = " F ";
board[1][0] = " 6 ";
board[2][0] = " 5 ";
board[3][0] = " 4 ";
board[4][0] = " 3 ";
board[5][0] = " 2 ";
board[6][0] = " 1 ";
}
public static void pawns(String[][] board){
//whitePawns
board[1][1] = "wDw";
board[1][2] = " wD";
board[1][3] = " wQ";
board[1][4] = " wK";
board[1][5] = " wM";
board[1][6] = " wDw ";
//blackPawns
board[6][1] = "bDw";
board[6][2] = " bM";
board[6][3] = " bK";
board[6][4] = " bQ";
board[6][5] = " bD";
board[6][6] = " bDw";
}
public static void enterMove(){
System.out.println("");
System.out.println("Select the piece you want to move(Example a1) if you want to quit enter: q");
Scanner gameInput = new Scanner(System.in);
String pieceSelect = gameInput.nextLine();
if(pieceSelect.equals("q")){
System.exit(0);
}
switch (pieceSelect){
case "a1":
}
System.out.println(" ");
System.out.println("Now select a finishing position for the piece to move to");
String piecePlace = gameInput.nextLine();
switch (piecePlace){
case "a2":
}
}
}
在enterMove()
部分中,我试图用switch
来做,但是不知道如何交换数组中的两个位置。我不确定使用switch
,因为我不确定是否可能。如果有人可以编写此代码的版本,则通过输入特定坐标(如a1)选择bDw
,然后输入另一个坐标并将bDw
与X
交换,我正在将其用作空白指示符,并在可能的情况下使用解释一下,我将不胜感激。
我以前制作过象棋程序,以前将所有棋子存储在一个数组中(这里是6x6数组),我使用索引值作为棋盘坐标。因此,在选择一块时,会调用一个函数来显示所选块的有效移动。
有效的移动可以存储在某个数组中并可以进行检查。然后只需交换数组本身内的值即可。