Java版TicTacToe

MainFrame.java

package com.bu_ish;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MainFrame extends JFrame {
    
    private Cell[][] cells;
    private JLabel statusLabel;
    private JButton startButton;
    private char whoseTurn;
    private boolean isStarted;
    
    private MainFrame(String title) {
        super(title);
        setSize(360, 360);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel mainPanel = new JPanel(new GridLayout(3, 3));
        cells = new Cell[3][3];
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j) {
                cells[i][j] = new Cell();
                mainPanel.add(cells[i][j]);
            }
        }
        mainPanel.setVisible(true);
        add(mainPanel);
        JPanel statusPanel = new JPanel(new BorderLayout());
        statusLabel = new JLabel();
        statusLabel.setHorizontalAlignment(JLabel.CENTER);
        statusPanel.add(statusLabel);
        startButton = new JButton("开始");
        statusPanel.add(startButton, BorderLayout.EAST);
        add(statusPanel, BorderLayout.SOUTH);
        startButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                String text = startButton.getText();
                if (text.equals("开始") || text.equals("重新开始")) {
                    startButton.setText("已开始");
                    startButton.setEnabled(false);
                    statusLabel.setText("O先行");
                    whoseTurn = 'O';
                    isStarted = true;
                    for (int i = 0; i < 3; ++i) {
                        for (int j = 0; j < 3; ++j) {
                            cells[i][j].clear();
                        }
                    }
                }
            }
        });
    }
    
    private boolean isWon() {
        for (int i = 0; i < 3; ++i) {
            if (cells[i][0].getToken() == whoseTurn && cells[i][1].getToken() == whoseTurn && cells[i][2].getToken() == whoseTurn) {
                return true;
            }
        }
        for (int j = 0; j < 3; ++j) {
            if (cells[0][j].getToken() == whoseTurn && cells[1][j].getToken() == whoseTurn && cells[2][j].getToken() == whoseTurn) {
                return true;
            }
        }
        if (cells[0][0].getToken() == whoseTurn && cells[1][1].getToken() == whoseTurn && cells[2][2].getToken() == whoseTurn) {
            return true;
        }
        if (cells[2][0].getToken() == whoseTurn && cells[1][1].getToken() == whoseTurn && cells[0][2].getToken() == whoseTurn) {
            return true;
        }
        return false;
    }
    
    private boolean isFull() {
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j) {
                if (cells[i][j].getToken() == ' ') {
                    return false;
                }
            }
        }
        return true;
    }
    
    private class Cell extends JPanel {
        
        private char token = ' ';
        private boolean isTokenSet;
        
        private Cell() {
            setBackground(Color.BLACK);
            setBorder(BorderFactory.createLineBorder(Color.WHITE));
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    super.mouseClicked(e);
                    if (isStarted) {
                        setToken(whoseTurn);
                        if (isWon()) {
                            statusLabel.setText(whoseTurn + "胜利!游戏结束^_^");
                            isStarted = false;
                            startButton.setText("重新开始");
                            startButton.setEnabled(true);
                        } else {
                            if (isFull()) {
                                statusLabel.setText("平局!游戏结束^_^");
                                isStarted = false;
                                startButton.setText("重新开始");
                                startButton.setEnabled(true);
                            } else {
                                whoseTurn = whoseTurn == 'O' ? 'X' : 'O';
                                statusLabel.setText("轮到" + whoseTurn + "行");
                            }
                        }
                    }
                }
            });
        }
        
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            if (isTokenSet) {
                g.setColor(Color.WHITE);
                if (token == 'O') {
                    g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
                } else {
                    int x = getWidth() - 10;
                    int y = getHeight() - 10;
                    g.drawLine(10, 10, x, y);
                    g.drawLine(x, 10, 10, y);
                }
            }
        }
        
        private void setToken(char token) {
            repaint();
            this.token = token;
            isTokenSet = true;
        }
        
        private char getToken() {
            return token;
        }
        
        private void clear() {
            isTokenSet = false;
            token = ' ';
            repaint();
        }
    }
    
    public static void main(String[] args) {
        new MainFrame("TicTacToe").setVisible(true);
    }
}

 

转载于:https://www.cnblogs.com/buyishi/p/10325510.html

你可能感兴趣的:(Java版TicTacToe)