Basics of client server communication and drawing of gamestates finished
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
Manifest-Version: 1.0
|
||||
Main-Class: networking.Client
|
||||
Main-Class: game.TicTacToe_Client
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package games.TicTacToe;
|
||||
package game;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.event.EventHandler;
|
||||
@@ -26,8 +26,6 @@ public class TicTacToe_Client extends Application {
|
||||
grid.setGridLinesVisible(true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void drawCross(int column, int row){
|
||||
Text cross = new Text("X");
|
||||
cross.setFont(Font.font("Tahoma", FontWeight.NORMAL, 200));
|
||||
@@ -40,7 +38,6 @@ public class TicTacToe_Client extends Application {
|
||||
grid.add(circle, column, row);
|
||||
}
|
||||
|
||||
|
||||
private void drawEmptyField(int column, int row) {
|
||||
Text emptyField = new Text(" ");
|
||||
emptyField.setFont(Font.font("Tahoma", FontWeight.NORMAL, 200));
|
||||
@@ -53,8 +50,8 @@ public class TicTacToe_Client extends Application {
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < gameState.length(); i++){
|
||||
int column = i % 3;
|
||||
int row = i / 3;
|
||||
int column = i / 3;
|
||||
int row = i % 3;
|
||||
if (gameState.charAt(i) == 'x'){
|
||||
this.drawCross(column, row);
|
||||
} else if (gameState.charAt(i) == 'o'){
|
||||
@@ -73,8 +70,16 @@ public class TicTacToe_Client extends Application {
|
||||
scene.setOnMousePressed(new EventHandler<MouseEvent>() {
|
||||
@Override
|
||||
public void handle(MouseEvent event) {
|
||||
client.sendToServer(String.format("(%f|%f)", event.getX(), event.getY()));
|
||||
client.getGameState();
|
||||
client.sendToServer("update");
|
||||
client.sendToServer(String.format("%f|%f", event.getX(), event.getY()));
|
||||
String gameState = client.getResponse();
|
||||
if (gameState.length() == 9) {
|
||||
drawBoard(gameState);
|
||||
} else {
|
||||
int column = (int) event.getX() / 300;
|
||||
int row = (int) event.getY() / 300;
|
||||
System.err.printf("You are not allowed to place at %f|%f%n", column, row);
|
||||
}
|
||||
}
|
||||
});
|
||||
return scene;
|
||||
@@ -82,6 +87,9 @@ public class TicTacToe_Client extends Application {
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) {
|
||||
client = new Client("localhost", 2589, "TestClient");
|
||||
client.handshake();
|
||||
|
||||
primaryStage.setTitle("TicTacToe");
|
||||
primaryStage.setResizable(false);
|
||||
|
||||
@@ -91,10 +99,7 @@ public class TicTacToe_Client extends Application {
|
||||
|
||||
primaryStage.show();
|
||||
|
||||
this.drawBoard("---------");
|
||||
|
||||
client = new Client("localhost", 2589, "TestClient");
|
||||
client.handshake();
|
||||
this.drawBoard(client.getGameState());
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
Before Width: | Height: | Size: 4.1 KiB After Width: | Height: | Size: 4.1 KiB |
@@ -2,6 +2,8 @@ package networking;
|
||||
|
||||
import logging.ClientLogger;
|
||||
import logging.LogType;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
@@ -37,8 +39,9 @@ public class Client {
|
||||
out.writeInt(165313125);
|
||||
out.flush();
|
||||
success = in.readInt() == 200;
|
||||
if (success){
|
||||
if (success) {
|
||||
out.writeUTF(name);
|
||||
out.flush();
|
||||
serverName = in.readUTF();
|
||||
clientLogger.printLog("You successfully connected to me", serverName, success, LogType.Log);
|
||||
} else {
|
||||
@@ -55,18 +58,37 @@ public class Client {
|
||||
out.writeUTF(message);
|
||||
out.flush();
|
||||
success = in.readInt() == 200;
|
||||
clientLogger.printLog(String.format("Sent the message: %s", message), serverName, success, LogType.Log);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public String getGameState(){
|
||||
public String getResponse() {
|
||||
try {
|
||||
out.writeUTF("gamestate");
|
||||
return in.readUTF();
|
||||
String message = in.readUTF();
|
||||
clientLogger.printLog(String.format("Message recieved: %s", message), serverName, success, LogType.Log);
|
||||
return message;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public String getGameState() {
|
||||
this.sendToServer("gameState");
|
||||
String gameState = this.getResponse();
|
||||
return gameState;
|
||||
}
|
||||
|
||||
public void exitProcess(){
|
||||
try {
|
||||
out.writeUTF("exit()");
|
||||
out.flush();
|
||||
success = in.readInt()==200;
|
||||
clientLogger.printLog("Closing connection to server", serverName, success, LogType.Log);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,6 +96,8 @@ public class Client {
|
||||
return success;
|
||||
}
|
||||
|
||||
public String getName(){return name;}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user