以下是一个基本的实现,你可以根据需求进行修改和完善。
Tictactoe.java
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Tictactoe implements ActionListener {
private JFrame frame;
private JMenuBar menubar;
private JMenu menu;
private JMenuItem startItem, saveItem, exitItem;
private TicPanel ticPanel;
public Tictactoe() {
frame = new JFrame("Tictactoe");
menubar = new JMenuBar();
menu = new JMenu("操作");
startItem = new JMenuItem("开始游戏");
saveItem = new JMenuItem("保存结果");
exitItem = new JMenuItem("退出游戏");
startItem.addActionListener(this);
saveItem.addActionListener(this);
exitItem.addActionListener(this);
menu.add(startItem);
menu.add(saveItem);
menu.add(exitItem);
menubar.add(menu);
frame.setJMenuBar(menubar);
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startItem) {
if (ticPanel != null) {
frame.remove(ticPanel);
}
ticPanel = new TicPanel();
frame.add(ticPanel);
frame.validate();
} else if (e.getSource() == saveItem) {
if (ticPanel != null) {
String result = ticPanel.getResult();
try {
File file = new File("myText.txt");
FileWriter writer = new FileWriter(file, true);
writer.write(result);
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
} else if (e.getSource() == exitItem) {
System.exit(0);
}
}
public static void main(String[] args) {
new Tictactoe();
}
}
```
TicPanel.java
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class TicPanel extends JPanel implements ActionListener {
private int gridSize = 3;
private int cellSize = 100;
private int margin = 10;
private int[][] board;
private boolean playerTurn;
private boolean gameOver;
private JButton startButton;
private JButton newGameButton;
private JComboBox<String> sizeBox;
private JLabel resultLabel;
public TicPanel() {
setLayout(null);
setBackground(Color.LIGHT_GRAY);
sizeBox = new JComboBox<>();
for (int i = 3; i <= 10; i++) {
sizeBox.addItem(i + "x" + i);
}
sizeBox.setSelectedItem("3x3");
sizeBox.setBounds(margin, margin, cellSize, cellSize / 2);
add(sizeBox);
startButton = new JButton("开始");
startButton.addActionListener(this);
startButton.setBounds(cellSize + margin * 2, margin, cellSize, cellSize / 2);
add(startButton);
newGameButton = new JButton("再来一局");
newGameButton.addActionListener(this);
newGameButton.setBounds(cellSize * 2 + margin * 3, margin, cellSize, cellSize / 2);
add(newGameButton);
newGameButton.setEnabled(false);
resultLabel = new JLabel();
resultLabel.setFont(new Font("宋体", Font.BOLD, 20));
resultLabel.setHorizontalAlignment(SwingConstants.CENTER);
resultLabel.setBounds(0, cellSize * gridSize + margin * 2, getWidth(), cellSize / 2);
add(resultLabel);
board = new int[gridSize][gridSize];
playerTurn = true;
gameOver = false;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startButton) {
gridSize = sizeBox.getSelectedIndex() + 3;
cellSize = 500 / gridSize;
margin = 50 / gridSize;
removeAll();
setLayout(null);
setBackground(Color.LIGHT_GRAY);
sizeBox.setBounds(margin, margin, cellSize, cellSize / 2);
add(sizeBox);
startButton.setBounds(cellSize + margin * 2, margin, cellSize, cellSize / 2);
add(startButton);
newGameButton.setBounds(cellSize * 2 + margin * 3, margin, cellSize, cellSize / 2);
add(newGameButton);
newGameButton.setEnabled(false);
resultLabel.setBounds(0, cellSize * gridSize + margin * 2, getWidth(), cellSize / 2);
add(resultLabel);
board = new int[gridSize][gridSize];
playerTurn = true;
gameOver = false;
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) {
JButton button = new JButton();
button.setFont(new Font("宋体", Font.PLAIN, cellSize / 2));
button.setBounds(j * cellSize + margin, i * cellSize + margin * 2, cellSize, cellSize);
button.addActionListener(this);
add(button);
}
}
validate();
} else if (e.getSource() == newGameButton) {
for (Component component : getComponents()) {
if (component instanceof JButton) {
((JButton) component).setText("");
}
}
board = new int[gridSize][gridSize];
playerTurn = true;
gameOver = false;
resultLabel.setText("");
newGameButton.setEnabled(false);
} else if (e.getSource() instanceof JButton) {
JButton button = (JButton) e.getSource();
int row = (button.getY() - margin * 2) / cellSize;
int col = (button.getX() - margin) / cellSize;
if (board[row][col] != 0 || gameOver) {
return;
}
if (playerTurn) {
button.setText("X");
board[row][col] = 1;
} else {
button.setText("O");
board[row][col] = 2;
}
playerTurn = !playerTurn;
if (checkWin()) {
gameOver = true;
resultLabel.setText(playerTurn ? "O赢了!" : "X赢了!");
newGameButton.setEnabled(true);
} else if (checkDraw()) {
gameOver = true;
resultLabel.setText("平局!");
newGameButton.setEnabled(true);
} else if (!playerTurn) {
computerTurn();
}
}
}
private boolean checkWin() {
int player = playerTurn ? 2 : 1;
for (int i = 0; i < gridSize; i++) {
if (board[i][0] == player && board[i][1] == player && board[i][2] == player) {
return true;
}
if (board[0][i] == player && board[1][i] == player && board[2][i] == player) {
return true;
}
}
if (board[0][0] == player && board[1][1] == player && board[2][2] == player) {
return true;
}
if (board[0][2] == player && board[1][1] == player && board[2][0] == player) {
return true;
}
return false;
}
private boolean checkDraw() {
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) {
if (board[i][j] == 0) {
return false;
}
}
}
return true;
}
private void computerTurn() {
List<int[]> emptyCells = new ArrayList<>();
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) {
if (board[i][j] == 0) {
emptyCells.add(new int[]{i, j});
}
}
}
if (emptyCells.size() > 0) {
Random random = new Random();
int[] cell = emptyCells.get(random.nextInt(emptyCells.size()));
int row = cell[0];
int col = cell[1];
JButton button = null;
for (Component component : getComponents()) {
if (component instanceof JButton) {
JButton b = (JButton) component;
if ((b.getX() - margin) / cellSize == col && (b.getY() - margin * 2) / cellSize == row) {
button = b;
break;
}
}
}
if (button != null) {
button.setText("O");
board[row][col] = 2;
playerTurn = true;
if (checkWin()) {
gameOver = true;
resultLabel.setText(playerTurn ? "O赢了!" : "X赢了!");
newGameButton.setEnabled(true);
} else if (checkDraw()) {
gameOver = true;
resultLabel.setText("平局!");
newGameButton.setEnabled(true);
}
}
}
}
public String getResult() {
return resultLabel.getText() + "\n";
}
}
```