Singleplayer functions flawless Multiplayer first round

This commit is contained in:
2021-03-23 10:34:34 +01:00
parent 76a0ff52fa
commit e96708b64e
4 changed files with 23 additions and 26 deletions

View File

@@ -16,11 +16,18 @@ public class TicTacToe_Client {
public TicTacToe_Client() { public TicTacToe_Client() {
renderEngine = Engine.waitForEngine(); renderEngine = Engine.waitForEngine();
client = new Client("server", 2589, clientName); client = new Client("81.169.149.143", 2589, clientName);
client.handshake(); client.handshake();
isPlayerOne = client.isPlayerOne(); isPlayerOne = client.isPlayerOne();
isAllowedToMove = isPlayerOne; isAllowedToMove = isPlayerOne;
this.setWindowTitle(isPlayerOne); //this.setWindowTitle(isPlayerOne);
Platform.runLater(new Runnable() {
@Override
public void run() {
renderEngine.updateTitle(clientName);
}
});
client.sendToServer("ready"); client.sendToServer("ready");
} }
@@ -56,6 +63,7 @@ public class TicTacToe_Client {
break; break;
case "userInput": case "userInput":
client.printLog("Waiting for userInput", true, LogType.Log);
while (!renderEngine.isMouseClicked()) { while (!renderEngine.isMouseClicked()) {
try { try {
Thread.sleep(100); Thread.sleep(100);
@@ -94,6 +102,7 @@ public class TicTacToe_Client {
e.printStackTrace(); e.printStackTrace();
} }
client.resetBoard(); client.resetBoard();
isAllowedToMove = isPlayerOne;
} }
private void onInvalidInput(){ private void onInvalidInput(){

View File

@@ -50,6 +50,7 @@ public class Client {
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
System.exit(0);
} }
} }

View File

@@ -62,8 +62,9 @@ public class TicTacToe_Server {
for (Socket client: clients.values()) { for (Socket client: clients.values()) {
try { try {
boolean gameEnded = ticTacToe_gameRules.gameEnded(); boolean gameEnded = ticTacToe_gameRules.gameEnded();
outstreams.get(client).writeBoolean(gameEnded);
if (gameEnded) { if (gameEnded) {
outstreams.get(client).writeUTF("gameEnded");
outstreams.get(client).flush();
//send coordinates //send coordinates
String coordinates = ""; String coordinates = "";
for (Point point : ticTacToe_gameRules.getWinCoordinates()) { for (Point point : ticTacToe_gameRules.getWinCoordinates()) {
@@ -185,6 +186,7 @@ public class TicTacToe_Server {
if (ticTacToe_gameRules.gameEnded()){ if (ticTacToe_gameRules.gameEnded()){
sendGameState(); sendGameState();
this.onGameEnd(); this.onGameEnd();
break;
} }
if (!isSingleServer()) { if (!isSingleServer()) {
outstreams.get(clients.get(1 - clientIds.get(client))).writeUTF("opponentMove"); outstreams.get(clients.get(1 - clientIds.get(client))).writeUTF("opponentMove");
@@ -192,11 +194,15 @@ public class TicTacToe_Server {
sendGameState(); sendGameState();
} else { } else {
sendGameState(); sendGameState();
ticTacToe_gameRules.makeComputerMove();
serverLogger.printLog("Trigger computer move", LogType.Log); serverLogger.printLog("Trigger computer move", LogType.Log);
ticTacToe_gameRules.makeComputerMove();
outstreams.get(client).writeUTF("opponentMove"); outstreams.get(client).writeUTF("opponentMove");
outstreams.get(client).flush(); outstreams.get(client).flush();
sendGameState(); sendGameState();
if (ticTacToe_gameRules.gameEnded()){
this.onGameEnd();
break;
}
} }
} else { } else {
@@ -228,25 +234,6 @@ public class TicTacToe_Server {
} }
break; 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": case "exit":
try { try {
outstreams.get(client).close(); outstreams.get(client).close();
@@ -260,7 +247,7 @@ public class TicTacToe_Server {
case "reset": case "reset":
ticTacToe_gameRules.resetGameState(); ticTacToe_gameRules.resetGameState();
this.sendGameState(); sendGameState(client);
break; break;
} }
} }

View File

@@ -12,7 +12,7 @@ public class TicTacToe_GameRules {
Point startWin, endWin; Point startWin, endWin;
public TicTacToe_GameRules(){ public TicTacToe_GameRules(){
gameState = "o--x-o--x"; gameState = "---------";
} }
public void resetGameState() { public void resetGameState() {
@@ -141,7 +141,7 @@ public class TicTacToe_GameRules {
} }
public boolean gameEnded(){ public boolean gameEnded(){
return horizontalWin() || verticalWin() || diagonalWin(); return horizontalWin() || verticalWin() || diagonalWin() || this.gameState.matches("([xo]){9}");
} }
public Point[] getWinCoordinates(){ public Point[] getWinCoordinates(){