ООП моделирование - игра в шахматы разных классов, позволяющая прогрессировать игре - PullRequest
0 голосов
/ 05 ноября 2018

Пока я в растерянности, как поступить. На данный момент, я держу это просто, имея только 4 ладьи и 4 слона на доске. У меня есть игровой класс, который запускает игру, и класс доски, который устанавливает начальное состояние доски. Я не уверен, какие еще классы должны быть, игроки? и как позволить различным игрокам по очереди, то есть черным или белым, назначенным для класса игрока или класса фигуры? (я использую сканер терминала, чтобы получить печатный ввод от пользователя)

вот что у меня есть

public class Game {

    private static String WHITEPLAYS_MSG    = "White plays. Enter move:";
    private static String BLACKPLAYS_MSG    = "Black plays. Enter move:";
    private static String ILLEGALMOVE_MSG   = "Illegal move!";
    private static String WHITEWINS_MSG     = "White wins!";
    private static String BLACKWINS_MSG     = "Black win!";

    private Board gameBoard;


    public Game() {
        gameBoard = new Board();
    }
 // Build on this method to implement game logic.
    public void play() {

        EasyIn2 reader = new EasyIn2();

        gameBoard = new Board();    
        boolean done = false;

        while(!done) {                     //keeps looping when no one has won yet
            gameBoard.printBoard();

            System.out.println(WHITEPLAYS_MSG);

            String pos1 = reader.getString();         //gets user input ... move from... to....
            String pos2 = reader.getString();

        //not sure how to let game progress here...



            System.out.println(WHITEWINS_MSG);
            done = true;
        }
    }
}




public class Board {

    private static final char FREE         = '.';
    private static final char WHITEROOK    = '♖';
    private static final char BLACKROOK    = '♜';
    private static final char WHITEBISHOP  = '♗';
    private static final char BLACKBISHOP  = '♝';


    // people might find them useful for extensions
    private static final char WHITEKING    = '♔';
    private static final char BLACKKING    = '♚';
    private static final char WHITEQUEEN   = '♕';
    private static final char BLACKQUEEN   = '♛';
    private static final char WHITEKNIGHT  = '♘';
    private static final char BLACKKNIGHT  = '♞';
    private static final char WHITEPAWN    = '♙';
    private static final char BLACKPAWN    = '♟';

    private int boardsize;
    private char[][] board;


    public Board() {
        this.boardsize = DEFAULT_SIZE;

        board = new char[boardsize][boardsize];

        // Clear all playable fields
        for(int x=0; x<boardsize; x++)
            for(int y=0; y<boardsize; y++)
                board[x][y] = FREE;


        board[0][7] = BLACKROOK;
        board[2][7] = BLACKBISHOP;
        board[5][7] = BLACKBISHOP;
        board[7][7] = BLACKROOK;
        board[0][0] = WHITEROOK;
        board[2][0] = WHITEBISHOP;
        board[5][0] = WHITEBISHOP;
        board[7][0] = WHITEROOK;


    }


    public void printBoard() {

        // Print the letters at the top
        System.out.print(" ");
        for(int x=0; x<boardsize; x++)
            System.out.print(" " + (char)(x + 'a'));
        System.out.println();

        for(int y=0; y<boardsize; y++)
        {
            // Print the numbers on the left side
            System.out.print(8-y);

            // Print the actual board fields
            for(int x=0; x<boardsize; x++) {
                System.out.print(" ");
                char value = board[x][y];
                if(value == FREE) {
                    System.out.print(".");
                } else if(value >= WHITEKING && value <= BLACKPAWN) {
                    System.out.print(value);
                } else {
                    System.out.print("X");
                }
            }
            // Print the numbers on the right side
            System.out.println(" " + (8-y));
        }
        // Print the letters at the bottom
        System.out.print(" ");
        for(int x=0; x<boardsize; x++)
            System.out.print(" " + (char)(x + 'a'));
        System.out.print("\n\n");
}

public class W09Practical {

    public static void main(String[] args) {

        Game game = new Game();

        game.play();
    }
}
...