Compare commits
1 Commits
6d96455ac4
...
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,9 +1,6 @@
|
||||
package game;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import sudoku.structure.MultiDoku;
|
||||
@@ -15,13 +12,11 @@ public class Game {
|
||||
}
|
||||
|
||||
private final Map<Integer, Player> players;
|
||||
private final List<Player> leaderboard;
|
||||
private GameState gameState;
|
||||
private MultiDoku doku;
|
||||
|
||||
public Game() {
|
||||
this.players = new HashMap<>();
|
||||
this.leaderboard = new ArrayList<>();
|
||||
this.gameState = GameState.GameNotStarted;
|
||||
}
|
||||
|
||||
@@ -31,18 +26,10 @@ public class Game {
|
||||
|
||||
public void addPlayer(Player player) {
|
||||
players.put(player.getId(), player);
|
||||
leaderboard.add(player);
|
||||
}
|
||||
|
||||
public void setPlayerScore(Player player, int newScore) {
|
||||
player.setScore(newScore);
|
||||
Collections.sort(this.leaderboard,
|
||||
(player1, player2) -> Integer.compare(player1.getScore(), player2.getScore()));
|
||||
}
|
||||
|
||||
public void removePlayer(int id) {
|
||||
this.leaderboard.remove(getPlayerById(id));
|
||||
this.players.remove(id);
|
||||
players.remove(id);
|
||||
}
|
||||
|
||||
public Map<Integer, Player> getPlayers() {
|
||||
@@ -62,8 +49,4 @@ public class Game {
|
||||
return doku;
|
||||
}
|
||||
|
||||
public List<Player> getLeaderboard() {
|
||||
return leaderboard;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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 getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
void setScore(int score) {
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
public String getPseudo() {
|
||||
|
||||
@@ -47,8 +47,6 @@ public class RenderableMultidoku {
|
||||
return cells.get(index);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static record PositionConstraint(Sudoku sudoku1, Sudoku sudoku2, Coordinate offset) {
|
||||
}
|
||||
|
||||
|
||||
@@ -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,36 +1,22 @@
|
||||
package gui.menu;
|
||||
|
||||
import gui.widget.LeaderboardRenderer;
|
||||
import gui.widget.SudokuRenderer;
|
||||
import gui.widget.TimerRenderer;
|
||||
import gui.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;
|
||||
private final TimerRenderer timerRenderer;
|
||||
private static final float GAME_DURATION = 10 * 60;
|
||||
|
||||
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);
|
||||
// TODO: sync timer
|
||||
this.timerRenderer = new TimerRenderer(GAME_DURATION);
|
||||
}
|
||||
|
||||
private void onCellChange(Cell cell) {
|
||||
this.client.sendCellChange(cell);
|
||||
}
|
||||
|
||||
public void onDisconnect() {
|
||||
@@ -41,8 +27,6 @@ public class MultiPlayerDokuView extends BaseView{
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
this.timerRenderer.render();
|
||||
this.leaderboardRenderer.render();
|
||||
this.sudokuRenderer.render();
|
||||
if (ImGui.button("Quitter")) {
|
||||
this.client.stop();
|
||||
|
||||
@@ -1,27 +1,24 @@
|
||||
package gui.menu;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import game.Player;
|
||||
import gui.widget.SudokuSelector;
|
||||
import imgui.ImGui;
|
||||
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 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)));
|
||||
@@ -37,22 +34,15 @@ 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 {
|
||||
if (this.doku == null)
|
||||
ImGui.beginDisabled();
|
||||
if (ImGui.button("Démarrer")) {
|
||||
this.server.startGame(this.doku);
|
||||
// temp
|
||||
MultiDoku doku = SudokuFactory.createBasicXShapedMultidoku(3, Arrays.asList(Constraint.Diagonal));
|
||||
this.server.startGame(doku);
|
||||
}
|
||||
if (this.doku == null)
|
||||
ImGui.endDisabled();
|
||||
selector.render();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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.concurrent.CancellationException;
|
||||
|
||||
import gui.widget.SudokuRenderer;
|
||||
import gui.SudokuRenderer;
|
||||
import imgui.ImGui;
|
||||
import imgui.ImGuiStyle;
|
||||
import sudoku.io.SudokuSerializer;
|
||||
|
||||
@@ -1,70 +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;
|
||||
|
||||
public LeaderboardRenderer(Game game, Player player) {
|
||||
this.game = game;
|
||||
this.currentPlayer = player;
|
||||
}
|
||||
|
||||
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(player.getScore());
|
||||
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,27 +0,0 @@
|
||||
package gui.widget;
|
||||
|
||||
import imgui.ImGui;
|
||||
|
||||
public class TimerRenderer {
|
||||
|
||||
private float time;
|
||||
private final float duration;
|
||||
|
||||
public TimerRenderer(float duration) {
|
||||
this.time = 0;
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
public void render() {
|
||||
this.time += ImGui.getIO().getDeltaTime();
|
||||
float remainingTime = this.duration - this.time;
|
||||
int seconds = (int) remainingTime;
|
||||
int minutes = seconds / 60;
|
||||
seconds %= 60;
|
||||
String text = minutes + ":" + seconds;
|
||||
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();
|
||||
|
||||
@@ -7,11 +7,7 @@ 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;
|
||||
|
||||
public class Client {
|
||||
private final ClientConnexion clientConnection;
|
||||
@@ -22,8 +18,6 @@ public class Client {
|
||||
public final Signal onClosed = new Signal();
|
||||
public final Signal onGameStarted = new Signal();
|
||||
|
||||
Player player;
|
||||
|
||||
String disconnectReason = null;
|
||||
|
||||
public Client(String address, short port) throws UnknownHostException, IOException {
|
||||
@@ -60,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();
|
||||
}
|
||||
|
||||
@@ -75,23 +73,4 @@ public class ClientConnexion extends Connexion {
|
||||
this.client.onGameStarted.emit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(EndGamePacket packet) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPacket(UpdatePlayerScorePacket packet) {
|
||||
Player player = this.client.getGame().getPlayerById(packet.getPlayerId());
|
||||
assert(player != null);
|
||||
this.client.getGame().setPlayerScore(player, packet.getCellsLeft());
|
||||
System.out.println("Score for " + player.getPseudo() + " : " + 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,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,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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -70,9 +70,6 @@ public class Server {
|
||||
|
||||
public void startGame(MultiDoku doku) {
|
||||
this.game.startGame(doku);
|
||||
for (ServerConnexion connexion : this.connexions) {
|
||||
connexion.setSudoku(doku.clone());
|
||||
}
|
||||
broadcastPacket(new StartGamePacket(SudokuSerializer.serializeSudoku(doku).toString()));
|
||||
}
|
||||
|
||||
|
||||
@@ -6,19 +6,14 @@ 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;
|
||||
|
||||
public class ServerConnexion extends Connexion {
|
||||
|
||||
@@ -26,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);
|
||||
@@ -35,7 +29,7 @@ public class ServerConnexion extends Connexion {
|
||||
}
|
||||
|
||||
public boolean update() {
|
||||
if (shouldClose || isClosed())
|
||||
if (shouldClose | isClosed())
|
||||
return false;
|
||||
return this.keepAliveHandler.update();
|
||||
}
|
||||
@@ -50,7 +44,7 @@ public class ServerConnexion extends Connexion {
|
||||
|
||||
@Override
|
||||
public synchronized void close() {
|
||||
if (shouldClose)
|
||||
if(shouldClose)
|
||||
return;
|
||||
super.close();
|
||||
shouldClose = true;
|
||||
@@ -60,19 +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.getScore()));
|
||||
}
|
||||
}
|
||||
|
||||
this.server.broadcastPacket(new PlayerJoinPacket(player));
|
||||
sendPacket(new ConnexionInfoPacket(player.getId()));
|
||||
|
||||
if (this.server.getGame().getGameState() == GameState.GameGoing) {
|
||||
setSudoku(this.server.getGame().getDoku().clone());
|
||||
sendPacket(
|
||||
new StartGamePacket(SudokuSerializer.serializeSudoku(this.server.getGame().getDoku()).toString()));
|
||||
sendPacket(new StartGamePacket(SudokuSerializer.serializeSudoku(this.server.getGame().getDoku()).toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,43 +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().setPlayerScore(player, player.getScore() + 1);
|
||||
this.server.broadcastPacket(new UpdatePlayerScorePacket(player.getId(), player.getScore()));
|
||||
return;
|
||||
}
|
||||
// on rajoute un chiffre à la grille
|
||||
if (cell.trySetValue(packet.getNewValue())) {
|
||||
this.server.getGame().setPlayerScore(player, player.getScore() - 1);
|
||||
this.server.broadcastPacket(new UpdatePlayerScorePacket(player.getId(), player.getScore()));
|
||||
}
|
||||
}
|
||||
|
||||
public void setSudoku(MultiDoku doku) {
|
||||
this.doku = doku;
|
||||
assert (player != null);
|
||||
this.server.getGame().setPlayerScore(player, this.doku.getEmptyCells().size());
|
||||
this.server.broadcastPacket(new UpdatePlayerScorePacket(player.getId(), player.getScore()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,15 +3,40 @@
|
||||
*/
|
||||
package sudoku;
|
||||
|
||||
import gui.RenderableMultidoku;
|
||||
import gui.Symbols;
|
||||
import sudoku.io.ConsoleInterface;
|
||||
import sudoku.io.SudokuPrinter;
|
||||
import sudoku.solver.RandomSolver;
|
||||
import sudoku.solver.Solver;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.SudokuFactory;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class Main {
|
||||
public String getGreeting() {
|
||||
return "Hello World!";
|
||||
}
|
||||
|
||||
public static void voidTest(){
|
||||
MultiDoku md = SudokuFactory.createBasicXShapedMultidoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
SudokuPrinter.printMultiDoku(RenderableMultidoku.fromMultidoku(md), Symbols.Numbers, 3, 3);
|
||||
}
|
||||
|
||||
public static void filledTest(){
|
||||
MultiDoku md = SudokuFactory.createBasicXShapedMultidoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
new RandomSolver().solve(md);
|
||||
SudokuPrinter.printMultiDoku(RenderableMultidoku.fromMultidoku(md), Symbols.Numbers, 3, 3);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ConsoleInterface console = new ConsoleInterface();
|
||||
console.start();
|
||||
/*
|
||||
voidTest();
|
||||
filledTest();
|
||||
filledTest();
|
||||
*/
|
||||
console.welcome();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,27 @@
|
||||
package sudoku.io;
|
||||
|
||||
import gui.RenderableMultidoku;
|
||||
import gui.Symbols;
|
||||
import sudoku.constraint.*;
|
||||
import sudoku.solver.RandomSolver;
|
||||
import sudoku.structure.Difficulty;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.SudokuFactory;
|
||||
|
||||
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;
|
||||
|
||||
public class ConsoleInterface {
|
||||
public Scanner reader = new Scanner(System.in);
|
||||
|
||||
public void welcome(){
|
||||
System.out.println("Welcome to our Sudoku Solver!");
|
||||
System.out.println("This is the project of Melvyn Bauvent, Lilas Grenier and Simon Priblyski.");
|
||||
start();
|
||||
}
|
||||
|
||||
public void start(){
|
||||
welcome();
|
||||
System.out.println("First of all, you need to tell me the size of the sudoku you want to generate.");
|
||||
int width = getBlockWidth();
|
||||
int height = getBlockHeight();
|
||||
@@ -27,26 +33,21 @@ public class ConsoleInterface {
|
||||
pickSymbols(listSymbols, numberOfSymbols);
|
||||
}
|
||||
else {
|
||||
// TODO
|
||||
System.out.println("Simon doit finir sa partie.");
|
||||
assert false;
|
||||
listSymbols = Symbols.Numbers.getSymbols();
|
||||
}
|
||||
List<Constraint> listConstraints = getListConstraints();
|
||||
|
||||
|
||||
System.out.println("Now that we have the size of our sudoku, would you rather have a single grid ('one', default), " +
|
||||
"or a a multidoku composed of 5 subgrids ('multi') ?");
|
||||
List<Sudoku> subGrids = new ArrayList<>();
|
||||
MultiDoku doku;
|
||||
if (reader.next().equalsIgnoreCase("multi")) {
|
||||
doku = SudokuFactory.createBasicEmptyRectangleDoku(width, height, listConstraints);
|
||||
}
|
||||
else {
|
||||
doku = SudokuFactory.createBasicXShapedMultidoku(width, height, listConstraints);
|
||||
}
|
||||
else {
|
||||
doku = SudokuFactory.createBasicEmptyRectangleDoku(width, height, listConstraints);
|
||||
}
|
||||
RenderableMultidoku rm = RenderableMultidoku.fromMultidoku(doku);
|
||||
System.out.println("Your sudoku will look like this:");
|
||||
// TODO printMultiDoku method not yet implemented
|
||||
SudokuPrinter.printMultiDoku(doku, width, height);
|
||||
SudokuPrinter.printMultiDoku(rm, listSymbols, width, height);
|
||||
System.out.println("We now will fill this sudoku.");
|
||||
System.out.println("What level of difficulty would you like? ('very easy', 'easy', 'medium' (default), 'hard', 'full' (sudoku fully completed))");
|
||||
String difficulty = reader.next().toLowerCase();
|
||||
@@ -57,12 +58,7 @@ public class ConsoleInterface {
|
||||
generatePartialDoku(doku, difficulty);
|
||||
}
|
||||
System.out.println("Here's your sudoku !");
|
||||
SudokuPrinter.printMultiDoku(doku, width, height);
|
||||
}
|
||||
|
||||
public void welcome(){
|
||||
System.out.println("Welcome to our Sudoku Solver!");
|
||||
System.out.println("This is the project of Melvyn Bauvent, Lilas Grenier and Simon Priblyski.");
|
||||
SudokuPrinter.printMultiDoku(rm, listSymbols, width, height);
|
||||
}
|
||||
|
||||
public int getBlockWidth() {
|
||||
|
||||
@@ -1,28 +1,89 @@
|
||||
package sudoku.io;
|
||||
|
||||
import gui.RenderableMultidoku;
|
||||
import gui.Symbols;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Sudoku;
|
||||
|
||||
public class SudokuPrinter {
|
||||
import java.util.List;
|
||||
|
||||
public static void printRectangleSudoku(final Sudoku s, int blockWidth, int blockHeight) {
|
||||
for (int y = 0; y < s.getSize(); y++) {
|
||||
if (y % blockHeight == 0 && y > 0) {
|
||||
System.out.println();
|
||||
}
|
||||
StringBuilder line = new StringBuilder("[ ");
|
||||
for (int x = 0; x < s.getSize(); x++) {
|
||||
line.append((s.getCell(x, y).getSymbolIndex() + 1)).append(" ");
|
||||
if (x % blockWidth == blockWidth - 1 && x != blockWidth * blockHeight - 1) {
|
||||
line.append("| ");
|
||||
}
|
||||
}
|
||||
line.append("]");
|
||||
System.out.println(line);
|
||||
}
|
||||
public class SudokuPrinter {
|
||||
public static final String ANSI_RESET = "\u001B[0m";
|
||||
public static final String ANSI_RED = "\u001B[31m";
|
||||
public static final String ANSI_GREEN = "\u001B[32m";
|
||||
public static final String ANSI_YELLOW = "\u001B[33m";
|
||||
public static final String ANSI_BLUE = "\u001B[34m";
|
||||
public static final String ANSI_PURPLE = "\u001B[35m";
|
||||
public static final String ANSI_CYAN = "\u001B[36m";
|
||||
|
||||
public static void printRectangleSudoku(final Sudoku s, int blockWidth, int blockHeight, Symbols symbols) {
|
||||
printRectangleSudoku(s, blockWidth, blockHeight, symbols.getSymbols());
|
||||
}
|
||||
|
||||
public static String toStringRectangleSudoku(final Sudoku s, int blockWidth, int blockHeight) {
|
||||
public static void printRectangleSudoku(final Sudoku s, int blockWidth, int blockHeight, List<String> listSymbols){
|
||||
for (int y = 0; y < s.getSize(); y++) {
|
||||
if (y % blockHeight == 0 && y > 0) {
|
||||
System.out.println();
|
||||
}
|
||||
StringBuilder line = new StringBuilder("[ ");
|
||||
for (int x = 0; x < s.getSize(); x++) {
|
||||
Cell c = s.getCell(x, y);
|
||||
if (c.getSymbolIndex() == Cell.NOSYMBOL) {
|
||||
line.append(" ");
|
||||
}
|
||||
else {
|
||||
line.append(listSymbols.get(c.getSymbolIndex())).append(" ");
|
||||
}
|
||||
if (x % blockWidth == blockWidth - 1 && x != blockWidth * blockHeight - 1) {
|
||||
line.append("| ");
|
||||
}
|
||||
}
|
||||
line.append("]");
|
||||
System.out.println(line);
|
||||
}
|
||||
}
|
||||
|
||||
public static void printMultiDoku(final RenderableMultidoku rm, Symbols symbols, int blockWidth, int blockHeight) {
|
||||
printMultiDoku(rm, symbols.getSymbols(), blockWidth, blockHeight);
|
||||
}
|
||||
|
||||
public static void printMultiDoku(final RenderableMultidoku rm, List<String> listSymbols, int blockWidth, int blockHeight) {
|
||||
StringBuilder line = new StringBuilder("\n");
|
||||
int nBlockInWidth = rm.getWidth() / blockWidth;
|
||||
for (int y = 0; y < rm.getHeight(); y++) {
|
||||
if (y % blockHeight == 0) {
|
||||
line.append("__".repeat(Math.max(0, rm.getWidth()+nBlockInWidth))).append("_\n");
|
||||
}
|
||||
line.append("[ ");
|
||||
for (int x = 0; x < rm.getWidth(); x++) {
|
||||
if (x % blockWidth == 0 && x > 0) {
|
||||
line.append("| ");
|
||||
}
|
||||
Cell cell = rm.getCell(x, y);
|
||||
if (cell != null) {
|
||||
if (cell.getSymbolIndex() == Cell.NOSYMBOL) {
|
||||
line.append("- ");
|
||||
}
|
||||
else {
|
||||
line.append(listSymbols.get(cell.getSymbolIndex())).append(" ");
|
||||
}
|
||||
}
|
||||
else {
|
||||
line.append(" ");
|
||||
}
|
||||
}
|
||||
line.append("]\n");
|
||||
}
|
||||
line.append("__".repeat(Math.max(0, rm.getWidth()+nBlockInWidth))).append("_\n");
|
||||
System.out.println(line);
|
||||
}
|
||||
|
||||
public static String toStringRectangleSudoku(final Sudoku s, int blockWidth, int blockHeight, Symbols symbols){
|
||||
return toStringRectangleSudoku(s, blockWidth, blockHeight, symbols.getSymbols());
|
||||
}
|
||||
|
||||
public static String toStringRectangleSudoku(final Sudoku s, int blockWidth, int blockHeight, List<String> listSymbols) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (int y = 0; y < s.getSize(); y++) {
|
||||
// Ajouter une ligne vide entre les blocs horizontaux
|
||||
@@ -32,7 +93,13 @@ public class SudokuPrinter {
|
||||
StringBuilder line = new StringBuilder("[ ");
|
||||
for (int x = 0; x < s.getSize(); x++) {
|
||||
// Ajouter la valeur de la cellule
|
||||
line.append((s.getCell(x, y).getSymbolIndex() + 1)).append(" ");
|
||||
Cell cell = s.getCell(x, y);
|
||||
if (cell.getSymbolIndex() == Cell.NOSYMBOL) {
|
||||
line.append(" ");
|
||||
}
|
||||
else {
|
||||
line.append(listSymbols.get(cell.getSymbolIndex())).append(" ");
|
||||
}
|
||||
|
||||
// Ajouter un séparateur vertical entre les blocs
|
||||
if (x % blockWidth == blockWidth - 1 && x != s.getSize() - 1) {
|
||||
@@ -45,7 +112,12 @@ public class SudokuPrinter {
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public static void printMultiDoku(final MultiDoku doku, int blockWidth, int blockHeight){
|
||||
// TODO
|
||||
public static void printMultiDoku(final MultiDoku doku, int blockWidth, int blockHeight, Symbols symbols){
|
||||
if (doku.getNbSubGrids()==1) {
|
||||
printRectangleSudoku(doku.getSubGrid(0), blockWidth, blockHeight, symbols);
|
||||
}
|
||||
else {
|
||||
printMultiDoku(RenderableMultidoku.fromMultidoku(doku), symbols, blockWidth, blockHeight);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
@@ -17,8 +18,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
|
||||
@@ -34,7 +33,8 @@ 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;
|
||||
|
||||
@@ -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,9 +178,4 @@ public class MultiDoku {
|
||||
int randomIndex = rand.nextInt(emptyCells.size());
|
||||
return emptyCells.get(randomIndex);
|
||||
}
|
||||
|
||||
public MultiDoku clone() {
|
||||
//TODO: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah
|
||||
return SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(this));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package sudoku.solver;
|
||||
|
||||
import gui.Symbols;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import sudoku.io.SudokuPrinter;
|
||||
import sudoku.io.SudokuSerializer;
|
||||
@@ -38,7 +39,8 @@ class SolverTest {
|
||||
|
||||
assert (sudokuToTest.setImmutableCellsSymbol(immutableCells));
|
||||
|
||||
SudokuPrinter.printRectangleSudoku(dokuToTest.getSubGrid(0), 3, 3);
|
||||
//SudokuPrinter.printRectangleSudoku(dokuToTest.getSubGrid(0), 3, 3);
|
||||
SudokuPrinter.printMultiDoku(dokuToTest, 3, 3, Symbols.Numbers);
|
||||
|
||||
List<Integer> correctCells = List.of(7, 6, 0, 3, 4, 2, 8, 5, 1,
|
||||
2, 3, 8, 1, 5, 6, 7, 0, 4,
|
||||
@@ -53,14 +55,15 @@ class SolverTest {
|
||||
sudokuResult.setCellsSymbol(correctCells);
|
||||
|
||||
System.out.println("\n****************************Doku Control\n");
|
||||
SudokuPrinter.printRectangleSudoku(sudokuResult, 3, 3);
|
||||
SudokuPrinter.printRectangleSudoku(sudokuResult, 3, 3, Symbols.Russian);
|
||||
|
||||
assert (dokuResult.isSolved());
|
||||
|
||||
new RandomSolver().solve(dokuToTest);
|
||||
|
||||
System.out.println("\n****************************\nDoku solved");
|
||||
SudokuPrinter.printRectangleSudoku(dokuToTest.getSubGrid(0), 3, 3);
|
||||
//SudokuPrinter.printRectangleSudoku(dokuToTest.getSubGrid(0), 3, 3);
|
||||
SudokuPrinter.printMultiDoku(dokuToTest, 3, 3, Symbols.Emojis);
|
||||
|
||||
assert (dokuToTest.isSolved());
|
||||
|
||||
@@ -97,6 +100,7 @@ class SolverTest {
|
||||
|
||||
new RandomSolver().solve(dokuToTest3);
|
||||
|
||||
SudokuPrinter.printRectangleSudoku(dokuToTest3.getSubGrid(0), 3, 3);
|
||||
//SudokuPrinter.printRectangleSudoku(dokuToTest3.getSubGrid(0), 3, 3);
|
||||
SudokuPrinter.printMultiDoku(dokuToTest3, 3, 3, Symbols.Letters);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user