Java Swing : How to build Connect 4 GUI

标签 java swing user-interface model-view-controller

所以我尝试使用 MVC 模式在 Java 中创建 Connect 4 GUI 程序,其中此类是模型:

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 的粗略模型:

Connect 4 GUI mockup

顶行是 JButton,单击时会将 token 放入面板的相应列中。我正在考虑使用 2D 6*7 JLabels 数组 来表示主板。 如何以上述方式显示

非常感谢您的帮助!

最佳答案

How can I display JLabels in the above manner (like a grid)?

您可以使用GridLayout .

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

How do I set up handling for all this in the Controller class?

您将拥有多个 Controller 类。在 Java Swing 中, Action 监听器是模型/ View / Controller 模式中的 Controller 。

用于放下碎片的 JButton 每个都有一个单独的操作监听器。

关于Java Swing : How to build Connect 4 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40062306/

相关文章:

java - JSP/Servlet 验证 - 转发与重定向

java - Firebase云消息不遵循后台实现

java - 集合功能方法是否调用 get、put 等?

java - 组件的宽度属性意外更改

java - 如何在 myadapter 中启动新 Intent

java - JFilechooser 关闭时退出

c++ - 如何在 Visual Studio 2012 上使用带控制台的 GUI Windows 应用程序?

swift - 如何应用阴影、圆角、图像和高斯模糊?

java - JFrame 在窗口大小最大化之前不会更新

java - 与在 Java 中使用 Swing 创建 GUI 相关