英文:
6x6 Chess with two dimensional array. Moving elements in an array. <JAVA>
问题
这是老师给我们的一份作业。基本上是一个6x6的棋盘,上面有5个棋子:2个矮人(Dwarf),1只驴(Donkey),1把迷你机枪(Mini-gun),1个皇后(Queen)和1个国王(King)。棋子可以按照以下顺序移动:
-
矮人(Dwarf):每次只能向前移动一格。当它到达棋盘的另一端时,按照相同的规则开始向后移动。
-
驴(Donkey):每三步只能向任意方向移动两格。
-
迷你机枪(Mini-gun):可以向左/右或上/下移动一格。
-
皇后(Queen):可以斜对角移动一格。
-
国王(King):可以向任意方向移动一格。
我的问题是如何在这个数组中使它们移动?我知道这对大多数人来说可能很容易,但我仍然在学习编程(不仅限于Java)。
以下是代码部分:
public class TheONEChessGameYouNEVER_EXPECTED {
public static void main(String[] args) {
String[][] board = new String[6][6];
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
board[i][j] = " ";
}
}
for (int i = 0; i < 5; i++){
for (int j = 1; j < 4; j++){
board[i][j] = " ";
}
}
// Dwarfs
board[0][0] = "wD1";
board[0][5] = "wD2";
board[5][0] = "bD1";
board[5][5] = "bD2";
// Machine-guns
board[0][4] = "wMG";
board[5][1] = "bMG";
// Donkeys
board[0][1] = "wDK";
board[5][4] = "bDK";
//Queens
board[0][2] = "wQN";
board[5][3] = "bQN";
// Kings
board[0][3] = "wKG";
board[5][2] = "bKG";
System.out.println(" ───────────────────────");
System.out.println("│" + board[0][0] + "│" + board[0][1] + "│" + board[0][2] + "│" + board[0][3] + "│" + board[0][4] + "│" + board[0][5] + "│");
System.out.println("-------------------------");
System.out.println("│" + board[1][0] + "│" + board[1][1] + "│" + board[1][2] + "│" + board[1][3] + "│" + board[1][4] + "│" + board[1][5] + "│");
System.out.println("-------------------------");
System.out.println("│" + board[2][0] + "│" + board[2][1] + "│" + board[2][2] + "│" + board[2][3] + "│" + board[2][4] + "│" + board[2][5] + "│");
System.out.println("-------------------------");
System.out.println("│" + board[3][0] + "│" + board[3][1] + "│" + board[3][2] + "│" + board[3][3] + "│" + board[3][4] + "│" + board[3][5] + "│");
System.out.println("-------------------------");
System.out.println("│" + board[4][0] + "│" + board[4][1] + "│" + board[4][2] + "│" + board[4][3] + "│" + board[4][4] + "│" + board[4][5] + "│");
System.out.println("-------------------------");
System.out.println("│" + board[5][0] + "│" + board[5][1] + "│" + board[5][2] + "│" + board[5][3] + "│" + board[5][4] + "│" + board[5][5] + "│");
System.out.println(" ───────────────────────");
}
}
请注意,这段代码创建了一个6x6的棋盘数组,然后将各个棋子放置在适当的位置,并以合适的方式打印出来。
英文:
This is a homework that the teacher gave us. It's basically a 6x6 chess with 5 pieces: 2x Dwarf,1x Donkey,1x Mini-gun,1x Queen and 1x King. The pieces can move in this order:
Dwarf- One square forward at a time. When it hits the other end of the board it starts to move backwards by the same rule.
Donkey- Two squares in any direction, but only once every third move.
Mini-gun- One square left/right or up/down
Queen- One square diagonally
King- One square in any direction
My question is how to make them move in this array? I know that this is probably very easy for most of you, but I am still learning to code(not specifically on java but at all).
public class TheONEChessGameYouNEVER_EXPECTED {
public static void main(String[] args) {
String[][] board = new String[6][6];
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
board[i][j] = " ";
}
}
for (int i = 0; i < 5; i++){
for (int j = 1; j < 4; j++){
board[i][j] = " ";
}
}
// Dwarfs
board[0][0] = "wD1";
board[0][5] = "wD2";
board[5][0] = "bD1";
board[5][5] = "bD2";
// Machine-guns
board[0][4] = "wMG";
board[5][1] = "bMG";
// Donkeys
board[0][1] = "wDK";
board[5][4] = "bDK";
//Queens
board[0][2] = "wQN";
board[5][3] = "bQN";
// Kings
board[0][3] = "wKG";
board[5][2] = "bKG";
System.out.println(" ───────────────────────");
System.out.println("│" + board[0][0] + "│" + board[0][1] + "│" + board[0][2] + "│" + board[0][3] + "│" + board[0][4] + "│" + board[0][5] + "│");
System.out.println("-------------------------");
System.out.println("│" + board[1][0] + "│" + board[1][1] + "│" + board[1][2] + "│" + board[1][3] + "│" + board[1][4] + "│" + board[1][5] + "│");
System.out.println("-------------------------");
System.out.println("│" + board[2][0] + "│" + board[2][1] + "│" + board[2][2] + "│" + board[2][3] + "│" + board[2][4] + "│" + board[2][5] + "│");
System.out.println("-------------------------");
System.out.println("│" + board[3][0] + "│" + board[3][1] + "│" + board[3][2] + "│" + board[3][3] + "│" + board[3][4] + "│" + board[3][5] + "│");
System.out.println("-------------------------");
System.out.println("│" + board[4][0] + "│" + board[4][1] + "│" + board[4][2] + "│" + board[4][3] + "│" + board[4][4] + "│" + board[4][5] + "│");
System.out.println("-------------------------");
System.out.println("│" + board[5][0] + "│" + board[5][1] + "│" + board[5][2] + "│" + board[5][3] + "│" + board[5][4] + "│" + board[5][5] + "│");
System.out.println(" ───────────────────────");
}
}
答案1
得分: 1
根据我理解你的问题,我的答案是:
你可以创建一个6x6的数组,值为0,并为每个棋子赋予一个值,比如小矮人:1,驴:2,依此类推。
当你想要移动棋子时,只需根据数组的索引值进行移动,例如,如果一个皇后在arr[1][3],它可以移动到arr[0][2],arr[0][4],arr[2][2]和arr[2][4]。
英文:
From what Ive understood from your question, my answer is that
you could create a 6x6 array with values 0
and give each piece a value like dwarf:1, donkey:2, so n so.
when u want to move just move them based on the index values of the array
e.g., if a queen is in arr[1][3] it can move to arr[0][2], arr[0][4], arr[2][2]
and arr[2][4].
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论