package GPServer; /** *
Title:
*Description:
*Copyright: Copyright (c) 2000
*Company:
* @author not attributable * @version 1.0 */ import java.io.*; import java.net.*; public class GameThread extends Thread { static GameServer server; private Socket s1,s2; private DataInputStream din1,din2; private DataOutputStream dout1,dout2; private boolean THIS_THREAD_ACTIVE; private String ID; private int turn; public GameThread(Socket s) { s1 = s; ID = s1.toString(); try{ din1 = new DataInputStream(s1.getInputStream()); dout1 = new DataOutputStream(s1.getOutputStream()); } catch(IOException e){ e.printStackTrace(); } THIS_THREAD_ACTIVE = true; turn = 0; start(); } public void run(){ String message=""; try{ while (THIS_THREAD_ACTIVE) { if(turn == 0){ System.out.println("turn = 0"); message = din1.readUTF(); } else if(turn ==1){ System.out.println("turn = 1"); message = din1.readUTF(); } else if(turn ==2){ System.out.println("turn = 2"); message = din2.readUTF(); } if(message.equalsIgnoreCase("turn")){ if(turn == 1){ turn =2; dout2.writeUTF("your turn"); } if(turn == 2){ turn=1; dout1.writeUTF("your turn"); } } if(message.regionMatches(0,"board:",0,6)){ dout1.writeUTF(message); dout2.writeUTF(message); } } } catch(EOFException f){ } //catch(InterruptedException ie){ //} catch(IOException e){ e.printStackTrace(); } finally{ server.removeOpenGame(ID); } } public void joinGame(Socket s){ s2 = s; server.removeOpenGame(ID); try{ din2 = new DataInputStream(s2.getInputStream()); dout2 = new DataOutputStream(s2.getOutputStream()); } catch(IOException e){ e.printStackTrace(); } startGame(); } private void startGame(){ turn = (int) java.lang.Math.rint(java.lang.Math.random()*10)%2 + 1; try{ //din1.notify(); dout1.writeUTF("Game started"); if(turn == 1){ dout1.writeUTF("your turn"); System.out.println(turn); } if(turn == 2){ dout2.writeUTF("your turn"); System.out.println(turn); } } catch(IOException i){ i.printStackTrace(); } } }