简体   繁体   中英

6x6 Chess with two dimensional array. Moving elements in an array. <JAVA>

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(" ───────────────────────");



  }
  }

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 eg, 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].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM