Compare commits
16 Commits
a160042ef4
...
refactor
| Author | SHA1 | Date | |
|---|---|---|---|
| b87bd5183e | |||
| a20a5387a7 | |||
| a1032335a5 | |||
| 336d8378ae | |||
| c165ecdae5 | |||
| 352aee49e4 | |||
| f22debdf5f | |||
| 02089c649b | |||
| e98199e1ec | |||
| 438252a8ca | |||
| caf6569409 | |||
| 3863c812c8 | |||
| 6d96455ac4 | |||
| a5c046f891 | |||
|
|
b7f9ca8a98 | ||
| f47e4cc309 |
@@ -16,25 +16,14 @@ project.ext.os = System.properties['os.name'].toLowerCase().split(" ")[0]
|
||||
repositories {
|
||||
// Use Maven Central for resolving dependencies.
|
||||
mavenCentral()
|
||||
flatDir {
|
||||
dirs("$rootProject.projectDir/libs")
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// Use JUnit Jupiter for testing.
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.1'
|
||||
|
||||
// This dependency is used by the application.
|
||||
implementation 'com.google.guava:guava:31.1-jre'
|
||||
|
||||
// uml
|
||||
implementation 'com.github.javaparser:javaparser-symbol-solver-core:3.26.2'
|
||||
|
||||
implementation 'org.json:json:20250107'
|
||||
|
||||
implementation 'com.fasterxml.jackson.core:jackson-databind:2.18.2'
|
||||
|
||||
implementation "io.github.spair:imgui-java-app:1.88.0"
|
||||
|
||||
implementation "org.lwjgl:lwjgl-stb:3.3.4"
|
||||
@@ -44,12 +33,19 @@ dependencies {
|
||||
|
||||
application {
|
||||
// Define the main class for the application.
|
||||
mainClass = 'gui.Main'
|
||||
mainClass = 'org.polytech.ryuk.gui.Main'
|
||||
}
|
||||
|
||||
tasks.named('test') {
|
||||
// Use JUnit Platform for unit tests.
|
||||
useJUnitPlatform()
|
||||
// Add libraries into the final jar
|
||||
jar {
|
||||
archiveBaseName = rootProject.getName()
|
||||
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
|
||||
manifest {
|
||||
attributes "Main-Class": application.mainClass
|
||||
}
|
||||
from {
|
||||
configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) }
|
||||
}
|
||||
}
|
||||
|
||||
run {
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
package game;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import sudoku.structure.MultiDoku;
|
||||
|
||||
public class Game {
|
||||
|
||||
public static enum GameState {
|
||||
GameNotStarted, GameGoing, GameEnd
|
||||
}
|
||||
|
||||
private final Map<Integer, Player> players;
|
||||
private GameState gameState;
|
||||
private MultiDoku doku;
|
||||
|
||||
public Game() {
|
||||
this.players = new HashMap<>();
|
||||
this.gameState = GameState.GameNotStarted;
|
||||
}
|
||||
|
||||
public Player getPlayerById(int id) {
|
||||
return players.get(id);
|
||||
}
|
||||
|
||||
public void addPlayer(Player player) {
|
||||
players.put(player.getId(), player);
|
||||
}
|
||||
|
||||
public void removePlayer(int id) {
|
||||
players.remove(id);
|
||||
}
|
||||
|
||||
public Map<Integer, Player> getPlayers() {
|
||||
return players;
|
||||
}
|
||||
|
||||
public void startGame(MultiDoku doku) {
|
||||
this.doku = doku;
|
||||
this.gameState = GameState.GameGoing;
|
||||
}
|
||||
|
||||
public GameState getGameState() {
|
||||
return gameState;
|
||||
}
|
||||
|
||||
public MultiDoku getDoku() {
|
||||
return doku;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package gui.menu;
|
||||
|
||||
import gui.widget.LeaderboardRenderer;
|
||||
import gui.widget.SudokuRenderer;
|
||||
import imgui.ImGui;
|
||||
import network.client.Client;
|
||||
import network.server.Server;
|
||||
import sudoku.structure.Cell;
|
||||
|
||||
public class MultiPlayerDokuView extends BaseView{
|
||||
|
||||
private final Client client;
|
||||
private final Server server;
|
||||
private final SudokuRenderer sudokuRenderer;
|
||||
private final LeaderboardRenderer leaderboardRenderer;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private void onCellChange(Cell cell) {
|
||||
this.client.sendCellChange(cell);
|
||||
}
|
||||
|
||||
public void onDisconnect() {
|
||||
if (server == null) {
|
||||
closeMenu();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
this.leaderboardRenderer.render();
|
||||
this.sudokuRenderer.render();
|
||||
if (ImGui.button("Quitter")) {
|
||||
this.client.stop();
|
||||
this.closeMenu(3);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package gui.widget;
|
||||
|
||||
import game.Game;
|
||||
import game.Player;
|
||||
import imgui.ImGui;
|
||||
|
||||
public class LeaderboardRenderer {
|
||||
|
||||
private final Game game;
|
||||
private final Player currentPlayer;
|
||||
|
||||
public LeaderboardRenderer(Game game, Player player) {
|
||||
this.game = game;
|
||||
this.currentPlayer = player;
|
||||
}
|
||||
|
||||
public void render() {
|
||||
ImGui.text("Leaderboard");
|
||||
for (var entry : game.getPlayers().entrySet()) {
|
||||
Player player = entry.getValue();
|
||||
ImGui.text(player.getPseudo() + " : " + player.getScore());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
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) {
|
||||
packet.accept(this);
|
||||
}
|
||||
|
||||
void visitPacket(ConnexionInfoPacket packet);
|
||||
void visitPacket(DisconnectPacket packet);
|
||||
void visitPacket(KeepAlivePacket packet);
|
||||
void visitPacket(LoginPacket packet);
|
||||
void visitPacket(PlayerJoinPacket packet);
|
||||
void visitPacket(PlayerLeavePacket packet);
|
||||
void visitPacket(StartGamePacket packet);
|
||||
void visitPacket(EndGamePacket packet);
|
||||
void visitPacket(UpdatePlayerScorePacket packet);
|
||||
void visitPacket(ChangeCellPacket packet);
|
||||
|
||||
}
|
||||
@@ -1,26 +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();
|
||||
|
||||
private final int winnerId;
|
||||
|
||||
public EndGamePacket(int winnerId) {
|
||||
this.winnerId = winnerId;
|
||||
}
|
||||
|
||||
public int getWinnerId() {
|
||||
return winnerId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(PacketVisitor packetVisitor) {
|
||||
packetVisitor.visitPacket(this);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package network.protocol.packets;
|
||||
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
import network.protocol.Packets;
|
||||
|
||||
public class StartGamePacket extends Packet {
|
||||
|
||||
static private final long serialVersionUID = Packets.StartGame.ordinal();
|
||||
|
||||
private final String serializedSudoku;
|
||||
|
||||
public StartGamePacket(String serializedSudoku) {
|
||||
this.serializedSudoku = serializedSudoku;
|
||||
}
|
||||
|
||||
public String getSerializedSudoku() {
|
||||
return serializedSudoku;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(PacketVisitor packetVisitor) {
|
||||
packetVisitor.visitPacket(this);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package common;
|
||||
package org.polytech.ryuk.common;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
@@ -1,4 +1,4 @@
|
||||
package common;
|
||||
package org.polytech.ryuk.common;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
86
app/src/main/java/org/polytech/ryuk/game/Game.java
Normal file
86
app/src/main/java/org/polytech/ryuk/game/Game.java
Normal file
@@ -0,0 +1,86 @@
|
||||
package org.polytech.ryuk.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 org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
|
||||
public class Game {
|
||||
|
||||
public static enum GameState {
|
||||
GameNotStarted, GameGoing
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public Player getPlayerById(int id) {
|
||||
return players.get(id);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public Map<Integer, Player> getPlayers() {
|
||||
return players;
|
||||
}
|
||||
|
||||
public void startGame(MultiDoku doku, Instant startTime, long gameDuration) {
|
||||
this.doku = doku;
|
||||
this.gameState = GameState.GameGoing;
|
||||
this.startTime = startTime;
|
||||
this.gameDuration = gameDuration;
|
||||
}
|
||||
|
||||
public void stopGame() {
|
||||
this.gameState = GameState.GameNotStarted;
|
||||
}
|
||||
|
||||
public GameState getGameState() {
|
||||
return gameState;
|
||||
}
|
||||
|
||||
public MultiDoku getDoku() {
|
||||
return doku;
|
||||
}
|
||||
|
||||
public List<Player> getLeaderboard() {
|
||||
return leaderboard;
|
||||
}
|
||||
|
||||
public Instant getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
public long getGameDuration() {
|
||||
return gameDuration;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package game;
|
||||
package org.polytech.ryuk.game;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@@ -16,11 +16,11 @@ public class Player implements Serializable {
|
||||
this.score = 0;
|
||||
}
|
||||
|
||||
public int getScore() {
|
||||
public int getRemainingCells() {
|
||||
return score;
|
||||
}
|
||||
|
||||
public void setScore(int score) {
|
||||
void setRemainingCells(int score) {
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.polytech.ryuk.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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package gui;
|
||||
package org.polytech.ryuk.gui;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -1,4 +1,4 @@
|
||||
package gui;
|
||||
package org.polytech.ryuk.gui;
|
||||
|
||||
import imgui.ImFont;
|
||||
import imgui.ImFontConfig;
|
||||
@@ -1,4 +1,4 @@
|
||||
package gui;
|
||||
package org.polytech.ryuk.gui;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package gui;
|
||||
package org.polytech.ryuk.gui;
|
||||
|
||||
import org.polytech.ryuk.gui.menu.MainMenu;
|
||||
import org.polytech.ryuk.gui.menu.StateMachine;
|
||||
|
||||
import gui.menu.MainMenu;
|
||||
import gui.menu.StateMachine;
|
||||
import imgui.ImGui;
|
||||
import imgui.app.Application;
|
||||
import imgui.app.Configuration;
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package gui;
|
||||
package org.polytech.ryuk.gui;
|
||||
|
||||
public class Options {
|
||||
|
||||
public static Symbols Symboles = Symbols.Numbers;
|
||||
public static float BackgroundSpeed = 1.0f;
|
||||
|
||||
}
|
||||
@@ -1,15 +1,15 @@
|
||||
package gui;
|
||||
package org.polytech.ryuk.gui;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import sudoku.structure.Block;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.Coordinate;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Sudoku;
|
||||
import org.polytech.ryuk.sudoku.structure.Block;
|
||||
import org.polytech.ryuk.sudoku.structure.Cell;
|
||||
import org.polytech.ryuk.sudoku.structure.Coordinate;
|
||||
import org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
import org.polytech.ryuk.sudoku.structure.Sudoku;
|
||||
|
||||
public class RenderableMultidoku {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package gui;
|
||||
package org.polytech.ryuk.gui;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import sudoku.constraint.Constraint;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.SudokuFactory;;
|
||||
import org.polytech.ryuk.sudoku.constraint.Constraint;
|
||||
import org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
import org.polytech.ryuk.sudoku.structure.SudokuFactory;;
|
||||
|
||||
public enum SudokuType {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package gui;
|
||||
package org.polytech.ryuk.gui;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -1,4 +1,4 @@
|
||||
package gui.menu;
|
||||
package org.polytech.ryuk.gui.menu;
|
||||
|
||||
import imgui.ImGui;
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package gui.menu;
|
||||
package org.polytech.ryuk.gui.menu;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import org.polytech.ryuk.network.client.Client;
|
||||
import org.polytech.ryuk.network.server.Server;
|
||||
|
||||
import imgui.ImGui;
|
||||
import network.client.Client;
|
||||
import network.server.Server;
|
||||
|
||||
public class ConnexionStatusView extends BaseView {
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package org.polytech.ryuk.gui.menu;
|
||||
|
||||
import org.polytech.ryuk.game.Player;
|
||||
import org.polytech.ryuk.gui.ColorGenerator;
|
||||
import org.polytech.ryuk.gui.widget.SudokuRenderer;
|
||||
import org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
|
||||
import imgui.ImGui;
|
||||
import imgui.ImVec4;
|
||||
|
||||
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,4 +1,4 @@
|
||||
package gui.menu;
|
||||
package org.polytech.ryuk.gui.menu;
|
||||
|
||||
import imgui.ImGui;
|
||||
import imgui.ImVec2;
|
||||
@@ -1,4 +1,4 @@
|
||||
package gui.menu;
|
||||
package org.polytech.ryuk.gui.menu;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package org.polytech.ryuk.gui.menu;
|
||||
|
||||
import org.polytech.ryuk.game.Player;
|
||||
import org.polytech.ryuk.gui.widget.LeaderboardRenderer;
|
||||
import org.polytech.ryuk.gui.widget.MultiPlayerCompleteProgress;
|
||||
import org.polytech.ryuk.gui.widget.SudokuRenderer;
|
||||
import org.polytech.ryuk.gui.widget.TimerRenderer;
|
||||
import org.polytech.ryuk.network.client.Client;
|
||||
import org.polytech.ryuk.network.server.Server;
|
||||
import org.polytech.ryuk.sudoku.solver.BacktrackingSolver;
|
||||
import org.polytech.ryuk.sudoku.solver.Solver;
|
||||
import org.polytech.ryuk.sudoku.structure.Cell;
|
||||
import org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
|
||||
import imgui.ImGui;
|
||||
|
||||
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() {
|
||||
if (server == null) {
|
||||
closeMenu();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
this.timerRenderer.render();
|
||||
this.leaderboardRenderer.render();
|
||||
this.completeProgress.render();
|
||||
this.sudokuRenderer.render();
|
||||
if (ImGui.button("Quitter")) {
|
||||
this.client.stop();
|
||||
this.closeMenu(3);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
package gui.menu;
|
||||
package org.polytech.ryuk.gui.menu;
|
||||
|
||||
import org.polytech.ryuk.game.Player;
|
||||
import org.polytech.ryuk.gui.widget.SudokuSelector;
|
||||
import org.polytech.ryuk.network.client.Client;
|
||||
import org.polytech.ryuk.network.server.Server;
|
||||
import org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
|
||||
import game.Player;
|
||||
import gui.widget.SudokuSelector;
|
||||
import imgui.ImGui;
|
||||
import network.client.Client;
|
||||
import network.server.Server;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import imgui.type.ImInt;
|
||||
|
||||
public class MultiPlayerView extends BaseView {
|
||||
|
||||
@@ -14,6 +16,8 @@ public class MultiPlayerView extends BaseView {
|
||||
|
||||
private final SudokuSelector selector;
|
||||
|
||||
private ImInt gameDurationMinutes = new ImInt(10);
|
||||
|
||||
private MultiDoku doku = null;
|
||||
|
||||
public MultiPlayerView(StateMachine stateMachine, Client client, Server server) {
|
||||
@@ -45,25 +49,32 @@ public class MultiPlayerView extends BaseView {
|
||||
if (this.server == null) {
|
||||
ImGui.text("En attente de l'administrateur du serveur ...");
|
||||
} else {
|
||||
if (this.doku == null)
|
||||
ImGui.beginDisabled();
|
||||
renderTimer();
|
||||
ImGui.beginDisabled(this.doku == null);
|
||||
if (ImGui.button("Démarrer")) {
|
||||
this.server.startGame(this.doku);
|
||||
this.server.startGame(this.doku, this.gameDurationMinutes.get() * 60);
|
||||
}
|
||||
if (this.doku == null)
|
||||
ImGui.endDisabled();
|
||||
selector.render();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
private void renderPlayers() {
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
package gui.menu;
|
||||
package org.polytech.ryuk.gui.menu;
|
||||
|
||||
import org.polytech.ryuk.gui.Options;
|
||||
import org.polytech.ryuk.gui.Symbols;
|
||||
|
||||
import gui.Options;
|
||||
import gui.Symbols;
|
||||
import imgui.ImGui;
|
||||
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);
|
||||
@@ -19,6 +21,9 @@ 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,9 @@
|
||||
package gui.menu;
|
||||
package org.polytech.ryuk.gui.menu;
|
||||
|
||||
import org.polytech.ryuk.gui.widget.SudokuSelector;
|
||||
import org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
|
||||
import gui.widget.SudokuSelector;
|
||||
import imgui.ImGui;
|
||||
import sudoku.structure.MultiDoku;
|
||||
|
||||
public class SoloMenu extends BaseView {
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package gui.menu;
|
||||
package org.polytech.ryuk.gui.menu;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
import gui.Images;
|
||||
import org.polytech.ryuk.gui.AnimatedBackground;
|
||||
|
||||
import imgui.ImGui;
|
||||
import imgui.ImVec2;
|
||||
import imgui.flag.ImGuiKey;
|
||||
@@ -11,9 +12,11 @@ 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() {
|
||||
@@ -27,6 +30,11 @@ 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();
|
||||
@@ -40,12 +48,7 @@ public class StateMachine {
|
||||
|
||||
public void render() {
|
||||
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(0, 0));
|
||||
ImGui.end();
|
||||
this.background.render();
|
||||
ImGui.setNextWindowPos(new ImVec2(0.0f, 0.0f));
|
||||
ImGui.setNextWindowSize(displaySize);
|
||||
ImGui.begin("##Main Window", null, ImGuiWindowFlags.NoDecoration | ImGuiWindowFlags.NoMove
|
||||
@@ -1,16 +1,17 @@
|
||||
package gui.menu;
|
||||
package org.polytech.ryuk.gui.menu;
|
||||
|
||||
import java.util.concurrent.CancellationException;
|
||||
|
||||
import gui.widget.SudokuRenderer;
|
||||
import org.polytech.ryuk.gui.widget.SudokuRenderer;
|
||||
import org.polytech.ryuk.sudoku.io.SudokuSerializer;
|
||||
import org.polytech.ryuk.sudoku.solver.BacktrackingSolver;
|
||||
import org.polytech.ryuk.sudoku.solver.HumanSolver;
|
||||
import org.polytech.ryuk.sudoku.solver.MixedSolver;
|
||||
import org.polytech.ryuk.sudoku.solver.Solver;
|
||||
import org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
|
||||
import imgui.ImGui;
|
||||
import imgui.ImGuiStyle;
|
||||
import sudoku.io.SudokuSerializer;
|
||||
import sudoku.solver.BacktrackingSolver;
|
||||
import sudoku.solver.HumanSolver;
|
||||
import sudoku.solver.MixedSolver;
|
||||
import sudoku.solver.Solver;
|
||||
import sudoku.structure.MultiDoku;
|
||||
|
||||
public class SudokuView extends BaseView {
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package org.polytech.ryuk.gui.widget;
|
||||
|
||||
import org.polytech.ryuk.game.Game;
|
||||
import org.polytech.ryuk.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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.polytech.ryuk.gui.widget;
|
||||
|
||||
import org.polytech.ryuk.game.Game;
|
||||
import org.polytech.ryuk.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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.polytech.ryuk.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,4 +1,4 @@
|
||||
package gui.widget;
|
||||
package org.polytech.ryuk.gui.widget;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
@@ -6,24 +6,25 @@ 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 org.polytech.ryuk.common.ConsumerSignal;
|
||||
import org.polytech.ryuk.common.Signal;
|
||||
import org.polytech.ryuk.gui.ColorGenerator;
|
||||
import org.polytech.ryuk.gui.ColorGenerator.Color;
|
||||
import org.polytech.ryuk.gui.Fonts;
|
||||
import org.polytech.ryuk.gui.Options;
|
||||
import org.polytech.ryuk.gui.RenderableMultidoku;
|
||||
import org.polytech.ryuk.gui.Symbols;
|
||||
import org.polytech.ryuk.sudoku.constraint.Constraint;
|
||||
import org.polytech.ryuk.sudoku.structure.Block;
|
||||
import org.polytech.ryuk.sudoku.structure.Cell;
|
||||
import org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
import org.polytech.ryuk.sudoku.structure.Sudoku;
|
||||
|
||||
import imgui.ImGui;
|
||||
import imgui.ImVec2;
|
||||
import imgui.ImVec4;
|
||||
import imgui.flag.ImGuiCol;
|
||||
import imgui.flag.ImGuiStyleVar;
|
||||
import sudoku.constraint.Constraint;
|
||||
import sudoku.structure.Block;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Sudoku;
|
||||
|
||||
public class SudokuRenderer {
|
||||
|
||||
@@ -107,7 +108,7 @@ public class SudokuRenderer {
|
||||
if (offsetX > 0) {
|
||||
ImGui.setCursorPosX(offsetX);
|
||||
}
|
||||
ImGui.beginChild(1, new ImVec2(cellSize.x * doku.getWidth(), cellSize.y * doku.getHeight()));
|
||||
ImGui.beginChild("sudokuChild", 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,19 +1,20 @@
|
||||
package gui.widget;
|
||||
package org.polytech.ryuk.gui.widget;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import common.ConsumerSignal;
|
||||
import gui.SudokuType;
|
||||
import org.polytech.ryuk.common.ConsumerSignal;
|
||||
import org.polytech.ryuk.gui.SudokuType;
|
||||
import org.polytech.ryuk.sudoku.constraint.Constraint;
|
||||
import org.polytech.ryuk.sudoku.structure.Difficulty;
|
||||
import org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
import org.polytech.ryuk.sudoku.structure.SudokuFactory;
|
||||
|
||||
import imgui.ImGui;
|
||||
import imgui.extension.imguifiledialog.ImGuiFileDialog;
|
||||
import imgui.extension.imguifiledialog.flag.ImGuiFileDialogFlags;
|
||||
import imgui.type.ImBoolean;
|
||||
import imgui.type.ImInt;
|
||||
import sudoku.constraint.Constraint;
|
||||
import sudoku.structure.Difficulty;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.SudokuFactory;
|
||||
|
||||
public class SudokuSelector {
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.polytech.ryuk.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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
package network;
|
||||
package org.polytech.ryuk.network;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
import org.polytech.ryuk.network.protocol.Packet;
|
||||
import org.polytech.ryuk.network.protocol.PacketVisitor;
|
||||
|
||||
public abstract class Connexion implements PacketVisitor {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package network;
|
||||
package org.polytech.ryuk.network;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
|
||||
import network.protocol.Packet;
|
||||
import org.polytech.ryuk.network.protocol.Packet;
|
||||
|
||||
public class ConnexionThread extends Thread {
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
package network.client;
|
||||
package org.polytech.ryuk.network.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Random;
|
||||
|
||||
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;
|
||||
import org.polytech.ryuk.common.ConsumerSignal;
|
||||
import org.polytech.ryuk.common.Signal;
|
||||
import org.polytech.ryuk.game.Game;
|
||||
import org.polytech.ryuk.game.Player;
|
||||
import org.polytech.ryuk.network.protocol.packets.ChangeCellPacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.LoginPacket;
|
||||
import org.polytech.ryuk.sudoku.structure.Cell;
|
||||
import org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
import org.polytech.ryuk.sudoku.structure.Sudoku;
|
||||
|
||||
public class Client {
|
||||
private final ClientConnexion clientConnection;
|
||||
@@ -21,6 +22,7 @@ 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;
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
package network.client;
|
||||
package org.polytech.ryuk.network.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
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;
|
||||
import org.polytech.ryuk.game.Player;
|
||||
import org.polytech.ryuk.network.Connexion;
|
||||
import org.polytech.ryuk.network.protocol.packets.ChangeCellPacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.ConnexionInfoPacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.DisconnectPacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.EndGamePacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.KeepAlivePacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.LoginPacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.PlayerJoinPacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.PlayerLeavePacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.StartGamePacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.UpdatePlayerScorePacket;
|
||||
import org.polytech.ryuk.sudoku.io.SudokuSerializer;
|
||||
|
||||
public class ClientConnexion extends Connexion {
|
||||
|
||||
@@ -71,22 +71,23 @@ public class ClientConnexion extends Connexion {
|
||||
|
||||
@Override
|
||||
public void visitPacket(StartGamePacket packet) {
|
||||
this.client.getGame().startGame(SudokuSerializer.deserializeSudoku(packet.getSerializedSudoku()));
|
||||
this.client.getGame().startGame(SudokuSerializer.deserializeSudoku(packet.getSerializedSudoku()),
|
||||
packet.getInstant(), packet.getGameDuration());
|
||||
this.client.onGameStarted.emit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(EndGamePacket packet) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
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);
|
||||
player.setScore(packet.getCellsLeft());
|
||||
System.out.println("Score for " + player.getPseudo() + " : " + packet.getCellsLeft());
|
||||
assert (player != null);
|
||||
this.client.getGame().setPlayerRemainingCells(player, packet.getCellsLeft());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1,4 +1,4 @@
|
||||
package network.protocol;
|
||||
package org.polytech.ryuk.network.protocol;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.polytech.ryuk.network.protocol;
|
||||
|
||||
import org.polytech.ryuk.network.protocol.packets.ChangeCellPacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.ConnexionInfoPacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.DisconnectPacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.EndGamePacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.KeepAlivePacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.LoginPacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.PlayerJoinPacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.PlayerLeavePacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.StartGamePacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.UpdatePlayerScorePacket;
|
||||
|
||||
public interface PacketVisitor {
|
||||
|
||||
default void visit(Packet packet) {
|
||||
packet.accept(this);
|
||||
}
|
||||
|
||||
void visitPacket(ConnexionInfoPacket packet);
|
||||
void visitPacket(DisconnectPacket packet);
|
||||
void visitPacket(KeepAlivePacket packet);
|
||||
void visitPacket(LoginPacket packet);
|
||||
void visitPacket(PlayerJoinPacket packet);
|
||||
void visitPacket(PlayerLeavePacket packet);
|
||||
void visitPacket(StartGamePacket packet);
|
||||
void visitPacket(EndGamePacket packet);
|
||||
void visitPacket(UpdatePlayerScorePacket packet);
|
||||
void visitPacket(ChangeCellPacket packet);
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package network.protocol;
|
||||
package org.polytech.ryuk.network.protocol;
|
||||
|
||||
public enum Packets {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package network.protocol.packets;
|
||||
package org.polytech.ryuk.network.protocol.packets;
|
||||
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
import network.protocol.Packets;
|
||||
import org.polytech.ryuk.network.protocol.Packet;
|
||||
import org.polytech.ryuk.network.protocol.PacketVisitor;
|
||||
import org.polytech.ryuk.network.protocol.Packets;
|
||||
|
||||
public class ChangeCellPacket extends Packet {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package network.protocol.packets;
|
||||
package org.polytech.ryuk.network.protocol.packets;
|
||||
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
import network.protocol.Packets;
|
||||
import org.polytech.ryuk.network.protocol.Packet;
|
||||
import org.polytech.ryuk.network.protocol.PacketVisitor;
|
||||
import org.polytech.ryuk.network.protocol.Packets;
|
||||
|
||||
public class ConnexionInfoPacket extends Packet {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package network.protocol.packets;
|
||||
package org.polytech.ryuk.network.protocol.packets;
|
||||
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
import network.protocol.Packets;
|
||||
import org.polytech.ryuk.network.protocol.Packet;
|
||||
import org.polytech.ryuk.network.protocol.PacketVisitor;
|
||||
import org.polytech.ryuk.network.protocol.Packets;
|
||||
|
||||
public class DisconnectPacket extends Packet {
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.polytech.ryuk.network.protocol.packets;
|
||||
|
||||
import org.polytech.ryuk.network.protocol.Packet;
|
||||
import org.polytech.ryuk.network.protocol.PacketVisitor;
|
||||
import org.polytech.ryuk.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,8 +1,8 @@
|
||||
package network.protocol.packets;
|
||||
package org.polytech.ryuk.network.protocol.packets;
|
||||
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
import network.protocol.Packets;
|
||||
import org.polytech.ryuk.network.protocol.Packet;
|
||||
import org.polytech.ryuk.network.protocol.PacketVisitor;
|
||||
import org.polytech.ryuk.network.protocol.Packets;
|
||||
|
||||
public class KeepAlivePacket extends Packet {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package network.protocol.packets;
|
||||
package org.polytech.ryuk.network.protocol.packets;
|
||||
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
import network.protocol.Packets;
|
||||
import org.polytech.ryuk.network.protocol.Packet;
|
||||
import org.polytech.ryuk.network.protocol.PacketVisitor;
|
||||
import org.polytech.ryuk.network.protocol.Packets;
|
||||
|
||||
public class LoginPacket extends Packet {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package network.protocol.packets;
|
||||
package org.polytech.ryuk.network.protocol.packets;
|
||||
|
||||
import game.Player;
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
import network.protocol.Packets;
|
||||
import org.polytech.ryuk.game.Player;
|
||||
import org.polytech.ryuk.network.protocol.Packet;
|
||||
import org.polytech.ryuk.network.protocol.PacketVisitor;
|
||||
import org.polytech.ryuk.network.protocol.Packets;
|
||||
|
||||
public class PlayerJoinPacket extends Packet{
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package network.protocol.packets;
|
||||
package org.polytech.ryuk.network.protocol.packets;
|
||||
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
import network.protocol.Packets;
|
||||
import org.polytech.ryuk.network.protocol.Packet;
|
||||
import org.polytech.ryuk.network.protocol.PacketVisitor;
|
||||
import org.polytech.ryuk.network.protocol.Packets;
|
||||
|
||||
public class PlayerLeavePacket extends Packet{
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.polytech.ryuk.network.protocol.packets;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
import org.polytech.ryuk.network.protocol.Packet;
|
||||
import org.polytech.ryuk.network.protocol.PacketVisitor;
|
||||
import org.polytech.ryuk.network.protocol.Packets;
|
||||
|
||||
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) {
|
||||
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,8 +1,8 @@
|
||||
package network.protocol.packets;
|
||||
package org.polytech.ryuk.network.protocol.packets;
|
||||
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
import network.protocol.Packets;
|
||||
import org.polytech.ryuk.network.protocol.Packet;
|
||||
import org.polytech.ryuk.network.protocol.PacketVisitor;
|
||||
import org.polytech.ryuk.network.protocol.Packets;
|
||||
|
||||
public class UpdatePlayerScorePacket extends Packet {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package network.server;
|
||||
package org.polytech.ryuk.network.server;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import network.protocol.packets.KeepAlivePacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.KeepAlivePacket;
|
||||
|
||||
public class KeepAliveHandler {
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
package network.server;
|
||||
package org.polytech.ryuk.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 network.protocol.Packet;
|
||||
import network.protocol.packets.StartGamePacket;
|
||||
import sudoku.io.SudokuSerializer;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import org.polytech.ryuk.game.Game;
|
||||
import org.polytech.ryuk.game.Game.GameState;
|
||||
import org.polytech.ryuk.game.Player;
|
||||
import org.polytech.ryuk.network.protocol.Packet;
|
||||
import org.polytech.ryuk.network.protocol.packets.EndGamePacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.StartGamePacket;
|
||||
import org.polytech.ryuk.sudoku.io.SudokuSerializer;
|
||||
import org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
|
||||
public class Server {
|
||||
|
||||
@@ -37,7 +40,16 @@ public class Server {
|
||||
}
|
||||
}
|
||||
|
||||
public void update() {
|
||||
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() {
|
||||
for (var it = connexions.iterator(); it.hasNext();) {
|
||||
ServerConnexion connexion = it.next();
|
||||
if (!connexion.update()) {
|
||||
@@ -48,6 +60,11 @@ public class Server {
|
||||
}
|
||||
}
|
||||
|
||||
public void update() {
|
||||
checkTimer();
|
||||
checkConnexions();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
this.acceptThread.cancel();
|
||||
this.logicThread.cancel();
|
||||
@@ -68,12 +85,19 @@ public class Server {
|
||||
return game;
|
||||
}
|
||||
|
||||
public void startGame(MultiDoku doku) {
|
||||
this.game.startGame(doku);
|
||||
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()));
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package network.server;
|
||||
package org.polytech.ryuk.network.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
@@ -1,24 +1,25 @@
|
||||
package network.server;
|
||||
package org.polytech.ryuk.network.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
|
||||
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;
|
||||
import org.polytech.ryuk.game.Game;
|
||||
import org.polytech.ryuk.game.Game.GameState;
|
||||
import org.polytech.ryuk.game.Player;
|
||||
import org.polytech.ryuk.network.Connexion;
|
||||
import org.polytech.ryuk.network.protocol.packets.ChangeCellPacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.ConnexionInfoPacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.DisconnectPacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.EndGamePacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.KeepAlivePacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.LoginPacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.PlayerJoinPacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.PlayerLeavePacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.StartGamePacket;
|
||||
import org.polytech.ryuk.network.protocol.packets.UpdatePlayerScorePacket;
|
||||
import org.polytech.ryuk.sudoku.io.SudokuSerializer;
|
||||
import org.polytech.ryuk.sudoku.structure.Cell;
|
||||
import org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
|
||||
public class ServerConnexion extends Connexion {
|
||||
|
||||
@@ -62,17 +63,20 @@ public class ServerConnexion extends Connexion {
|
||||
for (Player p : this.server.getGame().getPlayers().values()) {
|
||||
if (p.getId() != player.getId()) {
|
||||
sendPacket(new PlayerJoinPacket(p));
|
||||
sendPacket(new UpdatePlayerScorePacket(p.getId(), p.getScore()));
|
||||
sendPacket(new UpdatePlayerScorePacket(p.getId(), p.getRemainingCells()));
|
||||
}
|
||||
}
|
||||
|
||||
this.server.broadcastPacket(new PlayerJoinPacket(player));
|
||||
sendPacket(new ConnexionInfoPacket(player.getId()));
|
||||
|
||||
if (this.server.getGame().getGameState() == GameState.GameGoing) {
|
||||
setSudoku(this.server.getGame().getDoku().clone());
|
||||
Game game = this.server.getGame();
|
||||
|
||||
if (game.getGameState() == GameState.GameGoing) {
|
||||
setSudoku(game.getDoku().clone());
|
||||
sendPacket(
|
||||
new StartGamePacket(SudokuSerializer.serializeSudoku(this.server.getGame().getDoku()).toString()));
|
||||
new StartGamePacket(SudokuSerializer.serializeSudoku(game.getDoku()).toString(),
|
||||
game.getStartTime(), game.getGameDuration()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,22 +139,29 @@ public class ServerConnexion extends Connexion {
|
||||
}
|
||||
if (cell.getSymbolIndex() != Cell.NOSYMBOL && packet.getNewValue() == Cell.NOSYMBOL) {
|
||||
cell.empty();
|
||||
player.setScore(player.getScore() + 1);
|
||||
this.server.broadcastPacket(new UpdatePlayerScorePacket(player.getId(), player.getScore()));
|
||||
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())) {
|
||||
player.setScore(player.getScore() - 1);
|
||||
this.server.broadcastPacket(new UpdatePlayerScorePacket(player.getId(), player.getScore()));
|
||||
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);
|
||||
player.setScore(this.doku.getEmptyCells().size());
|
||||
this.server.broadcastPacket(new UpdatePlayerScorePacket(player.getId(), player.getScore()));
|
||||
this.server.getGame().setPlayerRemainingCells(player, this.doku.getEmptyCells().size());
|
||||
this.server.broadcastPacket(new UpdatePlayerScorePacket(player.getId(), player.getRemainingCells()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package network.server;
|
||||
package org.polytech.ryuk.network.server;
|
||||
|
||||
public class ServerLogicThread extends Thread {
|
||||
|
||||
@@ -19,7 +19,7 @@ public class ServerLogicThread extends Thread {
|
||||
try {
|
||||
Thread.sleep(50);
|
||||
} catch (InterruptedException e) {
|
||||
// e.printStackTrace();
|
||||
e.printStackTrace();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
/*
|
||||
* This Java source file was generated by the Gradle 'init' task.
|
||||
*/
|
||||
package sudoku;
|
||||
package org.polytech.ryuk.sudoku;
|
||||
|
||||
import sudoku.io.ConsoleInterface;
|
||||
import org.polytech.ryuk.sudoku.io.ConsoleInterface;
|
||||
|
||||
public class Main {
|
||||
public String getGreeting() {
|
||||
@@ -1,7 +1,7 @@
|
||||
package sudoku.constraint;
|
||||
package org.polytech.ryuk.sudoku.constraint;
|
||||
|
||||
import sudoku.structure.Block;
|
||||
import sudoku.structure.Sudoku;
|
||||
import org.polytech.ryuk.sudoku.structure.Block;
|
||||
import org.polytech.ryuk.sudoku.structure.Sudoku;
|
||||
|
||||
public class BlockConstraint implements IConstraint{
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package sudoku.constraint;
|
||||
package org.polytech.ryuk.sudoku.constraint;
|
||||
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.Sudoku;
|
||||
import org.polytech.ryuk.sudoku.structure.Cell;
|
||||
import org.polytech.ryuk.sudoku.structure.Sudoku;
|
||||
|
||||
public class ColumnConstraint implements IConstraint {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package sudoku.constraint;
|
||||
package org.polytech.ryuk.sudoku.constraint;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import sudoku.structure.Sudoku;
|
||||
import org.polytech.ryuk.sudoku.structure.Sudoku;
|
||||
|
||||
public enum Constraint {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package sudoku.constraint;
|
||||
package org.polytech.ryuk.sudoku.constraint;
|
||||
|
||||
import sudoku.structure.Sudoku;
|
||||
import org.polytech.ryuk.sudoku.structure.Sudoku;
|
||||
|
||||
public class DiagonalConstraint implements IConstraint {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package sudoku.constraint;
|
||||
package org.polytech.ryuk.sudoku.constraint;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import sudoku.structure.Sudoku;
|
||||
import org.polytech.ryuk.sudoku.structure.Sudoku;
|
||||
|
||||
public interface IConstraint {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package sudoku.constraint;
|
||||
package org.polytech.ryuk.sudoku.constraint;
|
||||
|
||||
import sudoku.structure.Sudoku;
|
||||
import org.polytech.ryuk.sudoku.structure.Sudoku;
|
||||
|
||||
public class LineConstraint implements IConstraint {
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
package sudoku.io;
|
||||
package org.polytech.ryuk.sudoku.io;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import sudoku.constraint.Constraint;
|
||||
import sudoku.solver.RandomSolver;
|
||||
import sudoku.structure.Difficulty;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Sudoku;
|
||||
import sudoku.structure.SudokuFactory;
|
||||
import org.polytech.ryuk.sudoku.constraint.Constraint;
|
||||
import org.polytech.ryuk.sudoku.solver.RandomSolver;
|
||||
import org.polytech.ryuk.sudoku.structure.Difficulty;
|
||||
import org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
import org.polytech.ryuk.sudoku.structure.Sudoku;
|
||||
import org.polytech.ryuk.sudoku.structure.SudokuFactory;
|
||||
|
||||
public class ConsoleInterface {
|
||||
public Scanner reader = new Scanner(System.in);
|
||||
@@ -0,0 +1,7 @@
|
||||
package org.polytech.ryuk.sudoku.io;
|
||||
|
||||
public class SudokuFile {
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package sudoku.io;
|
||||
package org.polytech.ryuk.sudoku.io;
|
||||
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Sudoku;
|
||||
import org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
import org.polytech.ryuk.sudoku.structure.Sudoku;
|
||||
|
||||
public class SudokuPrinter {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package sudoku.io;
|
||||
package org.polytech.ryuk.sudoku.io;
|
||||
|
||||
public class SudokuSave {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package sudoku.io;
|
||||
package org.polytech.ryuk.sudoku.io;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
@@ -10,12 +10,11 @@ import java.util.List;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import sudoku.constraint.Constraint;
|
||||
import sudoku.structure.Block;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Sudoku;
|
||||
import org.polytech.ryuk.sudoku.constraint.Constraint;
|
||||
import org.polytech.ryuk.sudoku.structure.Block;
|
||||
import org.polytech.ryuk.sudoku.structure.Cell;
|
||||
import org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
import org.polytech.ryuk.sudoku.structure.Sudoku;
|
||||
|
||||
public class SudokuSerializer {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package sudoku.solver;
|
||||
package org.polytech.ryuk.sudoku.solver;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CancellationException;
|
||||
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import org.polytech.ryuk.sudoku.structure.Cell;
|
||||
import org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
|
||||
public class BacktrackingSolver implements Solver {
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package sudoku.solver;
|
||||
package org.polytech.ryuk.sudoku.solver;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import sudoku.io.SudokuPrinter;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Sudoku;
|
||||
import org.polytech.ryuk.sudoku.io.SudokuPrinter;
|
||||
import org.polytech.ryuk.sudoku.structure.Cell;
|
||||
import org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
import org.polytech.ryuk.sudoku.structure.Sudoku;
|
||||
|
||||
public class HumanSolver implements Solver {
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
package sudoku.solver;
|
||||
package org.polytech.ryuk.sudoku.solver;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import sudoku.io.SudokuPrinter;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Sudoku;
|
||||
import org.polytech.ryuk.sudoku.io.SudokuPrinter;
|
||||
import org.polytech.ryuk.sudoku.structure.Cell;
|
||||
import org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
import org.polytech.ryuk.sudoku.structure.Sudoku;
|
||||
|
||||
public class MixedSolver implements Solver{
|
||||
|
||||
@@ -17,8 +17,6 @@ public class MixedSolver implements Solver{
|
||||
* backtracking.
|
||||
*
|
||||
* @param doku MultiDoku, MultiDoku à résoudre.
|
||||
* @param rand Random, pour tester aléatoirement les symboles, lors du
|
||||
* backtracking.
|
||||
* @return boolean, valant true si le MultiDoku est résolu, false sinon.
|
||||
*/
|
||||
@Override
|
||||
@@ -40,28 +38,19 @@ public class MixedSolver implements Solver{
|
||||
return true;
|
||||
}
|
||||
|
||||
List<Cell> cellsToFill = doku.getEmptyCells();
|
||||
if (cellsToFill.isEmpty()) {
|
||||
Cell cellToFill = doku.getFirstEmptyCell();
|
||||
if (cellToFill == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Règles de déduction
|
||||
for (Cell cellToFill : cellsToFill) {
|
||||
|
||||
List<Integer> possibleSymbols = cellToFill.getPossibleSymbols();
|
||||
if (possibleSymbols.size() != 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (possibleSymbols.size() == 1) {
|
||||
cellToFill.setSymbolIndex(possibleSymbols.getFirst());
|
||||
|
||||
return this.solve(doku);
|
||||
if (this.solve(doku)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// 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());
|
||||
@@ -71,9 +60,9 @@ public class MixedSolver implements Solver{
|
||||
if (this.solve(doku)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
cellToFill.setSymbolIndex(Cell.NOSYMBOL);
|
||||
possibleSymbols.remove(nextPossibleSymbolIndex);
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -1,14 +1,14 @@
|
||||
package sudoku.solver;
|
||||
package org.polytech.ryuk.sudoku.solver;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import sudoku.io.SudokuPrinter;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Sudoku;
|
||||
import org.polytech.ryuk.sudoku.io.SudokuPrinter;
|
||||
import org.polytech.ryuk.sudoku.structure.Cell;
|
||||
import org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
import org.polytech.ryuk.sudoku.structure.Sudoku;
|
||||
|
||||
public class RandomSolver implements Solver {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package sudoku.solver;
|
||||
package org.polytech.ryuk.sudoku.solver;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import org.polytech.ryuk.sudoku.structure.Cell;
|
||||
import org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
|
||||
public interface Solver {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package sudoku.solver;
|
||||
package org.polytech.ryuk.sudoku.solver;
|
||||
|
||||
import java.util.concurrent.CancellationException;
|
||||
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Sudoku;
|
||||
import org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
import org.polytech.ryuk.sudoku.structure.Sudoku;
|
||||
|
||||
/**
|
||||
* Class de test non utilisé
|
||||
@@ -1,4 +1,4 @@
|
||||
package sudoku.structure;
|
||||
package org.polytech.ryuk.sudoku.structure;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -1,4 +1,4 @@
|
||||
package sudoku.structure;
|
||||
package org.polytech.ryuk.sudoku.structure;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -1,4 +1,4 @@
|
||||
package sudoku.structure;
|
||||
package org.polytech.ryuk.sudoku.structure;
|
||||
|
||||
/**
|
||||
* Représente les coordonnées d'une Cell
|
||||
@@ -1,4 +1,4 @@
|
||||
package sudoku.structure;
|
||||
package org.polytech.ryuk.sudoku.structure;
|
||||
|
||||
//TODO: melvyn va passer par là
|
||||
public enum Difficulty {
|
||||
@@ -1,4 +1,4 @@
|
||||
package sudoku.structure;
|
||||
package org.polytech.ryuk.sudoku.structure;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
@@ -6,7 +6,7 @@ import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
|
||||
import sudoku.io.SudokuSerializer;
|
||||
import org.polytech.ryuk.sudoku.io.SudokuSerializer;
|
||||
|
||||
/**
|
||||
* @class MultiDoku
|
||||
@@ -183,6 +183,15 @@ public class MultiDoku {
|
||||
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));
|
||||
@@ -1,4 +1,4 @@
|
||||
package sudoku.structure;
|
||||
package org.polytech.ryuk.sudoku.structure;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -1,13 +1,10 @@
|
||||
package sudoku.structure;
|
||||
|
||||
import sudoku.constraint.BlockConstraint;
|
||||
import sudoku.constraint.Constraint;
|
||||
import sudoku.constraint.IConstraint;
|
||||
import sudoku.io.SudokuPrinter;
|
||||
package org.polytech.ryuk.sudoku.structure;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.polytech.ryuk.sudoku.constraint.Constraint;
|
||||
|
||||
/**
|
||||
* @class Sudoku
|
||||
* @brief Représent un Sudoku
|
||||
@@ -1,4 +1,4 @@
|
||||
package sudoku.structure;
|
||||
package org.polytech.ryuk.sudoku.structure;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
@@ -9,10 +9,10 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import sudoku.constraint.Constraint;
|
||||
import sudoku.io.SudokuSerializer;
|
||||
import sudoku.solver.RandomSolver;
|
||||
import sudoku.solver.Solver;
|
||||
import org.polytech.ryuk.sudoku.constraint.Constraint;
|
||||
import org.polytech.ryuk.sudoku.io.SudokuSerializer;
|
||||
import org.polytech.ryuk.sudoku.solver.RandomSolver;
|
||||
import org.polytech.ryuk.sudoku.solver.Solver;
|
||||
|
||||
public class SudokuFactory {
|
||||
|
||||
@@ -145,7 +145,7 @@ public class SudokuFactory {
|
||||
cellsThatCanBeEmptied.remove(cellToEmpty);
|
||||
}
|
||||
|
||||
return newDokuFromFilledOne(doku, --nbCellsToEmpty, solver);
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -181,21 +181,22 @@ public class SudokuFactory {
|
||||
* @param sudoku2 Sudoku, second sudoku à connecter.
|
||||
* @param offset Coordinate, décalage entre les deux Sudokus.
|
||||
*/
|
||||
private static void linkSquareSudokus(Sudoku sudoku1, Sudoku sudoku2, Coordinate offset) {
|
||||
private static void linkRectangleSudokus(Sudoku sudoku1, Sudoku sudoku2, Coordinate offset) {
|
||||
int blockWidth = sudoku1.getBlockWidth();
|
||||
for (int dx = 0; dx < blockWidth; dx++) {
|
||||
int blockHeight = sudoku1.getSize() / blockWidth;
|
||||
for (int dx = 0; dx < blockHeight; 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 < blockWidth) && (block1X >= 0) && (block1Y >= 0) && (block1Y < blockWidth)) {
|
||||
Block block1 = sudoku1.getBlocks().get(block1Y * blockWidth + block1X);
|
||||
Block block2 = sudoku2.getBlocks().get(block2Y * blockWidth + block2X);
|
||||
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);
|
||||
|
||||
// on remplace le bloc
|
||||
sudoku2.getBlocks().set(block2Y * blockWidth + block2X, block1);
|
||||
sudoku2.getBlocks().set(block2Y * blockHeight + block2X, block1);
|
||||
block1.getSudokus().add(sudoku2);
|
||||
|
||||
// on remplace les cellules
|
||||
@@ -231,10 +232,10 @@ public class SudokuFactory {
|
||||
Sudoku sudoku4 = createSquareSudoku(size, constraints);
|
||||
Sudoku sudoku5 = createSquareSudoku(size, constraints);
|
||||
|
||||
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));
|
||||
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));
|
||||
|
||||
return new MultiDoku(Arrays.asList(sudoku1, sudoku2, sudoku3, sudoku4, sudoku5));
|
||||
}
|
||||
@@ -263,10 +264,10 @@ public class SudokuFactory {
|
||||
Sudoku sudoku4 = createRectangleSudoku(width, height, constraints);
|
||||
Sudoku sudoku5 = createRectangleSudoku(width, height, constraints);
|
||||
|
||||
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));
|
||||
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));
|
||||
|
||||
return new MultiDoku(Arrays.asList(sudoku1, sudoku2, sudoku3, sudoku4, sudoku5));
|
||||
}
|
||||
@@ -276,9 +277,6 @@ 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();
|
||||
}
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
package sudoku.io;
|
||||
|
||||
public class SudokuFile {
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
/*
|
||||
* This Java source file was generated by the Gradle 'init' task.
|
||||
*/
|
||||
package sudoku;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class AppTest {
|
||||
@Test void appHasAGreeting() {
|
||||
Main classUnderTest = new Main();
|
||||
assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
|
||||
}
|
||||
}
|
||||
@@ -8,11 +8,10 @@ import java.util.Random;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import sudoku.io.SudokuSerializer;
|
||||
import sudoku.solver.RandomSolver;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.SudokuFactory;
|
||||
import org.polytech.ryuk.sudoku.io.SudokuSerializer;
|
||||
import org.polytech.ryuk.sudoku.solver.RandomSolver;
|
||||
import org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
import org.polytech.ryuk.sudoku.structure.SudokuFactory;
|
||||
|
||||
public class SudokuSerializerTest {
|
||||
|
||||
|
||||
@@ -1,18 +1,19 @@
|
||||
package sudoku.solver;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import sudoku.io.SudokuPrinter;
|
||||
import sudoku.io.SudokuSerializer;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Sudoku;
|
||||
import sudoku.structure.SudokuFactory;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.polytech.ryuk.sudoku.io.SudokuPrinter;
|
||||
import org.polytech.ryuk.sudoku.io.SudokuSerializer;
|
||||
import org.polytech.ryuk.sudoku.solver.RandomSolver;
|
||||
import org.polytech.ryuk.sudoku.structure.Cell;
|
||||
import org.polytech.ryuk.sudoku.structure.MultiDoku;
|
||||
import org.polytech.ryuk.sudoku.structure.Sudoku;
|
||||
import org.polytech.ryuk.sudoku.structure.SudokuFactory;
|
||||
|
||||
class SolverTest {
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user