Java版TicTacToe
2024-08-22 22:35:16
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);
}
}
最新文章
- 如何安装并使用hibernate tools
- spring mvc 工作流程
- JAVA内存机制
- 认真学习shell的第一天-数学运算
- General: Know How to Use InetAddress
- hdu 1199 Color the Ball(离散化线段树)
- Java CopyOnWriteArrayList分析
- HTML在Select具体的使用说明
- SQL SERVER与C#的数据类型对应表
- php 实现购物车功能,以大苹果购物网为例,上图上代码。。。。
- mac os 安装 wget
- 《UNIX实用教程》读书笔记
- goland 文件头自动注释
- .NET Core整理之配置EFCore
- tofixed方法 四舍五入
- 如何删除新添加的项目Module
- Python-mysql 权限 pymysql 注入共计
- vue js校验金钱、数字
- 你可能不知道的git clean
- 网络对抗——web基础