java代码实现网络编程实现五子棋
时间: 2024-03-10 18:47:12 浏览: 58
java网络五子棋的源代码
以下是使用Java实现网络对战五子棋的示例代码:
服务端代码:
```java
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("服务器已启动,等待连接...");
while (true) {
Socket socket = serverSocket.accept();
Game game = new Game(socket);
game.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
客户端代码:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client {
private Socket socket;
private BufferedReader reader;
private PrintWriter writer;
public Client() {
try {
socket = new Socket("localhost", 8888);
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(socket.getOutputStream(), true);
System.out.println("连接服务器成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
public void start() throws IOException {
String inputLine;
while ((inputLine = reader.readLine()) != null) {
System.out.println(inputLine);
if (inputLine.equals("GAME OVER")) {
break;
}
if (inputLine.startsWith("YOUR TURN")) {
String[] parts = inputLine.split(" ");
int x = Integer.parseInt(parts[2]);
int y = Integer.parseInt(parts[3]);
int result = play(x, y);
writer.println("PLAY " + x + " " + y + " " + result);
}
}
reader.close();
writer.close();
socket.close();
}
private int play(int x, int y) {
// TODO: 实现下棋逻辑
return 0;
}
public static void main(String[] args) throws IOException {
Client client = new Client();
client.start();
}
}
```
游戏逻辑代码:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Game extends Thread {
private Socket socket1;
private Socket socket2;
private BufferedReader reader1;
private BufferedReader reader2;
private PrintWriter writer1;
private PrintWriter writer2;
private char[][] board;
private boolean player1Turn;
public Game(Socket socket) throws IOException {
this.socket1 = socket;
this.reader1 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
this.writer1 = new PrintWriter(socket.getOutputStream(), true);
this.board = new char[15][15];
this.player1Turn = true;
}
public void run() {
try {
writer1.println("WELCOME");
if (socket2 == null) {
socket2 = new Socket("localhost", 8888);
reader2 = new BufferedReader(new InputStreamReader(socket2.getInputStream()));
writer2 = new PrintWriter(socket2.getOutputStream(), true);
writer2.println("WELCOME");
writer2.println("WAITING FOR PLAYER 1");
}
writer1.println("PLAYER 1 READY");
writer2.println("PLAYER 2 READY");
while (true) {
String inputLine1 = reader1.readLine();
if (inputLine1.startsWith("PLAY")) {
String[] parts = inputLine1.split(" ");
int x = Integer.parseInt(parts[1]);
int y = Integer.parseInt(parts[2]);
int result = Integer.parseInt(parts[3]);
board[x][y] = player1Turn ? 'X' : 'O';
writer2.println("OPPONENT PLAYED " + x + " " + y);
writer2.println("YOUR TURN " + !player1Turn);
writer1.println("VALID MOVE " + result);
writer1.println("WAITING FOR OPPONENT");
player1Turn = !player1Turn;
}
String inputLine2 = reader2.readLine();
if (inputLine2.startsWith("PLAY")) {
String[] parts = inputLine2.split(" ");
int x = Integer.parseInt(parts[1]);
int y = Integer.parseInt(parts[2]);
int result = Integer.parseInt(parts[3]);
board[x][y] = player1Turn ? 'O' : 'X';
writer1.println("OPPONENT PLAYED " + x + " " + y);
writer1.println("YOUR TURN " + player1Turn);
writer2.println("VALID MOVE " + result);
writer2.println("WAITING FOR OPPONENT");
player1Turn = !player1Turn;
}
if (inputLine1.equals("QUIT") || inputLine2.equals("QUIT")) {
break;
}
}
writer1.println("GAME OVER");
writer2.println("GAME OVER");
reader1.close();
writer1.close();
socket1.close();
reader2.close();
writer2.close();
socket2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
请注意,以上示例代码只是一个简单的实现,还有很多需要完善的地方,例如处理异常、实现下棋逻辑、判断胜负等等。
阅读全文