利用Java编写井字游戏”Tictactoe.java“,该界面带有简单的菜单栏,且只有一个“操作”菜单,下含“开始游戏”,“保存结果”和“退出游戏”三个菜单条,游戏界面包含下拉框——九宫格选项 (3X3,4X4,...10X10),界面可随用户选择变换大小,“开始”按钮,“再来一局”按钮,要求: (1)程序启动后,程序界面为灰色,需要启动菜单项的开始游戏,界面才允许选择九宫格选项;点击“开始”/“再来一局”,游戏开始对战,其中双方各执“x”或者“O”不同符号表示对战; (3)利用软件技术基础中所学树的广度优先搜索或深度优先搜索算法实现人机自由对战,直到一方胜利或者失败,或者和棋; (4)自动跳出游戏对战结果; (5)可将每局对战结果追加写入当前目录下myTexttxt文件 (可点击菜单项中的“保存结果”实现),如果该文件不存在,则新建文件; (6)退出游戏功能:退出程序; (7)有异常处理、多线程的应用评定加分。
时间: 2024-04-04 09:34:24 浏览: 22
好的,这是一个关于Java编写井字游戏的问题。要编写一个带有菜单栏的界面,包含开始游戏、保存结果和退出游戏三个菜单条,以及下拉框选项和开始、再来一局按钮。需要使用广度优先搜索或深度优先搜索算法实现人机自由对战,并能自动跳出游戏对战结果。最后,可将每局对战结果追加写入当前目录下的myText.txt文件,并有异常处理和多线程的应用评定加分。感谢您的提问,有什么需要我帮忙的吗?
相关问题
用java编写井字游戏“Tictactoe.java”,该页面带有简单的菜单栏,且只有一个“操作”菜单,下含“开始游戏”,“保存游戏”,“退出游戏”三个菜单条,游戏界面包括下拉框——九宫格选项(33,44;...10*10),页面可随用户选择变换大小,有“开始”,“再来一局”按钮:要求程序启动后,程序界面为灰色,需要启动菜单栏的开始游戏才能允许九宫格选项,点击开始/再来一局按钮游戏开始对战,双方各执“X”,"O"不同符号表示对战
好的,下面是一个基本的实现示例:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Tictactoe extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel gamePanel;
private JComboBox<String> sizeComboBox;
private JButton startButton;
private JButton restartButton;
private JMenuItem startMenuItem;
private JMenuItem saveMenuItem;
private JMenuItem exitMenuItem;
private JFileChooser fileChooser;
private char[][] board;
private char currentPlayer;
private boolean gameEnded;
public Tictactoe() {
// 设置窗口标题和大小
super("Tictactoe");
setSize(400, 400);
// 创建菜单栏和菜单条
JMenuBar menuBar = new JMenuBar();
JMenu operationMenu = new JMenu("操作");
menuBar.add(operationMenu);
setJMenuBar(menuBar);
// 创建菜单项并添加到菜单条中
startMenuItem = new JMenuItem("开始游戏");
operationMenu.add(startMenuItem);
saveMenuItem = new JMenuItem("保存游戏");
operationMenu.add(saveMenuItem);
exitMenuItem = new JMenuItem("退出游戏");
operationMenu.add(exitMenuItem);
// 为菜单项添加事件处理程序
startMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
startGame();
}
});
saveMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveGame();
}
});
exitMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// 创建下拉框并添加到内容面板中
String[] sizes = {"3x3", "4x4", "5x5", "6x6", "7x7", "8x8", "9x9", "10x10"};
sizeComboBox = new JComboBox<>(sizes);
sizeComboBox.setEnabled(false);
JPanel controlPanel = new JPanel(new FlowLayout());
controlPanel.add(new JLabel("九宫格大小:"));
controlPanel.add(sizeComboBox);
startButton = new JButton("开始");
startButton.setEnabled(false);
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
startButton.setEnabled(false);
sizeComboBox.setEnabled(false);
gamePanel.removeAll();
gamePanel.setLayout(new GridLayout(getBoardSize(), getBoardSize()));
board = new char[getBoardSize()][getBoardSize()];
currentPlayer = 'X';
gameEnded = false;
for (int i = 0; i < getBoardSize(); i++) {
for (int j = 0; j < getBoardSize(); j++) {
JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!gameEnded) {
JButton clickedButton = (JButton) e.getSource();
int row = getButtonRow(clickedButton);
int col = getButtonCol(clickedButton);
if (board[row][col] == '\0') {
board[row][col] = currentPlayer;
clickedButton.setText("" + currentPlayer);
if (checkWin(row, col)) {
JOptionPane.showMessageDialog(Tictactoe.this, "恭喜" + currentPlayer + "获胜!");
gameEnded = true;
startButton.setEnabled(true);
} else if (checkTie()) {
JOptionPane.showMessageDialog(Tictactoe.this, "平局!");
gameEnded = true;
startButton.setEnabled(true);
} else {
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
}
}
}
});
gamePanel.add(button);
}
}
gamePanel.revalidate();
gamePanel.repaint();
}
});
controlPanel.add(startButton);
restartButton = new JButton("再来一局");
restartButton.setEnabled(false);
restartButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < getBoardSize(); i++) {
for (int j = 0; j < getBoardSize(); j++) {
board[i][j] = '\0';
}
}
for (Component c : gamePanel.getComponents()) {
JButton button = (JButton) c;
button.setText("");
}
currentPlayer = 'X';
gameEnded = false;
restartButton.setEnabled(false);
}
});
controlPanel.add(restartButton);
add(controlPanel, BorderLayout.NORTH);
// 创建游戏面板并添加到内容面板中
gamePanel = new JPanel();
gamePanel.setBackground(Color.GRAY);
add(gamePanel, BorderLayout.CENTER);
// 创建文件选择器
fileChooser = new JFileChooser();
}
private int getBoardSize() {
String size = (String) sizeComboBox.getSelectedItem();
return Integer.parseInt(size.substring(0, size.indexOf('x')));
}
private int getButtonRow(JButton button) {
return gamePanel.getComponentZOrder(button) / getBoardSize();
}
private int getButtonCol(JButton button) {
return gamePanel.getComponentZOrder(button) % getBoardSize();
}
private boolean checkWin(int row, int col) {
// 检查行
for (int i = 0; i < getBoardSize(); i++) {
if (board[row][i] != currentPlayer) {
break;
}
if (i == getBoardSize() - 1) {
return true;
}
}
// 检查列
for (int i = 0; i < getBoardSize(); i++) {
if (board[i][col] != currentPlayer) {
break;
}
if (i == getBoardSize() - 1) {
return true;
}
}
// 检查对角线
if (row == col) {
for (int i = 0; i < getBoardSize(); i++) {
if (board[i][i] != currentPlayer) {
break;
}
if (i == getBoardSize() - 1) {
return true;
}
}
}
if (row + col == getBoardSize() - 1) {
for (int i = 0; i < getBoardSize(); i++) {
if (board[i][getBoardSize() - i - 1] != currentPlayer) {
break;
}
if (i == getBoardSize() - 1) {
return true;
}
}
}
return false;
}
private boolean checkTie() {
for (int i = 0; i < getBoardSize(); i++) {
for (int j = 0; j < getBoardSize(); j++) {
if (board[i][j] == '\0') {
return false;
}
}
}
return true;
}
private void startGame() {
sizeComboBox.setEnabled(true);
}
private void saveGame() {
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
// TODO: 保存游戏状态到文件
}
}
public static void main(String[] args) {
Tictactoe game = new Tictactoe();
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.setVisible(true);
}
}
```
在这个示例中,我们创建了一个Tictactoe类作为游戏的主类,继承自JFrame类。在构造函数中,我们创建了菜单栏、下拉框、按钮和游戏面板,并且为菜单项和按钮添加了事件处理程序。我们使用二维数组来表示游戏面板,并且使用GridLayout布局管理器来动态改变游戏面板的大小。当用户点击一个按钮时,我们检查游戏是否结束,如果没有结束,我们更新游戏状态并检查是否有获胜者或平局。最后,我们使用文件选择器来实现保存游戏状态的功能。
请注意,这只是一个基本的实现示例,您可能需要根据您的具体需求进行修改和扩展。
利用Java编写井字游戏”Tictactoe.java“代码,该界面带有简单的菜单栏,且只有一个“操作”菜单,下含“开始游戏”,“保存结果”和“退出游戏”三个菜单条,游戏界面包含下拉框——九宫格选项 (3X3,4X4,...10X10),界面可随用户选择变换大小,“开始”按钮,“再来一局”按钮,要求: (1)程序启动后,程序界面为灰色,需要启动菜单项的开始游戏,界面才允许选择九宫格选项;点击“开始”/“再来一局”,游戏开始对战,其中双方各执“x”或者“O”不同符号表示对战; (3)利用软件技术基础中所学树的广度优先搜索或深度优先搜索算法实现人机自由对战,直到一方胜利或者失败,或者和棋; (4)自动跳出游戏对战结果; (5)可将每局对战结果追加写入当前目录下myTexttxt文件 (可点击菜单项中的“保存结果”实现),如果该文件不存在,则新建文件; (6)退出游戏功能:退出程序;
以下是一个基本的实现,你可以根据需求进行修改和完善。
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";
}
}
```
阅读全文