Functional code, is now object orientated

This commit is contained in:
Bas de Jong
2025-11-06 15:32:15 +01:00
parent fa4e1ad5e3
commit ca11151381
9 changed files with 28 additions and 31 deletions

View File

@@ -2,28 +2,25 @@ package org.toop.app;
public class GameInformation {
public enum Type {
TICTACTOE,
REVERSI,
CONNECT4,
BATTLESHIP;
TICTACTOE(2, 5),
REVERSI(2, 10),
CONNECT4(2, 7),
BATTLESHIP(2, 5);
private final int playerCount;
private final int maxDepth;
public static int playerCount(Type type) {
return switch (type) {
case TICTACTOE -> 2;
case REVERSI -> 2;
case CONNECT4 -> 2;
case BATTLESHIP -> 2;
};
Type(int playerCount, int maxDepth) {
this.playerCount = playerCount;
this.maxDepth = maxDepth;
}
public static int maxDepth(Type type) {
return switch (type) {
case TICTACTOE -> 5; // Todo. 5 seems to always draw or win. could increase to 9 but that might affect performance
case REVERSI -> 10; // Todo. 10 is a guess. might be too slow or too bad.
case CONNECT4 -> 7;
case BATTLESHIP -> 5;
};
public int getPlayerCount() {
return playerCount;
}
public int getMaxDepth() {
return maxDepth;
}
}
@@ -39,7 +36,7 @@ public class GameInformation {
public GameInformation(Type type) {
this.type = type;
players = new Player[Type.playerCount(type)];
players = new Player[type.getPlayerCount()];
for (int i = 0; i < players.length; i++) {
players[i] = new Player();

View File

@@ -106,7 +106,7 @@ public class Connect4Game {
while (isRunning.get()) {
final int currentTurn = game.getCurrentTurn();
final String currentValue = currentTurn == 0? "RED" : "BLUE";
final int nextTurn = (currentTurn + 1) % GameInformation.Type.playerCount(information.type);
final int nextTurn = (currentTurn + 1) % information.type.getPlayerCount();
view.nextPlayer(information.players[currentTurn].isHuman,
information.players[currentTurn].name,

View File

@@ -116,7 +116,7 @@ public final class ReversiGame {
final int currentTurn = game.getCurrentTurn();
final String currentValue = currentTurn == 0? "BLACK" : "WHITE";
final int nextTurn = (currentTurn + 1) % GameInformation.Type.playerCount(information.type);
final int nextTurn = (currentTurn + 1) % information.type.getPlayerCount();
primary.nextPlayer(information.players[currentTurn].isHuman,
information.players[currentTurn].name,

View File

@@ -102,7 +102,7 @@ public final class TicTacToeGame {
while (isRunning.get()) {
final int currentTurn = game.getCurrentTurn();
final String currentValue = currentTurn == 0? "X" : "O";
final int nextTurn = (currentTurn + 1) % GameInformation.Type.playerCount(information.type);
final int nextTurn = (currentTurn + 1) % information.type.getPlayerCount();
primary.nextPlayer(information.players[currentTurn].isHuman,
information.players[currentTurn].name,

View File

@@ -87,7 +87,7 @@ public final class ChallengeView extends View {
final Slider computerDifficultySlider = slider();
computerDifficultySlider.setMin(0);
computerDifficultySlider.setMax(GameInformation.Type.maxDepth(Server.gameToType(game)));
computerDifficultySlider.setMax(Server.gameToType(game).getMaxDepth());
computerDifficultySlider.setValue(playerInformation.computerDifficulty);
computerDifficultySlider.valueProperty().addListener((_, _, newValue) -> {
playerInformation.computerDifficulty = newValue.intValue();

View File

@@ -83,7 +83,7 @@ public final class LocalMultiplayerView extends View {
}
private VBox[] setupPlayers() {
final VBox[] playerBoxes = new VBox[GameInformation.Type.playerCount(information.type)];
final VBox[] playerBoxes = new VBox[information.type.getPlayerCount()];
for (int i = 0; i < playerBoxes.length; i++) {
final int index = i;
@@ -141,7 +141,7 @@ public final class LocalMultiplayerView extends View {
final Slider computerDifficultySlider = slider();
computerDifficultySlider.setMin(0);
computerDifficultySlider.setMax(GameInformation.Type.maxDepth(information.type));
computerDifficultySlider.setMax(information.type.getMaxDepth());
computerDifficultySlider.setValue(information.players[i].computerDifficulty);
computerDifficultySlider.valueProperty().addListener((_, _, newValue) -> {
information.players[index].computerDifficulty = newValue.intValue();

View File

@@ -86,7 +86,7 @@ public final class SendChallengeView extends View {
final Slider computerDifficultySlider = slider();
computerDifficultySlider.setMin(0);
computerDifficultySlider.setMax(GameInformation.Type.maxDepth(Server.gameToType(gamesCombobox.getValue())));
computerDifficultySlider.setMax(Server.gameToType(gamesCombobox.getValue()).getMaxDepth());
computerDifficultySlider.setValue(playerInformation.computerDifficulty);
computerDifficultySlider.valueProperty().addListener((_, _, newValue) -> {
playerInformation.computerDifficulty = newValue.intValue();

View File

@@ -53,7 +53,7 @@ public final class SendChallengePopup extends PopupWidget {
newGame -> {
playerInformation.computerDifficulty = Math.min(
playerInformation.computerDifficulty,
GameInformation.Type.maxDepth(Server.gameToType(newGame))
Server.gameToType(newGame).getMaxDepth()
);
},
games.toArray(new String[0])

View File

@@ -49,7 +49,7 @@ public class LocalMultiplayerView extends ViewWidget {
}
private ScrollPane setupPlayerSections() {
int playerCount = GameInformation.Type.playerCount(information.type);
int playerCount = information.type.getPlayerCount();
VBox[] playerBoxes = new VBox[playerCount];
for (int i = 0; i < playerCount; i++) {