diff --git a/src/game/AbstractGame.java b/src/game/AbstractGame.java index 983b29f..2fedc1b 100644 --- a/src/game/AbstractGame.java +++ b/src/game/AbstractGame.java @@ -3,9 +3,14 @@ import java.awt.Font; import java.awt.Point; +import java.io.DataInputStream; +import java.io.DataOutputStream; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.UnknownHostException; import java.util.ArrayList; import org.json.*; @@ -62,6 +67,11 @@ int difficulty; int fontSize; + //Network stuff + ServerSocket server; + Socket s; + boolean isHost; + /*public static void main(String[] args) { AbstractGame game = new AbstractGame(); game.getPlayer(game.currentPlayer).deactiveCards(); @@ -75,6 +85,7 @@ protected void init(){ loadOptions(); loadFont(); + establishConnection(); CARDS_PER_PLAYER = (NR_PLAYERS < 3) ? 5 : 4; hints = MAX_HINTS; thunders = 0; @@ -93,10 +104,9 @@ root = new JSONObject(tokener); difficulty = root.getInt("difficulty"); fontSize = root.getInt("fontSize"); + isHost = root.getBoolean("host"); if(difficulty < 0) difficulty = 0; if(fontSize <= 0) fontSize = 20; - System.out.println("FontSize: " + fontSize); - System.out.println("Difficulty: " + difficulty); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -109,6 +119,34 @@ } } + protected void establishConnection(){ + try { + if(isHost){ + server = new ServerSocket(8765); + printMessage("Your server is running on port 8765", 2); + s = server.accept(); + DataInputStream inStream = new DataInputStream(s.getInputStream()); + String str = inStream.readUTF(); + System.out.println("Message: " + str); + s.close(); + server.close(); + } else { + s = new Socket("localhost", 8765); + DataOutputStream outStream = new DataOutputStream(s.getOutputStream()); + outStream.writeUTF("Hello"); + outStream.flush(); + outStream.close(); + s.close(); + } + } catch(UnknownHostException uhe){ + printMessage("Unknown host '???'", 0); + } catch (IOException e) { + printMessage(e.getLocalizedMessage(), 0); + System.exit(0); //TODO status code + } + + } + public void resetMoveValues(){ selectedPlayer = -1; chosenColor = -1;