请接着给出服务器端对应的具体代码
时间: 2024-02-25 19:55:40 浏览: 49
服务器端的代码
以下是服务器端对应的具体代码示例:
```java
import java.net.*;
import java.io.*;
public class TicTacToeServer {
private ServerSocket serverSocket = null;
private Socket player1 = null;
private Socket player2 = null;
private char[][] board = new char[3][3];
private int currentPlayer = 1;
private PrintWriter[] outs = new PrintWriter[2];
public TicTacToeServer(int port) {
try {
serverSocket = new ServerSocket(port);
System.out.println("Server started: " + serverSocket);
} catch (IOException e) {
System.out.println("Could not listen on port: " + port);
System.exit(-1);
}
}
public void acceptPlayers() {
try {
System.out.println("Waiting for players...");
player1 = serverSocket.accept();
System.out.println("Player 1 connected: " + player1);
outs[0] = new PrintWriter(player1.getOutputStream(), true);
outs[0].println("WELCOME 1");
player2 = serverSocket.accept();
System.out.println("Player 2 connected: " + player2);
outs[1] = new PrintWriter(player2.getOutputStream(), true);
outs[1].println("WELCOME 2");
} catch (IOException e) {
System.out.println("Accept failed: " + serverSocket);
System.exit(-1);
}
}
public void playGame() {
try {
BufferedReader[] ins = new BufferedReader[2];
ins[0] = new BufferedReader(new InputStreamReader(player1.getInputStream()));
ins[1] = new BufferedReader(new InputStreamReader(player2.getInputStream()));
String[] names = {"Player 1", "Player 2"};
String inputLine;
while (true) {
int row = -1;
int col = -1;
do {
outs[currentPlayer - 1].println("YOUR_MOVE");
inputLine = ins[currentPlayer - 1].readLine();
String[] move = inputLine.split(" ");
row = Integer.parseInt(move[0]);
col = Integer.parseInt(move[1]);
} while (!isValidMove(row, col));
board[row][col] = (currentPlayer == 1) ? 'X' : 'O';
outs[currentPlayer - 1].println("VALID_MOVE");
outs[currentPlayer - 1].println(getBoard());
if (isWinner(currentPlayer)) {
outs[currentPlayer - 1].println("VICTORY");
outs[(currentPlayer % 2)].println("DEFEAT");
break;
} else if (isBoardFull()) {
outs[0].println("TIE");
outs[1].println("TIE");
break;
}
currentPlayer = (currentPlayer % 2) + 1;
}
} catch (IOException e) {
System.out.println("Error during game: " + e.getMessage());
}
}
private boolean isValidMove(int row, int col) {
if (row < 0 || row >= 3 || col < 0 || col >= 3) {
return false;
}
if (board[row][col] != '\0') {
return false;
}
return true;
}
private boolean isWinner(int player) {
char symbol = (player == 1) ? 'X' : 'O';
for (int i = 0; i < 3; i++) {
if (board[i][0] == symbol && board[i][1] == symbol && board[i][2] == symbol) {
return true;
}
if (board[0][i] == symbol && board[1][i] == symbol && board[2][i] == symbol) {
return true;
}
}
if (board[0][0] == symbol && board[1][1] == symbol && board[2][2] == symbol) {
return true;
}
if (board[0][2] == symbol && board[1][1] == symbol && board[2][0] == symbol) {
return true;
}
return false;
}
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;
}
private String getBoard() {
String s = "";
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
s += board[i][j];
}
s += "\n";
}
return s;
}
public static void main(String[] args) {
TicTacToeServer server = new TicTacToeServer(5000);
server.acceptPlayers();
server.playGame();
}
}
```
上述代码实现了服务器端的功能,包括监听客户端连接、处理游戏逻辑、发送游戏状态更新等。其中,`acceptPlayers()` 方法用于等待两个客户端连接,并向客户端发送欢迎消息;`playGame()` 方法用于处理游戏逻辑,包括等待客户端下棋请求、检查请求的合法性、更新游戏状态和发送游戏状态更新消息等。在 `playGame()` 方法中,使用 `BufferedReader` 和 `PrintWriter` 实现了服务器端与客户端的通信。 `isValidMove()` 方法用于检查下棋请求是否合法,`isWinner()` 方法用于检查当前玩家是否获胜,`isBoardFull()` 方法用于检查棋盘是否填满,`getBoard()` 方法用于获取当前的棋局状态。在 `main()` 方法中,创建了一个 `TicTacToeServer` 对象并调用其方法以启动服务器。
阅读全文