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,29 +2,26 @@ package org.toop.app;
public class GameInformation { public class GameInformation {
public enum Type { public enum Type {
TICTACTOE, TICTACTOE(2, 5),
REVERSI, REVERSI(2, 10),
CONNECT4, CONNECT4(2, 7),
BATTLESHIP; BATTLESHIP(2, 5);
private final int playerCount;
private final int maxDepth;
public static int playerCount(Type type) { Type(int playerCount, int maxDepth) {
return switch (type) { this.playerCount = playerCount;
case TICTACTOE -> 2; this.maxDepth = maxDepth;
case REVERSI -> 2; }
case CONNECT4 -> 2;
case BATTLESHIP -> 2;
};
}
public static int maxDepth(Type type) { public int getPlayerCount() {
return switch (type) { return playerCount;
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; public int getMaxDepth() {
case BATTLESHIP -> 5; return maxDepth;
}; }
}
} }
public static class Player { public static class Player {
@@ -39,7 +36,7 @@ public class GameInformation {
public GameInformation(Type type) { public GameInformation(Type type) {
this.type = type; this.type = type;
players = new Player[Type.playerCount(type)]; players = new Player[type.getPlayerCount()];
for (int i = 0; i < players.length; i++) { for (int i = 0; i < players.length; i++) {
players[i] = new Player(); players[i] = new Player();

View File

@@ -106,7 +106,7 @@ public class Connect4Game {
while (isRunning.get()) { while (isRunning.get()) {
final int currentTurn = game.getCurrentTurn(); final int currentTurn = game.getCurrentTurn();
final String currentValue = currentTurn == 0? "RED" : "BLUE"; 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, view.nextPlayer(information.players[currentTurn].isHuman,
information.players[currentTurn].name, information.players[currentTurn].name,

View File

@@ -116,7 +116,7 @@ public final class ReversiGame {
final int currentTurn = game.getCurrentTurn(); final int currentTurn = game.getCurrentTurn();
final String currentValue = currentTurn == 0? "BLACK" : "WHITE"; 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, primary.nextPlayer(information.players[currentTurn].isHuman,
information.players[currentTurn].name, information.players[currentTurn].name,

View File

@@ -102,7 +102,7 @@ public final class TicTacToeGame {
while (isRunning.get()) { while (isRunning.get()) {
final int currentTurn = game.getCurrentTurn(); final int currentTurn = game.getCurrentTurn();
final String currentValue = currentTurn == 0? "X" : "O"; 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, primary.nextPlayer(information.players[currentTurn].isHuman,
information.players[currentTurn].name, information.players[currentTurn].name,

View File

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

View File

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

View File

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

View File

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

View File

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