OOP建模-使用不同类的国际象棋游戏,使游戏得以进行

时间:2018-11-05 20:46:19

标签: java oop chess

到目前为止,我对如何继续一无所知。目前,我只剩下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();
    }
}

0 个答案:

没有答案