6x6国际象棋的二维数组。在数组中移动元素。 <JAVA>

问题描述 投票:-1回答:1

这是老师给我们布置的作业,基本上是一个6x6的象棋,有5个棋子:2个矮子,1个驴子,1个Mini-gun。基本上是一个6x6的国际象棋,有5个棋子:2x矮子,1x驴子,1x小炮,1x皇后和1x国王。棋子可以按照这样的顺序移动。

矮人--每次向前走一格. 当它到达棋盘的另一端时,它开始按照同样的规则向后移动。

驴子- 向任何方向移动两个格子,但每三步只能移动一次。

迷你枪--向左向右或向上移动一格。

皇后-对角线上的一个正方形

王--任何方向的一个方块

我的问题是如何让它们在这个数组中移动?我知道这对你们大多数人来说可能很简单,但我还在学习编程(不是专门在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(" ───────────────────────");



  }
  }
java arrays multidimensional-array
1个回答
0
投票

从你的问题中,我的答案是...

你可以创建一个6x6的数组,数值为0,并给每个棋子一个数值,比如矮人:1, 驴:2, so n so.

当你要移动时,只需根据数组的索引值来移动它们,例如,如果一个皇后在arr[1][3]中,它可以移动到arr[0][2]、arr[0][4]、arr[2][2]和arr[2][4]。

© www.soinside.com 2019 - 2024. All rights reserved.