Added support for easy deployment to offsite server
This commit is contained in:
@@ -23,21 +23,21 @@ public class TicTacToe_Client extends Application {
|
||||
Client client;
|
||||
Scene scene;
|
||||
|
||||
private void initializeGrid(){
|
||||
private void initializeGrid() {
|
||||
grid = new GridPane();
|
||||
grid.setMinSize(900,900);
|
||||
grid.setMinSize(900, 900);
|
||||
grid.setAlignment(Pos.CENTER);
|
||||
grid.setHgap(150);
|
||||
grid.setVgap(75);
|
||||
}
|
||||
|
||||
private void drawCross(int column, int row){
|
||||
private void drawCross(int column, int row) {
|
||||
Text cross = new Text("X");
|
||||
cross.setFont(Font.font("Tahoma", FontWeight.NORMAL, 200));
|
||||
grid.add(cross, column, row);
|
||||
}
|
||||
|
||||
private void drawCircle(int column, int row){
|
||||
private void drawCircle(int column, int row) {
|
||||
Text circle = new Text("O");
|
||||
circle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 200));
|
||||
grid.add(circle, column, row);
|
||||
@@ -49,17 +49,17 @@ public class TicTacToe_Client extends Application {
|
||||
grid.add(emptyField, column, row);
|
||||
}
|
||||
|
||||
private void drawBoard(String gameState){
|
||||
if (gameState.length() != 9){
|
||||
private void drawBoard(String gameState) {
|
||||
if (gameState.length() != 9) {
|
||||
System.err.println("Wrong length of gameState string");
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < gameState.length(); i++){
|
||||
for (int i = 0; i < gameState.length(); i++) {
|
||||
int column = i / 3;
|
||||
int row = i % 3;
|
||||
if (gameState.charAt(i) == 'x'){
|
||||
if (gameState.charAt(i) == 'x') {
|
||||
this.drawCross(column, row);
|
||||
} else if (gameState.charAt(i) == 'o'){
|
||||
} else if (gameState.charAt(i) == 'o') {
|
||||
this.drawCircle(column, row);
|
||||
} else {
|
||||
this.drawEmptyField(column, row);
|
||||
@@ -68,7 +68,7 @@ public class TicTacToe_Client extends Application {
|
||||
}
|
||||
}
|
||||
|
||||
private Scene setScene(){
|
||||
private Scene setScene() {
|
||||
scene = new Scene(grid, 900, 900);
|
||||
scene.getStylesheets().add
|
||||
(TicTacToe_Client.class.getResource("TicTacToe_Client.css").toExternalForm());
|
||||
@@ -81,19 +81,20 @@ public class TicTacToe_Client extends Application {
|
||||
return scene;
|
||||
}
|
||||
|
||||
private void onMouseClick(MouseEvent event){
|
||||
private void onMouseClick(MouseEvent event) {
|
||||
client.sendToServer("update");
|
||||
client.sendToServer(String.format("%f|%f", event.getX(), event.getY()));
|
||||
String gameState = client.getResponse();
|
||||
System.out.println(gameState);
|
||||
if (gameState.length() == 9) {
|
||||
drawBoard(gameState);
|
||||
if (!client.getGameEnded()){
|
||||
if (!client.getGameEnded()) {
|
||||
gameState = client.getResponse();
|
||||
drawBoard(gameState);
|
||||
} else {
|
||||
LinkedList<Integer> winCoordinates = new LinkedList<>();
|
||||
String response = client.getResponse();
|
||||
for (String s: Arrays.copyOfRange(response.split(";"),0, 4)) {
|
||||
for (String s : Arrays.copyOfRange(response.split(";"), 0, 4)) {
|
||||
winCoordinates.add(Integer.valueOf(s) * 300);
|
||||
}
|
||||
this.drawWinningLine(winCoordinates);
|
||||
@@ -106,24 +107,22 @@ public class TicTacToe_Client extends Application {
|
||||
}
|
||||
}
|
||||
|
||||
private void drawWinningLine(LinkedList<Integer>winCoordinates){
|
||||
private void drawWinningLine(LinkedList<Integer> winCoordinates) {
|
||||
Line winningLine = new Line(winCoordinates.get(0), winCoordinates.get(1), winCoordinates.get(2), winCoordinates.get(3));
|
||||
winningLine.setFill(Color.RED);
|
||||
grid.add(winningLine, winCoordinates.get(0)/300, winCoordinates.get(1)/300, 3, 3);
|
||||
grid.add(winningLine, winCoordinates.get(0) / 300, winCoordinates.get(1) / 300, 3, 3);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) {
|
||||
client = new Client("localhost", 2589, "TestClient");
|
||||
client = new Client("server", 2589, "Cato");
|
||||
client.handshake();
|
||||
|
||||
primaryStage.setTitle("TicTacToe");
|
||||
primaryStage.setResizable(false);
|
||||
|
||||
this.initializeGrid();
|
||||
|
||||
primaryStage.setScene(this.setScene());
|
||||
|
||||
primaryStage.show();
|
||||
|
||||
this.drawBoard(client.getGameState());
|
||||
@@ -132,4 +131,5 @@ public class TicTacToe_Client extends Application {
|
||||
public static void main(String[] args) {
|
||||
launch(args);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user