【问题标题】:Java Swing: How to build Connect 4 GUIJava Swing:如何构建 Connect 4 GUI
【发布时间】:2016-10-15 17:47:50
【问题描述】:

所以我正在尝试使用 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 的粗略模型:

最上面一行是 JButtons,当点击它时,它会在板子的相应列中放置一个标记。我正在考虑使用 2D 6*7 的 JLabels 数组 来表示板子。 我怎样才能以上述方式显示它

我们将不胜感激!

【问题讨论】:

  • "...so if anyone could provide an outline of the methods required in each class that would be a fantastic help!" -- 过于宽泛,可能超出了本网站的范围,对于多部分问题也是如此。这是一个问答网站,因此请在每个帖子中提出一个具体问题。
  • 你应该已经有了一个简单的视图 - JFrame + 一些按钮/标签等。将一些动作监听器附加到你的视图的一些地方,然后相应地更新你的模型。那是控制器
  • @HovercraftFullOfEels 谢谢,我更新了帖子以提出一个具体问题。
  • @cricket_007 感谢您的帮助。我现在已经将问题修改为专门关于如何实现 GUI 元素 - 任何指针?

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


【解决方案1】:

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

您可以使用GridLayout

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

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

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

每个用于放置片段的 JButton 都有一个单独的动作监听器。

【讨论】:

    猜你喜欢
    • 2010-10-07
    • 2011-01-22
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多