To complete the code, we'll need to create a simple class to handle the game logic and update the client-side UI accordingly. Here's the completed code:
Game.java:
public class Game {
private char[] board;
private char currentPlayer;
public Game() {
board = new char[9];
for (int i = 0; i < 9; i++) {
board[i] = ' ';
}
currentPlayer = 'X';
}
public boolean makeMove(int index) {
if (board[index] == ' ') {
board[index] = currentPlayer;
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
return true;
}
return false;
}
public char[] getBoard() {
return board;
}
public boolean isGameOver() {
return hasWinner() || isBoardFull();
}
public char getWinner() {
if (hasWinner()) {
return currentPlayer == 'X' ? 'O' : 'X';
}
return ' ';
}
private boolean isBoardFull() {
for (char c : board) {
if (c == ' ')
return false;
}
return true;
}
private boolean hasWinner() {
int[][] lines = {
{0, 1, 2}, {3, 4, 5}, {6, 7, 8},
{0, 3, 6}, {1, 4, 7}, {2, 5, 8},
{0, 4, 8}, {2, 4, 6}
};
for (int[] line : lines) {
if (board[line[0]] != ' ' &&
board[line[0]] == board[line[1]] &&
board[line[0]] == board[line[2]]) {
return true;
}
}
return false;
}
}
Server.java:
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) {
System.out.println("Server started. Waiting for clients...");
try {
ServerSocket serverSocket = new ServerSocket(5000);
Game game = new Game();
while (game.getWinner() == ' ' && !game.isGameOver()) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected.");
DataInputStream dis = new DataInputStream(clientSocket.getInputStream());
DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream());
while (true) {
int index = dis.readInt();
game.makeMove(index);
dos.writeChars(String.valueOf(game.getBoard()));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client.java:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
import java.io.*;
import java.net.*;
public class Client extends Application {
private DataInputStream dis;
private DataOutputStream dos;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
GridPane gridPane = new GridPane();
primaryStage.setTitle("Tic Tac Toe");
try {
Socket socket = new Socket("localhost", 5000);
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Button button = createButton(i, j);
gridPane.add(button, i, j);
}
}
primaryStage.setScene(new Scene(gridPane, 300, 300));
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
private Button createButton(int x, int y) {
Button button = new Button();
button.setMinSize(100, 100);
button.setOnAction(event -> {
try {
int index = x * 3 + y;
dos.writeInt(index);
updateBoard();
} catch (IOException e) {
e.printStackTrace();
}
});
return button;
}
private void updateBoard() throws IOException {
String board = "";
for (int i = 0; i < 9; i++) {
board += dis.readChar();
}
for (int i = 0; i < 9; i++) {
Button button = (Button) (((GridPane) ((Stage) getScene().getWindow()).getScene().getRoot()).getChildren().get(i));
button.setText(String.valueOf(board.charAt(i)));
}
}
}
This code should create a simple network-based Tic Tac Toe game. You can run the Server.java application and start one or more instances of the Client.java application to connect and play the game. The game logic is quite basic, so additional features like displaying the winner or handling game reset can be added if needed.