Compare commits
1 Commits
8c672e24ad
...
HAAAAAAAAA
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0795f9256d |
@@ -1,27 +0,0 @@
|
||||
package common;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class ConsumerSignal<T> {
|
||||
private final Set<Consumer<T>> listeners;
|
||||
|
||||
public ConsumerSignal() {
|
||||
this.listeners = new HashSet<>();
|
||||
}
|
||||
|
||||
public void connect(Consumer<T> listener) {
|
||||
this.listeners.add(listener);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
this.listeners.clear();
|
||||
}
|
||||
|
||||
public void emit(T arg) {
|
||||
for (Consumer<T> listener : this.listeners) {
|
||||
listener.accept(arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,6 @@
|
||||
package game;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import sudoku.structure.MultiDoku;
|
||||
@@ -12,19 +8,15 @@ import sudoku.structure.MultiDoku;
|
||||
public class Game {
|
||||
|
||||
public static enum GameState {
|
||||
GameNotStarted, GameGoing
|
||||
GameNotStarted, GameGoing, GameEnd
|
||||
}
|
||||
|
||||
private final Map<Integer, Player> players;
|
||||
private final List<Player> leaderboard;
|
||||
private GameState gameState;
|
||||
private MultiDoku doku;
|
||||
private Instant startTime = null;
|
||||
private long gameDuration;
|
||||
|
||||
public Game() {
|
||||
this.players = new HashMap<>();
|
||||
this.leaderboard = new ArrayList<>();
|
||||
this.gameState = GameState.GameNotStarted;
|
||||
}
|
||||
|
||||
@@ -34,33 +26,19 @@ public class Game {
|
||||
|
||||
public void addPlayer(Player player) {
|
||||
players.put(player.getId(), player);
|
||||
leaderboard.add(player);
|
||||
}
|
||||
|
||||
public void setPlayerRemainingCells(Player player, int newScore) {
|
||||
player.setRemainingCells(newScore);
|
||||
Collections.sort(this.leaderboard,
|
||||
(player1, player2) -> Integer.compare(player1.getRemainingCells(), player2.getRemainingCells()));
|
||||
}
|
||||
|
||||
public void removePlayer(int id) {
|
||||
this.leaderboard.remove(getPlayerById(id));
|
||||
this.players.remove(id);
|
||||
players.remove(id);
|
||||
}
|
||||
|
||||
public Map<Integer, Player> getPlayers() {
|
||||
return players;
|
||||
}
|
||||
|
||||
public void startGame(MultiDoku doku, Instant startTime, long gameDuration) {
|
||||
public void startGame(MultiDoku doku) {
|
||||
this.doku = doku;
|
||||
this.gameState = GameState.GameGoing;
|
||||
this.startTime = startTime;
|
||||
this.gameDuration = gameDuration;
|
||||
}
|
||||
|
||||
public void stopGame() {
|
||||
this.gameState = GameState.GameNotStarted;
|
||||
}
|
||||
|
||||
public GameState getGameState() {
|
||||
@@ -71,16 +49,4 @@ public class Game {
|
||||
return doku;
|
||||
}
|
||||
|
||||
public List<Player> getLeaderboard() {
|
||||
return leaderboard;
|
||||
}
|
||||
|
||||
public Instant getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
public long getGameDuration() {
|
||||
return gameDuration;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,20 +8,10 @@ public class Player implements Serializable {
|
||||
|
||||
private final String pseudo;
|
||||
private final int id;
|
||||
private int score;
|
||||
|
||||
public Player(int id, String pseudo) {
|
||||
this.pseudo = pseudo;
|
||||
this.id = id;
|
||||
this.score = 0;
|
||||
}
|
||||
|
||||
public int getRemainingCells() {
|
||||
return score;
|
||||
}
|
||||
|
||||
void setRemainingCells(int score) {
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
public String getPseudo() {
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
package gui;
|
||||
|
||||
import imgui.ImGui;
|
||||
import imgui.ImVec2;
|
||||
import imgui.flag.ImGuiWindowFlags;
|
||||
|
||||
public class AnimatedBackground {
|
||||
|
||||
private float backgroundOffset = 0;
|
||||
|
||||
private static final float defaultSpeed = 0.05f;
|
||||
|
||||
public AnimatedBackground() {
|
||||
|
||||
}
|
||||
|
||||
public void render() {
|
||||
backgroundOffset += ImGui.getIO().getDeltaTime() * defaultSpeed * Options.BackgroundSpeed;
|
||||
var displaySize = ImGui.getIO().getDisplaySize();
|
||||
ImGui.setNextWindowPos(new ImVec2(0.0f, 0.0f));
|
||||
ImGui.setNextWindowSize(displaySize);
|
||||
ImGui.begin("Background", null, ImGuiWindowFlags.NoDecoration | ImGuiWindowFlags.NoMove
|
||||
| ImGuiWindowFlags.NoSavedSettings | ImGuiWindowFlags.NoBackground
|
||||
| ImGuiWindowFlags.NoBringToFrontOnFocus | ImGuiWindowFlags.NoInputs);
|
||||
ImGui.image(Images.BACKGROUND, displaySize, new ImVec2(backgroundOffset, backgroundOffset),
|
||||
new ImVec2(1.0f + backgroundOffset, 1.0f + backgroundOffset));
|
||||
ImGui.end();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,6 +3,5 @@ package gui;
|
||||
public class Options {
|
||||
|
||||
public static Symbols Symboles = Symbols.Numbers;
|
||||
public static float BackgroundSpeed = 1.0f;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package gui.widget;
|
||||
package gui;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
@@ -6,13 +6,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import common.ConsumerSignal;
|
||||
import common.Signal;
|
||||
import gui.ColorGenerator;
|
||||
import gui.Fonts;
|
||||
import gui.Options;
|
||||
import gui.RenderableMultidoku;
|
||||
import gui.Symbols;
|
||||
import gui.ColorGenerator.Color;
|
||||
import imgui.ImGui;
|
||||
import imgui.ImVec2;
|
||||
@@ -39,7 +33,6 @@ public class SudokuRenderer {
|
||||
private final Set<Cell> diagonals = new HashSet<>();
|
||||
|
||||
public final Signal onResolve = new Signal();
|
||||
public final ConsumerSignal<Cell> onCellChange = new ConsumerSignal<>();
|
||||
|
||||
public SudokuRenderer(MultiDoku doku) {
|
||||
this.doku = RenderableMultidoku.fromMultidoku(doku);
|
||||
@@ -79,13 +72,11 @@ public class SudokuRenderer {
|
||||
if (currentCell.getSymbolIndex() == i) {
|
||||
if (ImGui.button("X", cellSize)) {
|
||||
currentCell.setSymbolIndex(Cell.NOSYMBOL);
|
||||
this.onCellChange.emit(currentCell);
|
||||
ImGui.closeCurrentPopup();
|
||||
}
|
||||
} else {
|
||||
if (ImGui.button(Options.Symboles.getSymbols().get(i), cellSize)) {
|
||||
if (currentCell.trySetValue(i))
|
||||
this.onCellChange.emit(currentCell);
|
||||
currentCell.trySetValue(i);
|
||||
if (this.doku.getDoku().isSolved())
|
||||
this.onResolve.emit();
|
||||
ImGui.closeCurrentPopup();
|
||||
@@ -107,7 +98,7 @@ public class SudokuRenderer {
|
||||
if (offsetX > 0) {
|
||||
ImGui.setCursorPosX(offsetX);
|
||||
}
|
||||
ImGui.beginChild("sudokuChild", new ImVec2(cellSize.x * doku.getWidth(), cellSize.y * doku.getHeight()));
|
||||
ImGui.beginChild(1, new ImVec2(cellSize.x * doku.getWidth(), cellSize.y * doku.getHeight()));
|
||||
|
||||
ImGui.pushStyleVar(ImGuiStyleVar.FrameBorderSize, 2.0f);
|
||||
ImGui.pushStyleVar(ImGuiStyleVar.ItemSpacing, new ImVec2(0.0f, 0.0f));
|
||||
@@ -1,10 +1,9 @@
|
||||
package gui.widget;
|
||||
package gui;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import common.ConsumerSignal;
|
||||
import gui.SudokuType;
|
||||
import common.Signal;
|
||||
import imgui.ImGui;
|
||||
import imgui.extension.imguifiledialog.ImGuiFileDialog;
|
||||
import imgui.extension.imguifiledialog.flag.ImGuiFileDialogFlags;
|
||||
@@ -17,7 +16,7 @@ import sudoku.structure.SudokuFactory;
|
||||
|
||||
public class SudokuSelector {
|
||||
|
||||
public final ConsumerSignal<MultiDoku> onSelect = new ConsumerSignal<>();
|
||||
public final Signal onSelect = new Signal();
|
||||
private MultiDoku doku;
|
||||
|
||||
private final boolean canGenEmptyGrid;
|
||||
@@ -27,16 +26,16 @@ public class SudokuSelector {
|
||||
private final ImInt difficulty = new ImInt(Difficulty.Medium.ordinal());
|
||||
private final List<ImBoolean> contraints = new ArrayList<>();
|
||||
|
||||
private static final String[] sudokuTypes = { "Carré", "Rectangle", "Multidoku" };
|
||||
private static final int SQUARE = 0, RECTANGLE = 1, MULTIDOKU = 2;
|
||||
|
||||
private final ImInt sudokuSize = new ImInt(3);
|
||||
|
||||
private final ImInt sudokuWidth = new ImInt(3);
|
||||
private final ImInt sudokuHeight = new ImInt(3);
|
||||
|
||||
private final String confirmMessage;
|
||||
|
||||
public SudokuSelector(boolean canGenEmptyGrid, String confirmMessage) {
|
||||
public SudokuSelector(boolean canGenEmptyGrid) {
|
||||
this.canGenEmptyGrid = canGenEmptyGrid;
|
||||
this.confirmMessage = confirmMessage;
|
||||
initConstraints();
|
||||
}
|
||||
|
||||
@@ -64,7 +63,7 @@ public class SudokuSelector {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
this.onSelect.emit(this.doku);
|
||||
this.onSelect.emit();
|
||||
}
|
||||
|
||||
public void renderFileDialog() {
|
||||
@@ -76,7 +75,7 @@ public class SudokuSelector {
|
||||
String filePath = entry.getValue();
|
||||
this.doku = SudokuFactory.fromfile(filePath);
|
||||
if (this.doku != null)
|
||||
this.onSelect.emit(this.doku);
|
||||
this.onSelect.emit();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -99,7 +98,7 @@ public class SudokuSelector {
|
||||
switch (currentType.getMakerParamCount()) {
|
||||
case 1:
|
||||
ImGui.inputInt("Taille", sudokuSize);
|
||||
if (ImGui.button(confirmMessage)) {
|
||||
if (ImGui.button("Résoudre un sudoku")) {
|
||||
selectSudoku(currentType.createDoku(getConstraints(), sudokuSize.get()), false);
|
||||
}
|
||||
if (canGenEmptyGrid && ImGui.button("Générer une grille vide")) {
|
||||
@@ -110,7 +109,7 @@ public class SudokuSelector {
|
||||
case 2:
|
||||
ImGui.inputInt("Largeur", sudokuHeight);
|
||||
ImGui.inputInt("Longueur", sudokuWidth);
|
||||
if (ImGui.button(confirmMessage)) {
|
||||
if (ImGui.button("Résoudre un sudoku")) {
|
||||
selectSudoku(currentType.createDoku(getConstraints(), sudokuWidth.get(), sudokuHeight.get()),
|
||||
false);
|
||||
}
|
||||
@@ -130,4 +129,8 @@ public class SudokuSelector {
|
||||
renderFileDialog();
|
||||
}
|
||||
|
||||
public MultiDoku getDoku() {
|
||||
return doku;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package gui.menu;
|
||||
|
||||
import game.Player;
|
||||
import gui.ColorGenerator;
|
||||
import gui.widget.SudokuRenderer;
|
||||
import imgui.ImGui;
|
||||
import imgui.ImVec4;
|
||||
import sudoku.structure.MultiDoku;
|
||||
|
||||
public class EndGameView extends BaseView {
|
||||
|
||||
private final Player winner;
|
||||
private float time = 0;
|
||||
|
||||
private static final ImVec4 YELLOW = new ImVec4(1, 1, 0, 1);
|
||||
|
||||
private final SudokuRenderer sudokuRenderer;
|
||||
|
||||
public EndGameView(StateMachine stateMachine, MultiDoku resolved, Player winner) {
|
||||
super(stateMachine);
|
||||
this.winner = winner;
|
||||
this.sudokuRenderer = new SudokuRenderer(resolved);
|
||||
}
|
||||
|
||||
private ImVec4 getPseudoColor() {
|
||||
time += ImGui.getIO().getDeltaTime();
|
||||
float factor = (float) Math.cos(time);
|
||||
var color = ColorGenerator.hslToRgb(factor * factor, 0.9f, 0.4f);
|
||||
return new ImVec4(color.r, color.g, color.b, 1.0f);
|
||||
}
|
||||
|
||||
private void renderWinText() {
|
||||
String winText = " a gagné !";
|
||||
String text = winner.getPseudo() + winText;
|
||||
float textWidth = ImGui.calcTextSizeX(text);
|
||||
ImGui.setCursorPosX(ImGui.getIO().getDisplaySizeX() / 2.0f - textWidth / 2.0f);
|
||||
ImGui.textColored(getPseudoColor(), winner.getPseudo());
|
||||
ImGui.sameLine();
|
||||
ImGui.textColored(YELLOW, winText);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
renderWinText();
|
||||
this.sudokuRenderer.render();
|
||||
renderReturnButton();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,50 +1,22 @@
|
||||
package gui.menu;
|
||||
|
||||
import game.Player;
|
||||
import gui.widget.LeaderboardRenderer;
|
||||
import gui.widget.MultiPlayerCompleteProgress;
|
||||
import gui.widget.SudokuRenderer;
|
||||
import gui.widget.TimerRenderer;
|
||||
import gui.SudokuRenderer;
|
||||
import imgui.ImGui;
|
||||
import network.client.Client;
|
||||
import network.server.Server;
|
||||
import sudoku.solver.BacktrackingSolver;
|
||||
import sudoku.solver.Solver;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
|
||||
public class MultiPlayerDokuView extends BaseView {
|
||||
public class MultiPlayerDokuView extends BaseView{
|
||||
|
||||
private final Client client;
|
||||
private final Server server;
|
||||
private final SudokuRenderer sudokuRenderer;
|
||||
private final LeaderboardRenderer leaderboardRenderer;
|
||||
private final TimerRenderer timerRenderer;
|
||||
private final MultiPlayerCompleteProgress completeProgress;
|
||||
|
||||
public MultiPlayerDokuView(StateMachine stateMachine, Client client, Server server) {
|
||||
super(stateMachine);
|
||||
this.client = client;
|
||||
this.server = server;
|
||||
this.sudokuRenderer = new SudokuRenderer(this.client.getGame().getDoku());
|
||||
this.leaderboardRenderer = new LeaderboardRenderer(client.getGame(), client.getPlayer());
|
||||
this.sudokuRenderer.onCellChange.connect(this::onCellChange);
|
||||
this.client.onDisconnect.connect(this::onDisconnect);
|
||||
this.client.onGameEnd.connect(this::onGameEnd);
|
||||
this.timerRenderer = new TimerRenderer(this.client.getGame().getStartTime(), this.client.getGame().getGameDuration());
|
||||
this.completeProgress = new MultiPlayerCompleteProgress(this.client.getGame());
|
||||
}
|
||||
|
||||
private void onGameEnd(Player winner) {
|
||||
MultiDoku doku = this.client.getGame().getDoku();
|
||||
doku.clearMutableCells();
|
||||
Solver solver = new BacktrackingSolver();
|
||||
solver.solve(doku);
|
||||
this.stateMachine.overrideState(new EndGameView(stateMachine, doku, winner));
|
||||
}
|
||||
|
||||
private void onCellChange(Cell cell) {
|
||||
this.client.sendCellChange(cell);
|
||||
}
|
||||
|
||||
public void onDisconnect() {
|
||||
@@ -55,9 +27,6 @@ public class MultiPlayerDokuView extends BaseView {
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
this.timerRenderer.render();
|
||||
this.leaderboardRenderer.render();
|
||||
this.completeProgress.render();
|
||||
this.sudokuRenderer.render();
|
||||
if (ImGui.button("Quitter")) {
|
||||
this.client.stop();
|
||||
|
||||
@@ -1,30 +1,24 @@
|
||||
package gui.menu;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import game.Player;
|
||||
import gui.widget.SudokuSelector;
|
||||
import imgui.ImGui;
|
||||
import imgui.type.ImInt;
|
||||
import network.client.Client;
|
||||
import network.server.Server;
|
||||
import sudoku.constraint.Constraint;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.SudokuFactory;
|
||||
|
||||
public class MultiPlayerView extends BaseView {
|
||||
|
||||
private final Client client;
|
||||
private final Server server;
|
||||
|
||||
private final SudokuSelector selector;
|
||||
|
||||
private ImInt gameDurationMinutes = new ImInt(10);
|
||||
|
||||
private MultiDoku doku = null;
|
||||
|
||||
public MultiPlayerView(StateMachine stateMachine, Client client, Server server) {
|
||||
super(stateMachine);
|
||||
this.client = client;
|
||||
this.server = server;
|
||||
this.selector = new SudokuSelector(false, "Sélectionner le sudoku");
|
||||
this.selector.onSelect.connect(this::onSelected);
|
||||
this.client.onDisconnect.connect(this::onDisconnect);
|
||||
this.client.onGameStarted
|
||||
.connect(() -> this.stateMachine.pushState(new MultiPlayerDokuView(stateMachine, client, server)));
|
||||
@@ -40,40 +34,26 @@ public class MultiPlayerView extends BaseView {
|
||||
this.stateMachine.popState();
|
||||
}
|
||||
|
||||
private void onSelected(MultiDoku doku) {
|
||||
this.doku = doku;
|
||||
}
|
||||
|
||||
public void renderGameStatus() {
|
||||
if (this.server == null) {
|
||||
ImGui.text("En attente de l'administrateur du serveur ...");
|
||||
} else {
|
||||
renderTimer();
|
||||
ImGui.beginDisabled(this.doku == null);
|
||||
if (ImGui.button("Démarrer")) {
|
||||
this.server.startGame(this.doku, this.gameDurationMinutes.get() * 60);
|
||||
// temp
|
||||
MultiDoku doku = SudokuFactory.createBasicXShapedMultidoku(3, Arrays.asList(Constraint.Diagonal));
|
||||
this.server.startGame(doku);
|
||||
}
|
||||
ImGui.endDisabled();
|
||||
selector.render();
|
||||
}
|
||||
}
|
||||
|
||||
private void renderPlayers() {
|
||||
@Override
|
||||
public void render() {
|
||||
ImGui.text("Joueurs :");
|
||||
{
|
||||
for (Player player : this.client.getGame().getPlayers().values()) {
|
||||
ImGui.bulletText(player.getPseudo());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void renderTimer() {
|
||||
ImGui.inputInt("Temps de la partie (minutes)", gameDurationMinutes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
renderPlayers();
|
||||
renderGameStatus();
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@ import imgui.type.ImInt;
|
||||
public class OptionsMenu extends BaseView {
|
||||
|
||||
private ImInt currentValue = new ImInt();
|
||||
private float backgroundSpeed[] = new float[]{Options.BackgroundSpeed};
|
||||
|
||||
public OptionsMenu(StateMachine stateMachine) {
|
||||
super(stateMachine);
|
||||
@@ -20,9 +19,6 @@ public class OptionsMenu extends BaseView {
|
||||
if(ImGui.combo("Jeu de symboles", currentValue, Symbols.getSymbolsNames())){
|
||||
Options.Symboles = Symbols.values()[currentValue.get()];
|
||||
}
|
||||
if(ImGui.sliderFloat("Vitesse d'animation de l'arrière plan", backgroundSpeed, 0.0f, 10.0f)){
|
||||
Options.BackgroundSpeed = backgroundSpeed[0];
|
||||
}
|
||||
renderReturnButton();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
package gui.menu;
|
||||
|
||||
import gui.widget.SudokuSelector;
|
||||
import gui.SudokuSelector;
|
||||
import imgui.ImGui;
|
||||
import sudoku.structure.MultiDoku;
|
||||
|
||||
public class SoloMenu extends BaseView {
|
||||
|
||||
@@ -10,12 +9,12 @@ public class SoloMenu extends BaseView {
|
||||
|
||||
public SoloMenu(StateMachine stateMachine) {
|
||||
super(stateMachine);
|
||||
this.sudokuSelector = new SudokuSelector(true, "Résoudre le sudoku");
|
||||
this.sudokuSelector = new SudokuSelector(true);
|
||||
this.sudokuSelector.onSelect.connect(this::pushSudokuState);
|
||||
}
|
||||
|
||||
private void pushSudokuState(MultiDoku doku) {
|
||||
this.stateMachine.pushState(new SudokuView(stateMachine, doku));
|
||||
private void pushSudokuState() {
|
||||
this.stateMachine.pushState(new SudokuView(stateMachine, this.sudokuSelector.getDoku()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -2,7 +2,7 @@ package gui.menu;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
import gui.AnimatedBackground;
|
||||
import gui.Images;
|
||||
import imgui.ImGui;
|
||||
import imgui.ImVec2;
|
||||
import imgui.flag.ImGuiKey;
|
||||
@@ -11,11 +11,9 @@ import imgui.flag.ImGuiWindowFlags;
|
||||
public class StateMachine {
|
||||
|
||||
private final Stack<BaseView> menus;
|
||||
private final AnimatedBackground background;
|
||||
|
||||
public StateMachine() {
|
||||
this.menus = new Stack<>();
|
||||
this.background = new AnimatedBackground();
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
@@ -29,11 +27,6 @@ public class StateMachine {
|
||||
menus.add(menu);
|
||||
}
|
||||
|
||||
public void overrideState(BaseView menu) {
|
||||
menus.getLast().cleanResources();
|
||||
menus.set(menus.size() - 1, menu);
|
||||
}
|
||||
|
||||
public void popState() {
|
||||
menus.getLast().cleanResources();
|
||||
menus.pop();
|
||||
@@ -47,7 +40,12 @@ public class StateMachine {
|
||||
|
||||
public void render() {
|
||||
var displaySize = ImGui.getIO().getDisplaySize();
|
||||
this.background.render();
|
||||
ImGui.setNextWindowPos(new ImVec2(0.0f, 0.0f));
|
||||
ImGui.setNextWindowSize(displaySize);
|
||||
ImGui.begin("Background", null, ImGuiWindowFlags.NoDecoration | ImGuiWindowFlags.NoMove
|
||||
| ImGuiWindowFlags.NoSavedSettings | ImGuiWindowFlags.NoBackground | ImGuiWindowFlags.NoBringToFrontOnFocus | ImGuiWindowFlags.NoInputs);
|
||||
ImGui.image(Images.BACKGROUND, displaySize, new ImVec2(0, 0));
|
||||
ImGui.end();
|
||||
ImGui.setNextWindowPos(new ImVec2(0.0f, 0.0f));
|
||||
ImGui.setNextWindowSize(displaySize);
|
||||
ImGui.begin("##Main Window", null, ImGuiWindowFlags.NoDecoration | ImGuiWindowFlags.NoMove
|
||||
|
||||
@@ -2,7 +2,7 @@ package gui.menu;
|
||||
|
||||
import java.util.concurrent.CancellationException;
|
||||
|
||||
import gui.widget.SudokuRenderer;
|
||||
import gui.SudokuRenderer;
|
||||
import imgui.ImGui;
|
||||
import imgui.ImGuiStyle;
|
||||
import sudoku.io.SudokuSerializer;
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
package gui.widget;
|
||||
|
||||
import game.Game;
|
||||
import game.Player;
|
||||
import imgui.ImGui;
|
||||
import imgui.ImVec2;
|
||||
import imgui.ImVec4;
|
||||
import imgui.flag.ImGuiCol;
|
||||
import imgui.flag.ImGuiStyleVar;
|
||||
|
||||
public class LeaderboardRenderer {
|
||||
|
||||
private final Game game;
|
||||
private final Player currentPlayer;
|
||||
|
||||
private final float cellHeight = 75;
|
||||
private final ImVec2 cellSize = new ImVec2(12 * cellHeight, cellHeight);
|
||||
private final ImVec2 rankSize = new ImVec2(cellHeight, cellHeight);
|
||||
private final ImVec2 scoreSize = rankSize;
|
||||
private final ImVec2 nameSize = new ImVec2(cellSize.x - cellHeight * 2.0f, cellHeight);
|
||||
private final ImVec4 cellColorPlayer = new ImVec4(0.20f, 0.67f, 1.0f, 0.5f);
|
||||
private final ImVec4 cellColorEnemy = new ImVec4(1.0f, 0.0f, 0.0f, 0.5f);
|
||||
private final int maxPlayersShowed = 2;
|
||||
|
||||
private final int emptyCellCount;
|
||||
|
||||
public LeaderboardRenderer(Game game, Player player) {
|
||||
this.game = game;
|
||||
this.currentPlayer = player;
|
||||
this.emptyCellCount = game.getDoku().getEmptyCells().size();
|
||||
}
|
||||
|
||||
private void renderRank(int rank) {
|
||||
ImGui.button(Integer.toString(rank), rankSize);
|
||||
}
|
||||
|
||||
private void renderName(String name) {
|
||||
ImGui.button(name, nameSize);
|
||||
}
|
||||
|
||||
private void renderScore(int score) {
|
||||
ImGui.button(Integer.toString(score), scoreSize);
|
||||
}
|
||||
|
||||
private void renderCell(Player player, int rank, ImVec4 color) {
|
||||
ImGui.pushStyleColor(ImGuiCol.Button, color);
|
||||
ImGui.pushStyleColor(ImGuiCol.ButtonHovered, color);
|
||||
ImGui.pushStyleColor(ImGuiCol.ButtonActive, color);
|
||||
ImGui.beginChild(player.getPseudo() + "##" + player.getId(), cellSize);
|
||||
renderRank(rank);
|
||||
ImGui.sameLine();
|
||||
renderName(player.getPseudo());
|
||||
ImGui.sameLine();
|
||||
renderScore(emptyCellCount - player.getRemainingCells());
|
||||
ImGui.endChild();
|
||||
ImGui.popStyleColor(3);
|
||||
}
|
||||
|
||||
public void render() {
|
||||
var displaySize = ImGui.getIO().getDisplaySize();
|
||||
ImGui.setCursorPosX(displaySize.x / 2.0f - cellSize.x / 2.0f);
|
||||
ImGui.beginChild("Leaderboard", new ImVec2(cellSize.x + 15.0f, cellHeight * maxPlayersShowed));
|
||||
ImGui.pushStyleVar(ImGuiStyleVar.ItemSpacing, new ImVec2());
|
||||
ImGui.pushStyleVar(ImGuiStyleVar.FrameBorderSize, 3.0f);
|
||||
for (int i = 0; i < game.getLeaderboard().size(); i++) {
|
||||
Player player = game.getLeaderboard().get(i);
|
||||
renderCell(player, i + 1, player == currentPlayer ? cellColorPlayer : cellColorEnemy);
|
||||
}
|
||||
ImGui.popStyleVar(2);
|
||||
ImGui.endChild();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package gui.widget;
|
||||
|
||||
import game.Game;
|
||||
import game.Player;
|
||||
import imgui.ImGui;
|
||||
import imgui.ImVec2;
|
||||
|
||||
public class MultiPlayerCompleteProgress {
|
||||
|
||||
private final Game game;
|
||||
private final int emptyCellCount;
|
||||
private final ImVec2 progressSize = new ImVec2(700, 50);
|
||||
private final SmoothProgressBar progressBar;
|
||||
|
||||
public MultiPlayerCompleteProgress(Game game) {
|
||||
this.game = game;
|
||||
this.emptyCellCount = game.getDoku().getEmptyCells().size();
|
||||
this.progressBar = new SmoothProgressBar();
|
||||
}
|
||||
|
||||
public void render() {
|
||||
Player firstPlayer = game.getLeaderboard().getFirst();
|
||||
ImGui.setCursorPosX(ImGui.getIO().getDisplaySizeX() / 2.0f - progressSize.x / 2.0f);
|
||||
String progressText = firstPlayer.getPseudo() + " - " + (emptyCellCount - firstPlayer.getRemainingCells()) + "/" + emptyCellCount;
|
||||
this.progressBar.render(progressText, progressSize, 1.0f - firstPlayer.getRemainingCells() / (float) emptyCellCount);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package gui.widget;
|
||||
|
||||
import imgui.ImGui;
|
||||
import imgui.ImVec2;
|
||||
|
||||
public class SmoothProgressBar {
|
||||
|
||||
private float lastProgress = 0;
|
||||
private final float speed = 2.0f;
|
||||
private final float clipConstant = 0.001f;
|
||||
|
||||
public void render(String label, ImVec2 size, float progress) {
|
||||
float delta = progress - lastProgress;
|
||||
if (Math.abs(delta) < clipConstant)
|
||||
lastProgress = progress;
|
||||
else
|
||||
lastProgress = lastProgress + delta * ImGui.getIO().getDeltaTime() * speed;
|
||||
ImGui.progressBar(lastProgress, size, label);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package gui.widget;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
import imgui.ImGui;
|
||||
|
||||
public class TimerRenderer {
|
||||
|
||||
private final long endTime;
|
||||
|
||||
public TimerRenderer(Instant startTime, long duration) {
|
||||
this.endTime = startTime.getEpochSecond() + duration;
|
||||
}
|
||||
|
||||
private long getTimeRemaining() {
|
||||
long currentTime = Instant.now().getEpochSecond();
|
||||
return endTime - currentTime;
|
||||
}
|
||||
|
||||
public void render() {
|
||||
long seconds = getTimeRemaining();
|
||||
long minutes = seconds / 60;
|
||||
String text = String.format("%02d:%02d", minutes, seconds % 60);
|
||||
var textSize = ImGui.calcTextSize(text);
|
||||
ImGui.setCursorPosX(ImGui.getIO().getDisplaySizeX() / 2.0f - textSize.x / 2.0f);
|
||||
ImGui.text(text);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,7 +22,7 @@ public class ConnexionThread extends Thread {
|
||||
// System.out.println(objectInputStream.available());
|
||||
Object o = objectInputStream.readObject();
|
||||
if (o instanceof Packet packet) {
|
||||
connexion.visit(packet);
|
||||
connexion.visitPacket(packet);
|
||||
}
|
||||
} catch (ClassNotFoundException | IOException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -4,15 +4,10 @@ import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Random;
|
||||
|
||||
import common.ConsumerSignal;
|
||||
import common.Signal;
|
||||
import game.Game;
|
||||
import game.Player;
|
||||
import network.protocol.packets.ChangeCellPacket;
|
||||
import network.protocol.packets.LoginPacket;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Sudoku;
|
||||
|
||||
public class Client {
|
||||
private final ClientConnexion clientConnection;
|
||||
@@ -22,9 +17,6 @@ public class Client {
|
||||
public final Signal onDisconnect = new Signal();
|
||||
public final Signal onClosed = new Signal();
|
||||
public final Signal onGameStarted = new Signal();
|
||||
public final ConsumerSignal<Player> onGameEnd = new ConsumerSignal<>();
|
||||
|
||||
Player player;
|
||||
|
||||
String disconnectReason = null;
|
||||
|
||||
@@ -62,20 +54,4 @@ public class Client {
|
||||
stop();
|
||||
}
|
||||
|
||||
public void sendCellChange(Cell cell) {
|
||||
MultiDoku doku = getGame().getDoku();
|
||||
for (int sudokuIndex = 0; sudokuIndex < doku.getNbSubGrids(); sudokuIndex++) {
|
||||
Sudoku sudoku = doku.getSubGrid(sudokuIndex);
|
||||
int cellIndex = sudoku.getCells().indexOf(cell);
|
||||
if (cellIndex != -1) {
|
||||
this.clientConnection.sendPacket(new ChangeCellPacket(sudokuIndex, cellIndex, cell.getSymbolIndex()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
return player;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,21 +6,19 @@ import java.net.UnknownHostException;
|
||||
|
||||
import game.Player;
|
||||
import network.Connexion;
|
||||
import network.protocol.packets.ChangeCellPacket;
|
||||
import network.protocol.packets.ConnexionInfoPacket;
|
||||
import network.protocol.packets.DisconnectPacket;
|
||||
import network.protocol.packets.EndGamePacket;
|
||||
import network.protocol.packets.KeepAlivePacket;
|
||||
import network.protocol.packets.LoginPacket;
|
||||
import network.protocol.packets.PlayerJoinPacket;
|
||||
import network.protocol.packets.PlayerLeavePacket;
|
||||
import network.protocol.packets.StartGamePacket;
|
||||
import network.protocol.packets.UpdatePlayerScorePacket;
|
||||
import sudoku.io.SudokuSerializer;
|
||||
|
||||
public class ClientConnexion extends Connexion {
|
||||
|
||||
private final Client client;
|
||||
private Player player = null;
|
||||
|
||||
public ClientConnexion(String address, short port, Client client) throws UnknownHostException, IOException {
|
||||
super(new Socket(address, port));
|
||||
@@ -37,7 +35,7 @@ public class ClientConnexion extends Connexion {
|
||||
|
||||
@Override
|
||||
public void visitPacket(ConnexionInfoPacket packet) {
|
||||
this.client.player = this.client.getGame().getPlayerById(packet.getConnectionId());
|
||||
this.player = this.client.getGame().getPlayerById(packet.getConnectionId());
|
||||
client.onConnect.emit();
|
||||
}
|
||||
|
||||
@@ -71,28 +69,8 @@ public class ClientConnexion extends Connexion {
|
||||
|
||||
@Override
|
||||
public void visitPacket(StartGamePacket packet) {
|
||||
this.client.getGame().startGame(SudokuSerializer.deserializeSudoku(packet.getSerializedSudoku()),
|
||||
packet.getInstant(), packet.getGameDuration());
|
||||
this.client.getGame().startGame(SudokuSerializer.deserializeSudoku(packet.getSerializedSudoku()));
|
||||
this.client.onGameStarted.emit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(EndGamePacket packet) {
|
||||
Player winner = this.client.getGame().getLeaderboard().getFirst();
|
||||
this.client.getGame().stopGame();
|
||||
this.client.onGameEnd.emit(winner);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(UpdatePlayerScorePacket packet) {
|
||||
Player player = this.client.getGame().getPlayerById(packet.getPlayerId());
|
||||
assert (player != null);
|
||||
this.client.getGame().setPlayerRemainingCells(player, packet.getCellsLeft());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(ChangeCellPacket packet) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacketChangeCell'");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,19 +1,16 @@
|
||||
package network.protocol;
|
||||
|
||||
import network.protocol.packets.ChangeCellPacket;
|
||||
import network.protocol.packets.ConnexionInfoPacket;
|
||||
import network.protocol.packets.DisconnectPacket;
|
||||
import network.protocol.packets.EndGamePacket;
|
||||
import network.protocol.packets.KeepAlivePacket;
|
||||
import network.protocol.packets.LoginPacket;
|
||||
import network.protocol.packets.PlayerJoinPacket;
|
||||
import network.protocol.packets.PlayerLeavePacket;
|
||||
import network.protocol.packets.StartGamePacket;
|
||||
import network.protocol.packets.UpdatePlayerScorePacket;
|
||||
|
||||
public interface PacketVisitor {
|
||||
|
||||
default void visit(Packet packet) {
|
||||
default void visitPacket(Packet packet) {
|
||||
packet.accept(this);
|
||||
}
|
||||
|
||||
@@ -24,8 +21,5 @@ public interface PacketVisitor {
|
||||
void visitPacket(PlayerJoinPacket packet);
|
||||
void visitPacket(PlayerLeavePacket packet);
|
||||
void visitPacket(StartGamePacket packet);
|
||||
void visitPacket(EndGamePacket packet);
|
||||
void visitPacket(UpdatePlayerScorePacket packet);
|
||||
void visitPacket(ChangeCellPacket packet);
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,6 @@ package network.protocol;
|
||||
|
||||
public enum Packets {
|
||||
|
||||
ConnectionInfo, KeepAlive, Disconnect, Login, PlayerJoin, PlayerLeave, StartGame, ChangeCell, EndGame, UpdatePlayerScore
|
||||
ConnectionInfo, KeepAlive, Disconnect, Login, PlayerJoin, PlayerLeave, StartGame
|
||||
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
package network.protocol.packets;
|
||||
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
import network.protocol.Packets;
|
||||
|
||||
public class ChangeCellPacket extends Packet {
|
||||
|
||||
static private final long serialVersionUID = Packets.ChangeCell.ordinal();
|
||||
|
||||
private final int sudokuIndex;
|
||||
private final int cellIndex;
|
||||
private final int newValue;
|
||||
|
||||
public ChangeCellPacket(int sudokuIndex, int cellIndex, int newValue) {
|
||||
this.sudokuIndex = sudokuIndex;
|
||||
this.cellIndex = cellIndex;
|
||||
this.newValue = newValue;
|
||||
}
|
||||
|
||||
public int getSudokuIndex() {
|
||||
return sudokuIndex;
|
||||
}
|
||||
|
||||
public int getCellIndex() {
|
||||
return cellIndex;
|
||||
}
|
||||
|
||||
public int getNewValue() {
|
||||
return newValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(PacketVisitor packetVisitor) {
|
||||
packetVisitor.visitPacket(this);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package network.protocol.packets;
|
||||
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
import network.protocol.Packets;
|
||||
|
||||
public class EndGamePacket extends Packet {
|
||||
|
||||
static private final long serialVersionUID = Packets.EndGame.ordinal();
|
||||
|
||||
public EndGamePacket() { }
|
||||
|
||||
@Override
|
||||
public void accept(PacketVisitor packetVisitor) {
|
||||
packetVisitor.visitPacket(this);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
package network.protocol.packets;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
import network.protocol.Packets;
|
||||
@@ -11,28 +9,15 @@ public class StartGamePacket extends Packet {
|
||||
static private final long serialVersionUID = Packets.StartGame.ordinal();
|
||||
|
||||
private final String serializedSudoku;
|
||||
// used to resume game
|
||||
private final Instant instant;
|
||||
private final long gameDuration;
|
||||
|
||||
public StartGamePacket(String serializedSudoku, Instant instant, long gameDuration) {
|
||||
public StartGamePacket(String serializedSudoku) {
|
||||
this.serializedSudoku = serializedSudoku;
|
||||
this.instant = instant;
|
||||
this.gameDuration = gameDuration;
|
||||
}
|
||||
|
||||
public String getSerializedSudoku() {
|
||||
return serializedSudoku;
|
||||
}
|
||||
|
||||
public Instant getInstant() {
|
||||
return instant;
|
||||
}
|
||||
|
||||
public long getGameDuration() {
|
||||
return gameDuration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(PacketVisitor packetVisitor) {
|
||||
packetVisitor.visitPacket(this);
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
package network.protocol.packets;
|
||||
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
import network.protocol.Packets;
|
||||
|
||||
public class UpdatePlayerScorePacket extends Packet {
|
||||
|
||||
static private final long serialVersionUID = Packets.UpdatePlayerScore.ordinal();
|
||||
|
||||
private final int playerId;
|
||||
private final int cellsLeft;
|
||||
|
||||
public UpdatePlayerScorePacket(int playerId, int cellsLeft) {
|
||||
this.playerId = playerId;
|
||||
this.cellsLeft = cellsLeft;
|
||||
}
|
||||
|
||||
public int getPlayerId() {
|
||||
return playerId;
|
||||
}
|
||||
|
||||
public int getCellsLeft() {
|
||||
return cellsLeft;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(PacketVisitor packetVisitor) {
|
||||
packetVisitor.visitPacket(this);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,15 +2,12 @@ package network.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import game.Game;
|
||||
import game.Player;
|
||||
import game.Game.GameState;
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.packets.EndGamePacket;
|
||||
import network.protocol.packets.StartGamePacket;
|
||||
import sudoku.io.SudokuSerializer;
|
||||
import sudoku.structure.MultiDoku;
|
||||
@@ -40,16 +37,7 @@ public class Server {
|
||||
}
|
||||
}
|
||||
|
||||
private void checkTimer() {
|
||||
if (getGame() == null || getGame().getGameState() != GameState.GameGoing)
|
||||
return;
|
||||
long now = Instant.now().getEpochSecond();
|
||||
long end = getGame().getStartTime().getEpochSecond() + getGame().getGameDuration();
|
||||
if (now > end)
|
||||
stopGame();
|
||||
}
|
||||
|
||||
private void checkConnexions() {
|
||||
public void update() {
|
||||
for (var it = connexions.iterator(); it.hasNext();) {
|
||||
ServerConnexion connexion = it.next();
|
||||
if (!connexion.update()) {
|
||||
@@ -60,11 +48,6 @@ public class Server {
|
||||
}
|
||||
}
|
||||
|
||||
public void update() {
|
||||
checkTimer();
|
||||
checkConnexions();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
this.acceptThread.cancel();
|
||||
this.logicThread.cancel();
|
||||
@@ -85,19 +68,9 @@ public class Server {
|
||||
return game;
|
||||
}
|
||||
|
||||
public void startGame(MultiDoku doku, long gameDuration) {
|
||||
Instant now = Instant.now();
|
||||
this.game.startGame(doku, now, gameDuration);
|
||||
for (ServerConnexion connexion : this.connexions) {
|
||||
connexion.setSudoku(doku.clone());
|
||||
}
|
||||
broadcastPacket(new StartGamePacket(SudokuSerializer.serializeSudoku(doku).toString(), now, gameDuration));
|
||||
}
|
||||
|
||||
public void stopGame() {
|
||||
// we don't need to specify the winner since it has to be the first
|
||||
broadcastPacket(new EndGamePacket());
|
||||
getGame().stopGame();
|
||||
public void startGame(MultiDoku doku) {
|
||||
this.game.startGame(doku);
|
||||
broadcastPacket(new StartGamePacket(SudokuSerializer.serializeSudoku(doku).toString()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,23 +3,17 @@ package network.server;
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
|
||||
import game.Game;
|
||||
import game.Player;
|
||||
import game.Game.GameState;
|
||||
import network.Connexion;
|
||||
import network.protocol.packets.ChangeCellPacket;
|
||||
import network.protocol.packets.ConnexionInfoPacket;
|
||||
import network.protocol.packets.DisconnectPacket;
|
||||
import network.protocol.packets.EndGamePacket;
|
||||
import network.protocol.packets.KeepAlivePacket;
|
||||
import network.protocol.packets.LoginPacket;
|
||||
import network.protocol.packets.PlayerJoinPacket;
|
||||
import network.protocol.packets.PlayerLeavePacket;
|
||||
import network.protocol.packets.StartGamePacket;
|
||||
import network.protocol.packets.UpdatePlayerScorePacket;
|
||||
import sudoku.io.SudokuSerializer;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
|
||||
public class ServerConnexion extends Connexion {
|
||||
|
||||
@@ -27,7 +21,6 @@ public class ServerConnexion extends Connexion {
|
||||
private final KeepAliveHandler keepAliveHandler;
|
||||
private boolean shouldClose = false;
|
||||
private Player player = null;
|
||||
private MultiDoku doku;
|
||||
|
||||
public ServerConnexion(Socket socket, Server server) throws IOException {
|
||||
super(socket);
|
||||
@@ -36,7 +29,7 @@ public class ServerConnexion extends Connexion {
|
||||
}
|
||||
|
||||
public boolean update() {
|
||||
if (shouldClose || isClosed())
|
||||
if (shouldClose | isClosed())
|
||||
return false;
|
||||
return this.keepAliveHandler.update();
|
||||
}
|
||||
@@ -51,7 +44,7 @@ public class ServerConnexion extends Connexion {
|
||||
|
||||
@Override
|
||||
public synchronized void close() {
|
||||
if (shouldClose)
|
||||
if(shouldClose)
|
||||
return;
|
||||
super.close();
|
||||
shouldClose = true;
|
||||
@@ -61,22 +54,13 @@ public class ServerConnexion extends Connexion {
|
||||
private void finishLogin() {
|
||||
// send players that have already joined (excluding this one)
|
||||
for (Player p : this.server.getGame().getPlayers().values()) {
|
||||
if (p.getId() != player.getId()) {
|
||||
if (p.getId() != player.getId())
|
||||
sendPacket(new PlayerJoinPacket(p));
|
||||
sendPacket(new UpdatePlayerScorePacket(p.getId(), p.getRemainingCells()));
|
||||
}
|
||||
}
|
||||
|
||||
this.server.broadcastPacket(new PlayerJoinPacket(player));
|
||||
sendPacket(new ConnexionInfoPacket(player.getId()));
|
||||
|
||||
Game game = this.server.getGame();
|
||||
|
||||
if (game.getGameState() == GameState.GameGoing) {
|
||||
setSudoku(game.getDoku().clone());
|
||||
sendPacket(
|
||||
new StartGamePacket(SudokuSerializer.serializeSudoku(game.getDoku()).toString(),
|
||||
game.getStartTime(), game.getGameDuration()));
|
||||
if (this.server.getGame().getGameState() == GameState.GameGoing) {
|
||||
sendPacket(new StartGamePacket(SudokuSerializer.serializeSudoku(this.server.getGame().getDoku()).toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,50 +102,4 @@ public class ServerConnexion extends Connexion {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacketStartGame'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(EndGamePacket packet) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(UpdatePlayerScorePacket packet) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(ChangeCellPacket packet) {
|
||||
Cell cell = this.doku.getSubGrid(packet.getSudokuIndex()).getCell(packet.getCellIndex());
|
||||
if (cell.getSymbolIndex() == Cell.NOSYMBOL && packet.getNewValue() == Cell.NOSYMBOL)
|
||||
return;
|
||||
if (cell.getSymbolIndex() != Cell.NOSYMBOL && packet.getNewValue() != Cell.NOSYMBOL) {
|
||||
cell.trySetValue(packet.getNewValue());
|
||||
return;
|
||||
}
|
||||
if (cell.getSymbolIndex() != Cell.NOSYMBOL && packet.getNewValue() == Cell.NOSYMBOL) {
|
||||
cell.empty();
|
||||
this.server.getGame().setPlayerRemainingCells(player, player.getRemainingCells() + 1);
|
||||
this.server.broadcastPacket(new UpdatePlayerScorePacket(player.getId(), player.getRemainingCells()));
|
||||
return;
|
||||
}
|
||||
// on rajoute un chiffre à la grille
|
||||
if (cell.trySetValue(packet.getNewValue())) {
|
||||
this.server.getGame().setPlayerRemainingCells(player, player.getRemainingCells() - 1);
|
||||
this.server.broadcastPacket(new UpdatePlayerScorePacket(player.getId(), player.getRemainingCells()));
|
||||
}
|
||||
checkWin();
|
||||
}
|
||||
|
||||
private void checkWin() {
|
||||
if (this.player.getRemainingCells() == 0) {
|
||||
this.server.stopGame();
|
||||
}
|
||||
}
|
||||
|
||||
public void setSudoku(MultiDoku doku) {
|
||||
this.doku = doku;
|
||||
assert (player != null);
|
||||
this.server.getGame().setPlayerRemainingCells(player, this.doku.getEmptyCells().size());
|
||||
this.server.broadcastPacket(new UpdatePlayerScorePacket(player.getId(), player.getRemainingCells()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ public class ServerLogicThread extends Thread {
|
||||
try {
|
||||
Thread.sleep(50);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
// e.printStackTrace();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,14 +4,12 @@ import gui.RenderableMultidoku;
|
||||
import gui.Symbols;
|
||||
import sudoku.constraint.*;
|
||||
import sudoku.solver.RandomSolver;
|
||||
import sudoku.solver.Solver;
|
||||
import sudoku.structure.Difficulty;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.SudokuFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class ConsoleInterface {
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.util.List;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import gui.Symbols;
|
||||
import sudoku.io.SudokuPrinter;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
@@ -26,7 +27,8 @@ public class HumanSolver implements Solver {
|
||||
logger.log(Level.FINE,
|
||||
'\n' + SudokuPrinter.toStringRectangleSudoku(sudoku,
|
||||
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getBlockWidth(),
|
||||
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getSize() / sudoku.getBlockWidth()));
|
||||
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getSize() / sudoku.getBlockWidth(),
|
||||
Symbols.Numbers));
|
||||
|
||||
if (doku.isSolved()) {
|
||||
return true;
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.util.Random;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import gui.Symbols;
|
||||
import sudoku.io.SudokuPrinter;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
@@ -32,26 +33,36 @@ public class MixedSolver implements Solver{
|
||||
'\n' + SudokuPrinter.toStringRectangleSudoku(
|
||||
sudoku,
|
||||
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getBlockWidth(),
|
||||
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getSize() / sudoku.getBlockWidth()));
|
||||
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getSize() / sudoku.getBlockWidth(),
|
||||
Symbols.Numbers));
|
||||
|
||||
if (doku.isSolved()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Cell cellToFill = doku.getFirstEmptyCell();
|
||||
if (cellToFill == null) {
|
||||
List<Cell> cellsToFill = doku.getEmptyCells();
|
||||
if (cellsToFill.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
List<Integer> possibleSymbols = cellToFill.getPossibleSymbols();
|
||||
// Règles de déduction
|
||||
for (Cell cellToFill : cellsToFill) {
|
||||
|
||||
if (possibleSymbols.size() == 1) {
|
||||
cellToFill.setSymbolIndex(possibleSymbols.getFirst());
|
||||
if (this.solve(doku)) {
|
||||
return true;
|
||||
List<Integer> possibleSymbols = cellToFill.getPossibleSymbols();
|
||||
if (possibleSymbols.size() != 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
cellToFill.setSymbolIndex(possibleSymbols.getFirst());
|
||||
|
||||
return this.solve(doku);
|
||||
}
|
||||
|
||||
// Si ça ne marche pas
|
||||
// On fait du backtracking
|
||||
Cell cellToFill = doku.getRandomEmptyCell(rand);
|
||||
List<Integer> possibleSymbols = cellToFill.getPossibleSymbols();
|
||||
|
||||
while (!possibleSymbols.isEmpty()) {
|
||||
int nextPossibleSymbolIndex = rand.nextInt(possibleSymbols.size());
|
||||
int nextSymbol = possibleSymbols.get(nextPossibleSymbolIndex);
|
||||
@@ -60,9 +71,9 @@ public class MixedSolver implements Solver{
|
||||
if (this.solve(doku)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
cellToFill.setSymbolIndex(Cell.NOSYMBOL);
|
||||
possibleSymbols.remove(nextPossibleSymbolIndex);
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.util.Random;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import gui.Symbols;
|
||||
import sudoku.io.SudokuPrinter;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
@@ -32,7 +33,8 @@ public class RandomSolver implements Solver {
|
||||
logger.log(Level.FINE,
|
||||
'\n' + SudokuPrinter.toStringRectangleSudoku(sudoku,
|
||||
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getBlockWidth(),
|
||||
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getSize() / sudoku.getBlockWidth()));
|
||||
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getSize() / sudoku.getBlockWidth(),
|
||||
Symbols.Numbers));
|
||||
|
||||
if (doku.isSolved()) {
|
||||
return true;
|
||||
|
||||
@@ -126,8 +126,6 @@ public class Cell {
|
||||
}
|
||||
|
||||
public boolean trySetValue(int newValue) {
|
||||
if (!isMutable())
|
||||
return false;
|
||||
if (!canHaveValue(newValue))
|
||||
return false;
|
||||
setSymbolIndex(newValue);
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
package sudoku.structure;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
|
||||
import sudoku.io.SudokuSerializer;
|
||||
|
||||
@@ -182,18 +178,4 @@ public class MultiDoku {
|
||||
int randomIndex = rand.nextInt(emptyCells.size());
|
||||
return emptyCells.get(randomIndex);
|
||||
}
|
||||
|
||||
public void clearMutableCells() {
|
||||
for (Sudoku s : getSubGrids()) {
|
||||
for (Cell cell : s.getCells()) {
|
||||
if (cell.isMutable())
|
||||
cell.clearCurrentSymbol();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public MultiDoku clone() {
|
||||
//TODO: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah
|
||||
return SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(this));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,7 +145,7 @@ public class SudokuFactory {
|
||||
cellsThatCanBeEmptied.remove(cellToEmpty);
|
||||
}
|
||||
|
||||
return false;
|
||||
return newDokuFromFilledOne(doku, --nbCellsToEmpty, solver);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -181,22 +181,21 @@ public class SudokuFactory {
|
||||
* @param sudoku2 Sudoku, second sudoku à connecter.
|
||||
* @param offset Coordinate, décalage entre les deux Sudokus.
|
||||
*/
|
||||
private static void linkRectangleSudokus(Sudoku sudoku1, Sudoku sudoku2, Coordinate offset) {
|
||||
private static void linkSquareSudokus(Sudoku sudoku1, Sudoku sudoku2, Coordinate offset) {
|
||||
int blockWidth = sudoku1.getBlockWidth();
|
||||
int blockHeight = sudoku1.getSize() / blockWidth;
|
||||
for (int dx = 0; dx < blockHeight; dx++) {
|
||||
for (int dx = 0; dx < blockWidth; dx++) {
|
||||
for (int dy = 0; dy < blockWidth; dy++) {
|
||||
int block1X = dx + offset.getX();
|
||||
int block1Y = dy + offset.getY();
|
||||
int block2X = dx;
|
||||
int block2Y = dy;
|
||||
|
||||
if ((block1X < blockHeight) && (block1X >= 0) && (block1Y >= 0) && (block1Y < blockWidth)) {
|
||||
Block block1 = sudoku1.getBlocks().get(block1Y * blockHeight + block1X);
|
||||
Block block2 = sudoku2.getBlocks().get(block2Y * blockHeight + block2X);
|
||||
if ((block1X < blockWidth) && (block1X >= 0) && (block1Y >= 0) && (block1Y < blockWidth)) {
|
||||
Block block1 = sudoku1.getBlocks().get(block1Y * blockWidth + block1X);
|
||||
Block block2 = sudoku2.getBlocks().get(block2Y * blockWidth + block2X);
|
||||
|
||||
// on remplace le bloc
|
||||
sudoku2.getBlocks().set(block2Y * blockHeight + block2X, block1);
|
||||
sudoku2.getBlocks().set(block2Y * blockWidth + block2X, block1);
|
||||
block1.getSudokus().add(sudoku2);
|
||||
|
||||
// on remplace les cellules
|
||||
@@ -232,10 +231,10 @@ public class SudokuFactory {
|
||||
Sudoku sudoku4 = createSquareSudoku(size, constraints);
|
||||
Sudoku sudoku5 = createSquareSudoku(size, constraints);
|
||||
|
||||
linkRectangleSudokus(sudoku1, sudoku2, new Coordinate(1 - size, 1 - size));
|
||||
linkRectangleSudokus(sudoku1, sudoku3, new Coordinate(size - 1, 1 - size));
|
||||
linkRectangleSudokus(sudoku1, sudoku4, new Coordinate(1 - size, size - 1));
|
||||
linkRectangleSudokus(sudoku1, sudoku5, new Coordinate(size - 1, size - 1));
|
||||
linkSquareSudokus(sudoku1, sudoku2, new Coordinate(1 - size, 1 - size));
|
||||
linkSquareSudokus(sudoku1, sudoku3, new Coordinate(size - 1, 1 - size));
|
||||
linkSquareSudokus(sudoku1, sudoku4, new Coordinate(1 - size, size - 1));
|
||||
linkSquareSudokus(sudoku1, sudoku5, new Coordinate(size - 1, size - 1));
|
||||
|
||||
return new MultiDoku(Arrays.asList(sudoku1, sudoku2, sudoku3, sudoku4, sudoku5));
|
||||
}
|
||||
@@ -264,10 +263,10 @@ public class SudokuFactory {
|
||||
Sudoku sudoku4 = createRectangleSudoku(width, height, constraints);
|
||||
Sudoku sudoku5 = createRectangleSudoku(width, height, constraints);
|
||||
|
||||
linkRectangleSudokus(sudoku1, sudoku2, new Coordinate(1 - height, 1 - width));
|
||||
linkRectangleSudokus(sudoku1, sudoku3, new Coordinate(height - 1, 1 - width));
|
||||
linkRectangleSudokus(sudoku1, sudoku4, new Coordinate(1 - height, width - 1));
|
||||
linkRectangleSudokus(sudoku1, sudoku5, new Coordinate(height - 1, width - 1));
|
||||
linkSquareSudokus(sudoku1, sudoku2, new Coordinate(1 - width, 1 - height));
|
||||
linkSquareSudokus(sudoku1, sudoku3, new Coordinate(width - 1, 1 - height));
|
||||
linkSquareSudokus(sudoku1, sudoku4, new Coordinate(1 - width, height - 1));
|
||||
linkSquareSudokus(sudoku1, sudoku5, new Coordinate(width - 1, height - 1));
|
||||
|
||||
return new MultiDoku(Arrays.asList(sudoku1, sudoku2, sudoku3, sudoku4, sudoku5));
|
||||
}
|
||||
@@ -277,6 +276,9 @@ public class SudokuFactory {
|
||||
solver.solve(doku);
|
||||
int nbCellsToEmpty = (int) (difficulty.getFactor() * doku.getNbCells());
|
||||
boolean successfull = newDokuFromFilledOne(doku, nbCellsToEmpty, solver);
|
||||
if (!successfull) {
|
||||
throw new Exception("Canno't create this doku with this difficulty");
|
||||
}
|
||||
doku.setFilledCellsImmutable();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user