简体   繁体   中英

Need help for java chess program

I have a project to do and I am completely stuck at this point and have no idea and inspiration on how to continue my code. I have certain tasks and specifications on how to write my code. This means the code should have a minumum of these classes and methods:

  1. 2 enums, one for Color with WHITE and BLACK, and one called ClassPieceType which means KING, QUEEN...
  2. ChessPiece, which is an abstract class and all classes for the types like King, queen which inherit from ChessPiece..
  3. a Board which represents the board with all his figures on it. But I somehow cannot progress on the board and I have no idea on how to implement the methods.
package chess;

public class Board {

    public static String[][] chessBoard2() {
        String[][] board = new String[8][8];

        char letter = 'a';
        int number;

        for (int row = 0; row < board.length; row++) {
            number = 8;
            for (int column = 0; column < board[0].length; column++) {
                board[row][column] = Character.toString(letter) + number;
                number--;
            }
            letter++;
        }
        return board;
    }
}

This is my idea of a create board method. But the task says

  1. Method has to be public Board() which creates the board in his normal state, and with unicode you have to add the pieces.

But I have no idea on how to implement the unicode, either. And how is it called just public Board ? There isn't a return type. I just don't know any more. I hope someone with HUGE patience can help me. I am just too incompetent to program and we have to hand our task in in several days.

"1. Method has to be public Board() which creates the board in his normal state [...]"

The confusion comes from the fact that such thing is not called a "method" but instead is called a "constructor". It initialize the object you want to create. The goal is that when you write new Board() it does initialize the board with the starting position of chess, something similar to what you are trying with the chessBoard2() method. The Board class can look like this:

public class Board {
    private final String[][] board;

    public Board() {
        this.board = new String[8][8];

        Board.fillFirstLine(this.board, 8, Color.BLACK);
        Board.fillPawnLine(this.board, 7, Color.BLACK);
        Board.fillPawnLine(this.board, 2, Color.WHITE);
        Board.fillFirstLine(this.board, 1, Color.WHITE);
    }

    private static void fillFirstLine(String[][] board, int rowNumber, Color color) {
        // ...
    }

    private static void fillPawnLine(String[][] board, int rowNumber, Color color) {
        // ...
    }
}

Then somewhere else you write

Board board = new Board();

and you have a fully initialized board ready to use.

"[...] and with unicode you have to add the pieces."

The chess piece icons are present in the unicode charset, see Chess symbols in Unicode . This means you can save the string "\♔" or the string "♔" inside the array to have this piece at the given position.

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