简体   繁体   English

Java Swing:如何构建Connect 4 GUI

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

So I'm trying to create a Connect 4 GUI program in Java using the MVC pattern, where this class is the Model: 因此,我尝试使用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;
    }

}

Now I'm trying to figure out how to start building the View class with Swing GUI elements. 现在,我试图弄清楚如何开始使用Swing GUI元素构建View类。 Below is a rough mockup of the GUI I have in mind: 以下是我想到的GUI的大致模型:

连接4 GUI样机

The top row are JButtons which will drop a token into the corresponding column of the board when clicked. 第一行是JButton,单击它们会将令牌放到开发板的相应列中。 I'm thinking of using a 2D 6*7 array of JLabels to represent the board. 我正在考虑使用JLabel2D 6 * 7数组来表示板。 How can I display this in the above manner ? 如何以上述方式显示此内容

Help would be greatly appreciated! 帮助将不胜感激!

How can I display JLabels in the above manner (like a grid)? 如何以上述方式显示JLabel(如网格)?

You would use the GridLayout . 您将使用GridLayout

Your other alternative is to use a JPanel as a drawing panel, and draw the game board. 您的另一种选择是使用JPanel作为绘图面板,然后绘制游戏板。 By drawing the game board, you can make the pieces circles instead of squares. 通过绘制游戏板,您可以使棋子变成圆形而不是正方形。

How do I set up handling for all this in the Controller class? 如何在Controller类中为所有这些设置处理?

You will have more than one controller class. 您将具有多个控制器类。 In Java Swing, action listeners are the controllers in your model / view / controller pattern. 在Java Swing中,动作侦听器是模型/视图/控制器模式中的控制器。

The JButtons to drop pieces will each have a separate action listener. 放置片段的JButton将各自具有单独的动作侦听器。

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

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