如何在GUI的TicTacToe游戏中实现逻辑?

时间:2019-05-10 04:04:48

标签: java

我们在编程过程中获得了此TicTacToe GUI,我想知道如何在游戏中实现逻辑,我不确定逻辑应采用哪种方法,我也想知道可以保持得分并跟踪获胜百分比。我了解GUI的一般概念,但很想知道这款游戏如何正常工作。

//TicTacToe class

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class TicTacToeGamePanel extends JPanel {

    private BorderLayout layout;
    private JLabel header;
    private JPanel footerPanel;
    private JLabel footerMessage;
    private JButton resetBtn;

    private JPanel statsPanel;
    private JLabel winsLabel, tiesLabel, lossLabel,
                    winsValue, tiesValue, lossValue, 
                    winPerLabel, winPerValue;



    private JButton[] gameBoard =  {
            new JButton(), new JButton(), new JButton(),
            new JButton(), new JButton(), new JButton(),
            new JButton(), new JButton(), new JButton()};
    private JPanel gameBoardPanel;

    private GameButtonListener listener;

    private Color preferredColor;

    public TicTacToeGamePanel() {

        preferredColor = Color.CYAN;

        layout = new BorderLayout();
        this.setLayout(layout);

        //Set heading
        header = new JLabel("Tic Tac Toe");
        header.setFont(new Font("Arial", Font.BOLD, 24));
        header.setHorizontalAlignment(SwingConstants.CENTER);

        //init footer panel
        initFooterPanel();

        //init stats panel
        initStatsPanel();

        //init game board
        initGameBoard();

        this.setBackground(preferredColor);

        this.add(header, BorderLayout.NORTH);
        this.add(footerPanel, BorderLayout.SOUTH);
        this.add(statsPanel, BorderLayout.WEST);
        this.add(gameBoardPanel, BorderLayout.CENTER);
    }

    private void initFooterPanel() {
        //Set footer panel
        footerPanel = new JPanel();
        BoxLayout footerLayout = new BoxLayout(footerPanel, BoxLayout.Y_AXIS);
        footerPanel.setLayout(footerLayout);

        footerMessage = new JLabel("Click a button to take that spot!");
        footerMessage.setFont(new Font("Arial", Font.BOLD, 18));
        footerMessage.setAlignmentX(CENTER_ALIGNMENT);

        resetBtn = new JButton("Reset game");
        resetBtn.setAlignmentX(CENTER_ALIGNMENT);
        resetBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                for(JButton b : gameBoard) {
                    b.setText("");
                }
                listener.resetGameTurnCount();
            }
        });

        footerPanel.add(Box.createRigidArea(new Dimension(0, 25)));
        footerPanel.add(footerMessage);
        footerPanel.add(Box.createRigidArea(new Dimension(0, 25)));
        footerPanel.add(resetBtn);
        footerPanel.add(Box.createRigidArea(new Dimension(0, 25)));

        footerPanel.setBackground(preferredColor);
    }

    private void initStatsPanel() {
        GridLayout statsLayout = new GridLayout(4, 2);

        statsPanel = new JPanel();
        statsPanel.setLayout(statsLayout);

        winsLabel = new JLabel("Wins:");
        tiesLabel = new JLabel("Ties:");
        lossLabel = new JLabel("Losses:");
        winPerLabel = new JLabel("Win %:");

        Font labelFont = new Font("Arial", Font.BOLD, 14);
        winsLabel.setFont(labelFont);
        tiesLabel.setFont(labelFont);
        lossLabel.setFont(labelFont);
        winPerLabel.setFont(labelFont);

        winsValue = new JLabel("");
        tiesValue = new JLabel("");
        lossValue = new JLabel("");
        winPerValue = new JLabel("");

        statsPanel.add(winsLabel);
        statsPanel.add(winsValue);
        statsPanel.add(tiesLabel);
        statsPanel.add(tiesValue);
        statsPanel.add(lossLabel);
        statsPanel.add(lossValue);
        statsPanel.add(winPerLabel);
        statsPanel.add(winPerValue);

        statsPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        statsPanel.setBackground(preferredColor);
    }

    private void initGameBoard() {
        gameBoardPanel = new JPanel();

        GridLayout gameBoardLayout = new GridLayout(3, 3);
        gameBoardPanel.setLayout(gameBoardLayout);

        gameBoardPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        gameBoardLayout.setHgap(20);
        gameBoardLayout.setVgap(20);

        Font buttonFont = new Font("Arial", (Font.BOLD + Font.ITALIC), 50);
        Dimension buttonDimension = new Dimension(50, 50);

        listener = new GameButtonListener();

        for(JButton b : gameBoard) {
            b.setFont(buttonFont);
            b.setPreferredSize(buttonDimension);
            gameBoardPanel.add(b);

            b.addActionListener(listener);
        }
        gameBoardPanel.setBackground(preferredColor);
    }

    private class GameButtonListener implements ActionListener {
        private int gameTurnCount = 0;


        public void actionPerformed(ActionEvent e) {
            JButton clickedButton = (JButton)e.getSource();

            clickedButton.setText(gameTurnCount%2 == 0 ? "X" : "O");


            gameTurnCount++;

        }

        public int getGameTurnCount() {
            return gameTurnCount;

        }

        public void resetGameTurnCount() {
            gameTurnCount = 0;
        }

    }

}

 //Driver Class 
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;

    public class LayoutManagerTest {

        public static void main(String[] args) {

            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JTabbedPane tabePane = new JTabbedPane();


            tabePane.addTab("Tic Tac Toe", new TicTacToeGamePanel());

            frame.getContentPane().add(tabePane);
            frame.pack();
            frame.setVisible(true);

        }
    }

0 个答案:

没有答案