Compare commits
17 Commits
createPlus
...
4a8644181a
| Author | SHA1 | Date | |
|---|---|---|---|
| 4a8644181a | |||
| efa357a1ab | |||
| 618e436270 | |||
| d5009371f2 | |||
| 87727f39e8 | |||
| 413201882b | |||
| d8289b553a | |||
|
|
adeb9b07e5 | ||
|
|
4903fd567b | ||
| 990c830590 | |||
| d806420d21 | |||
| 4b98341618 | |||
| 68021b796b | |||
| 06efbf649b | |||
| 4ee29d8f50 | |||
| eda2a1afae | |||
| f3bbfd9e6c |
@@ -11,8 +11,6 @@ plugins {
|
|||||||
id 'application'
|
id 'application'
|
||||||
}
|
}
|
||||||
|
|
||||||
project.ext.os = System.properties['os.name'].toLowerCase().split(" ")[0]
|
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
// Use Maven Central for resolving dependencies.
|
// Use Maven Central for resolving dependencies.
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
@@ -22,13 +20,17 @@ dependencies {
|
|||||||
// Use JUnit Jupiter for testing.
|
// Use JUnit Jupiter for testing.
|
||||||
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.1'
|
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.1'
|
||||||
|
|
||||||
|
implementation 'com.google.guava:guava:31.1-jre'
|
||||||
|
|
||||||
implementation 'org.json:json:20250107'
|
implementation 'org.json:json:20250107'
|
||||||
|
|
||||||
implementation "io.github.spair:imgui-java-app:1.88.0"
|
implementation "io.github.spair:imgui-java-app:1.88.0"
|
||||||
|
|
||||||
implementation "org.lwjgl:lwjgl-stb:3.3.4"
|
implementation "org.lwjgl:lwjgl-stb:3.3.4"
|
||||||
|
|
||||||
runtimeOnly "org.lwjgl:lwjgl-stb::natives-$os"
|
implementation "org.lwjgl:lwjgl-stb::natives-linux"
|
||||||
|
implementation "org.lwjgl:lwjgl-stb::natives-windows"
|
||||||
|
implementation "org.lwjgl:lwjgl-stb::natives-macos"
|
||||||
}
|
}
|
||||||
|
|
||||||
application {
|
application {
|
||||||
@@ -48,6 +50,10 @@ jar {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test {
|
||||||
|
useJUnitPlatform()
|
||||||
|
}
|
||||||
|
|
||||||
run {
|
run {
|
||||||
enableAssertions = true
|
enableAssertions = true
|
||||||
}
|
}
|
||||||
32
app/src/main/java/gui/AssetManager.java
Normal file
32
app/src/main/java/gui/AssetManager.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package gui;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
public class AssetManager {
|
||||||
|
|
||||||
|
public static byte[] getResource(String name) {
|
||||||
|
// we first search it in files
|
||||||
|
File f = new File(name);
|
||||||
|
if (f.exists()){
|
||||||
|
FileInputStream fis;
|
||||||
|
try {
|
||||||
|
fis = new FileInputStream(f);
|
||||||
|
return fis.readAllBytes();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// then in the jar
|
||||||
|
InputStream is = ClassLoader.getSystemResourceAsStream(name);
|
||||||
|
try {
|
||||||
|
return is.readAllBytes();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -31,7 +31,7 @@ public class Main extends Application {
|
|||||||
@Override
|
@Override
|
||||||
protected void preRun() {
|
protected void preRun() {
|
||||||
super.preRun();
|
super.preRun();
|
||||||
Images.loadImages();
|
Images.reloadImages();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package gui.constants;
|
package gui.constants;
|
||||||
|
|
||||||
|
import gui.AssetManager;
|
||||||
import imgui.ImFont;
|
import imgui.ImFont;
|
||||||
import imgui.ImFontConfig;
|
import imgui.ImFontConfig;
|
||||||
import imgui.ImFontGlyphRangesBuilder;
|
import imgui.ImFontGlyphRangesBuilder;
|
||||||
@@ -20,15 +21,15 @@ public class Fonts {
|
|||||||
ImFontGlyphRangesBuilder builder = new ImFontGlyphRangesBuilder();
|
ImFontGlyphRangesBuilder builder = new ImFontGlyphRangesBuilder();
|
||||||
builder.addRanges(ImGui.getIO().getFonts().getGlyphRangesDefault());
|
builder.addRanges(ImGui.getIO().getFonts().getGlyphRangesDefault());
|
||||||
builder.addRanges(ImGui.getIO().getFonts().getGlyphRangesCyrillic());
|
builder.addRanges(ImGui.getIO().getFonts().getGlyphRangesCyrillic());
|
||||||
// builder.addRanges(ImGui.getIO().getFonts().getGlyphRangesChineseFull());
|
|
||||||
ImFontConfig cfg = new ImFontConfig();
|
ImFontConfig cfg = new ImFontConfig();
|
||||||
cfg.setGlyphRanges(builder.buildRanges());
|
cfg.setGlyphRanges(builder.buildRanges());
|
||||||
|
|
||||||
COMIC = ImGui.getIO().getFonts().addFontFromFileTTF(baseDir + "comic.ttf", 50.0f);
|
COMIC = ImGui.getIO().getFonts().addFontFromMemoryTTF(AssetManager.getResource("comic.ttf"), 50.0f);
|
||||||
ARIAL_BOLD = ImGui.getIO().getFonts().addFontFromFileTTF(baseDir + "arial_bold.ttf", 50.0f);
|
ARIAL_BOLD = ImGui.getIO().getFonts().addFontFromMemoryTTF(AssetManager.getResource("arial_bold.ttf"), 50.0f);
|
||||||
ARIAL = ImGui.getIO().getFonts().addFontFromFileTTF(baseDir + "arial.ttf", 50.0f, cfg);
|
ARIAL = ImGui.getIO().getFonts().addFontFromMemoryTTF(AssetManager.getResource("arial.ttf"), 50.0f, cfg);
|
||||||
CHERI = ImGui.getIO().getFonts().addFontFromFileTTF(baseDir + "cheri.ttf", 50.0f);
|
CHERI = ImGui.getIO().getFonts().addFontFromMemoryTTF(AssetManager.getResource("cheri.ttf"), 50.0f);
|
||||||
INFECTED = ImGui.getIO().getFonts().addFontFromFileTTF(baseDir + "INFECTED.ttf", 50.0f);
|
INFECTED = ImGui.getIO().getFonts().addFontFromMemoryTTF(AssetManager.getResource("INFECTED.ttf"), 50.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,16 +5,21 @@ import java.nio.ByteBuffer;
|
|||||||
import org.lwjgl.opengl.GL11;
|
import org.lwjgl.opengl.GL11;
|
||||||
import org.lwjgl.stb.STBImage;
|
import org.lwjgl.stb.STBImage;
|
||||||
|
|
||||||
|
import gui.AssetManager;
|
||||||
|
|
||||||
public class Images {
|
public class Images {
|
||||||
|
|
||||||
public static int BACKGROUND;
|
public static int BACKGROUND;
|
||||||
|
|
||||||
private static int loadTexture(String fileName) {
|
private static int loadTexture(byte[] imageData) {
|
||||||
int[] width = new int[1];
|
int[] width = new int[1];
|
||||||
int[] height = new int[1];
|
int[] height = new int[1];
|
||||||
int[] channelCount = new int[1];
|
int[] channelCount = new int[1];
|
||||||
|
|
||||||
ByteBuffer pixels = STBImage.stbi_load(fileName, width, height, channelCount, 4);
|
ByteBuffer img = ByteBuffer.allocateDirect(imageData.length);
|
||||||
|
img.put(imageData);
|
||||||
|
img.flip();
|
||||||
|
ByteBuffer pixels = STBImage.stbi_load_from_memory(img, width, height, channelCount, 4);
|
||||||
|
|
||||||
int textureID = GL11.glGenTextures();
|
int textureID = GL11.glGenTextures();
|
||||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
|
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
|
||||||
@@ -32,8 +37,8 @@ public class Images {
|
|||||||
return textureID;
|
return textureID;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void loadImages() {
|
public static void reloadImages() {
|
||||||
BACKGROUND = loadTexture("background.png");
|
BACKGROUND = loadTexture(AssetManager.getResource(Options.BackgroundPath));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,5 +4,6 @@ public class Options {
|
|||||||
|
|
||||||
public static Symbols Symboles = Symbols.Numbers;
|
public static Symbols Symboles = Symbols.Numbers;
|
||||||
public static float BackgroundSpeed = 1.0f;
|
public static float BackgroundSpeed = 1.0f;
|
||||||
|
public static String BackgroundPath = "background.png";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,12 +14,12 @@ public class ConnexionStatusView extends BaseView {
|
|||||||
|
|
||||||
private String displayText = "Connecting ...";
|
private String displayText = "Connecting ...";
|
||||||
|
|
||||||
public ConnexionStatusView(StateMachine stateMachine, String address, short port)
|
public ConnexionStatusView(StateMachine stateMachine, String pseudo, String address, short port)
|
||||||
throws UnknownHostException, IOException {
|
throws UnknownHostException, IOException {
|
||||||
super(stateMachine);
|
super(stateMachine);
|
||||||
Thread t = new Thread(() -> {
|
Thread t = new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
this.client = new Client(address, port);
|
this.client = new Client(pseudo, address, port);
|
||||||
bindListeners();
|
bindListeners();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@@ -29,12 +29,13 @@ public class ConnexionStatusView extends BaseView {
|
|||||||
t.start();
|
t.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConnexionStatusView(StateMachine stateMachine, short port) throws UnknownHostException, IOException {
|
public ConnexionStatusView(StateMachine stateMachine, String pseudo, short port)
|
||||||
|
throws UnknownHostException, IOException {
|
||||||
super(stateMachine);
|
super(stateMachine);
|
||||||
Thread t = new Thread(() -> {
|
Thread t = new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
this.server = new Server(port);
|
this.server = new Server(port);
|
||||||
this.client = new Client("localhost", port);
|
this.client = new Client(pseudo, "localhost", port);
|
||||||
bindListeners();
|
bindListeners();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package gui.menu;
|
package gui.menu;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
import imgui.ImGui;
|
import imgui.ImGui;
|
||||||
import imgui.ImVec2;
|
import imgui.ImVec2;
|
||||||
@@ -11,6 +12,7 @@ public class MultiMenu extends BaseView {
|
|||||||
|
|
||||||
private final ImInt port = new ImInt(25565);
|
private final ImInt port = new ImInt(25565);
|
||||||
private final ImString address = new ImString("localhost");
|
private final ImString address = new ImString("localhost");
|
||||||
|
private final ImString pseudo = new ImString("Joueur" + new Random().nextInt());
|
||||||
|
|
||||||
public MultiMenu(StateMachine stateMachine) {
|
public MultiMenu(StateMachine stateMachine) {
|
||||||
super(stateMachine);
|
super(stateMachine);
|
||||||
@@ -22,9 +24,10 @@ public class MultiMenu extends BaseView {
|
|||||||
ImGui.beginChild("##CreateGame", new ImVec2(displaySize.x / 2.0f, displaySize.y * 8.0f / 9.0f));
|
ImGui.beginChild("##CreateGame", new ImVec2(displaySize.x / 2.0f, displaySize.y * 8.0f / 9.0f));
|
||||||
if (ImGui.inputInt("Port", port))
|
if (ImGui.inputInt("Port", port))
|
||||||
port.set(Math.clamp(port.get(), 1, 65535));
|
port.set(Math.clamp(port.get(), 1, 65535));
|
||||||
|
ImGui.inputText("Pseudo", pseudo);
|
||||||
if (ImGui.button("Créer")) {
|
if (ImGui.button("Créer")) {
|
||||||
try {
|
try {
|
||||||
this.stateMachine.pushState(new ConnexionStatusView(stateMachine, (short) port.get()));
|
this.stateMachine.pushState(new ConnexionStatusView(stateMachine, pseudo.get(), (short) port.get()));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -38,6 +41,7 @@ public class MultiMenu extends BaseView {
|
|||||||
ImGui.inputText("Adresse", address);
|
ImGui.inputText("Adresse", address);
|
||||||
if (ImGui.inputInt("Port", port))
|
if (ImGui.inputInt("Port", port))
|
||||||
port.set(Math.clamp(port.get(), 1, 65535));
|
port.set(Math.clamp(port.get(), 1, 65535));
|
||||||
|
ImGui.inputText("Pseudo", pseudo);
|
||||||
if (ImGui.button("Rejoindre")) {
|
if (ImGui.button("Rejoindre")) {
|
||||||
try {
|
try {
|
||||||
this.stateMachine.pushState(new ConnexionStatusView(stateMachine, address.get(), (short) port.get()));
|
this.stateMachine.pushState(new ConnexionStatusView(stateMachine, address.get(), (short) port.get()));
|
||||||
|
|||||||
@@ -78,4 +78,9 @@ public class MultiPlayerView extends BaseView {
|
|||||||
renderGameStatus();
|
renderGameStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cleanResources() {
|
||||||
|
this.selector.clean();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
package gui.menu;
|
package gui.menu;
|
||||||
|
|
||||||
|
import gui.constants.Images;
|
||||||
import gui.constants.Options;
|
import gui.constants.Options;
|
||||||
import gui.constants.Symbols;
|
import gui.constants.Symbols;
|
||||||
import imgui.ImGui;
|
import imgui.ImGui;
|
||||||
|
import imgui.extension.imguifiledialog.ImGuiFileDialog;
|
||||||
|
import imgui.extension.imguifiledialog.flag.ImGuiFileDialogFlags;
|
||||||
import imgui.type.ImInt;
|
import imgui.type.ImInt;
|
||||||
|
|
||||||
public class OptionsMenu extends BaseView {
|
public class OptionsMenu extends BaseView {
|
||||||
@@ -14,6 +17,30 @@ public class OptionsMenu extends BaseView {
|
|||||||
super(stateMachine);
|
super(stateMachine);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void renderImageSelectDialog() {
|
||||||
|
if (ImGuiFileDialog.display("browse-img", ImGuiFileDialogFlags.None)) {
|
||||||
|
if (ImGuiFileDialog.isOk()) {
|
||||||
|
var selection = ImGuiFileDialog.getSelection();
|
||||||
|
for (var entry : selection.entrySet()) {
|
||||||
|
try {
|
||||||
|
String filePath = entry.getValue();
|
||||||
|
Options.BackgroundPath = filePath;
|
||||||
|
Images.reloadImages();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ImGuiFileDialog.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void renderImageSelectButton() {
|
||||||
|
if (ImGui.button("Changer de fond d'écran"))
|
||||||
|
ImGuiFileDialog.openDialog("browse-img", "Choisissez un fichier", ".png,.jpg,.jpeg", ".");
|
||||||
|
renderImageSelectDialog();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render() {
|
public void render() {
|
||||||
ImGui.text("Options");
|
ImGui.text("Options");
|
||||||
@@ -23,6 +50,7 @@ public class OptionsMenu extends BaseView {
|
|||||||
if (ImGui.sliderFloat("Vitesse d'animation de l'arrière plan", backgroundSpeed, 0.0f, 10.0f)) {
|
if (ImGui.sliderFloat("Vitesse d'animation de l'arrière plan", backgroundSpeed, 0.0f, 10.0f)) {
|
||||||
Options.BackgroundSpeed = backgroundSpeed[0];
|
Options.BackgroundSpeed = backgroundSpeed[0];
|
||||||
}
|
}
|
||||||
|
renderImageSelectButton();
|
||||||
renderReturnButton();
|
renderReturnButton();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,4 +25,9 @@ public class SoloMenu extends BaseView {
|
|||||||
renderReturnButton();
|
renderReturnButton();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cleanResources() {
|
||||||
|
this.sudokuSelector.clean();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,6 +92,7 @@ public class SudokuView extends BaseView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void startSolve(Solver solver) {
|
private void startSolve(Solver solver) {
|
||||||
|
this.doku.clearMutableCells();
|
||||||
resolveThread = new Thread(() -> {
|
resolveThread = new Thread(() -> {
|
||||||
List<SolverStep> steps = new ArrayList<>();
|
List<SolverStep> steps = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
@@ -158,6 +159,8 @@ public class SudokuView extends BaseView {
|
|||||||
private void renderClearButton() {
|
private void renderClearButton() {
|
||||||
if (centeredButton("Effacer")) {
|
if (centeredButton("Effacer")) {
|
||||||
this.doku.clearMutableCells();
|
this.doku.clearMutableCells();
|
||||||
|
this.resolved = false;
|
||||||
|
this.unresolved = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,13 +9,22 @@ public class SmoothProgressBar {
|
|||||||
private final float speed = 2.0f;
|
private final float speed = 2.0f;
|
||||||
private final float clipConstant = 0.001f;
|
private final float clipConstant = 0.001f;
|
||||||
|
|
||||||
public void render(String label, ImVec2 size, float progress) {
|
private void updateProgress(float newProgress) {
|
||||||
float delta = progress - lastProgress;
|
float delta = newProgress - lastProgress;
|
||||||
if (Math.abs(delta) < clipConstant)
|
if (Math.abs(delta) < clipConstant)
|
||||||
lastProgress = progress;
|
lastProgress = newProgress;
|
||||||
else
|
else
|
||||||
lastProgress = lastProgress + delta * ImGui.getIO().getDeltaTime() * speed;
|
lastProgress = lastProgress + delta * ImGui.getIO().getDeltaTime() * speed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void render(String label, ImVec2 size, float progress) {
|
||||||
|
updateProgress(progress);
|
||||||
ImGui.progressBar(lastProgress, size, label);
|
ImGui.progressBar(lastProgress, size, label);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void render(float progress) {
|
||||||
|
updateProgress(progress);
|
||||||
|
ImGui.progressBar(lastProgress);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,10 +35,15 @@ public class SudokuSelector {
|
|||||||
|
|
||||||
private final String confirmMessage;
|
private final String confirmMessage;
|
||||||
|
|
||||||
|
private Thread genThread = null;
|
||||||
|
|
||||||
|
private final SmoothProgressBar genProgressBar;
|
||||||
|
|
||||||
public SudokuSelector(boolean canGenEmptyGrid, String confirmMessage) {
|
public SudokuSelector(boolean canGenEmptyGrid, String confirmMessage) {
|
||||||
this.canGenEmptyGrid = canGenEmptyGrid;
|
this.canGenEmptyGrid = canGenEmptyGrid;
|
||||||
this.confirmMessage = confirmMessage;
|
this.confirmMessage = confirmMessage;
|
||||||
initConstraints();
|
initConstraints();
|
||||||
|
this.genProgressBar = new SmoothProgressBar();
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<IConstraint> getConstraints() {
|
private List<IConstraint> getConstraints() {
|
||||||
@@ -56,16 +61,39 @@ public class SudokuSelector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void stopGenThread() {
|
||||||
|
if (this.genThread != null) {
|
||||||
|
this.genThread.interrupt();
|
||||||
|
this.genThread = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void renderGenProgress() {
|
||||||
|
if (ImGui.beginPopup("genProgress")) {
|
||||||
|
ImGui.text("Chargement de la grille ...");
|
||||||
|
int filled = this.doku.getFilledCells().size();
|
||||||
|
int total = this.doku.getCells().size();
|
||||||
|
this.genProgressBar.render(filled / (float) total);
|
||||||
|
ImGui.endPopup();
|
||||||
|
} else {
|
||||||
|
stopGenThread();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void selectSudoku(MultiDoku doku, boolean empty) {
|
private void selectSudoku(MultiDoku doku, boolean empty) {
|
||||||
this.doku = doku;
|
this.doku = doku;
|
||||||
if (!empty) {
|
ImGui.openPopup("genProgress");
|
||||||
|
this.genThread = new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
|
if (!empty) {
|
||||||
SudokuFactory.fillDoku(doku, Difficulty.values()[difficulty.get()]);
|
SudokuFactory.fillDoku(doku, Difficulty.values()[difficulty.get()]);
|
||||||
|
}
|
||||||
|
this.onSelect.emit(this.doku);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
this.onSelect.emit(this.doku);
|
this.genThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void renderFileDialog() {
|
public void renderFileDialog() {
|
||||||
@@ -131,7 +159,12 @@ public class SudokuSelector {
|
|||||||
if (ImGui.button("À partir d'un fichier")) {
|
if (ImGui.button("À partir d'un fichier")) {
|
||||||
ImGuiFileDialog.openDialog("browse-sudoku", "Choisissez un fichier", ".json", ".");
|
ImGuiFileDialog.openDialog("browse-sudoku", "Choisissez un fichier", ".json", ".");
|
||||||
}
|
}
|
||||||
|
renderGenProgress();
|
||||||
renderFileDialog();
|
renderFileDialog();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void clean() {
|
||||||
|
stopGenThread();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,12 +28,10 @@ public class Client {
|
|||||||
|
|
||||||
String disconnectReason = null;
|
String disconnectReason = null;
|
||||||
|
|
||||||
public Client(String address, short port) throws UnknownHostException, IOException {
|
public Client(String pseudo, String address, short port) throws UnknownHostException, IOException {
|
||||||
this.clientConnection = new ClientConnexion(address, port, this);
|
this.clientConnection = new ClientConnexion(address, port, this);
|
||||||
this.game = new Game();
|
this.game = new Game();
|
||||||
// temp
|
login(pseudo);
|
||||||
Random r = new Random();
|
|
||||||
login("Player" + r.nextInt());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void login(String pseudo) {
|
public void login(String pseudo) {
|
||||||
|
|||||||
@@ -227,7 +227,7 @@ public class ConsoleInterface {
|
|||||||
saveMultiDoku(doku);
|
saveMultiDoku(doku);
|
||||||
break;
|
break;
|
||||||
case "solution":
|
case "solution":
|
||||||
solve(doku);
|
solve(doku, listSymbols, width, height);
|
||||||
break;
|
break;
|
||||||
case "exit":
|
case "exit":
|
||||||
exit();
|
exit();
|
||||||
@@ -238,21 +238,52 @@ public class ConsoleInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void solve(MultiDoku doku){
|
private void applyStep(SolverStep step) {
|
||||||
System.out.println("Pick a solver to use : random ('random', default), human ('human') or mixed solver ('mixed').");
|
step.getCell().setSymbolIndex(step.getNewValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean showStep(MultiDoku doku, List<String> listSymbols, int width, int height, SolverStep step) {
|
||||||
|
System.out.println("Here is the step : \n");
|
||||||
|
showMultidoku(doku, listSymbols, width, height);
|
||||||
|
applyStep(step);
|
||||||
|
System.out.println("\nTurns into :\n");
|
||||||
|
showMultidoku(doku, listSymbols, width, height);
|
||||||
|
System.out.println("Do you want to see the next step ? (y/n, default n)");
|
||||||
|
return reader.next().equals("y");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void showSolveSteps(MultiDoku doku, List<String> listSymbols, int width, int height, List<SolverStep> steps) {
|
||||||
|
System.out.println("Would you like to see the steps of the solver ? (y/n, default n)");
|
||||||
|
doku.getStateManager().popState();
|
||||||
switch (reader.next()) {
|
switch (reader.next()) {
|
||||||
case "human":
|
case "y":
|
||||||
new HumanSolver().solve(doku);
|
int stepCount = 0;
|
||||||
break;
|
while(stepCount < steps.size() && showStep(doku, listSymbols, width, height, steps.get(stepCount))){stepCount++;}
|
||||||
case "mixed":
|
|
||||||
new MixedSolver().solve(doku);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
new RandomSolver().solve(doku);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void solve(MultiDoku doku, List<String> listSymbols, int width, int height){
|
||||||
|
System.out.println("Pick a solver to use : random ('random', default), human ('human') or mixed solver ('mixed').");
|
||||||
|
List<SolverStep> steps = new ArrayList<>();
|
||||||
|
doku.getStateManager().pushState();
|
||||||
|
switch (reader.next()) {
|
||||||
|
case "human":
|
||||||
|
new HumanSolver().solve(doku, steps);
|
||||||
|
break;
|
||||||
|
case "mixed":
|
||||||
|
new MixedSolver().solve(doku, steps);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
new RandomSolver().solve(doku, steps);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
showSolveSteps(doku, listSymbols, width, height, steps);
|
||||||
|
}
|
||||||
|
|
||||||
private void play(MultiDoku doku, List<String> listSymbols, int width, int height) {
|
private void play(MultiDoku doku, List<String> listSymbols, int width, int height) {
|
||||||
int x, y;
|
int x, y;
|
||||||
RenderableMultidoku rdoku = RenderableMultidoku.fromMultidoku(doku);
|
RenderableMultidoku rdoku = RenderableMultidoku.fromMultidoku(doku);
|
||||||
|
|||||||
@@ -1,14 +1,9 @@
|
|||||||
package sudoku.solver;
|
package sudoku.solver;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CancellationException;
|
|
||||||
import java.util.logging.Level;
|
|
||||||
|
|
||||||
import gui.constants.Symbols;
|
|
||||||
import sudoku.io.SudokuPrinter;
|
|
||||||
import sudoku.structure.Cell;
|
import sudoku.structure.Cell;
|
||||||
import sudoku.structure.MultiDoku;
|
import sudoku.structure.MultiDoku;
|
||||||
import sudoku.structure.Sudoku;
|
|
||||||
|
|
||||||
public class HumanSolver implements Solver {
|
public class HumanSolver implements Solver {
|
||||||
|
|
||||||
@@ -20,39 +15,25 @@ public class HumanSolver implements Solver {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean solve(MultiDoku doku, List<SolverStep> steps) {
|
public boolean solve(MultiDoku doku, List<SolverStep> steps) {
|
||||||
if (Thread.interrupted())
|
while (!doku.isSolved()) {
|
||||||
throw new CancellationException("User wants to stop the solver");
|
boolean filledCell = false;
|
||||||
|
for (Cell cell : doku.getCells()) {
|
||||||
|
if (!cell.isMutable() || !cell.isEmpty())
|
||||||
|
continue;
|
||||||
|
|
||||||
Sudoku sudoku = doku.getSubGrid(0);
|
List<Integer> possibleSymbols = cell.getPossibleSymbols();
|
||||||
logger.log(Level.FINE,
|
if (possibleSymbols.size() == 1) {
|
||||||
'\n' + SudokuPrinter.toStringRectangleSudoku(sudoku,
|
cell.setSymbolIndex(possibleSymbols.getFirst());
|
||||||
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getBlockWidth(),
|
addStep(cell, steps);
|
||||||
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getSize() / sudoku.getBlockWidth(),
|
filledCell = true;
|
||||||
Symbols.Numbers));
|
|
||||||
|
|
||||||
if (doku.isSolved()) {
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
List<Cell> cellsToFill = doku.getEmptyCells();
|
// on ne peut plus remplir de cases, on abandonne
|
||||||
if (cellsToFill.isEmpty()) {
|
if (!filledCell)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Cell cellToFill : cellsToFill) {
|
return true;
|
||||||
|
|
||||||
List<Integer> possibleSymbols = cellToFill.getPossibleSymbols();
|
|
||||||
if (possibleSymbols.size() != 1) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
cellToFill.setSymbolIndex(possibleSymbols.getFirst());
|
|
||||||
addStep(cellToFill, steps);
|
|
||||||
|
|
||||||
return this.solve(doku, steps);
|
|
||||||
}
|
|
||||||
|
|
||||||
return doku.isSolved();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,26 @@
|
|||||||
package sudoku.solver;
|
package sudoku.solver;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
|
||||||
import java.util.concurrent.CancellationException;
|
import java.util.concurrent.CancellationException;
|
||||||
import java.util.logging.Level;
|
|
||||||
|
|
||||||
import gui.constants.Symbols;
|
|
||||||
import sudoku.io.SudokuPrinter;
|
|
||||||
import sudoku.structure.Cell;
|
import sudoku.structure.Cell;
|
||||||
import sudoku.structure.MultiDoku;
|
import sudoku.structure.MultiDoku;
|
||||||
import sudoku.structure.Sudoku;
|
|
||||||
|
|
||||||
public class MixedSolver implements Solver {
|
public class MixedSolver implements Solver {
|
||||||
|
|
||||||
|
private Cell findCellToBacktrack(MultiDoku doku, int maxPossibilities) {
|
||||||
|
for (Cell cell : doku.getCells()) {
|
||||||
|
if (!cell.isMutable() || !cell.isEmpty())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
List<Integer> possibleSymbols = cell.getPossibleSymbols();
|
||||||
|
if (possibleSymbols.size() == maxPossibilities) {
|
||||||
|
return cell;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Résout le MultiDoku passé en paramètre, avec règles de déduction et
|
* Résout le MultiDoku passé en paramètre, avec règles de déduction et
|
||||||
* backtracking.
|
* backtracking.
|
||||||
@@ -22,55 +30,45 @@ public class MixedSolver implements Solver {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean solve(MultiDoku doku, List<SolverStep> steps) {
|
public boolean solve(MultiDoku doku, List<SolverStep> steps) {
|
||||||
Random rand = new Random();
|
|
||||||
|
|
||||||
if (Thread.interrupted()) {
|
if (Thread.interrupted()) {
|
||||||
throw new CancellationException("User wants to stop the solver");
|
throw new CancellationException("User wants to stop the solver");
|
||||||
}
|
}
|
||||||
|
|
||||||
Sudoku sudoku = doku.getSubGrid(0);
|
while (!doku.isSolved()) {
|
||||||
logger.log(Level.FINE,
|
boolean filledCell = false;
|
||||||
'\n' + SudokuPrinter.toStringRectangleSudoku(
|
for (Cell cell : doku.getCells()) {
|
||||||
sudoku,
|
if (!cell.isMutable() || !cell.isEmpty())
|
||||||
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getBlockWidth(),
|
continue;
|
||||||
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getSize() / sudoku.getBlockWidth(),
|
|
||||||
Symbols.Numbers));
|
|
||||||
|
|
||||||
if (doku.isSolved()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
Cell cellToFill = doku.getFirstEmptyCell();
|
|
||||||
if (cellToFill == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Integer> possibleSymbols = cellToFill.getPossibleSymbols();
|
|
||||||
|
|
||||||
|
List<Integer> possibleSymbols = cell.getPossibleSymbols();
|
||||||
if (possibleSymbols.size() == 1) {
|
if (possibleSymbols.size() == 1) {
|
||||||
cellToFill.setSymbolIndex(possibleSymbols.getFirst());
|
cell.setSymbolIndex(possibleSymbols.getFirst());
|
||||||
addStep(cellToFill, steps);
|
addStep(cell, steps);
|
||||||
if (this.solve(doku, steps)) {
|
filledCell = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// on ne peut plus remplir de cases, on tente de backtrack
|
||||||
|
if (!filledCell) {
|
||||||
|
int maxPossibilities = 2;
|
||||||
|
Cell backtrackCell = null;
|
||||||
|
while (backtrackCell == null) {
|
||||||
|
backtrackCell = findCellToBacktrack(doku, maxPossibilities);
|
||||||
|
maxPossibilities++;
|
||||||
|
}
|
||||||
|
// on fait du backtracking
|
||||||
|
List<Integer> possibilities = backtrackCell.getPossibleSymbols();
|
||||||
|
for (int symbol : possibilities) {
|
||||||
|
doku.getStateManager().pushState();
|
||||||
|
backtrackCell.setSymbolIndex(symbol);
|
||||||
|
if (solve(doku, steps))
|
||||||
|
return true;
|
||||||
|
doku.getStateManager().popState();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
while (!possibleSymbols.isEmpty()) {
|
|
||||||
int nextPossibleSymbolIndex = rand.nextInt(possibleSymbols.size());
|
|
||||||
int nextSymbol = possibleSymbols.get(nextPossibleSymbolIndex);
|
|
||||||
|
|
||||||
cellToFill.setSymbolIndex(nextSymbol);
|
|
||||||
addStep(cellToFill, steps);
|
|
||||||
if (this.solve(doku, steps)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
cellToFill.setSymbolIndex(Cell.NOSYMBOL);
|
|
||||||
addStep(cellToFill, steps);
|
|
||||||
possibleSymbols.remove(nextPossibleSymbolIndex);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,13 +3,9 @@ package sudoku.solver;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.concurrent.CancellationException;
|
import java.util.concurrent.CancellationException;
|
||||||
import java.util.logging.Level;
|
|
||||||
|
|
||||||
import gui.constants.Symbols;
|
|
||||||
import sudoku.io.SudokuPrinter;
|
|
||||||
import sudoku.structure.Cell;
|
import sudoku.structure.Cell;
|
||||||
import sudoku.structure.MultiDoku;
|
import sudoku.structure.MultiDoku;
|
||||||
import sudoku.structure.Sudoku;
|
|
||||||
|
|
||||||
public class RandomSolver implements Solver {
|
public class RandomSolver implements Solver {
|
||||||
|
|
||||||
@@ -28,13 +24,6 @@ public class RandomSolver implements Solver {
|
|||||||
if (Thread.interrupted())
|
if (Thread.interrupted())
|
||||||
throw new CancellationException("User wants to stop the solver");
|
throw new CancellationException("User wants to stop the solver");
|
||||||
|
|
||||||
Sudoku sudoku = doku.getSubGrid(0);
|
|
||||||
logger.log(Level.FINE,
|
|
||||||
'\n' + SudokuPrinter.toStringRectangleSudoku(sudoku,
|
|
||||||
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getBlockWidth(),
|
|
||||||
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getSize() / sudoku.getBlockWidth(),
|
|
||||||
Symbols.Numbers));
|
|
||||||
|
|
||||||
if (doku.isSolved()) {
|
if (doku.isSolved()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package sudoku.solver;
|
package sudoku.solver;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.concurrent.CancellationException;
|
import java.util.concurrent.CancellationException;
|
||||||
|
|
||||||
import sudoku.structure.MultiDoku;
|
import sudoku.structure.MultiDoku;
|
||||||
@@ -8,9 +9,9 @@ import sudoku.structure.Sudoku;
|
|||||||
/**
|
/**
|
||||||
* Class de test non utilisé
|
* Class de test non utilisé
|
||||||
*/
|
*/
|
||||||
public class StupidSolver {
|
public class StupidSolver implements Solver{
|
||||||
|
|
||||||
private static boolean solve(Sudoku sudoku, int index) {
|
private boolean solve(Sudoku sudoku, int index, List<SolverStep> steps) {
|
||||||
// mécanisme d'abandon
|
// mécanisme d'abandon
|
||||||
if (Thread.interrupted())
|
if (Thread.interrupted())
|
||||||
throw new CancellationException("User wants to stop the solver");
|
throw new CancellationException("User wants to stop the solver");
|
||||||
@@ -21,27 +22,30 @@ public class StupidSolver {
|
|||||||
|
|
||||||
// si la case n'est pas modifiable, on passe à la suivante
|
// si la case n'est pas modifiable, on passe à la suivante
|
||||||
if (!sudoku.getCell(index).isMutable())
|
if (!sudoku.getCell(index).isMutable())
|
||||||
return solve(sudoku, index + 1);
|
return solve(sudoku, index + 1, steps);
|
||||||
|
|
||||||
for (int symbol = 0; symbol < sudoku.getSize(); symbol++) {
|
for (int symbol = 0; symbol < sudoku.getSize(); symbol++) {
|
||||||
if (sudoku.getCell(index).trySetValue(symbol)) {
|
if (sudoku.getCell(index).trySetValue(symbol)) {
|
||||||
|
addStep(sudoku.getCell(index), steps);
|
||||||
// on tente de placer sur la case suivante
|
// on tente de placer sur la case suivante
|
||||||
if (solve(sudoku, index + 1)) {
|
if (solve(sudoku, index + 1, steps)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// on a tout essayé et rien n'a fonctionné
|
// on a tout essayé et rien n'a fonctionné
|
||||||
sudoku.getCell(index).empty();
|
sudoku.getCell(index).empty();
|
||||||
|
addStep(sudoku.getCell(index), steps);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean solve(MultiDoku doku) {
|
@Override
|
||||||
|
public boolean solve(MultiDoku doku, List<SolverStep> steps) {
|
||||||
if (doku.isSolved())
|
if (doku.isSolved())
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
for (Sudoku sudoku : doku.getSubGrids()) {
|
for (Sudoku sudoku : doku.getSubGrids()) {
|
||||||
if (!solve(sudoku, 0))
|
if (!solve(sudoku, 0, steps))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 399 KiB After Width: | Height: | Size: 399 KiB |
@@ -1,7 +1,6 @@
|
|||||||
package sudoku;
|
package sudoku;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
@@ -22,7 +21,7 @@ public class SudokuSerializerTest {
|
|||||||
new RandomSolver().solve(sudoku);
|
new RandomSolver().solve(sudoku);
|
||||||
JSONObject data = SudokuSerializer.serializeSudoku(sudoku);
|
JSONObject data = SudokuSerializer.serializeSudoku(sudoku);
|
||||||
MultiDoku multiDoku = SudokuSerializer.deserializeSudoku(data);
|
MultiDoku multiDoku = SudokuSerializer.deserializeSudoku(data);
|
||||||
assertTrue(data.toString().equals(SudokuSerializer.serializeSudoku(multiDoku).toString()));
|
assertEquals(data.toString(), SudokuSerializer.serializeSudoku(multiDoku).toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
void testSaveWithSize(int blockWidth, int blockHeight) {
|
void testSaveWithSize(int blockWidth, int blockHeight) {
|
||||||
@@ -41,7 +40,7 @@ public class SudokuSerializerTest {
|
|||||||
File fileToDelete = new File(savePath);
|
File fileToDelete = new File(savePath);
|
||||||
fileToDelete.delete();
|
fileToDelete.delete();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
System.out.println(e.getMessage());
|
||||||
assert false;
|
assert false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -52,7 +51,7 @@ public class SudokuSerializerTest {
|
|||||||
JSONObject data = SudokuSerializer.serializeSudoku(sudoku);
|
JSONObject data = SudokuSerializer.serializeSudoku(sudoku);
|
||||||
MultiDoku multiDoku = SudokuSerializer.deserializeSudoku(data);
|
MultiDoku multiDoku = SudokuSerializer.deserializeSudoku(data);
|
||||||
|
|
||||||
assertTrue(data.toString().equals(SudokuSerializer.serializeSudoku(multiDoku).toString()));
|
assertEquals(data.toString(), SudokuSerializer.serializeSudoku(multiDoku).toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -60,17 +59,17 @@ public class SudokuSerializerTest {
|
|||||||
Random r = new Random();
|
Random r = new Random();
|
||||||
int testCount = 20;
|
int testCount = 20;
|
||||||
for (int i = 0; i < testCount; i++) {
|
for (int i = 0; i < testCount; i++) {
|
||||||
int blockWidth = r.nextInt(4) + 1;
|
int blockWidth = r.nextInt(10) + 1;
|
||||||
int blockHeight = r.nextInt(4) + 1;
|
int blockHeight = r.nextInt(10) + 1;
|
||||||
testSerializeWithSize(blockWidth, blockHeight);
|
testSerializeWithSize(blockWidth, blockHeight);
|
||||||
}
|
}
|
||||||
for (int i = 0; i < testCount; i++) {
|
for (int i = 0; i < testCount; i++) {
|
||||||
int blockWidth = r.nextInt(4) + 1;
|
int blockWidth = r.nextInt(10) + 1;
|
||||||
int blockHeight = r.nextInt(4) + 1;
|
int blockHeight = r.nextInt(10) + 1;
|
||||||
testSaveWithSize(blockWidth, blockHeight);
|
testSaveWithSize(blockWidth, blockHeight);
|
||||||
}
|
}
|
||||||
for (int i = 0; i < testCount; i++) {
|
for (int i = 0; i < testCount; i++) {
|
||||||
int size = r.nextInt(2) + 2;
|
int size = r.nextInt(10) + 1;
|
||||||
testSerializeX(size);
|
testSerializeX(size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,28 +4,62 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import gui.constants.Symbols;
|
import gui.constants.Symbols;
|
||||||
import sudoku.io.SudokuPrinter;
|
import sudoku.io.SudokuPrinter;
|
||||||
import sudoku.io.SudokuSerializer;
|
import sudoku.io.SudokuSerializer;
|
||||||
import sudoku.structure.Cell;
|
import sudoku.structure.*;
|
||||||
import sudoku.structure.MultiDoku;
|
|
||||||
import sudoku.structure.Sudoku;
|
|
||||||
import sudoku.structure.SudokuFactory;
|
|
||||||
|
|
||||||
class SolverTest {
|
class SolverTest {
|
||||||
|
private int ns = Cell.NOSYMBOL;
|
||||||
|
protected static HumanSolver h;
|
||||||
|
private static RandomSolver r;
|
||||||
|
private static MixedSolver m;
|
||||||
|
|
||||||
@Test
|
@BeforeAll
|
||||||
void solveTest() {
|
public static void initializeSolvers(){
|
||||||
MultiDoku dokuToTest = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
h = new HumanSolver();
|
||||||
MultiDoku dokuResult = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
r = new RandomSolver();
|
||||||
|
m = new MixedSolver();
|
||||||
|
}
|
||||||
|
|
||||||
Sudoku sudokuToTest = dokuToTest.getSubGrid(0);
|
private void testSize2(Solver solver){
|
||||||
Sudoku sudokuResult = dokuResult.getSubGrid(0);
|
MultiDoku mdTest = SudokuFactory.createBasicEmptySquareDoku(2, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||||
|
MultiDoku mdResult = SudokuFactory.createBasicEmptySquareDoku(2, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||||
|
Sudoku test = mdTest.getSubGrid(0);
|
||||||
|
Sudoku result = mdResult.getSubGrid(0);
|
||||||
|
List<Integer> immutableCells = List.of(
|
||||||
|
ns, 2, 3, ns,
|
||||||
|
0, ns, ns, ns,
|
||||||
|
ns, ns, ns, 3,
|
||||||
|
ns, 0, 1, ns);
|
||||||
|
assert (test.setImmutableCellsSymbol(immutableCells));
|
||||||
|
List<Integer> correctCells = List.of(
|
||||||
|
1, 2, 3, 0,
|
||||||
|
0, 3, 2, 1,
|
||||||
|
2, 1, 0, 3,
|
||||||
|
3, 0, 1, 2);
|
||||||
|
assert(result.setCellsSymbol(correctCells));
|
||||||
|
assert(result.isSolved());
|
||||||
|
|
||||||
int ns = Cell.NOSYMBOL;
|
solver.solve(mdTest);
|
||||||
List<Integer> immutableCells = List.of(ns, ns, 0, ns, ns, 2, 8, ns, 1,
|
assert (mdTest.isSolved());
|
||||||
|
for (Cell cell : test.getCells()) {
|
||||||
|
cell.setImmutable();
|
||||||
|
}
|
||||||
|
assertEquals(SudokuSerializer.serializeSudoku(mdTest).toString(),
|
||||||
|
SudokuSerializer.serializeSudoku(mdResult).toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void testSize3(Solver solver){
|
||||||
|
MultiDoku mdTest = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||||
|
MultiDoku mdResult = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||||
|
Sudoku test = mdTest.getSubGrid(0);
|
||||||
|
Sudoku result = mdResult.getSubGrid(0);
|
||||||
|
List<Integer> immutableCells = List.of(
|
||||||
|
ns, ns, 0, ns, ns, 2, 8, ns, 1,
|
||||||
ns, 3, ns, ns, 5, 6, 7, ns, ns,
|
ns, 3, ns, ns, 5, 6, 7, ns, ns,
|
||||||
ns, ns, ns, 8, ns, 7, ns, ns, 6,
|
ns, ns, ns, 8, ns, 7, ns, ns, 6,
|
||||||
0, ns, 1, ns, ns, ns, ns, ns, ns,
|
0, ns, 1, ns, ns, ns, ns, ns, ns,
|
||||||
@@ -34,13 +68,9 @@ class SolverTest {
|
|||||||
ns, ns, 6, ns, ns, 8, ns, 7, 5,
|
ns, ns, 6, ns, ns, 8, ns, 7, 5,
|
||||||
8, 0, ns, 7, ns, 5, 2, ns, 3,
|
8, 0, ns, 7, ns, 5, 2, ns, 3,
|
||||||
5, ns, ns, ns, 3, 1, 0, ns, ns);
|
5, ns, ns, ns, 3, 1, 0, ns, ns);
|
||||||
|
assert (test.setImmutableCellsSymbol(immutableCells));
|
||||||
assert (sudokuToTest.setImmutableCellsSymbol(immutableCells));
|
List<Integer> correctCells = List.of(
|
||||||
|
7, 6, 0, 3, 4, 2, 8, 5, 1,
|
||||||
//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,
|
2, 3, 8, 1, 5, 6, 7, 0, 4,
|
||||||
1, 4, 5, 8, 0, 7, 3, 2, 6,
|
1, 4, 5, 8, 0, 7, 3, 2, 6,
|
||||||
0, 2, 1, 6, 8, 3, 5, 4, 7,
|
0, 2, 1, 6, 8, 3, 5, 4, 7,
|
||||||
@@ -49,56 +79,46 @@ class SolverTest {
|
|||||||
3, 1, 6, 0, 2, 8, 4, 7, 5,
|
3, 1, 6, 0, 2, 8, 4, 7, 5,
|
||||||
8, 0, 4, 7, 6, 5, 2, 1, 3,
|
8, 0, 4, 7, 6, 5, 2, 1, 3,
|
||||||
5, 7, 2, 4, 3, 1, 0, 6, 8);
|
5, 7, 2, 4, 3, 1, 0, 6, 8);
|
||||||
|
assert(result.setCellsSymbol(correctCells));
|
||||||
|
assert(result.isSolved());
|
||||||
|
|
||||||
sudokuResult.setCellsSymbol(correctCells);
|
solver.solve(mdTest);
|
||||||
|
assert (mdTest.isSolved());
|
||||||
System.out.println("\n****************************Doku Control\n");
|
for (Cell cell : test.getCells()) {
|
||||||
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.printMultiDoku(dokuToTest, 3, 3, Symbols.Emojis);
|
|
||||||
|
|
||||||
assert (dokuToTest.isSolved());
|
|
||||||
|
|
||||||
for (Cell cell : sudokuToTest.getCells()) {
|
|
||||||
cell.setImmutable();
|
cell.setImmutable();
|
||||||
}
|
}
|
||||||
|
assertEquals(SudokuSerializer.serializeSudoku(mdTest).toString(),
|
||||||
|
SudokuSerializer.serializeSudoku(mdResult).toString());
|
||||||
|
}
|
||||||
|
|
||||||
for (Cell cell : sudokuResult.getCells()) {
|
private void testMDSize3(Solver solver){
|
||||||
|
MultiDoku mdTest = SudokuFactory.createBasicXShapedMultidoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||||
|
try {
|
||||||
|
SudokuFactory.fillDoku(mdTest, Difficulty.Easy);
|
||||||
|
} catch (Exception e) {
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
MultiDoku result = SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(mdTest));
|
||||||
|
assert(result.isSolved());
|
||||||
|
solver.solve(mdTest);
|
||||||
|
assert (mdTest.isSolved());
|
||||||
|
for (Cell cell : mdTest.getCells()) {
|
||||||
cell.setImmutable();
|
cell.setImmutable();
|
||||||
}
|
}
|
||||||
|
assertEquals(SudokuSerializer.serializeSudoku(mdTest).toString(),
|
||||||
|
SudokuSerializer.serializeSudoku(result).toString());
|
||||||
|
}
|
||||||
|
|
||||||
assertEquals(SudokuSerializer.serializeSudoku(dokuResult).toString(),
|
@Test
|
||||||
SudokuSerializer.serializeSudoku(dokuToTest).toString());
|
void solveTest() {
|
||||||
|
testSize2(h);
|
||||||
MultiDoku dokuToTest2 = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
testSize3(h);
|
||||||
Sudoku sudokuToTest2 = dokuToTest2.getSubGrid(0);
|
testMDSize3(h);
|
||||||
|
testSize2(m);
|
||||||
List<Integer> immutableCells2 = List.of(ns, ns, 0, ns, ns, 2, 8, ns, 1,
|
testSize3(m);
|
||||||
1, 3, ns, ns, 5, 6, 7, ns, ns,
|
testMDSize3(m);
|
||||||
ns, ns, ns, 8, ns, 7, ns, ns, 6,
|
testSize2(r);
|
||||||
0, ns, 1, ns, ns, ns, ns, ns, ns,
|
testSize3(r);
|
||||||
4, 8, 7, 5, 1, ns, 6, ns, ns,
|
testMDSize3(r);
|
||||||
6, ns, 3, 2, ns, ns, ns, 8, 0,
|
|
||||||
ns, ns, 6, ns, ns, 8, ns, 7, 5,
|
|
||||||
8, 0, ns, 7, ns, 5, 2, ns, 3,
|
|
||||||
5, ns, ns, ns, 3, 1, 0, ns, ns);
|
|
||||||
sudokuToTest2.setImmutableCellsSymbol(immutableCells2);
|
|
||||||
|
|
||||||
boolean isSolved = new RandomSolver().solve(dokuToTest2);
|
|
||||||
|
|
||||||
assert (!isSolved);
|
|
||||||
|
|
||||||
MultiDoku dokuToTest3 = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
|
||||||
|
|
||||||
new RandomSolver().solve(dokuToTest3);
|
|
||||||
|
|
||||||
//SudokuPrinter.printRectangleSudoku(dokuToTest3.getSubGrid(0), 3, 3);
|
|
||||||
SudokuPrinter.printMultiDoku(dokuToTest3, 3, 3, Symbols.Letters);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user