import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import java.io.*;
import java.net.Socket;
public class TicTacToeClient extends Application {
private static final int PORT = 5000;
private static final String HOST = "localhost";
private static char marker;
private static Socket socket;
private static ObjectOutputStream outputStream;
private static ObjectInputStream inputStream;
private static char[][] board = new char[3][3];
private static boolean myTurn = false;
private static boolean gameOver = false;
@Override
public void start(Stage primaryStage) throws Exception {
// Connect to server
socket = new Socket(HOST, PORT);
outputStream = new ObjectOutputStream(socket.getOutputStream());
inputStream = new ObjectInputStream(socket.getInputStream());
// Get marker assigned by server
marker = inputStream.readChar();
// Create UI
GridPane gridPane = new GridPane();
gridPane.setAlignment(Pos.CENTER);
gridPane.setHgap(10);
gridPane.setVgap(10);
Button[][] buttons = new Button[3][3];
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
buttons[i][j] = new Button("-");
buttons[i][j].setPrefSize(50, 50);
int row = i;
int col = j;
// make buttons clickable and send move to server and update board and change turn and change the button text to marker
buttons[i][j].setOnAction(actionEvent -> {
if(!myTurn || gameOver || board[row][col] != '-') {
return;
}else{
buttons[row][col].setText(" "+marker);
}
try {
// Send move to server
outputStream.writeInt(row);
outputStream.writeInt(col);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
// Update board state
board[row][col] = marker;
updateBoard(buttons);
// Check for game over
try {
char result = inputStream.readChar();
if(result == 'W') {
Platform.runLater(() -> {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Game Over");
alert.setHeaderText("Winner: " + ((marker == 'X') ? "Player 1" : "Player 2"));
alert.showAndWait();
});
gameOver = true;
} else if(result == 'T') {
Platform.runLater(() -> {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Game Over");
alert.setHeaderText("Tie");
alert.showAndWait();
});
gameOver = true;
}
} catch (IOException e) {
e.printStackTrace();
}
// Update turn
myTurn = !myTurn;
});
gridPane.add(buttons[i][j], j, i);
}
}
Scene scene = new Scene(gridPane, 200, 200);
primaryStage.setScene(scene);
primaryStage.show();
// Listen for updates from server
new Thread(() -> {
try {
while(true) {
// Update board state
board = (char[][]) inputStream.readObject();
updateBoard(buttons);
// Check for game over
char result = inputStream.readChar();
if(result == 'W') {
Platform.runLater(() -> {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Game Over");
alert.setHeaderText("Winner: " + ((marker == 'X') ? "Player 1" : "Player 2"));
alert.showAndWait();
});
gameOver = true;
break;
} else if(result == 'T') {
Platform.runLater(() -> {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Game Over");
alert.setHeaderText("Tie");
alert.showAndWait();
});
gameOver = true;
break;
}
// Update turn
myTurn = !myTurn;
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}).start();
}
private static void updateBoard(Button[][] buttons) {
// Update board UI
Platform.runLater(() -> {
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
buttons[i][j].setText(String.valueOf(board[i][j]));
}
}
});
}
public static void main(String[] args) {
launch(args);
}
}
the buttons doesnt respond