繁体   English   中英

Java Swing:如何构建Connect 4 GUI

[英]Java Swing: How to build Connect 4 GUI

因此,我尝试使用MVC模式在Java中创建一个Connect 4 GUI程序,其中此类为Model:

public class ConnectFourGame {

    private Board board;
    private Player playerX;
    private Player playerO;
    private int turnCount;
    private CheckWinnerAlgorithm checkWinner;

    /**
     * Constructs a new ConnectFourGame instance.
     */
    public ConnectFourGame() {
        this.board = new Board();
        this.playerX = new Player('X');
        this.playerO = new Player('O');
        this.turnCount = 1;
        this.checkWinner = new CheckWinnerAlgorithm();
    }

    /**
     * Accesses the current game board.
     * @return a Board object representing the current game board
     */
    public Board getBoard() {
        return this.board;
    }

    /**
     * Accesses player X and their attributes. 
     * @return a PlayerX object
     */
    public Player getPlayerX() {
        return this.playerX;
    }

    /**
     * Accesses player O and their attributes.
     * @return a PlayerO object
     */
    public Player getPlayerO() {
        return this.playerO;
    }

    /**
     * Returns the winner of the game.
     * @return a CheckWinnerAlgorithm object containing the winner token
     */
    public CheckWinnerAlgorithm getWinner() {
        return this.checkWinner;
    }

    /**
     * Determines whose turn it is based on the game's turn counter.
     * @return the Player whose turn it currently is
     */
    public Player determineTurn() {
        if (this.turnCount % 2 == 0) {
            return this.playerO;
        } 
        else {
            return this.playerX;
        }
    }

    /**
     * Assesses whether a player move can be made, placing a token if valid.
     * @param colNum An int specifying the column chosen by the player
     * @param playerToken A char representing the player's token
     * @return a boolean, true if a valid move has been made and false otherwise
     */
    public boolean moveIsMade(int colNum, char playerToken) {
        // move cannot be made if selected column is full
        if (board.getBoard()[0][colNum-1] != ' ') {
            System.out.println("The selected column is full.");
            return false;
        }
        // if column is not full, place token at bottom-most available spot
        for (int row=0; row<6; row++) {
            if (board.getBoard()[row][colNum-1] != ' ') {
                board.getBoard()[row-1][colNum-1] = playerToken;
                this.turnCount++;
                return true;
            }
        }
        // place token at bottom of empty column
        board.getBoard()[5][colNum-1] = playerToken;
        this.turnCount++;
        return true;
    }

    /**
     * Specifies whether the game is over.
     * @return a boolean, true if the game is over and false otherwise
     */
    public boolean isOver() {
        if (this.checkWinner.findWinner(this.board)!=' ') {
            this.resetGame();
            return true;
        }
        return false;
    }

    /**
     * Resets the game to a beginning state.
     */
    public void resetGame() {
        this.board.clearBoard();
        this.checkWinner.resetWinnerToken();
        this.turnCount = 1;
    }

}

现在,我试图弄清楚如何开始使用Swing GUI元素构建View类。 以下是我想到的GUI的大致模型:

连接4 GUI样机

第一行是JButton,单击它们会将令牌放到开发板的相应列中。 我正在考虑使用JLabel2D 6 * 7数组来表示板。 如何以上述方式显示此内容

帮助将不胜感激!

如何以上述方式显示JLabel(如网格)?

您将使用GridLayout

您的另一种选择是使用JPanel作为绘图面板,然后绘制游戏板。 通过绘制游戏板,您可以使棋子变成圆形而不是正方形。

如何在Controller类中为所有这些设置处理?

您将具有多个控制器类。 在Java Swing中,动作侦听器是模型/视图/控制器模式中的控制器。

放置片段的JButton将各自具有单独的动作侦听器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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