java代码实现:编写网络版的带有图形用户界面的Tic-Tac-Toe游戏,游戏双方可以在不同的计算机上玩。 a. 在每一步后,判断游戏是否结束,是否出现赢家,并且能告知玩家结果。 b. 玩家可以申请与指定IP和端口号的玩家进行联网对战。 c. 图形界面自定义,用鼠标落子,图示仅供参考。 d. 主类名为Gui_TTT
时间: 2024-02-13 16:05:35 浏览: 100
很好,我将为您提供Java代码实现。以下是Gui_TTT.java的代码:
```java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class Gui_TTT extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton[][] board = new JButton[3][3];
private JPanel panel;
private JLabel message;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private boolean myTurn;
private String playerMark;
private String opponentMark;
private boolean waitingResponse;
public static void main(String[] args) {
Gui_TTT gui = new Gui_TTT();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
}
public Gui_TTT() {
super("Tic Tac Toe - Network Game");
// Create GUI components
message = new JLabel("");
panel = new JPanel();
panel.setLayout(new GridLayout(3, 3));
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = new JButton("");
board[i][j].addActionListener(this);
panel.add(board[i][j]);
}
}
// Add components to frame
add(panel, BorderLayout.CENTER);
add(message, BorderLayout.SOUTH);
setSize(300, 300);
// Connect to opponent
String ip = JOptionPane.showInputDialog("Enter IP address:");
int port = Integer.parseInt(JOptionPane.showInputDialog("Enter port number:"));
try {
socket = new Socket(ip, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
out.println("CONNECT");
} catch (IOException e) {
message.setText("Unable to connect to opponent.");
}
// Wait for opponent's response
waitingResponse = true;
while (waitingResponse) {
try {
String response = in.readLine();
if (response.startsWith("START")) {
myTurn = true;
playerMark = "X";
opponentMark = "O";
message.setText("Your turn.");
waitingResponse = false;
} else if (response.startsWith("WAIT")) {
myTurn = false;
playerMark = "O";
opponentMark = "X";
message.setText("Opponent's turn.");
waitingResponse = false;
waitForOpponent();
}
} catch (IOException e) {
message.setText("Error during communication with opponent.");
}
}
}
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
if (!myTurn || button.getText().length() > 0) {
return;
}
button.setText(playerMark);
out.println("MOVE " + getButtonIndex(button));
if (checkWin(playerMark)) {
out.println("WIN " + playerMark);
message.setText("You win!");
endGame();
} else if (checkDraw()) {
out.println("DRAW");
message.setText("Game drawn.");
endGame();
} else {
myTurn = false;
message.setText("Opponent's turn.");
waitForOpponent();
}
}
private int getButtonIndex(JButton button) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == button) {
return i * 3 + j;
}
}
}
return -1;
}
private void waitForOpponent() {
new Thread() {
public void run() {
try {
String response = in.readLine();
if (response.startsWith("MOVE")) {
int index = Integer.parseInt(response.substring(5));
int row = index / 3;
int col = index % 3;
board[row][col].setText(opponentMark);
if (checkWin(opponentMark)) {
message.setText("Opponent wins!");
out.println("LOSE");
endGame();
} else if (checkDraw()) {
message.setText("Game drawn.");
out.println("DRAW");
endGame();
} else {
myTurn = true;
message.setText("Your turn.");
}
} else if (response.startsWith("WIN")) {
message.setText("Opponent wins!");
endGame();
} else if (response.startsWith("DRAW")) {
message.setText("Game drawn.");
endGame();
} else if (response.startsWith("LOSE")) {
message.setText("You win!");
endGame();
}
} catch (IOException e) {
message.setText("Error during communication with opponent.");
}
}
}.start();
}
private boolean checkWin(String mark) {
// Check rows
for (int i = 0; i < 3; i++) {
if (mark.equals(board[i][0].getText()) && mark.equals(board[i][1].getText())
&& mark.equals(board[i][2].getText())) {
return true;
}
}
// Check columns
for (int i = 0; i < 3; i++) {
if (mark.equals(board[0][i].getText()) && mark.equals(board[1][i].getText())
&& mark.equals(board[2][i].getText())) {
return true;
}
}
// Check diagonals
if (mark.equals(board[0][0].getText()) && mark.equals(board[1][1].getText())
&& mark.equals(board[2][2].getText())) {
return true;
}
if (mark.equals(board[0][2].getText()) && mark.equals(board[1][1].getText())
&& mark.equals(board[2][0].getText())) {
return true;
}
return false;
}
private boolean checkDraw() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j].getText().length() == 0) {
return false;
}
}
}
return true;
}
private void endGame() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j].setEnabled(false);
}
}
try {
socket.close();
} catch (IOException e) {
}
}
}
```
注释说明:
- `Gui_TTT`类继承了`JFrame`并实现了`ActionListener`接口,用于监听按钮点击事件。
- `board`数组存储游戏棋盘上的所有按钮。
- `panel`和`message`组件用于显示游戏界面和提示消息。
- `socket`、`in`、`out`存储与对手之间的网络连接和通信。
- `myTurn`、`playerMark`、`opponentMark`、`waitingResponse`用于管理游戏过程中的状态和流程。
在`main`方法中,创建`Gui_TTT`对象并设置其可见性。在`Gui_TTT`构造函数中:
- 创建GUI组件并添加到窗体中。
- 提示用户输入对手的IP地址和端口号,并尝试建立与对手的网络连接。
- 等待对手的响应,以确定游戏的开始顺序和玩家标记。
在`actionPerformed`方法中,根据玩家的落子情况,更新游戏状态并向对手发送通知。如果玩家或对手取得胜利或游戏结束,结束游戏并关闭网络连接。在`waitForOpponent`方法中,等待对手的响应,并根据响应更新游戏状态和流程。
以上就是一个简单的网络版井字棋游戏的Java代码实现,图形界面自定义,用鼠标落子。
阅读全文