6×6国际象棋与二维数组。在数组中移动元素。

huangapple go评论61阅读模式
英文:

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 &lt; 6; i++) {
for (int j = 0; j &lt; 6; j++) {
board[i][j] = &quot;   &quot;;
}
}
for (int i = 0; i &lt; 5; i++){
for (int j = 1; j &lt; 4; j++){
board[i][j] = &quot;   &quot;;
}
}
// Dwarfs
board[0][0] = &quot;wD1&quot;;
board[0][5] = &quot;wD2&quot;;
board[5][0] = &quot;bD1&quot;;
board[5][5] = &quot;bD2&quot;;
// Machine-guns
board[0][4] = &quot;wMG&quot;;
board[5][1] = &quot;bMG&quot;;
// Donkeys
board[0][1] = &quot;wDK&quot;;
board[5][4] = &quot;bDK&quot;;
//Queens
board[0][2] = &quot;wQN&quot;;
board[5][3] = &quot;bQN&quot;;
// Kings
board[0][3] = &quot;wKG&quot;;
board[5][2] = &quot;bKG&quot;;
System.out.println(&quot; ───────────────────────&quot;);
System.out.println(&quot;│&quot; + board[0][0] + &quot;│&quot; + board[0][1] + &quot;│&quot; + board[0][2] + &quot;│&quot; + board[0][3] + &quot;│&quot; + board[0][4] + &quot;│&quot; + board[0][5] + &quot;│&quot;);
System.out.println(&quot;-------------------------&quot;);
System.out.println(&quot;│&quot; + board[1][0] + &quot;│&quot; + board[1][1] + &quot;│&quot; + board[1][2] + &quot;│&quot; + board[1][3] + &quot;│&quot; + board[1][4] + &quot;│&quot; + board[1][5] + &quot;│&quot;);
System.out.println(&quot;-------------------------&quot;);
System.out.println(&quot;│&quot; + board[2][0] + &quot;│&quot; + board[2][1] + &quot;│&quot; + board[2][2] + &quot;│&quot; + board[2][3] + &quot;│&quot; + board[2][4] + &quot;│&quot; + board[2][5] + &quot;│&quot;);
System.out.println(&quot;-------------------------&quot;);
System.out.println(&quot;│&quot; + board[3][0] + &quot;│&quot; + board[3][1] + &quot;│&quot; + board[3][2] + &quot;│&quot; + board[3][3] + &quot;│&quot; + board[3][4] + &quot;│&quot; + board[3][5] + &quot;│&quot;);
System.out.println(&quot;-------------------------&quot;);
System.out.println(&quot;│&quot; + board[4][0] + &quot;│&quot; + board[4][1] + &quot;│&quot; + board[4][2] + &quot;│&quot; + board[4][3] + &quot;│&quot; + board[4][4] + &quot;│&quot; + board[4][5] + &quot;│&quot;);
System.out.println(&quot;-------------------------&quot;);
System.out.println(&quot;│&quot; + board[5][0] + &quot;│&quot; + board[5][1] + &quot;│&quot; + board[5][2] + &quot;│&quot; + board[5][3] + &quot;│&quot; + board[5][4] + &quot;│&quot; + board[5][5] + &quot;│&quot;);
System.out.println(&quot; ───────────────────────&quot;);
}
}

答案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].

huangapple
  • 本文由 发表于 2020年5月4日 02:55:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/61579976.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定