Basics of client server communication and drawing of gamestates finished

This commit is contained in:
2021-02-18 11:42:11 +01:00
parent 8ed0e5196b
commit b7113b7ee8
15 changed files with 238 additions and 47 deletions

View File

@@ -0,0 +1,41 @@
package game;
import java.nio.charset.StandardCharsets;
public class TicTacToe_Server {
private String gameState;
public TicTacToe_Server(){
gameState = "---------";
}
public void setGameState(String gameState) {
this.gameState = gameState;
}
public String getGameState() {
return gameState;
}
public int makeClientMove(String input) {
int column = Double.valueOf(input.split("\\|")[0]).intValue() / 300;
int row = Double.valueOf(input.split("\\|")[1]).intValue() / 300;
int index = column * 3 + row;
if (isLegalMove(column, row)) {
byte[] gameStateBytes = gameState.getBytes();
gameStateBytes[index] = (byte) 'x';
gameState = new String(gameStateBytes, StandardCharsets.UTF_8);
return 200;
} else {
return 404;
}
}
private boolean isLegalMove(int column, int row){
int index = column * 3 + row;
return gameState.charAt(index) == '-';
}
}