enter code hereI'm developing a multiplayer snake game in Java, and for this, I require three classes: Server, Client, and RemoteBoard. The RemoteBoard class represents the game state, and it contains the information that needs to be sent to the client. However, I am encountering difficulties when the client receives this information and needs to update its own board.
There is a LocalBoard that represents the local game. I use it to create the RemoteBoard and provide it with the cells and obstacles from the local board. Subsequently, the server sends an object of type RemoteBoard, and the client receives it to update its own board.
Server
public Server(LocalBoard localBoard) {
// TODO
this.board = new RemoteBoard(localBoard);
}
private void processConnection() {
try {
do {
// Write object
out.writeObject(board);
System.out.println("Server sent a update");
try {
Thread.sleep(board.REMOTE_REFRESH_INTERVAL);
} catch (Exception e) {
// TODO: handle exception
}
} while (!board.isFinished());
} catch (IOException e) {
e.printStackTrace();
}
}
Client
private void processConnection() {
try {
board = new RemoteBoard();
SnakeGui gui = new SnakeGui(board,600,0);
gui.init();
do{
board = (RemoteBoard) in.readObject();
board.setChanged();
}while(!board.isFinished());
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
}
RemoteBoard
public RemoteBoard(LocalBoard localBoard) {
this.obstacles = localBoard.getObstacles();
this.cells = localBoard.getCells();
setChanged();
}