Combined SingleServer and MultiServer into one class
This commit is contained in:
@@ -24,7 +24,7 @@ public class TicTacToe_Client {
|
||||
if (isSingleServer){
|
||||
this.setWindowTitle(client.isPlayerOne());
|
||||
client.sendToServer("ready");
|
||||
while (client.isConnected()) {
|
||||
while (client.isConnected() && !renderEngine.isWindowClosed()) {
|
||||
String message = client.getResponse();
|
||||
//Check if message is gamestate
|
||||
if (message.charAt(0) == 'x' || message.charAt(0) == '-' || message.charAt(0) == 'o') {
|
||||
@@ -36,6 +36,11 @@ public class TicTacToe_Client {
|
||||
this.gameFlow(message);
|
||||
}
|
||||
}
|
||||
try {
|
||||
client.exitProcess();
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
this.setWindowTitle(client.isPlayerOne());
|
||||
}
|
||||
|
||||
@@ -13,21 +13,21 @@ public class Client {
|
||||
private Socket serverSocket;
|
||||
private DataOutputStream out;
|
||||
private DataInputStream in;
|
||||
private static Scanner scanner;
|
||||
private ClientLogger clientLogger;
|
||||
private static String name;
|
||||
private String serverName;
|
||||
private boolean success;
|
||||
private boolean authorizedToMove;
|
||||
|
||||
public Client(String ip, int port, String name) {
|
||||
try {
|
||||
scanner = new Scanner(System.in);
|
||||
serverSocket = new Socket(ip, port);
|
||||
serverName = serverSocket.getRemoteSocketAddress().toString();
|
||||
out = new DataOutputStream(serverSocket.getOutputStream());
|
||||
in = new DataInputStream(serverSocket.getInputStream());
|
||||
clientLogger = new ClientLogger();
|
||||
success = true;
|
||||
authorizedToMove = false;
|
||||
this.name = name;
|
||||
clientLogger.printLog(String.format("Client with the name %s successfully initialized", name), success, LogType.Log);
|
||||
} catch (IOException e) {
|
||||
@@ -64,16 +64,6 @@ public class Client {
|
||||
}
|
||||
}
|
||||
|
||||
public void sendConfirmation(){
|
||||
try {
|
||||
out.writeInt(200);
|
||||
out.flush();
|
||||
clientLogger.printLog("Sent verification code", serverName, true, LogType.Output);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public String getResponse() {
|
||||
try {
|
||||
String message = in.readUTF();
|
||||
@@ -108,12 +98,6 @@ public class Client {
|
||||
return serverType;
|
||||
}
|
||||
|
||||
public String getGameState() {
|
||||
this.sendToServer("gameState");
|
||||
String gameState = this.getResponse();
|
||||
return gameState;
|
||||
}
|
||||
|
||||
public boolean getGameEnded() {
|
||||
this.sendToServer("gameEnded");
|
||||
boolean gameEnded = false;
|
||||
@@ -126,10 +110,6 @@ public class Client {
|
||||
return gameEnded;
|
||||
}
|
||||
|
||||
public void waitForInput() {
|
||||
|
||||
}
|
||||
|
||||
public void exitProcess(){
|
||||
try {
|
||||
out.writeUTF("exit");
|
||||
@@ -152,12 +132,16 @@ public class Client {
|
||||
}
|
||||
}
|
||||
|
||||
public void printLog(String message, boolean success){
|
||||
clientLogger.printLog(message, success, LogType.Log);
|
||||
public boolean isAuthorizedToMove(){
|
||||
return authorizedToMove;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
public void setAuthorizedToMove(boolean isAuthorizedToMove){
|
||||
authorizedToMove = isAuthorizedToMove;
|
||||
}
|
||||
|
||||
public void printLog(String message, boolean success){
|
||||
clientLogger.printLog(message, success, LogType.Log);
|
||||
}
|
||||
|
||||
public boolean isConnected(){
|
||||
|
||||
@@ -21,6 +21,7 @@ public class Engine extends Application {
|
||||
private static final CountDownLatch latch = new CountDownLatch(1);
|
||||
private static Engine engine = null;
|
||||
private boolean mouseClicked = false;
|
||||
private boolean windowClosed = false;
|
||||
private Point coordinates = new Point();
|
||||
private Stage primaryStage;
|
||||
|
||||
@@ -62,7 +63,7 @@ public class Engine extends Application {
|
||||
latch.countDown();
|
||||
}
|
||||
|
||||
public void updateTitle(String title){
|
||||
public void updateTitle(String title) {
|
||||
System.out.println("title: " + title);
|
||||
primaryStage.setTitle(title);
|
||||
}
|
||||
@@ -90,7 +91,7 @@ public class Engine extends Application {
|
||||
System.err.println("Wrong length of gameState string");
|
||||
return;
|
||||
}
|
||||
if (gameState.equals("---------")){
|
||||
if (gameState.equals("---------")) {
|
||||
grid.getChildren().clear();
|
||||
}
|
||||
for (int i = 0; i < gameState.length(); i++) {
|
||||
@@ -112,6 +113,8 @@ public class Engine extends Application {
|
||||
coordinates.setLocation(event.getX(), event.getY());
|
||||
}
|
||||
|
||||
public boolean isWindowClosed() {return windowClosed;}
|
||||
|
||||
public boolean isMouseClicked() {
|
||||
return mouseClicked;
|
||||
}
|
||||
@@ -134,6 +137,10 @@ public class Engine extends Application {
|
||||
primaryStage.setScene(this.setScene());
|
||||
primaryStage.sizeToScene();
|
||||
primaryStage.show();
|
||||
|
||||
primaryStage.setOnCloseRequest(event -> {
|
||||
windowClosed = true;
|
||||
});
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Reference in New Issue
Block a user