⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 handleasession.java

📁 connectN 网络游戏。内含server and client端源程序
💻 JAVA
字号:
package server;

import utility.ClockThread;
import utility.ConnectNConstants;

import java.net.*;
import java.io.*;

import model.ServerModel;

public class HandleASession extends Thread implements ConnectNConstants {
	private Socket player1;
	private Socket player2;
	private ServerModel map;
	private int connectNumber;
	private int dropNumber;
	private DataInputStream fromPlayer1;
	private DataOutputStream toPlayer1;
	private DataInputStream fromPlayer2;
	private DataOutputStream toPlayer2;
	private boolean continueToPlay = true;
	private ClockThread timer;

	/** Construct a thread */
	public HandleASession(Socket player1, Socket player2, int row, int column,
			int n, int drop) {
		this.player1 = player1;
		this.player2 = player2;
		this.connectNumber = n;
		this.dropNumber = drop;
		map = new ServerModel(row, column, connectNumber);
		this.timer = new ClockThread(TIMEOUT,TIMEUNIT, this);
		this.timer.start();
		this.timer.stopTimer();
	}

	public void run() {
		try {
			// Create data input and output streams
			fromPlayer1 = new DataInputStream(player1.getInputStream());
			toPlayer1 = new DataOutputStream(player1.getOutputStream());
			fromPlayer2 = new DataInputStream(player2.getInputStream());
			toPlayer2 = new DataOutputStream(player2.getOutputStream());

			// Notify player 1 to start
			toPlayer1.writeInt(START);

			// Continuously serve the players and determine and report
			// the game status to the players
			while (continueToPlay) {
				int row, column;

				// Receive a move from player 1
				row = fromPlayer1.readInt();
				column = fromPlayer1.readInt();
				map.setPlayer(PLAYER1, row, column);

				// Check if Player 1 wins
				if (map.isWin(row, column, PLAYER1)) {
					toPlayer1.writeInt(PLAYER1_WON);
					toPlayer2.writeInt(PLAYER1_WON);
					sendMove(toPlayer2, row, column);
					break; // Break the loop
				} else if (map.isFull()) { // Check if all cells are filled
					toPlayer1.writeInt(DRAW);
					toPlayer2.writeInt(DRAW);
					sendMove(toPlayer2, row, column);
					break;
				} else if (dropNumber == 2) // Check if there is a second move
				{

					// Notify player 1 to continue
					toPlayer1.writeInt(SECONDTIME);
					// Notify player 2 to wait for second move
					toPlayer2.writeInt(WAITFORSECONDMOVE);
					// Send move to play 2
					sendMove(toPlayer2, row, column);
					// Receive second move from player 1
					row = fromPlayer1.readInt();
					column = fromPlayer1.readInt();
					map.setPlayer(PLAYER1, row, column);

					if (map.isWin(row, column, PLAYER1)) {
						toPlayer1.writeInt(PLAYER1_WON);
						toPlayer2.writeInt(PLAYER1_WON);
						sendMove(toPlayer2, row, column);
						break;
					} else if (map.isFull()) {
						toPlayer1.writeInt(DRAW);
						toPlayer2.writeInt(DRAW);
						sendMove(toPlayer2, row, column);
						break;
					} else {
						// Notify player 2 to take the turn
						toPlayer2.writeInt(CONTINUE);

						// Send player 1's selected row and column to player 2
						sendMove(toPlayer2, row, column);
					}
				} else {
					toPlayer2.writeInt(CONTINUE);
					// Send player 1's selected row and column to player 2
					sendMove(toPlayer2, row, column);
				}

				// Receive a move from Player 2
				row = fromPlayer2.readInt();
				column = fromPlayer2.readInt();
				map.setPlayer(PLAYER2, row, column);

				// Check if Player 2 wins
				if (map.isWin(row, column, PLAYER2)) {
					toPlayer1.writeInt(PLAYER2_WON);
					toPlayer2.writeInt(PLAYER2_WON);
					sendMove(toPlayer1, row, column);
					break;
				} else if (map.isFull()) {
					toPlayer1.writeInt(DRAW);
					toPlayer2.writeInt(DRAW);
					sendMove(toPlayer1, row, column);
					break;
				} else if (dropNumber == 2) {
					// Notify player 2 to continue
					toPlayer2.writeInt(SECONDTIME);

					toPlayer1.writeInt(WAITFORSECONDMOVE);
					sendMove(toPlayer1, row, column);
					// Receive second move from player 2
					row = fromPlayer2.readInt();
					column = fromPlayer2.readInt();
					map.setPlayer(PLAYER2, row, column);

					if (map.isWin(row, column, PLAYER2)) {
						toPlayer1.writeInt(PLAYER2_WON);
						toPlayer2.writeInt(PLAYER2_WON);
						sendMove(toPlayer1, row, column);
						break;
					} else if (map.isFull()) {
						toPlayer1.writeInt(DRAW);
						toPlayer2.writeInt(DRAW);
						sendMove(toPlayer1, row, column);
						break;
					} else {
						// Notify player 1 to continue
						toPlayer1.writeInt(CONTINUE);

						// Send player 1's selected row and column to player 2
						sendMove(toPlayer1, row, column);
					}
				} else {
					// Notify player 1 to take the turn
					toPlayer1.writeInt(CONTINUE);

					// Send player 2's selected row and column to player 1
					sendMove(toPlayer1, row, column);
				}
			}
		} catch (IOException ex) {
			System.err.println(ex);
		}
		finally
		{
			try {
				this.fromPlayer1.close();
				this.fromPlayer2.close();
				this.toPlayer1.close();
				this.toPlayer2.close();
				this.player1.close();
				this.player2.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
	}
	/* Send the move to other player */
	private void sendMove(DataOutputStream out, int row, int column)
			throws IOException {
		out.writeInt(row); // Send row index
		out.writeInt(column); // Send column index
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -