Reworked protocol between server and client
- Changed successCodes from int to boolean - Added exception handling for better debugging in TicTacToe_Client - fixed bug that disallowed singleplayer mode
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import javafx.application.Platform;
|
||||
import logging.LogType;
|
||||
import networking.Client;
|
||||
import render.Engine;
|
||||
|
||||
@@ -11,26 +12,29 @@ public class TicTacToe_Client {
|
||||
private Client client;
|
||||
private static String clientName;
|
||||
private boolean isPlayerOne;
|
||||
private boolean isAllowedToMove;
|
||||
|
||||
public TicTacToe_Client() {
|
||||
renderEngine = Engine.waitForEngine();
|
||||
client = new Client("server", 2589, clientName);
|
||||
client.handshake();
|
||||
isPlayerOne = client.isPlayerOne();
|
||||
isAllowedToMove = isPlayerOne;
|
||||
this.setWindowTitle(isPlayerOne);
|
||||
client.sendToServer("ready");
|
||||
}
|
||||
|
||||
private void ticTacToe_gameloop() {
|
||||
this.drawBoard(client.getGameState());
|
||||
if (isPlayerOne){
|
||||
this.userInput();
|
||||
}
|
||||
while (client.isConnected() && !renderEngine.isWindowClosed()) {
|
||||
client.printLog("Waiting for data", true, LogType.Log);
|
||||
String message = client.getResponse();
|
||||
//Check if message is gamestate
|
||||
if (message.charAt(0) == 'x' || message.charAt(0) == '-' || message.charAt(0) == 'o') {
|
||||
if (message.matches("([xo-]){9}")) {
|
||||
client.printLog("Board updated", true, LogType.Log);
|
||||
this.drawBoard(message);
|
||||
if (isAllowedToMove){
|
||||
this.gameFlow("userInput");
|
||||
}
|
||||
}
|
||||
//Handle everything else
|
||||
else {
|
||||
@@ -46,6 +50,11 @@ public class TicTacToe_Client {
|
||||
|
||||
private void gameFlow(String input) {
|
||||
switch (input) {
|
||||
case "opponentMove":
|
||||
//if opponent makes move allow a move
|
||||
isAllowedToMove = true;
|
||||
break;
|
||||
|
||||
case "userInput":
|
||||
while (!renderEngine.isMouseClicked()) {
|
||||
try {
|
||||
@@ -55,21 +64,21 @@ public class TicTacToe_Client {
|
||||
}
|
||||
}
|
||||
renderEngine.setMouseClicked(false);
|
||||
isAllowedToMove = false;
|
||||
this.userInput();
|
||||
break;
|
||||
|
||||
case "invalidInput":
|
||||
isAllowedToMove = true;
|
||||
this.onInvalidInput();
|
||||
break;
|
||||
|
||||
case ""
|
||||
case "gameEnded":
|
||||
this.onGameEnd();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void requestOpponentMove(){
|
||||
client.sendToServer("opponentMove");
|
||||
}
|
||||
|
||||
private void onGameEnd(){
|
||||
LinkedList<Integer> winCoordinates = new LinkedList<>();
|
||||
//Get winning fields
|
||||
@@ -91,27 +100,12 @@ public class TicTacToe_Client {
|
||||
int column = (int) renderEngine.getCoordinates().getX() / 300;
|
||||
int row = (int) renderEngine.getCoordinates().getY() / 300;
|
||||
System.err.printf("You are not allowed to place at %d|%d%n", column, row);
|
||||
this.userInput();
|
||||
this.gameFlow("userInput");
|
||||
}
|
||||
|
||||
private void userInput() {
|
||||
client.sendToServer("clientMove");
|
||||
client.sendToServer(String.format("%f|%f", renderEngine.getCoordinates().getX(), renderEngine.getCoordinates().getY()));
|
||||
//Get gameState
|
||||
String gameState = client.getResponse();
|
||||
if (gameState.length() == 9) {
|
||||
this.drawBoard(gameState);
|
||||
//Send command
|
||||
if (!client.getGameEnded()) {
|
||||
this.requestOpponentMove();
|
||||
} else {
|
||||
this.onGameEnd();
|
||||
}
|
||||
} else {
|
||||
int column = (int) renderEngine.getCoordinates().getX() / 300;
|
||||
int row = (int) renderEngine.getCoordinates().getY() / 300;
|
||||
System.err.printf("You are not allowed to place at %d|%d%n", column, row);
|
||||
}
|
||||
}
|
||||
|
||||
private void drawBoard(String gameState) {
|
||||
@@ -148,12 +142,12 @@ public class TicTacToe_Client {
|
||||
javafx.application.Application.launch(Engine.class);
|
||||
}
|
||||
}.start();
|
||||
TicTacToe_Client test = new TicTacToe_Client();
|
||||
try {
|
||||
clientName = args[0];
|
||||
} catch (Exception e) {
|
||||
clientName = "testing";
|
||||
}
|
||||
TicTacToe_Client test = new TicTacToe_Client();
|
||||
test.ticTacToe_gameloop();
|
||||
}
|
||||
|
||||
|
||||
@@ -54,8 +54,8 @@ public class ClientLogger {
|
||||
this.printLog(message, "server", success, logType);
|
||||
}
|
||||
|
||||
public void printConfirmation(int code){
|
||||
if (code == 200){
|
||||
public void printConfirmation(boolean success){
|
||||
if (success){
|
||||
printLog(" Status: sent!", true, LogType.Log);
|
||||
} else {
|
||||
printLog(" Status: not sent!", false, LogType.Log);
|
||||
|
||||
@@ -7,7 +7,7 @@ import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.util.Scanner;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Client {
|
||||
private Socket serverSocket;
|
||||
@@ -39,7 +39,7 @@ public class Client {
|
||||
try {
|
||||
out.writeInt(165313125);
|
||||
out.flush();
|
||||
success = in.readInt() == 200;
|
||||
success = in.readBoolean();
|
||||
if (success) {
|
||||
out.writeUTF(name);
|
||||
out.flush();
|
||||
@@ -54,11 +54,37 @@ public class Client {
|
||||
}
|
||||
|
||||
public void sendToServer(String message) {
|
||||
int availableBits = 0;
|
||||
try {
|
||||
out.writeUTF(message);
|
||||
out.flush();
|
||||
success = in.readInt() == 200;
|
||||
boolean success = in.readBoolean();
|
||||
clientLogger.printLog(String.format("Sent the message: %s", message), serverName, success, LogType.Output);
|
||||
if(in.available() > 0){
|
||||
availableBits = in.available();
|
||||
throw new Exception("More than just a boolean sent");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
this.exitProcess();
|
||||
this.printInputStream();
|
||||
e.printStackTrace();
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
private void printInputStream(){
|
||||
try {
|
||||
byte[] inputBytes = in.readAllBytes();
|
||||
for (byte b: inputBytes) {
|
||||
if (b == 1 || b == 0){
|
||||
System.out.println(b == 1);
|
||||
}
|
||||
System.out.print(Character.toString(b));
|
||||
}
|
||||
System.out.println(Arrays.toString(inputBytes));
|
||||
System.out.print("\n");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -124,7 +150,7 @@ public class Client {
|
||||
try {
|
||||
out.writeUTF("exit");
|
||||
out.flush();
|
||||
success = in.readInt()==200;
|
||||
success = in.readBoolean();
|
||||
clientLogger.printLog("Closing connection to server", serverName, success, LogType.Log);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@@ -135,7 +161,7 @@ public class Client {
|
||||
try {
|
||||
out.writeUTF("reset");
|
||||
out.flush();
|
||||
success = in.readInt()==200;
|
||||
success = in.readBoolean();
|
||||
clientLogger.printLog("Resetting board", serverName, success, LogType.Log);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@@ -150,8 +176,8 @@ public class Client {
|
||||
authorizedToMove = isAuthorizedToMove;
|
||||
}
|
||||
|
||||
public void printLog(String message, boolean success){
|
||||
clientLogger.printLog(message, success, LogType.Log);
|
||||
public void printLog(String message, boolean success, LogType logType){
|
||||
clientLogger.printLog(message, success, logType);
|
||||
}
|
||||
|
||||
public boolean isConnected(){
|
||||
|
||||
@@ -64,7 +64,6 @@ public class Engine extends Application {
|
||||
}
|
||||
|
||||
public void updateTitle(String title) {
|
||||
System.out.println("title: " + title);
|
||||
primaryStage.setTitle(title);
|
||||
}
|
||||
|
||||
@@ -87,10 +86,6 @@ public class Engine extends Application {
|
||||
}
|
||||
|
||||
public void drawBoard(String gameState) {
|
||||
if (gameState.length() != 9) {
|
||||
System.err.println("Wrong length of gameState string");
|
||||
return;
|
||||
}
|
||||
if (gameState.equals("---------")) {
|
||||
grid.getChildren().clear();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user