Skip to content

Commit

Permalink
Start Main refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Mw3y committed May 7, 2024
1 parent 94f2558 commit 3abcf1a
Showing 1 changed file with 45 additions and 25 deletions.
70 changes: 45 additions & 25 deletions src/ch/epfl/chacun/gui/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@
*/
public class Main extends Application {

/**
* The initial width of the window.
*/
private static final int INITIAL_WIDTH = 1440;

/**
* The initial height of the window.
*/
private static final int INITIAL_HEIGHT = 1080;

public static void main(String[] args) {
Expand All @@ -37,45 +44,26 @@ public void start(Stage primaryStage) throws Exception {
// Check if the number of players is valid
Preconditions.checkArgument(playerNames.size() >= 2 && playerNames.size() <= 5);
// Create the player map
List<PlayerColor> playerColors = PlayerColor.ALL.subList(0, playerNames.size());
Map<PlayerColor, String> players = new HashMap<>();
for (int i = 0; i < playerNames.size(); ++i) {
players.put(playerColors.get(i), playerNames.get(i));
}
Map<PlayerColor, String> players = createPlayers(playerNames);

// Apply the seed to ALL tiles
String rawSeed = params.getNamed().get("seed");
RandomGeneratorFactory<RandomGenerator> defaultRandomFactory = RandomGeneratorFactory.getDefault();

RandomGenerator shuffler;
if (rawSeed != null) {
long seed = Long.parseUnsignedLong(rawSeed);
shuffler = defaultRandomFactory.create(seed);
} else {
shuffler = defaultRandomFactory.create();
}

List<Tile> tiles = new ArrayList<>(Tiles.TILES);
Collections.shuffle(tiles, shuffler);
// Group tiles by kind to create the decks
Map<Tile.Kind, List<Tile>> decks = tiles.stream().collect(Collectors.groupingBy(Tile::kind));
TileDecks tileDecks = new TileDecks(decks);
TileDecks tileDecks = createTileDecksWithSeed(rawSeed);

TextMaker textMaker = new TextMakerFr(players);
GameState gameState = GameState.initial(players.keySet().stream().toList(), tileDecks, textMaker);
GameState initialGameState = GameState.initial(players.keySet().stream().toList(), tileDecks, textMaker);

// Dynamic UI properties
SimpleObjectProperty<Rotation> tileToPlaceRotationP = new SimpleObjectProperty<>(Rotation.NONE);
SimpleObjectProperty<Set<Occupant>> visibleOccupantsP = new SimpleObjectProperty<>(Set.of());
SimpleObjectProperty<Set<Integer>> highlightedTilesP = new SimpleObjectProperty<>(Set.of());
SimpleObjectProperty<String> textToDisplayP = new SimpleObjectProperty<>("");
SimpleObjectProperty<List<String>> actionsP = new SimpleObjectProperty<>(List.of());

SimpleObjectProperty<GameState> gameStateO = new SimpleObjectProperty<>(gameState);
// Dynamic game state properties
SimpleObjectProperty<GameState> gameStateO = new SimpleObjectProperty<>(initialGameState);
ObservableValue<MessageBoard> messageBoardO = gameStateO.map(GameState::messageBoard);

gameStateO.set(gameState);
gameStateO.set(gameStateO.get().withStartingTilePlaced());

Consumer<Rotation> applyRotation = rotation -> {
tileToPlaceRotationP.set(tileToPlaceRotationP.get().add(rotation));
};
Expand Down Expand Up @@ -143,6 +131,10 @@ public void start(Stage primaryStage) throws Exception {
visibleOccupantsP.set(Set.copyOf(occupantsToDisplay));
textToDisplayP.set(textMaker.clickToOccupy());
}
case RETAKE_PAWN -> {
// display player's occupants
textToDisplayP.set(textMaker.clickToUnoccupy());
}
}
}
};
Expand Down Expand Up @@ -170,5 +162,33 @@ public void start(Stage primaryStage) throws Exception {
primaryStage.setHeight(INITIAL_HEIGHT);
primaryStage.setTitle("ChaCuN");
primaryStage.show();

gameStateO.set(gameStateO.get().withStartingTilePlaced());
}

private Map<PlayerColor, String> createPlayers(List<String> playerNames) {
List<PlayerColor> playerColors = PlayerColor.ALL.subList(0, playerNames.size());
Map<PlayerColor, String> players = new HashMap<>();
for (int i = 0; i < playerNames.size(); ++i) {
players.put(playerColors.get(i), playerNames.get(i));
}
return Collections.unmodifiableMap(players);
}

private TileDecks createTileDecksWithSeed(String rawSeed) {
RandomGeneratorFactory<RandomGenerator> defaultRandomFactory = RandomGeneratorFactory.getDefault();

RandomGenerator shuffler;
if (rawSeed != null) {
long seed = Long.parseUnsignedLong(rawSeed);
shuffler = defaultRandomFactory.create(seed);
} else {
shuffler = defaultRandomFactory.create();
}

List<Tile> tiles = new ArrayList<>(Tiles.TILES);
Collections.shuffle(tiles, shuffler);
// Group tiles by kind to create the decks
return new TileDecks(tiles.stream().collect(Collectors.groupingBy(Tile::kind)));
}
}

0 comments on commit 3abcf1a

Please sign in to comment.