diff --git a/Client/src/TicTacToe_Client.java b/Client/src/TicTacToe_Client.java index 2acb959..5c58f98 100644 --- a/Client/src/TicTacToe_Client.java +++ b/Client/src/TicTacToe_Client.java @@ -16,11 +16,18 @@ public class TicTacToe_Client { public TicTacToe_Client() { renderEngine = Engine.waitForEngine(); - client = new Client("server", 2589, clientName); + client = new Client("81.169.149.143", 2589, clientName); client.handshake(); isPlayerOne = client.isPlayerOne(); isAllowedToMove = isPlayerOne; - this.setWindowTitle(isPlayerOne); + //this.setWindowTitle(isPlayerOne); + Platform.runLater(new Runnable() { + @Override + public void run() { + renderEngine.updateTitle(clientName); + } + }); + client.sendToServer("ready"); } @@ -56,6 +63,7 @@ public class TicTacToe_Client { break; case "userInput": + client.printLog("Waiting for userInput", true, LogType.Log); while (!renderEngine.isMouseClicked()) { try { Thread.sleep(100); @@ -94,6 +102,7 @@ public class TicTacToe_Client { e.printStackTrace(); } client.resetBoard(); + isAllowedToMove = isPlayerOne; } private void onInvalidInput(){ diff --git a/Client/src/networking/Client.java b/Client/src/networking/Client.java index 3057e91..1d95352 100644 --- a/Client/src/networking/Client.java +++ b/Client/src/networking/Client.java @@ -50,6 +50,7 @@ public class Client { } } catch (IOException e) { e.printStackTrace(); + System.exit(0); } } diff --git a/Server/src/networking/TicTacToe_Server.java b/Server/src/networking/TicTacToe_Server.java index 5bd8f7b..f12798a 100644 --- a/Server/src/networking/TicTacToe_Server.java +++ b/Server/src/networking/TicTacToe_Server.java @@ -62,8 +62,9 @@ public class TicTacToe_Server { for (Socket client: clients.values()) { try { boolean gameEnded = ticTacToe_gameRules.gameEnded(); - outstreams.get(client).writeBoolean(gameEnded); if (gameEnded) { + outstreams.get(client).writeUTF("gameEnded"); + outstreams.get(client).flush(); //send coordinates String coordinates = ""; for (Point point : ticTacToe_gameRules.getWinCoordinates()) { @@ -185,6 +186,7 @@ public class TicTacToe_Server { if (ticTacToe_gameRules.gameEnded()){ sendGameState(); this.onGameEnd(); + break; } if (!isSingleServer()) { outstreams.get(clients.get(1 - clientIds.get(client))).writeUTF("opponentMove"); @@ -192,11 +194,15 @@ public class TicTacToe_Server { sendGameState(); } else { sendGameState(); - ticTacToe_gameRules.makeComputerMove(); serverLogger.printLog("Trigger computer move", LogType.Log); + ticTacToe_gameRules.makeComputerMove(); outstreams.get(client).writeUTF("opponentMove"); outstreams.get(client).flush(); sendGameState(); + if (ticTacToe_gameRules.gameEnded()){ + this.onGameEnd(); + break; + } } } else { @@ -228,25 +234,6 @@ public class TicTacToe_Server { } break; - case "gameEnded": - try { - boolean gameEnded = ticTacToe_gameRules.gameEnded(); - outstreams.get(client).writeBoolean(gameEnded); - if (gameEnded) { - //send coordinates - String coordinates = ""; - for (Point point : ticTacToe_gameRules.getWinCoordinates()) { - coordinates += point.x + ";" + point.y + ";"; - } - //send winning fields - outstreams.get(client).writeUTF(coordinates); - serverLogger.printLog("Winning coordinates got sent", clientNames.get(client), LogType.Log); - } - } catch (IOException e) { - e.printStackTrace(); - } - break; - case "exit": try { outstreams.get(client).close(); @@ -260,7 +247,7 @@ public class TicTacToe_Server { case "reset": ticTacToe_gameRules.resetGameState(); - this.sendGameState(); + sendGameState(client); break; } } diff --git a/Server/src/res/TicTacToe_GameRules.java b/Server/src/res/TicTacToe_GameRules.java index 310dc14..f7c3df2 100644 --- a/Server/src/res/TicTacToe_GameRules.java +++ b/Server/src/res/TicTacToe_GameRules.java @@ -12,7 +12,7 @@ public class TicTacToe_GameRules { Point startWin, endWin; public TicTacToe_GameRules(){ - gameState = "o--x-o--x"; + gameState = "---------"; } public void resetGameState() { @@ -141,7 +141,7 @@ public class TicTacToe_GameRules { } public boolean gameEnded(){ - return horizontalWin() || verticalWin() || diagonalWin(); + return horizontalWin() || verticalWin() || diagonalWin() || this.gameState.matches("([xo]){9}"); } public Point[] getWinCoordinates(){