Java版TicTacToe

Posted 布伊什

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
    }
}

 

以上是关于Java版TicTacToe的主要内容,如果未能解决你的问题,请参考以下文章

Java 中的 2D Arrays TicTacToe 游戏.. 验证问题

官方 Android 示例应用程序 tictactoe 接收器代码 (html) 位于何处?

在C中挣扎着TicTacToe

[vscode]--HTML代码片段(基础版,reactvuejquery)

为什么这个指向指针二维数组的指针会像这样分配新的int

为啥我的 C 代码片段不起作用?简化版可以。为 unsigned long long 传递不带 VA_ARGS 的 args