java代码实现:创建一个 TicTacToe 类,用于 Tic-Tac-Toe 游戏。 (1)用图形用户界面来实现 ,界面形式为田字格形式界面,用X和O填入田字格。 (2)该类包含一个 3×3 的二维整型数组,其访问类型为 private (3)定义一个构造函数,用来将该数组初始化为 0 (4)允许 2 个玩家,当第 1 个玩家选定一个位置后,在该位置摆放一个值; 当第 2 个玩家选定一个位置后,在该位置摆放一个值; 两者不一样,以示区别。 (5)在每一步后,判断游戏是否结束,是否出现赢家 (6)可选题:如果有时间,可以实现人-机对弈,即游戏的另一方为电脑 程序。 要求: (1) 编写测试类 Test: 其中 args[0]包含输入的字符串,输入字符串格式示例为 : (1,1)(2,2)(3,3) 表示如下的 落子顺序: 输出结果为 true 或者 false,true 代表按照规定的落子顺序比赛结束,false 代表 比赛没有结束。可以通过命令行运行 java Test "(1,2)(3,3)(1,1)(2,2)" 测试程序。
时间: 2024-02-13 15:04:51 浏览: 27
以下是 TicTacToe 类的实现:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TicTacToe extends JFrame implements ActionListener {
private int[][] board;
private JButton[][] buttons;
public TicTacToe() {
board = new int[3][3];
buttons = new JButton[3][3];
// 初始化按钮数组
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
buttons[i][j] = new JButton();
buttons[i][j].setPreferredSize(new Dimension(100, 100));
buttons[i][j].addActionListener(this);
add(buttons[i][j]);
}
}
// 设置窗口属性
setLayout(new GridLayout(3, 3));
setSize(300, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
int x = -1, y = -1;
// 找出点击的按钮在数组中的位置
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (button == buttons[i][j]) {
x = i;
y = j;
break;
}
}
}
// 如果该位置已经有棋子,不允许再次落子
if (board[x][y] != 0) {
return;
}
// 落子
board[x][y] = (getTurn() == 1) ? 1 : -1;
button.setText(getTurn() == 1 ? "X" : "O");
// 判断游戏是否结束
int winner = checkWinner();
if (winner != 0) {
String message = (winner == 1) ? "X wins!" : "O wins!";
JOptionPane.showMessageDialog(null, message);
System.exit(0);
} else if (isBoardFull()) {
JOptionPane.showMessageDialog(null, "It's a tie!");
System.exit(0);
}
}
// 判断当前是哪一方落子
private int getTurn() {
int countX = 0, countO = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == 1) {
countX++;
} else if (board[i][j] == -1) {
countO++;
}
}
}
return (countX == countO) ? 1 : -1;
}
// 判断是否有一方获胜
private int checkWinner() {
// 检查行
for (int i = 0; i < 3; i++) {
int sum = board[i][0] + board[i][1] + board[i][2];
if (sum == 3) {
return 1;
} else if (sum == -3) {
return -1;
}
}
// 检查列
for (int i = 0; i < 3; i++) {
int sum = board[0][i] + board[1][i] + board[2][i];
if (sum == 3) {
return 1;
} else if (sum == -3) {
return -1;
}
}
// 检查对角线
int sum = board[0][0] + board[1][1] + board[2][2];
if (sum == 3) {
return 1;
} else if (sum == -3) {
return -1;
}
sum = board[0][2] + board[1][1] + board[2][0];
if (sum == 3) {
return 1;
} else if (sum == -3) {
return -1;
}
return 0;
}
// 判断棋盘是否已满
private boolean isBoardFull() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == 0) {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
new TicTacToe();
}
}
```
以下是 Test 类的实现:
```java
public class Test {
public static void main(String[] args) {
TicTacToe game = new TicTacToe();
// 解析输入字符串
String[] moves = args[0].split("\\)\\(");
for (int i = 0; i < moves.length; i++) {
moves[i] = moves[i].replaceAll("\\(", "").replaceAll("\\)", "");
String[] pos = moves[i].split(",");
int x = Integer.parseInt(pos[0]) - 1;
int y = Integer.parseInt(pos[1]) - 1;
// 如果该位置已经有棋子,比赛结束
if (game.board[x][y] != 0) {
System.out.println("false");
System.exit(0);
}
// 落子
game.board[x][y] = (game.getTurn() == 1) ? 1 : -1;
// 判断游戏是否结束
int winner = game.checkWinner();
if (winner != 0) {
System.out.println("true");
System.exit(0);
} else if (game.isBoardFull()) {
System.out.println("true");
System.exit(0);
}
}
System.out.println("false");
}
}
```
使用命令行运行 `java Test "(1,2)(3,3)(1,1)(2,2)"` 测试程序即可。