netchessthread.java

来自「Java五子棋 支持本地游戏」· Java 代码 · 共 48 行

JAVA
48
字号
import java.net.*;
import java.io.*;

/**
 * 
 * this class creates a thread, it listens on chess port and receives game information from the other player
 *
 */
class NetChessThread extends Thread
{
	Player netPlayer;
	ChessGame game;
	
	NetChessThread(ChessGame game)
	{
		this.game = game;
		this.netPlayer = game.notLocalPlayer;
	}
	
	public void run()
	{
		//loop until game is over
		while(game.status != GameStatus.Over)
		{
			//if it is not net player's turn, just ignores the operation
			if(this.netPlayer != game.activePlayer)
				continue;
			
			try{
				int x, y;
				
				//System.out.println("Begin listening on the other side!");
				//receives the x and y position of chess
		    	x = game.sock.getInputStream().read();
		    	y = game.sock.getInputStream().read();
				
				//System.out.println("Received data" + "x:" + x + " " + "y:" + y);
		    	game.playChess(x, y);
			}
			catch(IOException ex)
			{
	    		System.out.println("Connection Error!");
			}
		}
	}
}

⌨️ 快捷键说明

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