Compare commits
60 Commits
6801bd2efc
...
HAAAAAAAAA
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0795f9256d | ||
| a74bf42e59 | |||
| 1f92c49f3c | |||
| c262007ca8 | |||
| 73f572ee18 | |||
| ec07d88577 | |||
| 6caf5d5c54 | |||
|
|
696062e9db | ||
|
|
c825807aea | ||
| 39529e0b64 | |||
| 3fa22a4cf0 | |||
| 337911d038 | |||
| 1930bc02bd | |||
| b9788d6a51 | |||
| 7789209651 | |||
|
|
54a4bd9f22 | ||
| 8596781ce3 | |||
|
|
d6c3504bc7 | ||
| 44a4432ac0 | |||
| a42ec2f27c | |||
| bff7abcd02 | |||
|
|
bf67afe4a9 | ||
| 67da77af2e | |||
| c4becf2d55 | |||
|
|
f1d963e546 | ||
| b9a6617967 | |||
| 03f577828b | |||
| 9b776d4b0f | |||
| d4beaec8a8 | |||
| a742cace3a | |||
|
|
e12955ab4a | ||
|
|
412684e6f0 | ||
|
|
cd4d01e1e6 | ||
| ff85cbef01 | |||
| c16f2b8f5a | |||
| 5e26bea609 | |||
| cd792a0f8a | |||
| de1f3c59d6 | |||
| c0a3f85f24 | |||
| a616ab63e4 | |||
| 00866256a7 | |||
| ed9b636b58 | |||
| a10d2eda9a | |||
| c4a9bf6354 | |||
| 074131b69c | |||
| 21b7011ece | |||
| 859d5795d0 | |||
| cd81334c95 | |||
| 89653f8517 | |||
| 9213a10c17 | |||
| 4190bf15d8 | |||
|
|
a221233c06 | ||
| 5ad8fa30f4 | |||
| 532b2d152b | |||
| ec36e19c4e | |||
|
|
aa86e9b956 | ||
| b8553428ee | |||
|
|
7c06fe2e31 | ||
|
|
cd45d1c22c | ||
|
|
c7217894b2 |
21
README.md
Normal file
21
README.md
Normal file
@@ -0,0 +1,21 @@
|
||||
# Sudoku 🧩
|
||||
|
||||
## Features 🌟
|
||||
|
||||
- Graphical User Interface (GUI)
|
||||
- Sudoku saves
|
||||
- Multiplayer
|
||||
|
||||
## Develop ☝🤓
|
||||
|
||||
### Run 🏃
|
||||
|
||||
```sh
|
||||
./gradlew run
|
||||
```
|
||||
|
||||
### Run tests 🗣️🔥
|
||||
|
||||
```sh
|
||||
./gradlew test
|
||||
```
|
||||
BIN
app/background.png
Normal file
BIN
app/background.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 399 KiB |
@@ -11,9 +11,14 @@ plugins {
|
||||
id 'application'
|
||||
}
|
||||
|
||||
project.ext.os = System.properties['os.name'].toLowerCase().split(" ")[0]
|
||||
|
||||
repositories {
|
||||
// Use Maven Central for resolving dependencies.
|
||||
mavenCentral()
|
||||
flatDir {
|
||||
dirs("$rootProject.projectDir/libs")
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
@@ -31,6 +36,10 @@ dependencies {
|
||||
implementation 'com.fasterxml.jackson.core:jackson-databind:2.18.2'
|
||||
|
||||
implementation "io.github.spair:imgui-java-app:1.88.0"
|
||||
|
||||
implementation "org.lwjgl:lwjgl-stb:3.3.4"
|
||||
|
||||
runtimeOnly "org.lwjgl:lwjgl-stb::natives-$os"
|
||||
}
|
||||
|
||||
application {
|
||||
|
||||
0
app/save/.gitignore
vendored
Normal file
0
app/save/.gitignore
vendored
Normal file
572
app/save/save-1.json
Normal file
572
app/save/save-1.json
Normal file
@@ -0,0 +1,572 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"blockID": 0,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 0,
|
||||
"immutable": true,
|
||||
"symbolIndex": 8
|
||||
},
|
||||
{
|
||||
"blockID": 0,
|
||||
"immutable": true,
|
||||
"symbolIndex": 1
|
||||
},
|
||||
{
|
||||
"blockID": 1,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 1,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 1,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 2,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 2,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 2,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 0,
|
||||
"immutable": true,
|
||||
"symbolIndex": 2
|
||||
},
|
||||
{
|
||||
"blockID": 0,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 0,
|
||||
"immutable": true,
|
||||
"symbolIndex": 4
|
||||
},
|
||||
{
|
||||
"blockID": 1,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 1,
|
||||
"symbolIndex": 0
|
||||
},
|
||||
{
|
||||
"blockID": 1,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 2,
|
||||
"immutable": true,
|
||||
"symbolIndex": 6
|
||||
},
|
||||
{
|
||||
"blockID": 2,
|
||||
"immutable": true,
|
||||
"symbolIndex": 8
|
||||
},
|
||||
{
|
||||
"blockID": 2,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 0,
|
||||
"immutable": true,
|
||||
"symbolIndex": 3
|
||||
},
|
||||
{
|
||||
"blockID": 0,
|
||||
"immutable": true,
|
||||
"symbolIndex": 6
|
||||
},
|
||||
{
|
||||
"blockID": 0,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 1,
|
||||
"immutable": true,
|
||||
"symbolIndex": 5
|
||||
},
|
||||
{
|
||||
"blockID": 1,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 1,
|
||||
"immutable": true,
|
||||
"symbolIndex": 7
|
||||
},
|
||||
{
|
||||
"blockID": 2,
|
||||
"immutable": true,
|
||||
"symbolIndex": 2
|
||||
},
|
||||
{
|
||||
"blockID": 2,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 2,
|
||||
"immutable": true,
|
||||
"symbolIndex": 4
|
||||
},
|
||||
{
|
||||
"blockID": 3,
|
||||
"immutable": true,
|
||||
"symbolIndex": 8
|
||||
},
|
||||
{
|
||||
"blockID": 3,
|
||||
"immutable": true,
|
||||
"symbolIndex": 2
|
||||
},
|
||||
{
|
||||
"blockID": 3,
|
||||
"immutable": true,
|
||||
"symbolIndex": 3
|
||||
},
|
||||
{
|
||||
"blockID": 4,
|
||||
"immutable": true,
|
||||
"symbolIndex": 6
|
||||
},
|
||||
{
|
||||
"blockID": 4,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 4,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 5,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 5,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 5,
|
||||
"immutable": true,
|
||||
"symbolIndex": 1
|
||||
},
|
||||
{
|
||||
"blockID": 3,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 3,
|
||||
"immutable": true,
|
||||
"symbolIndex": 4
|
||||
},
|
||||
{
|
||||
"blockID": 3,
|
||||
"immutable": true,
|
||||
"symbolIndex": 5
|
||||
},
|
||||
{
|
||||
"blockID": 4,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 4,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 4,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 5,
|
||||
"immutable": true,
|
||||
"symbolIndex": 8
|
||||
},
|
||||
{
|
||||
"blockID": 5,
|
||||
"immutable": true,
|
||||
"symbolIndex": 6
|
||||
},
|
||||
{
|
||||
"blockID": 5,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 3,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 3,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 3,
|
||||
"immutable": true,
|
||||
"symbolIndex": 6
|
||||
},
|
||||
{
|
||||
"blockID": 4,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 4,
|
||||
"immutable": true,
|
||||
"symbolIndex": 7
|
||||
},
|
||||
{
|
||||
"blockID": 4,
|
||||
"immutable": true,
|
||||
"symbolIndex": 8
|
||||
},
|
||||
{
|
||||
"blockID": 5,
|
||||
"immutable": true,
|
||||
"symbolIndex": 4
|
||||
},
|
||||
{
|
||||
"blockID": 5,
|
||||
"immutable": true,
|
||||
"symbolIndex": 5
|
||||
},
|
||||
{
|
||||
"blockID": 5,
|
||||
"immutable": true,
|
||||
"symbolIndex": 3
|
||||
},
|
||||
{
|
||||
"blockID": 6,
|
||||
"immutable": true,
|
||||
"symbolIndex": 4
|
||||
},
|
||||
{
|
||||
"blockID": 6,
|
||||
"immutable": true,
|
||||
"symbolIndex": 3
|
||||
},
|
||||
{
|
||||
"blockID": 6,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 7,
|
||||
"immutable": true,
|
||||
"symbolIndex": 8
|
||||
},
|
||||
{
|
||||
"blockID": 7,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 7,
|
||||
"immutable": true,
|
||||
"symbolIndex": 1
|
||||
},
|
||||
{
|
||||
"blockID": 8,
|
||||
"immutable": true,
|
||||
"symbolIndex": 5
|
||||
},
|
||||
{
|
||||
"blockID": 8,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 8,
|
||||
"immutable": true,
|
||||
"symbolIndex": 6
|
||||
},
|
||||
{
|
||||
"blockID": 6,
|
||||
"immutable": true,
|
||||
"symbolIndex": 1
|
||||
},
|
||||
{
|
||||
"blockID": 6,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 6,
|
||||
"immutable": true,
|
||||
"symbolIndex": 8
|
||||
},
|
||||
{
|
||||
"blockID": 7,
|
||||
"immutable": true,
|
||||
"symbolIndex": 3
|
||||
},
|
||||
{
|
||||
"blockID": 7,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 7,
|
||||
"immutable": true,
|
||||
"symbolIndex": 6
|
||||
},
|
||||
{
|
||||
"blockID": 8,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 8,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 8,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 6,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 6,
|
||||
"immutable": true,
|
||||
"symbolIndex": 0
|
||||
},
|
||||
{
|
||||
"blockID": 6,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 7,
|
||||
"immutable": true,
|
||||
"symbolIndex": 7
|
||||
},
|
||||
{
|
||||
"blockID": 7,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 7,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 8,
|
||||
"immutable": true,
|
||||
"symbolIndex": 1
|
||||
},
|
||||
{
|
||||
"blockID": 8,
|
||||
"symbolIndex": -1
|
||||
},
|
||||
{
|
||||
"blockID": 8,
|
||||
"immutable": true,
|
||||
"symbolIndex": 8
|
||||
}
|
||||
],
|
||||
"blocks": [
|
||||
{"cellIDs": [
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
9,
|
||||
10,
|
||||
11,
|
||||
18,
|
||||
19,
|
||||
20
|
||||
]},
|
||||
{"cellIDs": [
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
12,
|
||||
13,
|
||||
14,
|
||||
21,
|
||||
22,
|
||||
23
|
||||
]},
|
||||
{"cellIDs": [
|
||||
6,
|
||||
7,
|
||||
8,
|
||||
15,
|
||||
16,
|
||||
17,
|
||||
24,
|
||||
25,
|
||||
26
|
||||
]},
|
||||
{"cellIDs": [
|
||||
27,
|
||||
28,
|
||||
29,
|
||||
36,
|
||||
37,
|
||||
38,
|
||||
45,
|
||||
46,
|
||||
47
|
||||
]},
|
||||
{"cellIDs": [
|
||||
30,
|
||||
31,
|
||||
32,
|
||||
39,
|
||||
40,
|
||||
41,
|
||||
48,
|
||||
49,
|
||||
50
|
||||
]},
|
||||
{"cellIDs": [
|
||||
33,
|
||||
34,
|
||||
35,
|
||||
42,
|
||||
43,
|
||||
44,
|
||||
51,
|
||||
52,
|
||||
53
|
||||
]},
|
||||
{"cellIDs": [
|
||||
54,
|
||||
55,
|
||||
56,
|
||||
63,
|
||||
64,
|
||||
65,
|
||||
72,
|
||||
73,
|
||||
74
|
||||
]},
|
||||
{"cellIDs": [
|
||||
57,
|
||||
58,
|
||||
59,
|
||||
66,
|
||||
67,
|
||||
68,
|
||||
75,
|
||||
76,
|
||||
77
|
||||
]},
|
||||
{"cellIDs": [
|
||||
60,
|
||||
61,
|
||||
62,
|
||||
69,
|
||||
70,
|
||||
71,
|
||||
78,
|
||||
79,
|
||||
80
|
||||
]}
|
||||
],
|
||||
"multidoku": [{
|
||||
"blockWidth": 3,
|
||||
"cells": [
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
6,
|
||||
7,
|
||||
8,
|
||||
9,
|
||||
10,
|
||||
11,
|
||||
12,
|
||||
13,
|
||||
14,
|
||||
15,
|
||||
16,
|
||||
17,
|
||||
18,
|
||||
19,
|
||||
20,
|
||||
21,
|
||||
22,
|
||||
23,
|
||||
24,
|
||||
25,
|
||||
26,
|
||||
27,
|
||||
28,
|
||||
29,
|
||||
30,
|
||||
31,
|
||||
32,
|
||||
33,
|
||||
34,
|
||||
35,
|
||||
36,
|
||||
37,
|
||||
38,
|
||||
39,
|
||||
40,
|
||||
41,
|
||||
42,
|
||||
43,
|
||||
44,
|
||||
45,
|
||||
46,
|
||||
47,
|
||||
48,
|
||||
49,
|
||||
50,
|
||||
51,
|
||||
52,
|
||||
53,
|
||||
54,
|
||||
55,
|
||||
56,
|
||||
57,
|
||||
58,
|
||||
59,
|
||||
60,
|
||||
61,
|
||||
62,
|
||||
63,
|
||||
64,
|
||||
65,
|
||||
66,
|
||||
67,
|
||||
68,
|
||||
69,
|
||||
70,
|
||||
71,
|
||||
72,
|
||||
73,
|
||||
74,
|
||||
75,
|
||||
76,
|
||||
77,
|
||||
78,
|
||||
79,
|
||||
80
|
||||
],
|
||||
"blocks": [
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
6,
|
||||
7,
|
||||
8
|
||||
],
|
||||
"constraints": [
|
||||
0,
|
||||
1,
|
||||
2
|
||||
]
|
||||
}]
|
||||
}
|
||||
10615
app/save/save.json
10615
app/save/save.json
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,8 @@
|
||||
package gui;
|
||||
|
||||
import imgui.ImFont;
|
||||
import imgui.ImFontConfig;
|
||||
import imgui.ImFontGlyphRangesBuilder;
|
||||
import imgui.ImGui;
|
||||
|
||||
public class Fonts {
|
||||
@@ -10,13 +12,23 @@ public class Fonts {
|
||||
public static ImFont CHERI;
|
||||
public static ImFont COMIC;
|
||||
public static ImFont INFECTED;
|
||||
public static ImFont EMOJIS;
|
||||
|
||||
private static final String baseDir = "";
|
||||
|
||||
public static void createFonts() {
|
||||
COMIC = ImGui.getIO().getFonts().addFontFromFileTTF("comic.ttf", 50.0f);
|
||||
ARIAL_BOLD = ImGui.getIO().getFonts().addFontFromFileTTF("arial_bold.ttf", 50.0f);
|
||||
ARIAL = ImGui.getIO().getFonts().addFontFromFileTTF("arial.ttf", 50.0f);
|
||||
CHERI = ImGui.getIO().getFonts().addFontFromFileTTF("cheri.ttf", 50.0f);
|
||||
INFECTED = ImGui.getIO().getFonts().addFontFromFileTTF("INFECTED.ttf", 50.0f);
|
||||
ImFontGlyphRangesBuilder builder = new ImFontGlyphRangesBuilder();
|
||||
builder.addRanges(ImGui.getIO().getFonts().getGlyphRangesDefault());
|
||||
builder.addRanges(ImGui.getIO().getFonts().getGlyphRangesCyrillic());
|
||||
// builder.addRanges(ImGui.getIO().getFonts().getGlyphRangesChineseFull());
|
||||
ImFontConfig cfg = new ImFontConfig();
|
||||
cfg.setGlyphRanges(builder.buildRanges());
|
||||
|
||||
COMIC = ImGui.getIO().getFonts().addFontFromFileTTF(baseDir + "comic.ttf", 50.0f);
|
||||
ARIAL_BOLD = ImGui.getIO().getFonts().addFontFromFileTTF(baseDir + "arial_bold.ttf", 50.0f);
|
||||
ARIAL = ImGui.getIO().getFonts().addFontFromFileTTF(baseDir + "arial.ttf", 50.0f, cfg);
|
||||
CHERI = ImGui.getIO().getFonts().addFontFromFileTTF(baseDir + "cheri.ttf", 50.0f);
|
||||
INFECTED = ImGui.getIO().getFonts().addFontFromFileTTF(baseDir + "INFECTED.ttf", 50.0f);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
39
app/src/main/java/gui/Images.java
Normal file
39
app/src/main/java/gui/Images.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package gui;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.stb.STBImage;
|
||||
|
||||
public class Images {
|
||||
|
||||
public static int BACKGROUND;
|
||||
|
||||
private static int loadTexture(String fileName) {
|
||||
int[] width = new int[1];
|
||||
int[] height = new int[1];
|
||||
int[] channelCount = new int[1];
|
||||
|
||||
ByteBuffer pixels = STBImage.stbi_load(fileName, width, height, channelCount, 4);
|
||||
|
||||
int textureID = GL11.glGenTextures();
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
|
||||
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
|
||||
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
|
||||
GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1); // Not on WebGL/ES
|
||||
GL11.glPixelStorei(GL11.GL_UNPACK_SKIP_PIXELS, 0); // Not on WebGL/ES
|
||||
GL11.glPixelStorei(GL11.GL_UNPACK_SKIP_ROWS, 0); // Not on WebGL/ES
|
||||
GL11.glPixelStorei(GL11.GL_UNPACK_ROW_LENGTH, 0); // Not on WebGL/ES
|
||||
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width[0], height[0], 0,
|
||||
GL11.GL_RGBA,
|
||||
GL11.GL_UNSIGNED_BYTE, pixels);
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
|
||||
|
||||
return textureID;
|
||||
}
|
||||
|
||||
public static void loadImages() {
|
||||
BACKGROUND = loadTexture("background.png");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -26,6 +26,12 @@ public class Main extends Application {
|
||||
Fonts.createFonts();
|
||||
stateMachine.pushState(new MainMenu(stateMachine));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void preRun() {
|
||||
super.preRun();
|
||||
Images.loadImages();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
|
||||
7
app/src/main/java/gui/Options.java
Normal file
7
app/src/main/java/gui/Options.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package gui;
|
||||
|
||||
public class Options {
|
||||
|
||||
public static Symbols Symboles = Symbols.Numbers;
|
||||
|
||||
}
|
||||
@@ -47,22 +47,6 @@ public class RenderableMultidoku {
|
||||
return cells.get(index);
|
||||
}
|
||||
|
||||
public boolean setCellValue(Cell cell, int value) {
|
||||
for (Sudoku s : doku.getSubGrids()) {
|
||||
int cellIndex = s.getCells().indexOf(cell);
|
||||
// la cellule existe
|
||||
if (cellIndex != -1) {
|
||||
int cellX = cellIndex % s.getSize();
|
||||
int cellY = cellIndex / s.getSize();
|
||||
if (!s.canBePlaced(cellX, cellY, value)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
cell.setSymbolIndex(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static record PositionConstraint(Sudoku sudoku1, Sudoku sudoku2, Coordinate offset) {
|
||||
}
|
||||
|
||||
|
||||
@@ -1,18 +1,23 @@
|
||||
package gui;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import common.Signal;
|
||||
import gui.ColorGenerator.Color;
|
||||
import imgui.ImGui;
|
||||
import imgui.ImVec2;
|
||||
import imgui.ImVec4;
|
||||
import imgui.flag.ImGuiCol;
|
||||
import imgui.flag.ImGuiStyleVar;
|
||||
import sudoku.constraint.Constraint;
|
||||
import sudoku.structure.Block;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Sudoku;
|
||||
|
||||
public class SudokuRenderer {
|
||||
|
||||
@@ -20,9 +25,30 @@ public class SudokuRenderer {
|
||||
private Cell currentCell = null;
|
||||
private final Map<Block, Color> colorPalette;
|
||||
|
||||
private static final ImVec4 BLACK = new ImVec4(0, 0, 0, 1);
|
||||
private static final ImVec4 TRANSPARENT = new ImVec4();
|
||||
private static final ImVec4 WHITE = new ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
private static final ImVec2 cellSize = new ImVec2(50, 50);
|
||||
|
||||
private final Set<Cell> diagonals = new HashSet<>();
|
||||
|
||||
public final Signal onResolve = new Signal();
|
||||
|
||||
public SudokuRenderer(MultiDoku doku) {
|
||||
this.doku = RenderableMultidoku.fromMultidoku(doku);
|
||||
this.colorPalette = initColors();
|
||||
initDiagonals();
|
||||
}
|
||||
|
||||
private void initDiagonals() {
|
||||
for (Sudoku sudoku : this.doku.getDoku().getSubGrids()) {
|
||||
if (sudoku.hasConstraint(Constraint.Diagonal)) {
|
||||
for (int i = 0; i < sudoku.getSize(); i++) {
|
||||
this.diagonals.add(sudoku.getCell(i, i));
|
||||
this.diagonals.add(sudoku.getCell(sudoku.getSize() - i - 1, i));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Map<Block, Color> initColors() {
|
||||
@@ -37,7 +63,6 @@ public class SudokuRenderer {
|
||||
}
|
||||
|
||||
private void renderPopup() {
|
||||
final ImVec2 buttonSize = new ImVec2(50, 50);
|
||||
if (ImGui.beginPopup("editPopup")) {
|
||||
Block block = currentCell.getBlock();
|
||||
int symbolCount = block.getCells().size();
|
||||
@@ -45,13 +70,15 @@ public class SudokuRenderer {
|
||||
if ((i + 1) % (int) (Math.sqrt(symbolCount)) != 1)
|
||||
ImGui.sameLine();
|
||||
if (currentCell.getSymbolIndex() == i) {
|
||||
if (ImGui.button("X", buttonSize)) {
|
||||
if (ImGui.button("X", cellSize)) {
|
||||
currentCell.setSymbolIndex(Cell.NOSYMBOL);
|
||||
ImGui.closeCurrentPopup();
|
||||
}
|
||||
}else {
|
||||
if (ImGui.button(Integer.toString(i + 1), buttonSize)) {
|
||||
this.doku.setCellValue(currentCell, i);
|
||||
} else {
|
||||
if (ImGui.button(Options.Symboles.getSymbols().get(i), cellSize)) {
|
||||
currentCell.trySetValue(i);
|
||||
if (this.doku.getDoku().isSolved())
|
||||
this.onResolve.emit();
|
||||
ImGui.closeCurrentPopup();
|
||||
}
|
||||
}
|
||||
@@ -61,9 +88,20 @@ public class SudokuRenderer {
|
||||
}
|
||||
|
||||
public void render() {
|
||||
if (Options.Symboles == Symbols.Russian) {
|
||||
ImGui.pushFont(Fonts.ARIAL);
|
||||
}
|
||||
final float sudokuViewWidth = cellSize.x * doku.getWidth();
|
||||
final float displayWidth = ImGui.getIO().getDisplaySizeX();
|
||||
float offsetX = displayWidth / 2.0f - sudokuViewWidth / 2.0f;
|
||||
// if the grid is too big, don't offset it
|
||||
if (offsetX > 0) {
|
||||
ImGui.setCursorPosX(offsetX);
|
||||
}
|
||||
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));
|
||||
ImGui.pushStyleColor(ImGuiCol.Border, new ImVec4(0.0f, 0.0f, 0.0f, 1.0f));
|
||||
for (int y = 0; y < doku.getHeight(); y++) {
|
||||
for (int x = 0; x < doku.getWidth(); x++) {
|
||||
if (x > 0)
|
||||
@@ -71,37 +109,38 @@ public class SudokuRenderer {
|
||||
int index = y * doku.getWidth() + x;
|
||||
Cell cell = doku.getCell(x, y);
|
||||
if (cell == null) {
|
||||
ImGui.pushStyleColor(ImGuiCol.Button, new ImVec4(0.0f, 0.0f, 0.0f, 1.0f));
|
||||
ImGui.button("##" + index, new ImVec2(50, 50));
|
||||
ImGui.pushStyleColor(ImGuiCol.Border, TRANSPARENT);
|
||||
ImGui.pushStyleColor(ImGuiCol.Button, TRANSPARENT);
|
||||
ImGui.button("##" + index, cellSize);
|
||||
} else {
|
||||
if (diagonals.contains(cell)) {
|
||||
ImGui.pushStyleColor(ImGuiCol.Border, WHITE);
|
||||
} else {
|
||||
ImGui.pushStyleColor(ImGuiCol.Border, BLACK);
|
||||
}
|
||||
int symbol = cell.getSymbolIndex();
|
||||
Color blockColor = colorPalette.get(cell.getBlock());
|
||||
if (!cell.isMutable()) {
|
||||
// ImGui.pushFont(Fonts.ARIAL_BOLD);
|
||||
blockColor = new Color(blockColor.r - 0.20f, blockColor.g - 0.20f, blockColor.b - 0.20f);
|
||||
// ImGui.pushStyleColor(ImGuiCol.Text, new ImVec4(0.1f, 0.1f, 0.1f, 1.0f));
|
||||
} else {
|
||||
// ImGui.pushFont(Fonts.CHERI);
|
||||
}
|
||||
ImGui.pushStyleColor(ImGuiCol.Button, new ImVec4(blockColor.r, blockColor.g, blockColor.b, 1.0f));
|
||||
String cellText = "";
|
||||
if (symbol != -1)
|
||||
cellText += Integer.toString(symbol + 1);
|
||||
if (ImGui.button(cellText + "##" + index, new ImVec2(50, 50))) {
|
||||
cellText += Options.Symboles.getSymbols().get(symbol);
|
||||
if (ImGui.button(cellText + "##" + index, cellSize) && cell.isMutable()) {
|
||||
ImGui.openPopup("editPopup");
|
||||
currentCell = cell;
|
||||
}
|
||||
if (!cell.isMutable()) {
|
||||
// ImGui.popStyleColor();
|
||||
}
|
||||
// ImGui.popFont();
|
||||
}
|
||||
ImGui.popStyleColor();
|
||||
ImGui.popStyleColor(2);
|
||||
}
|
||||
}
|
||||
ImGui.popStyleColor();
|
||||
ImGui.popStyleVar(2);
|
||||
renderPopup();
|
||||
ImGui.endChild();
|
||||
if (Options.Symboles == Symbols.Russian) {
|
||||
ImGui.popFont();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
136
app/src/main/java/gui/SudokuSelector.java
Normal file
136
app/src/main/java/gui/SudokuSelector.java
Normal file
@@ -0,0 +1,136 @@
|
||||
package gui;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import common.Signal;
|
||||
import imgui.ImGui;
|
||||
import imgui.extension.imguifiledialog.ImGuiFileDialog;
|
||||
import imgui.extension.imguifiledialog.flag.ImGuiFileDialogFlags;
|
||||
import imgui.type.ImBoolean;
|
||||
import imgui.type.ImInt;
|
||||
import sudoku.constraint.Constraint;
|
||||
import sudoku.structure.Difficulty;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.SudokuFactory;
|
||||
|
||||
public class SudokuSelector {
|
||||
|
||||
public final Signal onSelect = new Signal();
|
||||
private MultiDoku doku;
|
||||
|
||||
private final boolean canGenEmptyGrid;
|
||||
|
||||
private final ImInt sudokuType = new ImInt(0);
|
||||
|
||||
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);
|
||||
|
||||
public SudokuSelector(boolean canGenEmptyGrid) {
|
||||
this.canGenEmptyGrid = canGenEmptyGrid;
|
||||
initConstraints();
|
||||
}
|
||||
|
||||
private List<Constraint> getConstraints() {
|
||||
List<Constraint> constraints = new ArrayList<>();
|
||||
for (int i = 0; i < this.contraints.size(); i++) {
|
||||
if (this.contraints.get(i).get())
|
||||
constraints.add(Constraint.values()[i]);
|
||||
}
|
||||
return constraints;
|
||||
}
|
||||
|
||||
private void initConstraints() {
|
||||
for (Constraint cons : Constraint.values()) {
|
||||
contraints.add(new ImBoolean(SudokuFactory.DEFAULT_CONSTRAINTS.contains(cons)));
|
||||
}
|
||||
}
|
||||
|
||||
private void selectSudoku(MultiDoku doku, boolean empty) {
|
||||
this.doku = doku;
|
||||
if (!empty) {
|
||||
try {
|
||||
SudokuFactory.fillDoku(doku, Difficulty.values()[difficulty.get()]);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
this.onSelect.emit();
|
||||
}
|
||||
|
||||
public void renderFileDialog() {
|
||||
if (ImGuiFileDialog.display("browse-sudoku", ImGuiFileDialogFlags.None)) {
|
||||
if (ImGuiFileDialog.isOk()) {
|
||||
var selection = ImGuiFileDialog.getSelection();
|
||||
for (var entry : selection.entrySet()) {
|
||||
try {
|
||||
String filePath = entry.getValue();
|
||||
this.doku = SudokuFactory.fromfile(filePath);
|
||||
if (this.doku != null)
|
||||
this.onSelect.emit();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
ImGuiFileDialog.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void render() {
|
||||
ImGui.combo("Difficulté", difficulty, Difficulty.getDifficultyNames());
|
||||
if (ImGui.treeNode("Constraintes")) {
|
||||
for (Constraint cons : Constraint.values()) {
|
||||
ImGui.checkbox(cons.getDisplayName(), contraints.get(cons.ordinal()));
|
||||
}
|
||||
ImGui.treePop();
|
||||
}
|
||||
ImGui.combo("Type de Sudoku", sudokuType, SudokuType.getTypeNames());
|
||||
SudokuType currentType = SudokuType.values()[sudokuType.get()];
|
||||
switch (currentType.getMakerParamCount()) {
|
||||
case 1:
|
||||
ImGui.inputInt("Taille", sudokuSize);
|
||||
if (ImGui.button("Résoudre un sudoku")) {
|
||||
selectSudoku(currentType.createDoku(getConstraints(), sudokuSize.get()), false);
|
||||
}
|
||||
if (canGenEmptyGrid && ImGui.button("Générer une grille vide")) {
|
||||
selectSudoku(currentType.createDoku(getConstraints(), sudokuSize.get()), true);
|
||||
}
|
||||
break;
|
||||
|
||||
case 2:
|
||||
ImGui.inputInt("Largeur", sudokuHeight);
|
||||
ImGui.inputInt("Longueur", sudokuWidth);
|
||||
if (ImGui.button("Résoudre un sudoku")) {
|
||||
selectSudoku(currentType.createDoku(getConstraints(), sudokuWidth.get(), sudokuHeight.get()),
|
||||
false);
|
||||
}
|
||||
if (canGenEmptyGrid && ImGui.button("Générer une grille vide")) {
|
||||
selectSudoku(currentType.createDoku(getConstraints(), sudokuWidth.get(), sudokuHeight.get()), true);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
assert (false);
|
||||
break;
|
||||
}
|
||||
|
||||
if (ImGui.button("À partir d'un fichier")) {
|
||||
ImGuiFileDialog.openDialog("browse-sudoku", "Choisissez un fichier", ".json", ".");
|
||||
}
|
||||
renderFileDialog();
|
||||
}
|
||||
|
||||
public MultiDoku getDoku() {
|
||||
return doku;
|
||||
}
|
||||
|
||||
}
|
||||
63
app/src/main/java/gui/SudokuType.java
Normal file
63
app/src/main/java/gui/SudokuType.java
Normal file
@@ -0,0 +1,63 @@
|
||||
package gui;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import sudoku.constraint.Constraint;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.SudokuFactory;;
|
||||
|
||||
public enum SudokuType {
|
||||
|
||||
Square("Carré", 1,
|
||||
(constraints, params) -> SudokuFactory.createBasicEmptySquareDoku(params[0], constraints)),
|
||||
Rectangle("Rectangle", 2,
|
||||
(constraints, params) -> SudokuFactory.createBasicEmptyRectangleDoku(params[0], params[1], constraints)),
|
||||
RandomBloc("Blocs aléatoires", 1,
|
||||
(constraints, params) -> SudokuFactory.createBasicEmptyRandomBlockDoku(params[0], constraints)),
|
||||
MultiDokuSquare("Multidoku carré (X)", 1,
|
||||
(constraints, params) -> SudokuFactory.createBasicXShapedMultidoku(params[0], constraints)),
|
||||
MultidokuRectangle("Multidoku rectangle (X)", 2,
|
||||
(constraints, params) -> SudokuFactory.createBasicXShapedMultidoku(params[0], params[1], constraints));
|
||||
|
||||
String displayName;
|
||||
SudokuMaker maker;
|
||||
int paramCount;
|
||||
|
||||
private SudokuType(String displayName, int paramCount, SudokuMaker maker) {
|
||||
this.displayName = displayName;
|
||||
this.maker = maker;
|
||||
this.paramCount = paramCount;
|
||||
}
|
||||
|
||||
public String getDisplayName() {
|
||||
return this.displayName;
|
||||
}
|
||||
|
||||
public MultiDoku createDoku(List<Constraint> constraints, int... params) {
|
||||
return maker.makeSudoku(constraints, params);
|
||||
}
|
||||
|
||||
public int getMakerParamCount() {
|
||||
return this.paramCount;
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
private static interface SudokuMaker {
|
||||
MultiDoku makeSudoku(List<Constraint> constraints, int... params);
|
||||
}
|
||||
|
||||
private static final String[] dokuNames;
|
||||
|
||||
static {
|
||||
SudokuType[] types = SudokuType.values();
|
||||
dokuNames = new String[types.length];
|
||||
for (int i = 0; i < types.length; i++) {
|
||||
dokuNames[i] = types[i].getDisplayName();
|
||||
}
|
||||
}
|
||||
|
||||
public static String[] getTypeNames() {
|
||||
return dokuNames;
|
||||
}
|
||||
|
||||
}
|
||||
83
app/src/main/java/gui/Symbols.java
Normal file
83
app/src/main/java/gui/Symbols.java
Normal file
@@ -0,0 +1,83 @@
|
||||
package gui;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public enum Symbols {
|
||||
|
||||
Numbers("Nombres", getNumbers()),
|
||||
Letters("Lettres", getLetters()),
|
||||
Russian("Cyrilique", getRussian()),
|
||||
Emojis("Emojis (Console uniquement)", getEmojis());
|
||||
|
||||
String displayName;
|
||||
List<String> symbols;
|
||||
|
||||
private Symbols(String displayName, List<String> symbols) {
|
||||
this.symbols = symbols;
|
||||
this.displayName = displayName;
|
||||
}
|
||||
|
||||
public String getDisplayName() {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
public List<String> getSymbols() {
|
||||
return symbols;
|
||||
}
|
||||
|
||||
private static List<String> getNumbers() {
|
||||
List<String> sym = new ArrayList<>();
|
||||
for (int i = 1; i <= 100; i++) {
|
||||
sym.add(Integer.toString(i));
|
||||
}
|
||||
return sym;
|
||||
}
|
||||
|
||||
private static List<String> getLetters() {
|
||||
List<String> sym = new ArrayList<>();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
int currentOffset = i;
|
||||
String letter = "";
|
||||
while (currentOffset >= 26) {
|
||||
letter += Character.toString((char) ('A' + currentOffset % 26));
|
||||
currentOffset /= 26;
|
||||
currentOffset--;
|
||||
}
|
||||
letter += Character.toString((char) ('A' + currentOffset % 26));
|
||||
sym.add(new StringBuilder(letter).reverse().toString());
|
||||
}
|
||||
return sym;
|
||||
}
|
||||
|
||||
private static List<String> getRussian() {
|
||||
List<String> sym = new ArrayList<>();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
sym.add(new String(Character.toChars(0X0400 + i)));
|
||||
}
|
||||
return sym;
|
||||
}
|
||||
|
||||
private static List<String> getEmojis() {
|
||||
List<String> sym = new ArrayList<>();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
sym.add(new String(Character.toChars(0X1F600 + i)));
|
||||
}
|
||||
return sym;
|
||||
}
|
||||
|
||||
private static final String[] symbolNames;
|
||||
|
||||
static {
|
||||
Symbols[] symbols = Symbols.values();
|
||||
symbolNames = new String[symbols.length];
|
||||
for (int i = 0; i < symbols.length; i++) {
|
||||
symbolNames[i] = symbols[i].getDisplayName();
|
||||
}
|
||||
}
|
||||
|
||||
public static String[] getSymbolsNames() {
|
||||
return symbolNames;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
package gui.menu;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import game.Player;
|
||||
import imgui.ImGui;
|
||||
import network.client.Client;
|
||||
import network.server.Server;
|
||||
import sudoku.constraint.Constraint;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.SudokuFactory;
|
||||
|
||||
@@ -17,7 +20,8 @@ public class MultiPlayerView extends BaseView {
|
||||
this.client = client;
|
||||
this.server = server;
|
||||
this.client.onDisconnect.connect(this::onDisconnect);
|
||||
this.client.onGameStarted.connect(() -> this.stateMachine.pushState(new MultiPlayerDokuView(stateMachine, client, server)));
|
||||
this.client.onGameStarted
|
||||
.connect(() -> this.stateMachine.pushState(new MultiPlayerDokuView(stateMachine, client, server)));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -36,7 +40,7 @@ public class MultiPlayerView extends BaseView {
|
||||
} else {
|
||||
if (ImGui.button("Démarrer")) {
|
||||
// temp
|
||||
MultiDoku doku = SudokuFactory.createBasicSquareMultidoku(3);
|
||||
MultiDoku doku = SudokuFactory.createBasicXShapedMultidoku(3, Arrays.asList(Constraint.Diagonal));
|
||||
this.server.startGame(doku);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,24 @@
|
||||
package gui.menu;
|
||||
|
||||
import gui.Options;
|
||||
import gui.Symbols;
|
||||
import imgui.ImGui;
|
||||
import imgui.type.ImInt;
|
||||
|
||||
public class OptionsMenu extends BaseView {
|
||||
|
||||
private ImInt currentValue = new ImInt();
|
||||
|
||||
public OptionsMenu(StateMachine stateMachine) {
|
||||
super(stateMachine);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
// TODO Auto-generated method stub
|
||||
ImGui.text("Options");
|
||||
if(ImGui.combo("Jeu de symboles", currentValue, Symbols.getSymbolsNames())){
|
||||
Options.Symboles = Symbols.values()[currentValue.get()];
|
||||
}
|
||||
renderReturnButton();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,93 +1,26 @@
|
||||
package gui.menu;
|
||||
|
||||
import gui.SudokuSelector;
|
||||
import imgui.ImGui;
|
||||
import imgui.type.ImInt;
|
||||
import sudoku.solver.Solver;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Sudoku;
|
||||
import sudoku.structure.SudokuFactory;
|
||||
|
||||
public class SoloMenu extends BaseView {
|
||||
|
||||
private final ImInt sudokuType = new ImInt(0);
|
||||
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 SudokuSelector sudokuSelector;
|
||||
|
||||
public SoloMenu(StateMachine stateMachine) {
|
||||
super(stateMachine);
|
||||
this.sudokuSelector = new SudokuSelector(true);
|
||||
this.sudokuSelector.onSelect.connect(this::pushSudokuState);
|
||||
}
|
||||
|
||||
private void pushSudokuState(MultiDoku doku, boolean empty) {
|
||||
if (!empty) {
|
||||
try {
|
||||
int level = 0;
|
||||
for (Sudoku sudoku : doku.getSubGrids()) {
|
||||
level += sudoku.getSize() * sudoku.getSize() / 10 * 3;
|
||||
}
|
||||
Solver.solve(doku);
|
||||
level = (level - 1) / doku.getNbSubGrids();
|
||||
SudokuFactory.newDokuFromFilledOne(doku, level);
|
||||
for (Sudoku sudoku : doku.getSubGrids()) {
|
||||
for (Cell cell : sudoku.getCells()) {
|
||||
if (cell.getSymbolIndex() != Cell.NOSYMBOL) {
|
||||
cell.setImmutable();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
this.stateMachine.pushState(new SudokuView(stateMachine, doku));
|
||||
private void pushSudokuState() {
|
||||
this.stateMachine.pushState(new SudokuView(stateMachine, this.sudokuSelector.getDoku()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
ImGui.text("Solo");
|
||||
ImGui.combo("Type de Sudoku", sudokuType, sudokuTypes);
|
||||
switch (sudokuType.get()) {
|
||||
case SQUARE:
|
||||
ImGui.inputInt("Taille", sudokuSize);
|
||||
if (ImGui.button("Résoudre un sudoku")) {
|
||||
pushSudokuState(SudokuFactory.createBasicEmptySquareSudoku(sudokuSize.get()), false);
|
||||
}
|
||||
if (ImGui.button("Générer une grille vide")) {
|
||||
pushSudokuState(SudokuFactory.createBasicEmptySquareSudoku(sudokuSize.get()), true);
|
||||
}
|
||||
break;
|
||||
|
||||
case RECTANGLE:
|
||||
ImGui.inputInt("Largeur", sudokuHeight);
|
||||
ImGui.inputInt("Longueur", sudokuWidth);
|
||||
if (ImGui.button("Résoudre un sudoku")) {
|
||||
pushSudokuState(
|
||||
SudokuFactory.createBasicEmptyRectangleSudoku(sudokuWidth.get(), sudokuHeight.get()),
|
||||
false);
|
||||
}
|
||||
if (ImGui.button("Générer une grille vide")) {
|
||||
pushSudokuState(
|
||||
SudokuFactory.createBasicEmptyRectangleSudoku(sudokuWidth.get(), sudokuHeight.get()), true);
|
||||
}
|
||||
break;
|
||||
|
||||
case MULTIDOKU:
|
||||
ImGui.inputInt("Taille", sudokuSize);
|
||||
if (ImGui.button("Résoudre un sudoku")) {
|
||||
pushSudokuState(SudokuFactory.createBasicSquareMultidoku(sudokuSize.get()), false);
|
||||
}
|
||||
if (ImGui.button("Générer une grille vide")) {
|
||||
pushSudokuState(SudokuFactory.createBasicSquareMultidoku(sudokuSize.get()), true);
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
sudokuSelector.render();
|
||||
renderReturnButton();
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package gui.menu;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
import gui.Images;
|
||||
import imgui.ImGui;
|
||||
import imgui.ImVec2;
|
||||
import imgui.flag.ImGuiKey;
|
||||
@@ -41,6 +42,12 @@ public class StateMachine {
|
||||
var displaySize = ImGui.getIO().getDisplaySize();
|
||||
ImGui.setNextWindowPos(new ImVec2(0.0f, 0.0f));
|
||||
ImGui.setNextWindowSize(displaySize);
|
||||
ImGui.begin("Background", null, ImGuiWindowFlags.NoDecoration | ImGuiWindowFlags.NoMove
|
||||
| ImGuiWindowFlags.NoSavedSettings | ImGuiWindowFlags.NoBackground | ImGuiWindowFlags.NoBringToFrontOnFocus | ImGuiWindowFlags.NoInputs);
|
||||
ImGui.image(Images.BACKGROUND, displaySize, new ImVec2(0, 0));
|
||||
ImGui.end();
|
||||
ImGui.setNextWindowPos(new ImVec2(0.0f, 0.0f));
|
||||
ImGui.setNextWindowSize(displaySize);
|
||||
ImGui.begin("##Main Window", null, ImGuiWindowFlags.NoDecoration | ImGuiWindowFlags.NoMove
|
||||
| ImGuiWindowFlags.NoSavedSettings | ImGuiWindowFlags.NoBackground);
|
||||
menus.get(menus.size() - 1).render();
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package gui.menu;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.CancellationException;
|
||||
|
||||
import gui.SudokuRenderer;
|
||||
import imgui.ImGui;
|
||||
import imgui.ImGuiStyle;
|
||||
import sudoku.io.SudokuSerializer;
|
||||
import sudoku.solver.BacktrackingSolver;
|
||||
import sudoku.solver.HumanSolver;
|
||||
import sudoku.solver.MixedSolver;
|
||||
import sudoku.solver.Solver;
|
||||
import sudoku.structure.MultiDoku;
|
||||
|
||||
@@ -13,30 +17,19 @@ public class SudokuView extends BaseView {
|
||||
private final SudokuRenderer sudokuRenderer;
|
||||
private Thread resolveThread;
|
||||
private final MultiDoku doku;
|
||||
private String lastSavePath = null;
|
||||
|
||||
private boolean resolved = false;
|
||||
|
||||
public SudokuView(StateMachine stateMachine, MultiDoku doku) {
|
||||
super(stateMachine);
|
||||
this.doku = doku;
|
||||
this.sudokuRenderer = new SudokuRenderer(doku);
|
||||
// int level = 0;
|
||||
// for (Sudoku sudoku : this.doku.getSubGrids()) {
|
||||
// level += sudoku.getSize() * sudoku.getSize() / 10;
|
||||
// }
|
||||
// try {
|
||||
// Solver.solve(doku);
|
||||
// SudokuFactory.newDokuFromFilledOne(doku, level);
|
||||
// for (Sudoku sudoku : this.doku.getSubGrids()) {
|
||||
// for (Cell cell : sudoku.getCells()) {
|
||||
// if (cell.getSymbolIndex() != Cell.NOSYMBOL) {
|
||||
// cell.setImmutable();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// System.out.println("non ça n'arrivera pas");
|
||||
// //TODO: ça va arriver
|
||||
// }
|
||||
this.sudokuRenderer.onResolve.connect(this::onResolve);
|
||||
}
|
||||
|
||||
private void onResolve() {
|
||||
this.resolved = true;
|
||||
}
|
||||
|
||||
private void stopResolve() {
|
||||
@@ -46,11 +39,24 @@ public class SudokuView extends BaseView {
|
||||
}
|
||||
}
|
||||
|
||||
boolean centeredButton(String label) {
|
||||
ImGuiStyle style = ImGui.getStyle();
|
||||
|
||||
float size = ImGui.calcTextSizeX(label) + style.getFramePaddingX() * 2.0f;
|
||||
float avail = ImGui.getContentRegionAvailX();
|
||||
|
||||
float off = (avail - size) * 0.5f;
|
||||
if (off > 0.0f)
|
||||
ImGui.setCursorPosX(ImGui.getCursorPosX() + off);
|
||||
|
||||
return ImGui.button(label);
|
||||
}
|
||||
|
||||
private void renderCancelButton() {
|
||||
boolean wantsToStop = false;
|
||||
if (resolveThread != null && resolveThread.isAlive()) {
|
||||
// ImGui.endDisabled();
|
||||
if (ImGui.button("Annuler")) {
|
||||
if (centeredButton("Annuler")) {
|
||||
// we can't stop the Thread right now
|
||||
wantsToStop = true;
|
||||
}
|
||||
@@ -62,29 +68,63 @@ public class SudokuView extends BaseView {
|
||||
stopResolve();
|
||||
}
|
||||
|
||||
private void startSolve(Solver solver) {
|
||||
resolveThread = new Thread(() -> {
|
||||
try {
|
||||
solver.solve(this.doku);
|
||||
} catch (CancellationException e) {
|
||||
System.out.println("The user is bored !");
|
||||
}
|
||||
stopResolve();
|
||||
});
|
||||
}
|
||||
|
||||
private void renderSolvePopup() {
|
||||
if (ImGui.beginPopup("solve")) {
|
||||
if (ImGui.button("Résoudre avec backtrace")) {
|
||||
startSolve(new BacktrackingSolver());
|
||||
ImGui.closeCurrentPopup();
|
||||
}
|
||||
if (ImGui.button("Résoudre avec déduction")) {
|
||||
startSolve(new HumanSolver());
|
||||
ImGui.closeCurrentPopup();
|
||||
}
|
||||
if (ImGui.button("Résoudre avec déduction et backtrace")) {
|
||||
startSolve(new MixedSolver());
|
||||
ImGui.closeCurrentPopup();
|
||||
}
|
||||
ImGui.endPopup();
|
||||
}
|
||||
}
|
||||
|
||||
private void renderSolveButton() {
|
||||
if (resolveThread != null)
|
||||
ImGui.beginDisabled();
|
||||
|
||||
boolean beginSolve = false;
|
||||
|
||||
if (ImGui.button("Résoudre")) {
|
||||
beginSolve = true;
|
||||
if (!this.resolved && centeredButton("Résoudre")) {
|
||||
// beginSolve = true;
|
||||
ImGui.openPopup("solve");
|
||||
}
|
||||
|
||||
if (resolveThread != null)
|
||||
ImGui.endDisabled();
|
||||
|
||||
if (beginSolve) {
|
||||
resolveThread = new Thread(() -> {
|
||||
try {
|
||||
Random rand = new Random();
|
||||
Solver.solveRandom(doku, rand);
|
||||
Thread.sleep(200);
|
||||
} catch (CancellationException | InterruptedException e) {
|
||||
System.out.println("The user is bored !");
|
||||
}
|
||||
stopResolve();
|
||||
});
|
||||
if (this.resolved) {
|
||||
ImGui.text("Bravo !");
|
||||
}
|
||||
|
||||
renderSolvePopup();
|
||||
}
|
||||
|
||||
private void renderSaveButton() {
|
||||
if (ImGui.button("Sauvegarder l'état de la grille")) {
|
||||
lastSavePath = SudokuSerializer.saveMultiDoku(doku);
|
||||
ImGui.openPopup("saveDone");
|
||||
}
|
||||
if (ImGui.beginPopup("saveDone")) {
|
||||
ImGui.text("Sudoku sauvegardé dans ");
|
||||
ImGui.text(lastSavePath);
|
||||
ImGui.endPopup();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,6 +132,7 @@ public class SudokuView extends BaseView {
|
||||
public void render() {
|
||||
sudokuRenderer.render();
|
||||
renderSolveButton();
|
||||
renderSaveButton();
|
||||
renderCancelButton();
|
||||
renderReturnButton();
|
||||
}
|
||||
|
||||
@@ -3,45 +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.Arrays;
|
||||
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) {
|
||||
System.out.println(new Main().getGreeting());
|
||||
|
||||
int blockWidth = 2;
|
||||
int blockHeight = 2;
|
||||
var multidoku = SudokuFactory.createBasicEmptyRectangleSudoku(blockWidth, blockHeight);
|
||||
var sudoku = multidoku.getSubGrid(0);
|
||||
if(!sudoku.setCellsSymbol(Arrays.asList(0,1,2,3, 2,3,1,1, 1,0,3,2, 3,2,1,1))){
|
||||
System.out.println("At least one of those values does not respect the constraints.");
|
||||
}
|
||||
|
||||
|
||||
//sudoku.setCellSymbol(8,3,0);
|
||||
|
||||
SudokuPrinter.printRectangleSudoku(multidoku.getSubGrid(0), blockWidth , blockHeight);
|
||||
|
||||
ConsoleInterface console = new ConsoleInterface();
|
||||
/*
|
||||
Solver solver = new Solver();
|
||||
ArrayList<IConstraint> constraints = new ArrayList<>();
|
||||
constraints.add(new LineConstraint());
|
||||
constraints.add(new ColumnConstraint());
|
||||
constraints.add(new BlockConstraint());
|
||||
try {
|
||||
solver.solve(multidoku, constraints);
|
||||
} catch (Exception e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
voidTest();
|
||||
filledTest();
|
||||
filledTest();
|
||||
*/
|
||||
|
||||
|
||||
console.welcome();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package sudoku.constraint;
|
||||
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.Sudoku;
|
||||
|
||||
public class ColumnConstraint implements IConstraint {
|
||||
@@ -7,10 +8,12 @@ public class ColumnConstraint implements IConstraint {
|
||||
@Override
|
||||
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
|
||||
for (int i = 0; i < s.getSize(); i++) {
|
||||
if (s.getCell(x, i).getSymbolIndex() == newSymbolIndex)
|
||||
Cell cell = s.getCell(x, i);
|
||||
int symbol = cell.getSymbolIndex();
|
||||
if (symbol == newSymbolIndex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
52
app/src/main/java/sudoku/constraint/Constraint.java
Normal file
52
app/src/main/java/sudoku/constraint/Constraint.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package sudoku.constraint;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import sudoku.structure.Sudoku;
|
||||
|
||||
public enum Constraint {
|
||||
|
||||
Block("Bloc", new BlockConstraint()),
|
||||
Column("Colonne", new ColumnConstraint()),
|
||||
Line("Ligne", new LineConstraint()),
|
||||
Diagonal("Diagonale", new DiagonalConstraint());
|
||||
|
||||
String displayName;
|
||||
IConstraint constraint;
|
||||
|
||||
private Constraint(String displayName, IConstraint contraint) {
|
||||
this.constraint = contraint;
|
||||
this.displayName = displayName;
|
||||
}
|
||||
|
||||
public boolean canBePlaced(Sudoku s, int x, int y, int newValue) {
|
||||
return getConstraint().canBePlaced(s, x, y, newValue);
|
||||
}
|
||||
|
||||
public List<Integer> getPossibleSymbols(final Sudoku s, int x, int y) {
|
||||
return getConstraint().getPossibleSymbols(s, x, y);
|
||||
}
|
||||
|
||||
public String getDisplayName() {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
public IConstraint getConstraint() {
|
||||
return constraint;
|
||||
}
|
||||
|
||||
private static final String[] constraintNames;
|
||||
|
||||
static {
|
||||
Constraint[] cons = Constraint.values();
|
||||
constraintNames = new String[cons.length];
|
||||
for (int i = 0; i < cons.length; i++) {
|
||||
constraintNames[i] = cons[i].getDisplayName();
|
||||
}
|
||||
}
|
||||
|
||||
public static String[] getConstraintNames() {
|
||||
return constraintNames;
|
||||
}
|
||||
|
||||
}
|
||||
24
app/src/main/java/sudoku/constraint/DiagonalConstraint.java
Normal file
24
app/src/main/java/sudoku/constraint/DiagonalConstraint.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package sudoku.constraint;
|
||||
|
||||
import sudoku.structure.Sudoku;
|
||||
|
||||
public class DiagonalConstraint implements IConstraint {
|
||||
|
||||
@Override
|
||||
public boolean canBePlaced(Sudoku s, int x, int y, int newSymbolIndex) {
|
||||
if (x == y) {
|
||||
for (int i = 0; i < s.getSize(); i++) {
|
||||
if (s.getCell(i, i).getSymbolIndex() == newSymbolIndex)
|
||||
return false;
|
||||
}
|
||||
} else if (s.getSize() - x == y) {
|
||||
for (int i = 0; i < s.getSize(); i++) {
|
||||
if (s.getCell(s.getSize() - i - 1, i).getSymbolIndex() == newSymbolIndex)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// not in diagonal
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
134
app/src/main/java/sudoku/io/ConsoleInterface.java
Normal file
134
app/src/main/java/sudoku/io/ConsoleInterface.java
Normal file
@@ -0,0 +1,134 @@
|
||||
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;
|
||||
|
||||
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(){
|
||||
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();
|
||||
System.out.println("Your sudoku will have blocks of a " + width + " x " + height + " format.");
|
||||
int numberOfSymbols = width * height;
|
||||
System.out.println("Would you like to pick the " + numberOfSymbols + " symbols from the sudoku? (y/n, default 'no' will use numbers)" );
|
||||
List<String> listSymbols = new ArrayList<>();
|
||||
if(reader.next().equalsIgnoreCase("y")){
|
||||
pickSymbols(listSymbols, numberOfSymbols);
|
||||
}
|
||||
else {
|
||||
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') ?");
|
||||
MultiDoku doku;
|
||||
if (reader.next().equalsIgnoreCase("multi")) {
|
||||
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:");
|
||||
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();
|
||||
if (difficulty.equals("full")) {
|
||||
generateFullDoku(doku);
|
||||
}
|
||||
else {
|
||||
generatePartialDoku(doku, difficulty);
|
||||
}
|
||||
System.out.println("Here's your sudoku !");
|
||||
SudokuPrinter.printMultiDoku(rm, listSymbols, width, height);
|
||||
}
|
||||
|
||||
public int getBlockWidth() {
|
||||
System.out.println("Width of a block: ");
|
||||
int widthBlock = reader.nextInt();
|
||||
checkValidSize(widthBlock);
|
||||
while (!checkValidSize(widthBlock)) {
|
||||
System.out.println("That is not a valid width for a block. Try again:");
|
||||
widthBlock = reader.nextInt();
|
||||
}
|
||||
System.out.println("You have chose a width of " + widthBlock + ".");
|
||||
return widthBlock;
|
||||
}
|
||||
|
||||
public int getBlockHeight() {
|
||||
System.out.println("Height of a block: ");
|
||||
int heightBlock = reader.nextInt();
|
||||
checkValidSize(heightBlock);
|
||||
while (!checkValidSize(heightBlock)) {
|
||||
System.out.println("That is not a valid height for a block. Try again:");
|
||||
heightBlock = reader.nextInt();
|
||||
}
|
||||
System.out.println("You have chose a height of " + heightBlock + ".");
|
||||
return heightBlock;
|
||||
}
|
||||
|
||||
private Boolean checkValidSize(int size) {
|
||||
return (size > 0);
|
||||
}
|
||||
|
||||
private void pickSymbols(List<String> listSymbols, int numberOfSymbols) {
|
||||
System.out.println("You have chosen to pick your own symbols.");
|
||||
for (int i = 0; i < numberOfSymbols; i++) {
|
||||
System.out.println("Choose for the symbol number " + i + ": ");
|
||||
String newSymbol = reader.next();
|
||||
while (listSymbols.contains(newSymbol)) {
|
||||
System.out.println("This symbol has already been given. Try again:");
|
||||
newSymbol = reader.next();
|
||||
}
|
||||
listSymbols.add(newSymbol);
|
||||
}
|
||||
System.out.println("You chose the symbols: " + listSymbols.toString());
|
||||
}
|
||||
|
||||
private List<Constraint> getListConstraints() {
|
||||
List<Constraint> listConstraints = SudokuFactory.DEFAULT_CONSTRAINTS;
|
||||
System.out.println("The sudoku have constraints of blocks, lines and columns. Would you like to add the diagonal constraints ? (y/n, default 'no')");
|
||||
if(reader.next().equalsIgnoreCase("y")){
|
||||
listConstraints.add(Constraint.Diagonal);
|
||||
}
|
||||
return listConstraints;
|
||||
}
|
||||
|
||||
private void generatePartialDoku(MultiDoku doku, String difficultyName) {
|
||||
Difficulty difficulty;
|
||||
switch (difficultyName){
|
||||
case "very easy": difficulty = Difficulty.VeryEasy;
|
||||
case "easy": difficulty = Difficulty.Easy;
|
||||
case "hard": difficulty = Difficulty.Hard;
|
||||
default: difficulty = Difficulty.Medium;
|
||||
}
|
||||
try {
|
||||
SudokuFactory.fillDoku(doku, difficulty);
|
||||
} catch (Exception e) {
|
||||
System.out.println("There seems to be a problem with those settings. Let's start again.");
|
||||
}
|
||||
}
|
||||
|
||||
private void generateFullDoku(MultiDoku doku) {
|
||||
new RandomSolver().solve(doku);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,27 +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
|
||||
@@ -31,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) {
|
||||
@@ -43,4 +111,13 @@ public class SudokuPrinter {
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,26 +1,30 @@
|
||||
package sudoku.io;
|
||||
|
||||
import java.io.*;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import sudoku.constraint.Constraint;
|
||||
import sudoku.structure.Block;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Sudoku;
|
||||
import sudoku.structure.SudokuFactory;
|
||||
|
||||
public class SudokuSerializer {
|
||||
|
||||
public static JSONObject serializeSudoku(final MultiDoku multidoku) {
|
||||
List<Cell> cellIds = new ArrayList<>();
|
||||
List<Cell> cellIds = new ArrayList<>();
|
||||
List<Block> blockIds = new ArrayList<>();
|
||||
|
||||
JSONObject jsonRoot = new JSONObject();
|
||||
JSONArray jsonCells = new JSONArray();
|
||||
JSONObject jsonRoot = new JSONObject();
|
||||
JSONArray jsonCells = new JSONArray();
|
||||
JSONArray jsonBlocks = new JSONArray();
|
||||
JSONArray jsonSudokus = new JSONArray(multidoku.getNbSubGrids());
|
||||
|
||||
@@ -29,10 +33,10 @@ public class SudokuSerializer {
|
||||
|
||||
// init cells
|
||||
for (Cell cell : sudoku.getCells()) {
|
||||
if (!cellIds.contains(cell)) {
|
||||
cellIds.add(cell);
|
||||
if (cellIds.contains(cell))
|
||||
continue;
|
||||
|
||||
}
|
||||
cellIds.add(cell);
|
||||
|
||||
Block block = cell.getBlock();
|
||||
if (!blockIds.contains(block)) {
|
||||
@@ -40,6 +44,7 @@ public class SudokuSerializer {
|
||||
}
|
||||
|
||||
int blockID = blockIds.indexOf(block);
|
||||
assert(blockID >= 0);
|
||||
int symbolIndex = cell.getSymbolIndex();
|
||||
|
||||
JSONObject cellJsonObject = new JSONObject();
|
||||
@@ -55,22 +60,24 @@ public class SudokuSerializer {
|
||||
|
||||
// init blocks
|
||||
|
||||
for (Block blockId : blockIds) {
|
||||
JSONObject blockJsonObject = new JSONObject();
|
||||
JSONArray cellsJsonArray = new JSONArray();
|
||||
for (Cell cell : blockId.getCells()) {
|
||||
int cellID = cellIds.indexOf(cell);
|
||||
cellsJsonArray.put(cellID);
|
||||
}
|
||||
blockJsonObject.put("cellIDs", cellsJsonArray);
|
||||
jsonBlocks.put(blockJsonObject);
|
||||
}
|
||||
for (Block blockId : blockIds) {
|
||||
JSONObject blockJsonObject = new JSONObject();
|
||||
JSONArray cellsJsonArray = new JSONArray();
|
||||
for (Cell cell : blockId.getCells()) {
|
||||
int cellID = cellIds.indexOf(cell);
|
||||
assert (cellID >= 0);
|
||||
cellsJsonArray.put(cellID);
|
||||
}
|
||||
blockJsonObject.put("cellIDs", cellsJsonArray);
|
||||
jsonBlocks.put(blockJsonObject);
|
||||
}
|
||||
|
||||
for (int i = 0; i < multidoku.getNbSubGrids(); i++) {
|
||||
// serialise sub grid
|
||||
JSONObject jsonSudoku = new JSONObject();
|
||||
JSONArray cellsJsonArray = new JSONArray();
|
||||
JSONArray blocksJsonArray = new JSONArray();
|
||||
JSONArray constraintsJsonArray = new JSONArray();
|
||||
|
||||
Sudoku sudoku = multidoku.getSubGrid(i);
|
||||
|
||||
@@ -78,6 +85,7 @@ public class SudokuSerializer {
|
||||
|
||||
for (Cell cell : sudoku.getCells()) {
|
||||
int cellID = cellIds.indexOf(cell);
|
||||
assert (cellID >= 0);
|
||||
cellsJsonArray.put(cellID);
|
||||
}
|
||||
|
||||
@@ -85,9 +93,17 @@ public class SudokuSerializer {
|
||||
|
||||
for (Block block : sudoku.getBlocks()) {
|
||||
int blockID = blockIds.indexOf(block);
|
||||
assert (blockID >= 0);
|
||||
blocksJsonArray.put(blockID);
|
||||
}
|
||||
|
||||
// serialize constraints
|
||||
|
||||
for (Constraint cons : sudoku.getConstraints()) {
|
||||
constraintsJsonArray.put(cons.ordinal());
|
||||
}
|
||||
|
||||
jsonSudoku.put("constraints", constraintsJsonArray);
|
||||
jsonSudoku.put("cells", cellsJsonArray);
|
||||
jsonSudoku.put("blocks", blocksJsonArray);
|
||||
jsonSudoku.put("blockWidth", sudoku.getBlockWidth());
|
||||
@@ -101,25 +117,55 @@ public class SudokuSerializer {
|
||||
return jsonRoot;
|
||||
}
|
||||
|
||||
public static void saveMultiDoku(final MultiDoku doku) {
|
||||
/**
|
||||
* Save a serialized MultiDoku in a JSON file.
|
||||
*
|
||||
* @param doku MultiDoku, MultiDoku to save.
|
||||
* @return String, the path of the save.
|
||||
*/
|
||||
public static String saveMultiDoku(final MultiDoku doku) {
|
||||
|
||||
JSONObject jsonRoot = serializeSudoku(doku);
|
||||
|
||||
File f = new File("save", "save.json");
|
||||
InputStream is = null;
|
||||
int i = 1;
|
||||
int i = 0;
|
||||
|
||||
while (f.exists()) {
|
||||
String newName = "save-" + i + ".json";
|
||||
String newName = "save-" + ++i + ".json";
|
||||
f = new File("save", newName);
|
||||
i++;
|
||||
}
|
||||
|
||||
try (FileWriter file = new FileWriter(f)) {
|
||||
file.write(jsonRoot.toString(3));
|
||||
//file.flush();
|
||||
} catch (IOException e) {
|
||||
e.fillInStackTrace();
|
||||
e.printStackTrace();
|
||||
}
|
||||
return f.getAbsolutePath();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a MultiDoku from a pre-existing json save file.
|
||||
*
|
||||
* @param numberSave int, number of the save file to open.
|
||||
* @return MultiDoku, MultoDoku contained in the file.
|
||||
* @throws Exception when the given save file does not exist.
|
||||
*/
|
||||
public static MultiDoku getSavedMultiDoku(int numberSave) throws Exception {
|
||||
String fileName;
|
||||
if (numberSave < 1) {
|
||||
fileName = "save.json";
|
||||
} else {
|
||||
fileName = "save-" + numberSave + ".json";
|
||||
}
|
||||
|
||||
File f = new File("save", fileName);
|
||||
String fileContent;
|
||||
|
||||
if (!f.exists()) {
|
||||
throw new Exception("This save does not exist.");
|
||||
} else {
|
||||
fileContent = new String(Files.readAllBytes(Paths.get("save/" + fileName)));
|
||||
return deserializeSudoku(fileContent);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,9 +213,11 @@ public class SudokuSerializer {
|
||||
JSONObject sudokuJsonObject = multidokuJsonObject.getJSONObject(i);
|
||||
JSONArray sudokuCellsJsonArray = sudokuJsonObject.getJSONArray("cells");
|
||||
JSONArray sudokuBlocksJsonArray = sudokuJsonObject.getJSONArray("blocks");
|
||||
JSONArray sudokuConstraintsJsonArray = sudokuJsonObject.getJSONArray("constraints");
|
||||
|
||||
List<Cell> sudokuCells = new ArrayList<>();
|
||||
List<Block> sudokuBlocks = new ArrayList<>();
|
||||
List<Constraint> sudokuConstraints = new ArrayList<>();
|
||||
|
||||
for (int j = 0; j < sudokuCellsJsonArray.length(); j++) {
|
||||
int cellID = sudokuCellsJsonArray.getInt(j);
|
||||
@@ -181,8 +229,16 @@ public class SudokuSerializer {
|
||||
sudokuBlocks.add(blocks.get(blockID));
|
||||
}
|
||||
|
||||
Sudoku s = new Sudoku(sudokuCells, sudokuBlocks, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
for (int j = 0; j < sudokuConstraintsJsonArray.length(); j++) {
|
||||
int constraintID = sudokuConstraintsJsonArray.getInt(j);
|
||||
sudokuConstraints.add(Constraint.values()[constraintID]);
|
||||
}
|
||||
|
||||
Sudoku s = new Sudoku(sudokuCells, sudokuBlocks, sudokuConstraints);
|
||||
s.setBlockWidth(sudokuJsonObject.getInt("blockWidth"));
|
||||
for (Block block : s.getBlocks()) {
|
||||
block.getSudokus().add(s);
|
||||
}
|
||||
sudokus.add(s);
|
||||
}
|
||||
|
||||
|
||||
49
app/src/main/java/sudoku/solver/BacktrackingSolver.java
Normal file
49
app/src/main/java/sudoku/solver/BacktrackingSolver.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package sudoku.solver;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CancellationException;
|
||||
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
|
||||
public class BacktrackingSolver implements Solver {
|
||||
|
||||
/**
|
||||
* Résout le MultiDoku passé en paramètre, avec backtracking.
|
||||
*
|
||||
* @param doku MultiDoku, MultiDoku à résoudre.
|
||||
* @return boolean, valant true si le MultiDoku est résolu, false sinon.
|
||||
*/
|
||||
@Override
|
||||
public boolean solve(MultiDoku doku) {
|
||||
if (Thread.interrupted())
|
||||
throw new CancellationException("User wants to stop the solver");
|
||||
|
||||
if (doku.isSolved()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Cell cellToFill = doku.getFirstEmptyCell();
|
||||
if (cellToFill == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
List<Integer> possibleSymbols = cellToFill.getPossibleSymbols();
|
||||
if (possibleSymbols.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int symbol : possibleSymbols) {
|
||||
|
||||
cellToFill.setSymbolIndex(symbol);
|
||||
if (this.solve(doku)) {
|
||||
return true;
|
||||
} else {
|
||||
cellToFill.setSymbolIndex(Cell.NOSYMBOL);
|
||||
}
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
57
app/src/main/java/sudoku/solver/HumanSolver.java
Normal file
57
app/src/main/java/sudoku/solver/HumanSolver.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package sudoku.solver;
|
||||
|
||||
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;
|
||||
import sudoku.structure.Sudoku;
|
||||
|
||||
public class HumanSolver implements Solver {
|
||||
|
||||
/**
|
||||
* Résout le MultiDoku passé en paramètre, avec règles de déduction.
|
||||
*
|
||||
* @param doku MultiDoku, MultiDoku à résoudre.
|
||||
* @return boolean, valant true si le MultiDoku est résolu, false sinon.
|
||||
*/
|
||||
@Override
|
||||
public boolean solve(MultiDoku doku) {
|
||||
if (Thread.interrupted())
|
||||
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()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
List<Cell> cellsToFill = doku.getEmptyCells();
|
||||
if (cellsToFill.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (Cell cellToFill : cellsToFill) {
|
||||
|
||||
List<Integer> possibleSymbols = cellToFill.getPossibleSymbols();
|
||||
if (possibleSymbols.size() != 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
cellToFill.setSymbolIndex(possibleSymbols.getFirst());
|
||||
|
||||
return this.solve(doku);
|
||||
}
|
||||
|
||||
return doku.isSolved();
|
||||
}
|
||||
|
||||
}
|
||||
82
app/src/main/java/sudoku/solver/MixedSolver.java
Normal file
82
app/src/main/java/sudoku/solver/MixedSolver.java
Normal file
@@ -0,0 +1,82 @@
|
||||
package sudoku.solver;
|
||||
|
||||
import java.util.List;
|
||||
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;
|
||||
import sudoku.structure.Sudoku;
|
||||
|
||||
public class MixedSolver implements Solver{
|
||||
|
||||
/**
|
||||
* Résout le MultiDoku passé en paramètre, avec règles de déduction et
|
||||
* backtracking.
|
||||
*
|
||||
* @param doku MultiDoku, MultiDoku à résoudre.
|
||||
* @return boolean, valant true si le MultiDoku est résolu, false sinon.
|
||||
*/
|
||||
@Override
|
||||
public boolean solve(MultiDoku doku) {
|
||||
Random rand = new Random();
|
||||
|
||||
if (Thread.interrupted()) {
|
||||
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()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
List<Cell> cellsToFill = doku.getEmptyCells();
|
||||
if (cellsToFill.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Règles de déduction
|
||||
for (Cell cellToFill : cellsToFill) {
|
||||
|
||||
List<Integer> possibleSymbols = cellToFill.getPossibleSymbols();
|
||||
if (possibleSymbols.size() != 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
cellToFill.setSymbolIndex(possibleSymbols.getFirst());
|
||||
|
||||
return this.solve(doku);
|
||||
}
|
||||
|
||||
// Si ça ne marche pas
|
||||
// On fait du backtracking
|
||||
Cell cellToFill = doku.getRandomEmptyCell(rand);
|
||||
List<Integer> possibleSymbols = cellToFill.getPossibleSymbols();
|
||||
|
||||
while (!possibleSymbols.isEmpty()) {
|
||||
int nextPossibleSymbolIndex = rand.nextInt(possibleSymbols.size());
|
||||
int nextSymbol = possibleSymbols.get(nextPossibleSymbolIndex);
|
||||
|
||||
cellToFill.setSymbolIndex(nextSymbol);
|
||||
if (this.solve(doku)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
cellToFill.setSymbolIndex(Cell.NOSYMBOL);
|
||||
possibleSymbols.remove(nextPossibleSymbolIndex);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
65
app/src/main/java/sudoku/solver/RandomSolver.java
Normal file
65
app/src/main/java/sudoku/solver/RandomSolver.java
Normal file
@@ -0,0 +1,65 @@
|
||||
package sudoku.solver;
|
||||
|
||||
import java.util.List;
|
||||
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;
|
||||
import sudoku.structure.Sudoku;
|
||||
|
||||
public class RandomSolver implements Solver {
|
||||
|
||||
/**
|
||||
* Résout, si possible, le multidoku passé en paramètre
|
||||
* en testant toutes les possibilités, de manière aléatoire, avec un algorithme
|
||||
* de backtracking.
|
||||
*
|
||||
* @param doku Multidoku, à résoudre
|
||||
* @param rand Random, pour tester aléatoirement les symboles
|
||||
* @return boolean, true s'il est résolu ou false s'il ne l'est pas.
|
||||
*/
|
||||
@Override
|
||||
public boolean solve(MultiDoku doku) {
|
||||
Random rand = new Random();
|
||||
|
||||
if (Thread.interrupted())
|
||||
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()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Cell cellToFill = doku.getFirstEmptyCell();
|
||||
if (cellToFill == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
List<Integer> possibleSymbols = cellToFill.getPossibleSymbols();
|
||||
|
||||
while (!possibleSymbols.isEmpty()) {
|
||||
int nextPossibleSymbolIndex = rand.nextInt(possibleSymbols.size());
|
||||
int nextSymbol = possibleSymbols.get(nextPossibleSymbolIndex);
|
||||
|
||||
cellToFill.setSymbolIndex(nextSymbol);
|
||||
if (this.solve(doku)) {
|
||||
return true;
|
||||
}
|
||||
cellToFill.setSymbolIndex(Cell.NOSYMBOL);
|
||||
possibleSymbols.remove(nextPossibleSymbolIndex);
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,118 +1,47 @@
|
||||
package sudoku.solver;
|
||||
|
||||
import sudoku.io.SudokuPrinter;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.Sudoku;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class Solver {
|
||||
private static final Logger logger = Logger.getLogger("SolverLogger");
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
|
||||
/**
|
||||
* Résout le multidoku passé en paramètre si c'est possible.
|
||||
* En testant toutes les possibilités, de manière aléatoire, avec un algorithme
|
||||
* de backtracking.
|
||||
*
|
||||
* @param doku Multidoku, à résoudre
|
||||
* @param rand Random, pour tester aléatoirement les symboles
|
||||
* @return boolean, true s'il est résolu ou false s'il ne l'est pas.
|
||||
public interface Solver {
|
||||
|
||||
/**
|
||||
* Log du Solver, qui garde trace des actions réalisées.
|
||||
*/
|
||||
public static boolean solveRandom(MultiDoku doku, Random rand) {
|
||||
if (Thread.interrupted())
|
||||
throw new CancellationException("User wants to stop the solver");
|
||||
public static final Logger logger = Logger.getLogger("SolverLogger");
|
||||
|
||||
Sudoku sudoku = doku.getSubGrid(0);
|
||||
logger.log(Level.INFO,
|
||||
'\n' + SudokuPrinter.toStringRectangleSudoku(sudoku,
|
||||
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getBlockWidth(),
|
||||
sudoku.getBlockWidth() == 0 ? sudoku.getSize() : sudoku.getSize() / sudoku.getBlockWidth()));
|
||||
boolean solve(MultiDoku doku);
|
||||
|
||||
if (doku.isValid()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Cell cellToFill = doku.getFirstEmptyCell();
|
||||
if (cellToFill == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
List<Integer> possibleSymbols = doku.getPossibleSymbolsOfCell(cellToFill);
|
||||
|
||||
while (!possibleSymbols.isEmpty()) {
|
||||
int nextPossibleSymbolIndex = rand.nextInt(possibleSymbols.size());
|
||||
int nextSymbol = possibleSymbols.get(nextPossibleSymbolIndex);
|
||||
|
||||
cellToFill.setSymbolIndex(nextSymbol);
|
||||
if (Solver.solveRandom(doku, rand)) {
|
||||
return true;
|
||||
}
|
||||
cellToFill.setSymbolIndex(Cell.NOSYMBOL);
|
||||
possibleSymbols.remove(nextPossibleSymbolIndex);
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static int countSolution(MultiDoku doku) {
|
||||
/**
|
||||
* Compte le nombre de solutions possibles au MultiDoku passé en paramètres.
|
||||
*
|
||||
* @param doku MultiDoku, MultiDoku dont on veut le nombre de solutions.
|
||||
* @return int, nombre de solutions possibles.
|
||||
*/
|
||||
default int countSolution(MultiDoku doku) {
|
||||
int result = 0;
|
||||
|
||||
if (doku.isValid()) {
|
||||
if (doku.isSolved()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
Cell cellToFill = doku.getFirstEmptyCell();
|
||||
if (cellToFill == null) {
|
||||
return 0;
|
||||
}
|
||||
assert (cellToFill != null);
|
||||
|
||||
List<Integer> possibleSymbols = doku.getPossibleSymbolsOfCell(cellToFill);
|
||||
List<Integer> possibleSymbols = cellToFill.getPossibleSymbols();
|
||||
|
||||
for (int symbol : possibleSymbols) {
|
||||
|
||||
doku.getStateManager().pushState();
|
||||
cellToFill.setSymbolIndex(symbol);
|
||||
if (Solver.solve(doku)) {
|
||||
if (solve(doku)) {
|
||||
result++;
|
||||
}
|
||||
cellToFill.setSymbolIndex(Cell.NOSYMBOL);
|
||||
doku.getStateManager().popState();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static boolean solve(MultiDoku doku) {
|
||||
if (Thread.interrupted())
|
||||
throw new CancellationException("User wants to stop the solver");
|
||||
|
||||
if (doku.isValid()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Cell cellToFill = doku.getFirstEmptyCell();
|
||||
if (cellToFill == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
List<Integer> possibleSymbols = doku.getPossibleSymbolsOfCell(cellToFill);
|
||||
if (possibleSymbols.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int symbol : possibleSymbols) {
|
||||
|
||||
cellToFill.setSymbolIndex(symbol);
|
||||
if (Solver.solve(doku)) {
|
||||
return true;
|
||||
} else {
|
||||
cellToFill.setSymbolIndex(Cell.NOSYMBOL);
|
||||
}
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package sudoku.solver;
|
||||
|
||||
import java.util.concurrent.CancellationException;
|
||||
|
||||
import sudoku.structure.Coordinate;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Sudoku;
|
||||
|
||||
@@ -24,9 +23,8 @@ public class StupidSolver {
|
||||
if (!sudoku.getCell(index).isMutable())
|
||||
return solve(sudoku, index + 1);
|
||||
|
||||
Coordinate coords = sudoku.toCoords(index);
|
||||
for (int symbol = 0; symbol < sudoku.getSize(); symbol++) {
|
||||
if (sudoku.tryPlaceCellSymbol(coords.getX(), coords.getY(), symbol)) {
|
||||
if (sudoku.getCell(index).trySetValue(symbol)) {
|
||||
// on tente de placer sur la case suivante
|
||||
if (solve(sudoku, index + 1)) {
|
||||
return true;
|
||||
@@ -34,12 +32,12 @@ public class StupidSolver {
|
||||
}
|
||||
}
|
||||
// on a tout essayé et rien n'a fonctionné
|
||||
sudoku.clearCell(coords.getX(), coords.getY());
|
||||
sudoku.getCell(index).empty();
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean solve(MultiDoku doku) {
|
||||
if (doku.isValid())
|
||||
if (doku.isSolved())
|
||||
return true;
|
||||
|
||||
for (Sudoku sudoku : doku.getSubGrids()) {
|
||||
|
||||
@@ -5,7 +5,8 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* Class qui représente les block de chaque sudoku,
|
||||
* Un block étant un ensemble de cellule avec une contrainte de block qui lui ait associé
|
||||
* Un block étant un ensemble de cellule avec une contrainte de block qui lui
|
||||
* ait associé
|
||||
*/
|
||||
public class Block {
|
||||
|
||||
@@ -18,14 +19,15 @@ public class Block {
|
||||
* List de sudoku qui contiennent le block
|
||||
* Pour un acces plus rapide aux sudokus
|
||||
*/
|
||||
private List<Sudoku> sudokus;
|
||||
private final List<Sudoku> sudokus;
|
||||
|
||||
public Block(List<Cell> cells) {
|
||||
this.cells = cells;
|
||||
this.sudokus = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Block() {
|
||||
this.cells = new ArrayList<>();
|
||||
this(new ArrayList<>());
|
||||
}
|
||||
|
||||
public List<Cell> getCells() {
|
||||
@@ -34,6 +36,7 @@ public class Block {
|
||||
|
||||
/**
|
||||
* Ajoute une Cell au Block
|
||||
*
|
||||
* @param newCell Cell, à ajouter
|
||||
*/
|
||||
void addCell(Cell newCell) {
|
||||
@@ -42,6 +45,7 @@ public class Block {
|
||||
|
||||
/**
|
||||
* Cherche si le Block contient déjà un symbole donné.
|
||||
*
|
||||
* @param symbolIndex int, un index de symbole
|
||||
* @return boolean, true s'il contient le symbole et false sinon
|
||||
*/
|
||||
@@ -65,8 +69,4 @@ public class Block {
|
||||
public List<Sudoku> getSudokus() {
|
||||
return sudokus;
|
||||
}
|
||||
|
||||
void setSudokus(List<Sudoku> sudokus) {
|
||||
this.sudokus = sudokus;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,28 +24,21 @@ public class Cell {
|
||||
* Il est initialisé à Cell.NOSYMBOL.
|
||||
*/
|
||||
private int symbolIndex = Cell.NOSYMBOL;
|
||||
/**
|
||||
* Liste des index de symbole possibles pour cette Cell,
|
||||
* en fonction des contraintes de sudoku dans lequel elle est.
|
||||
*/
|
||||
private final List<Integer> possibleSymbols;
|
||||
/**
|
||||
* Si cette Cell peut être modififié ou non.
|
||||
*/
|
||||
private boolean isMutable = true;
|
||||
|
||||
public Cell() {
|
||||
this.possibleSymbols = new ArrayList<>();
|
||||
this(Cell.NOSYMBOL);
|
||||
}
|
||||
|
||||
public Cell(int symbolIndex) {
|
||||
this.symbolIndex = symbolIndex;
|
||||
this.possibleSymbols = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Cell(int symbolIndex, boolean isMutable) {
|
||||
this.symbolIndex = symbolIndex;
|
||||
this.possibleSymbols = new ArrayList<>();
|
||||
this.isMutable = isMutable;
|
||||
}
|
||||
|
||||
@@ -57,11 +50,6 @@ public class Cell {
|
||||
this.symbolIndex = symbolIndex;
|
||||
}
|
||||
|
||||
public void setPossibleSymbols(List<Integer> possibleSymbols) {
|
||||
this.possibleSymbols.clear();
|
||||
this.possibleSymbols.addAll(possibleSymbols);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rend la Cell immuable.
|
||||
*/
|
||||
@@ -95,14 +83,6 @@ public class Cell {
|
||||
return this.symbolIndex == Cell.NOSYMBOL;
|
||||
}
|
||||
|
||||
public void removeSymbolFromPossibilities(int indexSymbol) {
|
||||
possibleSymbols.remove(indexSymbol);
|
||||
}
|
||||
|
||||
public List<Integer> getPossibleSymbols() {
|
||||
return this.possibleSymbols;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renvoie si la Cell est modifiable
|
||||
* @return boolean, true si elle est modifiable ou false sinon.
|
||||
@@ -120,4 +100,35 @@ public class Cell {
|
||||
this.symbolIndex = Cell.NOSYMBOL;
|
||||
return oldSymbol;
|
||||
}
|
||||
|
||||
public boolean canHaveValue(int value) {
|
||||
for (Sudoku s :getBlock().getSudokus()) {
|
||||
int cellIndex = s.getCells().indexOf(this);
|
||||
// la cellule existe
|
||||
if (cellIndex != -1) {
|
||||
int cellX = cellIndex % s.getSize();
|
||||
int cellY = cellIndex / s.getSize();
|
||||
if (!s.canBePlaced(cellX, cellY, value)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<Integer> getPossibleSymbols() {
|
||||
List<Integer> result = new ArrayList<>();
|
||||
for (int i = 0; i < getBlock().getSudokus().get(0).getSize(); i++) {
|
||||
if (canHaveValue(i))
|
||||
result.add(i);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean trySetValue(int newValue) {
|
||||
if (!canHaveValue(newValue))
|
||||
return false;
|
||||
setSymbolIndex(newValue);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
38
app/src/main/java/sudoku/structure/Difficulty.java
Normal file
38
app/src/main/java/sudoku/structure/Difficulty.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package sudoku.structure;
|
||||
|
||||
//TODO: melvyn va passer par là
|
||||
public enum Difficulty {
|
||||
|
||||
VeryEasy("Très facile", 0.1), Easy("Facile", 0.25), Medium("Moyen", 0.5), Hard("Difficile", 0.75), VeryHard("Impossible", 0.78);
|
||||
|
||||
final double factor;
|
||||
final String displayName;
|
||||
|
||||
private Difficulty(String displayName, double factor) {
|
||||
this.factor = factor;
|
||||
this.displayName = displayName;
|
||||
}
|
||||
|
||||
public String getDisplayName() {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
public double getFactor() {
|
||||
return factor;
|
||||
}
|
||||
|
||||
private static final String[] difficultyNames;
|
||||
|
||||
static {
|
||||
Difficulty[] diffs = Difficulty.values();
|
||||
difficultyNames = new String[diffs.length];
|
||||
for (int i = 0; i < diffs.length; i++) {
|
||||
difficultyNames[i] = diffs[i].getDisplayName();
|
||||
}
|
||||
}
|
||||
|
||||
public static String[] getDifficultyNames() {
|
||||
return difficultyNames;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package sudoku.structure;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
import sudoku.io.SudokuSerializer;
|
||||
|
||||
@@ -17,90 +16,60 @@ public class MultiDoku {
|
||||
*/
|
||||
private final List<Sudoku> subGrids;
|
||||
|
||||
private final StateManager stateManager;
|
||||
|
||||
public MultiDoku(List<Sudoku> subGrids) {
|
||||
this.subGrids = subGrids;
|
||||
}
|
||||
|
||||
public MultiDoku clone() {
|
||||
//TODO: ahhhhhhhhhhhhhhhhhhhhhhh
|
||||
return SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(this));
|
||||
this.stateManager = new StateManager(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renvoie le nombre de sudoku contenu dans ce MultiDoku.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public int getNbSubGrids(){
|
||||
public int getNbSubGrids() {
|
||||
return subGrids.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Renvoie la ie sudoku contenue dans ce MultiDoku.
|
||||
*
|
||||
* @param i int, indice du sudoku à renvoyer.
|
||||
* @return Sudoku, ie Sudoku
|
||||
*/
|
||||
public Sudoku getSubGrid(int i){
|
||||
public Sudoku getSubGrid(int i) {
|
||||
return subGrids.get(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renvoie la liste des Cells contenue dans ce MultiDoku,
|
||||
* soit les Cells contenues de chaques sous-Sudoku.
|
||||
*
|
||||
* @return List<Cell>
|
||||
*/
|
||||
public List<Cell> getCells(){
|
||||
List<Cell> cells = new ArrayList<>();
|
||||
for (Sudoku sudoku : subGrids){
|
||||
cells.addAll(sudoku.getCells());
|
||||
}
|
||||
return cells;
|
||||
}
|
||||
|
||||
/**
|
||||
* Met à jour les symboles possibles de chaque Cell.
|
||||
* @throws Exception, si ce n'est pas possible.
|
||||
*/
|
||||
public void updateSymbolsPossibilities() throws Exception {
|
||||
for (Sudoku sudoku : subGrids){
|
||||
sudoku.updateSymbolsPossibilities();
|
||||
public List<Cell> getCells() {
|
||||
Set<Cell> cellsSet = new HashSet<>();
|
||||
for (Sudoku sudoku : subGrids) {
|
||||
cellsSet.addAll(sudoku.getCells());
|
||||
}
|
||||
return new ArrayList<>(cellsSet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Multidoku {");
|
||||
for (Sudoku sudoku : subGrids){
|
||||
for (Sudoku sudoku : subGrids) {
|
||||
sb.append("\n\t").append(sudoku.toString());
|
||||
}
|
||||
sb.append("\n}");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Renvoie les symboles possibles d'une Cell donnée.
|
||||
* @param cellToFill Cell.
|
||||
* @return List<Integer>, liste des symboles possible.
|
||||
*/
|
||||
public List<Integer> getPossibleSymbolsOfCell(Cell cellToFill) {
|
||||
List<Integer> result = new ArrayList<>();
|
||||
boolean hasBeenFill = false;
|
||||
|
||||
for (Sudoku sudoku : this.subGrids) {
|
||||
if (sudoku.contains(cellToFill)) {
|
||||
if (!hasBeenFill) {
|
||||
result.addAll(sudoku.getPossibleSymbolsOfCell(cellToFill));
|
||||
hasBeenFill = true;
|
||||
} else {
|
||||
result.retainAll(sudoku.getPossibleSymbolsOfCell(cellToFill));
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renvoie les sous-Sudoku
|
||||
*
|
||||
* @return List<Sudoku>
|
||||
*/
|
||||
public List<Sudoku> getSubGrids() {
|
||||
@@ -109,43 +78,26 @@ public class MultiDoku {
|
||||
|
||||
/**
|
||||
* Check si le MultiDoku est valide, en fonction de ses sous-Sudokus.
|
||||
*
|
||||
* @return boolean, true s'il est valide et false sinon.
|
||||
*/
|
||||
public boolean isValid() {
|
||||
boolean result = true;
|
||||
public boolean isSolved() {
|
||||
for (Sudoku sudoku : this.subGrids) {
|
||||
result = sudoku.isValid() && result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object) {
|
||||
if (!(object instanceof MultiDoku)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.getNbSubGrids() != ((MultiDoku) object).getNbSubGrids()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < this.getNbSubGrids(); i++) {
|
||||
if (!this.getSubGrid(i).equals(((MultiDoku) object).getSubGrid(i))) {
|
||||
if (!sudoku.isSolved())
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renvoie la 1re Cell vide des sous-Sudoku.
|
||||
*
|
||||
* @return Cell, une Cell vide, ou null s'il n'y en a pas.
|
||||
*/
|
||||
public Cell getFirstEmptyCell() {
|
||||
for (Sudoku sudoku : this.subGrids) {
|
||||
Cell cellTmp = sudoku.getFirstEmptyCell();
|
||||
if (cellTmp != null && cellTmp.isEmpty()) {
|
||||
if (cellTmp != null) {
|
||||
return cellTmp;
|
||||
}
|
||||
}
|
||||
@@ -154,11 +106,12 @@ public class MultiDoku {
|
||||
|
||||
/**
|
||||
* Renvoie la liste des Cells préalablement remplies du MultiDoku.
|
||||
*
|
||||
* @return List<Cell>, vide si aucune Cell n'est remplie.
|
||||
*/
|
||||
public List<Cell> getFilledCells() {
|
||||
List<Cell> result = new ArrayList<>();
|
||||
for (Cell cell : this.getCells()){
|
||||
for (Cell cell : this.getCells()) {
|
||||
if (!cell.isEmpty()) {
|
||||
result.add(cell);
|
||||
}
|
||||
@@ -166,8 +119,24 @@ public class MultiDoku {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renvoie la liste des Cells vides du MultiDoku.
|
||||
*
|
||||
* @return List<Cell>, vide si aucune Cell ne l'est.
|
||||
*/
|
||||
public List<Cell> getEmptyCells() {
|
||||
List<Cell> result = new ArrayList<>();
|
||||
for (Cell cell : this.getCells()) {
|
||||
if (cell.isEmpty()) {
|
||||
result.add(cell);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Vide une Cell donnée.
|
||||
*
|
||||
* @param cell Cell, à vider.
|
||||
*/
|
||||
public void empty(Cell cell) {
|
||||
@@ -175,5 +144,38 @@ public class MultiDoku {
|
||||
Cell cellToEmpty = cells.get(cells.indexOf(cell));
|
||||
cellToEmpty.setSymbolIndex(Cell.NOSYMBOL);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Renvoie le nombre de Cell contenue dans le MultiDoku.
|
||||
*
|
||||
* @return int, nombre de Cell dans le MultiDoku.
|
||||
*/
|
||||
public int getNbCells() {
|
||||
return getCells().size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Change les Cells de ce MultiDoku avec des symboles, en Cells immuables.
|
||||
*/
|
||||
public void setFilledCellsImmutable() {
|
||||
for (Cell filledCell : getFilledCells()) {
|
||||
filledCell.setImmutable();
|
||||
}
|
||||
}
|
||||
|
||||
public StateManager getStateManager() {
|
||||
return stateManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renvoie une Cell vide choisie aléatoirement.
|
||||
*
|
||||
* @param rand Random, pour le choix aléatoire.
|
||||
* @return Cell, une Cell vide.
|
||||
*/
|
||||
public Cell getRandomEmptyCell(Random rand) {
|
||||
List<Cell> emptyCells = getEmptyCells();
|
||||
int randomIndex = rand.nextInt(emptyCells.size());
|
||||
return emptyCells.get(randomIndex);
|
||||
}
|
||||
}
|
||||
|
||||
41
app/src/main/java/sudoku/structure/StateManager.java
Normal file
41
app/src/main/java/sudoku/structure/StateManager.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package sudoku.structure;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Stack;
|
||||
|
||||
//TODO: doc
|
||||
public class StateManager {
|
||||
|
||||
private final Stack<Map<Cell, Integer>> states;
|
||||
private final MultiDoku doku;
|
||||
|
||||
public StateManager(MultiDoku doku) {
|
||||
this.states = new Stack<>();
|
||||
this.doku = doku;
|
||||
}
|
||||
|
||||
public void pushState() {
|
||||
states.add(new HashMap<>());
|
||||
saveState();
|
||||
}
|
||||
|
||||
public void popState() {
|
||||
assert (states.size() > 0);
|
||||
restoreState();
|
||||
states.pop();
|
||||
}
|
||||
|
||||
private void restoreState() {
|
||||
for (var entry : this.states.getLast().entrySet()) {
|
||||
entry.getKey().setSymbolIndex(entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
private void saveState() {
|
||||
for (Cell cell : this.doku.getCells()) {
|
||||
states.getLast().put(cell, cell.getSymbolIndex());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
package sudoku.structure;
|
||||
|
||||
import sudoku.constraint.BlockConstraint;
|
||||
import sudoku.constraint.Constraint;
|
||||
import sudoku.constraint.IConstraint;
|
||||
import sudoku.io.SudokuPrinter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -18,30 +21,30 @@ public class Sudoku {
|
||||
/**
|
||||
* Liste des Cells contenus dans le Sudoku.
|
||||
*/
|
||||
private final List<Cell> cells;
|
||||
private List<Cell> cells = new ArrayList<>();
|
||||
/**
|
||||
* Liste des contraintes (TODO) du Sudoku.
|
||||
*/
|
||||
private final List<IConstraint> constraints;
|
||||
private final List<Constraint> constraints;
|
||||
/**
|
||||
* Largeur des Blocks s'ils sont rectangulaires, sinon ça vaut 0.
|
||||
* Largeur des Blocks s'ils sont rectangulaires, valant 0 si ce n'est pas le cas.
|
||||
*/
|
||||
private int blockWidth;
|
||||
|
||||
public Sudoku(List<Cell> cells, List<Block> blocks, List<IConstraint> constraints) {
|
||||
public Sudoku(List<Cell> cells, List<Block> blocks, List<Constraint> constraints) {
|
||||
this.cells = cells;
|
||||
this.blocks = blocks;
|
||||
this.constraints = constraints;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforme un index de Cell en Coordiante.
|
||||
* Transforme un index de Cell en Coordinate.
|
||||
*
|
||||
* @param index int, index d'une Cell.
|
||||
* @return Coordinate, correspondante à l'index donné.
|
||||
*/
|
||||
public Coordinate toCoords(int index) {
|
||||
return new Coordinate( index % getSize(), index / getSize() );
|
||||
return new Coordinate(index % getSize(), index / getSize());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,7 +59,7 @@ public class Sudoku {
|
||||
|
||||
/**
|
||||
* Vérifie que des coordonnées correspondent bien à une Cell dans le Sudoku.
|
||||
* @return boolean, wether the coords are in the sudoku
|
||||
* @return boolean, valant true si les coordonnées sont dans les bornes du Sudoku, false sinon.
|
||||
*/
|
||||
public boolean isValidCoords(int x, int y) {
|
||||
int index = toIndex(x, y);
|
||||
@@ -64,14 +67,22 @@ public class Sudoku {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return wether the coords are in the sudoku
|
||||
* Vérifie que l'index correspond bien à une Cell dans le Sudoku.
|
||||
* @return boolean, valant true si l'index est dans les bornes du Sudoku, false sinon.
|
||||
*/
|
||||
public boolean isValidCoords(int index) {
|
||||
return index < getSize() * getSize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Teste si on peut placer la value dans la Cell aux coordonnées x, y d'après les contraintes du Sudoku.
|
||||
* @param x int, abscisse de la Cell voulue.
|
||||
* @param y int, ordonnée de la Cell voulue.
|
||||
* @param value int, index du symbole qu'on veut placer.
|
||||
* @return boolean, true si on peut la placer et false sinon.
|
||||
*/
|
||||
public boolean canBePlaced(int x, int y, int value) {
|
||||
for (IConstraint constraint : this.constraints) {
|
||||
for (Constraint constraint : this.constraints) {
|
||||
if (!constraint.canBePlaced(this, x, y, value)) {
|
||||
return false;
|
||||
}
|
||||
@@ -79,14 +90,11 @@ public class Sudoku {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean tryPlaceCellSymbol(int x, int y, int value) {
|
||||
assert (isValidCoords(x, y));
|
||||
if (!canBePlaced(x, y, value))
|
||||
return false;
|
||||
Cell cell = getCell(x, y);
|
||||
cell.setSymbolIndex(value);
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Vide la Cell dotn les coordonnées sont renseignées de son symbole.
|
||||
* @param x int, abscisse de la Cell voulue.
|
||||
* @param y int, coordonnée de la Cell voulue.
|
||||
*/
|
||||
|
||||
public void clearCell(int x, int y) {
|
||||
assert (isValidCoords(x, y));
|
||||
@@ -94,6 +102,9 @@ public class Sudoku {
|
||||
cell.setSymbolIndex(Cell.NOSYMBOL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Vide toutes les Cell du Sudoku.
|
||||
*/
|
||||
public void clear() {
|
||||
for (int i = 0; i < getSize() * getSize(); i++) {
|
||||
Cell cell = getCell(i);
|
||||
@@ -107,13 +118,15 @@ public class Sudoku {
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to place a cell at the given coordinate
|
||||
*
|
||||
* @return Cell created or null if it can't be done
|
||||
* Place le symbole d'index value dans la Cell de coordonnées précisées.
|
||||
* @param x int, abscisse de la Cell voulue.
|
||||
* @param y int, coordonnée de la Cell voulue.
|
||||
* @param value int, index du symbole à placer.
|
||||
* @return Cell, la Cell qui a été modifiée.
|
||||
*/
|
||||
public Cell setCellSymbol(int x, int y, int value) {
|
||||
assert (isValidCoords(x, y));
|
||||
for (IConstraint constraint : this.constraints) {
|
||||
for (Constraint constraint : this.constraints) {
|
||||
if (!constraint.canBePlaced(this, x, y, value)) {
|
||||
return null;
|
||||
}
|
||||
@@ -123,6 +136,11 @@ public class Sudoku {
|
||||
return cell;
|
||||
}
|
||||
|
||||
/**
|
||||
* Place les symboles d'index contenus dans values dans les cases du Sudoku.
|
||||
* @param values List<Integer>, liste des index des symboles à placer.
|
||||
* @return boolean, vaut true si les symboles ont été placés, false sinon.
|
||||
*/
|
||||
public boolean setCellsSymbol(List<Integer> values) {
|
||||
if (values.size() > this.cells.size()) {
|
||||
return false;
|
||||
@@ -136,6 +154,11 @@ public class Sudoku {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Place les symboles d'index contenus dans values dans les cases du Sudoku et rend ces cases immuables.
|
||||
* @param values List<Integer>, liste des index des symboles à placer.
|
||||
* @return boolean, vaut true si les symboles ont été placés, false sinon.
|
||||
*/
|
||||
public boolean setImmutableCellsSymbol(List<Integer> values) {
|
||||
if (values.size() > this.cells.size()) {
|
||||
return false;
|
||||
@@ -156,7 +179,7 @@ public class Sudoku {
|
||||
}
|
||||
|
||||
public Cell getCell(int x, int y) {
|
||||
int index = y * getSize() + x;
|
||||
int index = toIndex(x, y);
|
||||
assert (isValidCoords(x, y));
|
||||
try {
|
||||
return this.cells.get(index);
|
||||
@@ -169,7 +192,7 @@ public class Sudoku {
|
||||
return this.cells.get(i);
|
||||
}
|
||||
|
||||
public List<IConstraint> getConstraints() {
|
||||
public List<Constraint> getConstraints() {
|
||||
return constraints;
|
||||
}
|
||||
|
||||
@@ -185,56 +208,15 @@ public class Sudoku {
|
||||
return this.blocks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Vérifie si une Cell appartient au Sudoku.
|
||||
* @param cell Cell, cellule dont on veut vérifier l'appartenance au Sudoku.
|
||||
* @return boolean, vaut true si la Cell appartient au Sudoku.
|
||||
*/
|
||||
public boolean contains(Cell cell) {
|
||||
return this.cells.contains(cell);
|
||||
}
|
||||
|
||||
private Coordinate getCoordinateCell(Cell c) throws Exception {
|
||||
int x = 0, y = 0;
|
||||
int size = this.getSize();
|
||||
|
||||
if (!this.contains(c)) {
|
||||
throw new Exception("The given cell is not in this sudoku.");
|
||||
}
|
||||
|
||||
// TODO: use this.cells.indexOf();
|
||||
|
||||
for (Cell cell : this.cells) {
|
||||
if (cell == c) {
|
||||
return new Coordinate(x, y);
|
||||
}
|
||||
if (x == size - 1) {
|
||||
y += 1;
|
||||
x = 0;
|
||||
} else {
|
||||
x += 1;
|
||||
}
|
||||
}
|
||||
return new Coordinate(x, y);
|
||||
}
|
||||
|
||||
public void updateSymbolsPossibilities() throws Exception {
|
||||
for (IConstraint constraint : constraints) {
|
||||
List<Cell> cells = this.getCells();
|
||||
for (Cell cell : cells) {
|
||||
Coordinate coord = null;
|
||||
try {
|
||||
coord = this.getCoordinateCell(cell);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
List<Integer> newPossibleSymbols = cell.getPossibleSymbols();
|
||||
newPossibleSymbols.retainAll(constraint.getPossibleSymbols(this, coord.getX(), coord.getY()));
|
||||
|
||||
cell.setPossibleSymbols(newPossibleSymbols);
|
||||
|
||||
if (cell.getPossibleSymbols().isEmpty()) {
|
||||
throw new Exception("Rollback bitch");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Sudoku {");
|
||||
@@ -249,6 +231,10 @@ public class Sudoku {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Renvoie la 1re Cell vide du Sudoku.
|
||||
* @return Cell, une Cell vide, ou null s'il n'y en a pas.
|
||||
*/
|
||||
public Cell getFirstEmptyCell() {
|
||||
for (Cell cell : this.cells) {
|
||||
if (cell.isEmpty()) {
|
||||
@@ -258,70 +244,43 @@ public class Sudoku {
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Integer> getPossibleSymbolsOfCell(Cell cellToFill) {
|
||||
List<Integer> result = new ArrayList<>();
|
||||
Coordinate cellCoordinates;
|
||||
try {
|
||||
cellCoordinates = this.getCoordinateCell(cellToFill);
|
||||
} catch (Exception e) {
|
||||
return result;
|
||||
}
|
||||
for (int i = 0; i < this.constraints.size(); i++) {
|
||||
IConstraint constraint = this.constraints.get(i);
|
||||
if (i == 0) {
|
||||
result.addAll(constraint.getPossibleSymbols(this, cellCoordinates.getX(), cellCoordinates.getY()));
|
||||
} else {
|
||||
result.retainAll(constraint.getPossibleSymbols(this, cellCoordinates.getX(), cellCoordinates.getY()));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
/**
|
||||
* Vérifie si le Sudoku est résolue, soit complet et cohérent avec ses contraintes.
|
||||
* @return boolean, valant true si le Sudoku est résolu, false sinon.
|
||||
*/
|
||||
public boolean isSolved() {
|
||||
boolean isComplete = isComplete();
|
||||
boolean isValid = isValid();
|
||||
return isComplete && isValid;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
for (Cell cell : this.cells) {
|
||||
if (cell.isMutable()) {
|
||||
if (cell.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
for (IConstraint constraint : this.constraints) {
|
||||
Coordinate coords;
|
||||
try {
|
||||
int symbolPlaced = cell.getSymbolIndex();
|
||||
coords = this.getCoordinateCell(cell);
|
||||
|
||||
cell.setSymbolIndex(Cell.NOSYMBOL);
|
||||
List<Integer> possibleSymbols = constraint.getPossibleSymbols(this, coords.getX(),
|
||||
coords.getY());
|
||||
cell.setSymbolIndex(symbolPlaced);
|
||||
if (possibleSymbols.size() != 1 || possibleSymbols.get(0) != symbolPlaced) {
|
||||
return false;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
/**
|
||||
* Vérifie que le Sudoku est complet, soit qu'il n'y ait aucune case vide.
|
||||
* @return boolean, true si le Sudoku est complet, false sinon.
|
||||
*/
|
||||
private boolean isComplete() {
|
||||
return getFirstEmptyCell() == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object) {
|
||||
if (!(object instanceof Sudoku)) {
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Vérifie si le Sudoku est valide, soit qu'il est cohérent avec ses contraintes.
|
||||
* @return bollean, true si le Sudoku est valide, false sinon
|
||||
*/
|
||||
private boolean isValid() {
|
||||
for (int i = 0; i < cells.size(); i++) {
|
||||
Cell cell = getCell(i);
|
||||
if (cell.isEmpty())
|
||||
continue;
|
||||
|
||||
if (this.getSize() != ((Sudoku) object).getSize()) {
|
||||
return false;
|
||||
}
|
||||
Coordinate coordinate = toCoords(i);
|
||||
|
||||
for (int i = 0; i < this.getSize(); i++) {
|
||||
if (this.getCell(i).getSymbolIndex() != ((Sudoku) object).getCell(i).getSymbolIndex()) {
|
||||
int symbolPlaced = cell.empty();
|
||||
if (!canBePlaced(coordinate.getX(), coordinate.getY(), symbolPlaced)) {
|
||||
cell.setSymbolIndex(symbolPlaced);
|
||||
return false;
|
||||
}
|
||||
cell.setSymbolIndex(symbolPlaced);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -329,4 +288,8 @@ public class Sudoku {
|
||||
this.blockWidth = blockWidth;
|
||||
}
|
||||
|
||||
public boolean hasConstraint(Constraint constraint) {
|
||||
return this.constraints.contains(constraint);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,22 +1,38 @@
|
||||
package sudoku.structure;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import sudoku.constraint.BlockConstraint;
|
||||
import sudoku.constraint.ColumnConstraint;
|
||||
import sudoku.constraint.IConstraint;
|
||||
import sudoku.constraint.LineConstraint;
|
||||
import sudoku.constraint.Constraint;
|
||||
import sudoku.io.SudokuSerializer;
|
||||
import sudoku.solver.RandomSolver;
|
||||
import sudoku.solver.Solver;
|
||||
|
||||
public class SudokuFactory {
|
||||
|
||||
/**
|
||||
* Générateur de nombre aléatoire.
|
||||
*/
|
||||
private static final Random random = new Random();
|
||||
public static List<IConstraint> DEFAULT_CONSTRAINTS = Arrays.asList(new BlockConstraint(), new LineConstraint(), new ColumnConstraint());
|
||||
|
||||
/**
|
||||
* Liste des contraintes par défaut d'un Multi- ou Sudoku.
|
||||
* Comprend les contraintes de blocs, de lignes, et de colonnes.
|
||||
*/
|
||||
public static List<Constraint> DEFAULT_CONSTRAINTS = Arrays.asList(Constraint.Block, Constraint.Column,
|
||||
Constraint.Line);
|
||||
|
||||
/**
|
||||
* Créée des Cells et les met dans une liste de taille size.
|
||||
* @param size int, nombre de Cells à initialiser.
|
||||
* @return List<Cell>, liste des Cells initialisées.
|
||||
*/
|
||||
private static List<Cell> initCells(int size) {
|
||||
List<Cell> cells = new ArrayList<>(size * size);
|
||||
for (int i = 0; i < size * size; i++) {
|
||||
@@ -25,6 +41,13 @@ public class SudokuFactory {
|
||||
return cells;
|
||||
}
|
||||
|
||||
/**
|
||||
* Créée des Blocks de taille width par height à partir des cellules données, et les met dans une liste.
|
||||
* @param cells List<Cell>, liste des Cells à découper en Blocks.
|
||||
* @param width int, largeur des Blocks à créer.
|
||||
* @param height int, hauteur des Blocks à créer.
|
||||
* @return List<Block>, liste des Blocks créés.
|
||||
*/
|
||||
private static List<Block> initRectangleBlocs(List<Cell> cells, int width, int height) {
|
||||
List<Block> blocs = new ArrayList<>();
|
||||
int size = width * height;
|
||||
@@ -48,16 +71,32 @@ public class SudokuFactory {
|
||||
return blocs;
|
||||
}
|
||||
|
||||
public static MultiDoku createBasicEmptyRectangleSudoku(int widthBlock, int heightBlock) {
|
||||
Sudoku s = createRectangleSudoku(widthBlock, heightBlock);
|
||||
return new MultiDoku(Arrays.asList(s));
|
||||
/**
|
||||
* Créée un MultiDoku vide dont les Blocks sont de taille widthBlock par heightBlock.
|
||||
* @param widthBlock int, largeur des Blocks.
|
||||
* @param heightBlock int, hauteur des Blocks.
|
||||
* @return MultiDoku, MultiDoku vide.
|
||||
*/
|
||||
public static MultiDoku createBasicEmptyRectangleDoku(int widthBlock, int heightBlock,
|
||||
List<Constraint> constraints) {
|
||||
return new MultiDoku(Arrays.asList(createRectangleSudoku(widthBlock, heightBlock, constraints)));
|
||||
}
|
||||
|
||||
public static MultiDoku createBasicEmptySquareSudoku(int size) {
|
||||
return createBasicEmptyRectangleSudoku(size, size);
|
||||
/**
|
||||
* Créée un MultiDoku vide dont les Blocks sont carrés de longueur size.
|
||||
* @param size int, taille des Blocks.
|
||||
* @return MultiDoku, MultiDoku vide.
|
||||
*/
|
||||
public static MultiDoku createBasicEmptySquareDoku(int size, List<Constraint> constraints) {
|
||||
return new MultiDoku(Arrays.asList(createSquareSudoku(size, constraints)));
|
||||
}
|
||||
|
||||
public static void setIMMutableCells(MultiDoku doku, Map<Coordinate, Integer> immutableCells) {
|
||||
/**
|
||||
* Place des Cells immutables de valeurs fournies, aux Coordinate fournies dans le MultiDoku doku fourni.
|
||||
* @param doku MultiDoku, MultiDoku à remplir.
|
||||
* @param immutableCells Map<Coordinate, Integer>, association de Coordinate coordonnées et Integer valeurs, correspondant aux cases à remplir.
|
||||
*/
|
||||
public static void setImmutableCells(MultiDoku doku, Map<Coordinate, Integer> immutableCells) {
|
||||
immutableCells.forEach((coordinate, symbol) -> {
|
||||
for (Sudoku sudoku : doku.getSubGrids()) {
|
||||
Cell cell = sudoku.getCell(coordinate.getX(), coordinate.getY());
|
||||
@@ -69,13 +108,21 @@ public class SudokuFactory {
|
||||
});
|
||||
}
|
||||
|
||||
public static boolean newDokuFromFilledOne (MultiDoku doku, int difficulty) throws Exception {
|
||||
/**
|
||||
* Créée un MultiDoku de difficulté difficulty à partir d'un MultiDoku fourni.
|
||||
*
|
||||
* @param doku MultiDoku, MultiDoku dont on doit vider des Cells.
|
||||
* @param nbCellsToEmpty int, nombre de cases à retirer.
|
||||
* @return boolean, valant true si un MultiDoku de difficulté donnée peut être créée, false sinon.
|
||||
* @throws Exception si la difficulté n'est pas compatible avec la taille du MultiDoku.
|
||||
*/
|
||||
public static boolean newDokuFromFilledOne(MultiDoku doku, int nbCellsToEmpty, Solver solver) throws Exception {
|
||||
|
||||
if (difficulty > doku.getCells().size()) {
|
||||
if (nbCellsToEmpty >= doku.getCells().size()) {
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
if (difficulty == 0) {
|
||||
if (nbCellsToEmpty == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -87,8 +134,9 @@ public class SudokuFactory {
|
||||
|
||||
int oldSymbol = cellToEmpty.empty();
|
||||
|
||||
if (Solver.countSolution(doku) == 1) {
|
||||
if (newDokuFromFilledOne(doku, --difficulty)) {
|
||||
int nbDokuSultions = solver.countSolution(doku);
|
||||
if (nbDokuSultions == 1) {
|
||||
if (newDokuFromFilledOne(doku, --nbCellsToEmpty, solver)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -97,22 +145,42 @@ public class SudokuFactory {
|
||||
cellsThatCanBeEmptied.remove(cellToEmpty);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
return newDokuFromFilledOne(doku, --nbCellsToEmpty, solver);
|
||||
}
|
||||
|
||||
private static Sudoku createRectangleSudoku(int width, int height) {
|
||||
int symbolCount = width * height;
|
||||
/**
|
||||
* Créée un Sudoku vide dont les Blocks sont de taille widthBlock par heightBlock.
|
||||
* @param widthBlock int, largeur des Blocks.
|
||||
* @param heightBlock int, hauteur des Blocks.
|
||||
* @return Sudoku, Sudoku vide.
|
||||
*/
|
||||
private static Sudoku createRectangleSudoku(int widthBlock, int heightBlock, List<Constraint> constraints) {
|
||||
int symbolCount = widthBlock * heightBlock;
|
||||
List<Cell> cases = initCells(symbolCount);
|
||||
List<Block> blocs = initRectangleBlocs(cases, width, height);
|
||||
Sudoku s = new Sudoku(cases, blocs, DEFAULT_CONSTRAINTS);
|
||||
s.setBlockWidth(width);
|
||||
List<Block> blocs = initRectangleBlocs(cases, widthBlock, heightBlock);
|
||||
Sudoku s = new Sudoku(cases, blocs, constraints);
|
||||
for (Block block : s.getBlocks()) {
|
||||
block.getSudokus().add(s);
|
||||
}
|
||||
s.setBlockWidth(widthBlock);
|
||||
return s;
|
||||
}
|
||||
|
||||
private static Sudoku createSquareSudoku(int size) {
|
||||
return createRectangleSudoku(size, size);
|
||||
/**
|
||||
* Créée un Sudoku vide dont les Blocks sont carrés de longueur size.
|
||||
* @param size int, taille des Blocks.
|
||||
* @return Sudoku, Sudoku vide.
|
||||
*/
|
||||
private static Sudoku createSquareSudoku(int size, List<Constraint> constraints) {
|
||||
return createRectangleSudoku(size, size, constraints);
|
||||
}
|
||||
|
||||
/**
|
||||
* Connecte deux Sudokus selon la décalage offset fourni.
|
||||
* @param sudoku1 Sudoku, premier sudoku à connecter.
|
||||
* @param sudoku2 Sudoku, second sudoku à connecter.
|
||||
* @param offset Coordinate, décalage entre les deux Sudokus.
|
||||
*/
|
||||
private static void linkSquareSudokus(Sudoku sudoku1, Sudoku sudoku2, Coordinate offset) {
|
||||
int blockWidth = sudoku1.getBlockWidth();
|
||||
for (int dx = 0; dx < blockWidth; dx++) {
|
||||
@@ -128,6 +196,7 @@ public class SudokuFactory {
|
||||
|
||||
// on remplace le bloc
|
||||
sudoku2.getBlocks().set(block2Y * blockWidth + block2X, block1);
|
||||
block1.getSudokus().add(sudoku2);
|
||||
|
||||
// on remplace les cellules
|
||||
for (int i = 0; i < block1.getCells().size(); i++) {
|
||||
@@ -142,20 +211,25 @@ public class SudokuFactory {
|
||||
}
|
||||
}
|
||||
|
||||
public static MultiDoku createBasicSquareMultidoku(int size) {
|
||||
/**
|
||||
* Créée un MultiDoku de Blocks carrés de taille size composé de cinq Sudokus, dont un central qui partage chacun de ses Blockss d'angle avec un autre Sudoku.
|
||||
* @param size int, largeur des Blocks unitraires des Sudokus à crééer.
|
||||
* @return MultiDoku, MultiDoku de forme X.
|
||||
*/
|
||||
public static MultiDoku createBasicXShapedMultidoku(int size, List<Constraint> constraints) {
|
||||
assert (size > 1);
|
||||
|
||||
/**
|
||||
/*
|
||||
* 2 3
|
||||
* 1
|
||||
* 4 5
|
||||
*/
|
||||
|
||||
Sudoku sudoku1 = createSquareSudoku(size);
|
||||
Sudoku sudoku2 = createSquareSudoku(size);
|
||||
Sudoku sudoku3 = createSquareSudoku(size);
|
||||
Sudoku sudoku4 = createSquareSudoku(size);
|
||||
Sudoku sudoku5 = createSquareSudoku(size);
|
||||
Sudoku sudoku1 = createSquareSudoku(size, constraints);
|
||||
Sudoku sudoku2 = createSquareSudoku(size, constraints);
|
||||
Sudoku sudoku3 = createSquareSudoku(size, constraints);
|
||||
Sudoku sudoku4 = createSquareSudoku(size, constraints);
|
||||
Sudoku sudoku5 = createSquareSudoku(size, constraints);
|
||||
|
||||
linkSquareSudokus(sudoku1, sudoku2, new Coordinate(1 - size, 1 - size));
|
||||
linkSquareSudokus(sudoku1, sudoku3, new Coordinate(size - 1, 1 - size));
|
||||
@@ -164,4 +238,82 @@ public class SudokuFactory {
|
||||
|
||||
return new MultiDoku(Arrays.asList(sudoku1, sudoku2, sudoku3, sudoku4, sudoku5));
|
||||
}
|
||||
|
||||
/**
|
||||
* Créée un MultiDoku de Blocks rectangulaires de forme width par height composé de cinq Sudokus,
|
||||
* dont un central qui partage chacun de ses Blocks d'angle avec un autre
|
||||
* Sudoku.
|
||||
*
|
||||
* @param width int, largeur des Blocks unitraires des Sudokus à crééer.
|
||||
* @param height int, hauteur des Blocks unitraires des Sudokus à crééer.
|
||||
* @return MultiDoku, MultiDoku de forme X.
|
||||
*/
|
||||
public static MultiDoku createBasicXShapedMultidoku(int width, int height, List<Constraint> constraints) {
|
||||
assert (width > 1 && height > 1);
|
||||
|
||||
/*
|
||||
* 2 3
|
||||
* 1
|
||||
* 4 5
|
||||
*/
|
||||
|
||||
Sudoku sudoku1 = createRectangleSudoku(width, height, constraints);
|
||||
Sudoku sudoku2 = createRectangleSudoku(width, height, constraints);
|
||||
Sudoku sudoku3 = createRectangleSudoku(width, height, constraints);
|
||||
Sudoku sudoku4 = createRectangleSudoku(width, height, constraints);
|
||||
Sudoku sudoku5 = createRectangleSudoku(width, height, constraints);
|
||||
|
||||
linkSquareSudokus(sudoku1, sudoku2, new Coordinate(1 - width, 1 - height));
|
||||
linkSquareSudokus(sudoku1, sudoku3, new Coordinate(width - 1, 1 - height));
|
||||
linkSquareSudokus(sudoku1, sudoku4, new Coordinate(1 - width, height - 1));
|
||||
linkSquareSudokus(sudoku1, sudoku5, new Coordinate(width - 1, height - 1));
|
||||
|
||||
return new MultiDoku(Arrays.asList(sudoku1, sudoku2, sudoku3, sudoku4, sudoku5));
|
||||
}
|
||||
|
||||
public static void fillDoku(MultiDoku doku, Difficulty difficulty) throws Exception {
|
||||
Solver solver = new RandomSolver();
|
||||
solver.solve(doku);
|
||||
int nbCellsToEmpty = (int) (difficulty.getFactor() * doku.getNbCells());
|
||||
boolean successfull = newDokuFromFilledOne(doku, nbCellsToEmpty, solver);
|
||||
if (!successfull) {
|
||||
throw new Exception("Canno't create this doku with this difficulty");
|
||||
}
|
||||
doku.setFilledCellsImmutable();
|
||||
}
|
||||
|
||||
public static MultiDoku fromfile(String filePath) {
|
||||
try {
|
||||
String content = Files.readString(Paths.get(filePath));
|
||||
MultiDoku doku = SudokuSerializer.deserializeSudoku(content);
|
||||
return doku;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static MultiDoku createBasicEmptyRandomBlockDoku(int blockSize, List<Constraint> constraints) {
|
||||
int blockCellCount = blockSize * blockSize;
|
||||
List<Cell> cells = initCells(blockCellCount);
|
||||
List<Cell> homeLessCells = new ArrayList<>();
|
||||
homeLessCells.addAll(cells);
|
||||
List<Block> blocks = new ArrayList<>();
|
||||
Random r = new Random();
|
||||
for (int i = 0 ; i < blockCellCount; i++) {
|
||||
Block b = new Block();
|
||||
for (int j = 0; j < blockCellCount; j++) {
|
||||
int cellIndex = r.nextInt(homeLessCells.size());
|
||||
Cell cell = homeLessCells.remove(cellIndex);
|
||||
b.addCell(cell);
|
||||
cell.setBlock(b);
|
||||
}
|
||||
blocks.add(b);
|
||||
}
|
||||
Sudoku sudoku = new Sudoku(cells, blocks, constraints);
|
||||
for (Block block : blocks) {
|
||||
block.getSudokus().add(sudoku);
|
||||
}
|
||||
return new MultiDoku(Arrays.asList(sudoku));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,34 +3,76 @@ package sudoku;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Random;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import sudoku.io.SudokuPrinter;
|
||||
import sudoku.io.SudokuSerializer;
|
||||
import sudoku.solver.RandomSolver;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.SudokuFactory;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class SudokuSerializerTest {
|
||||
|
||||
void testSerializeWithSize(int blockWidth, int blockHeight) {
|
||||
var sudoku = SudokuFactory.createBasicEmptyRectangleSudoku(blockWidth, blockHeight);
|
||||
var sudoku = SudokuFactory.createBasicEmptyRectangleDoku(blockWidth, blockHeight,
|
||||
SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
new RandomSolver().solve(sudoku);
|
||||
JSONObject data = SudokuSerializer.serializeSudoku(sudoku);
|
||||
SudokuSerializer.saveMultiDoku(sudoku);
|
||||
//MultiDoku multiDoku = SudokuSerializer.deserializeSudoku(data);
|
||||
//assertEquals(data, SudokuSerializer.serializeSudoku(multiDoku));
|
||||
MultiDoku multiDoku = SudokuSerializer.deserializeSudoku(data);
|
||||
assertTrue(data.toString().equals(SudokuSerializer.serializeSudoku(multiDoku).toString()));
|
||||
}
|
||||
|
||||
void testSaveWithSize(int blockWidth, int blockHeight) {
|
||||
MultiDoku doku = SudokuFactory.createBasicEmptyRectangleDoku(blockWidth, blockHeight,
|
||||
SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
new RandomSolver().solve(doku);
|
||||
String savePath = SudokuSerializer.saveMultiDoku(doku);
|
||||
MultiDoku otherDoku = null;
|
||||
try {
|
||||
otherDoku = SudokuFactory.fromfile(savePath);
|
||||
assert (otherDoku != null);
|
||||
|
||||
assertEquals(SudokuSerializer.serializeSudoku(doku).toString(),
|
||||
SudokuSerializer.serializeSudoku(otherDoku).toString());
|
||||
// clean file after test
|
||||
File fileToDelete = new File(savePath);
|
||||
fileToDelete.delete();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
assert false;
|
||||
}
|
||||
}
|
||||
|
||||
void testSerializeX(int size) {
|
||||
var sudoku = SudokuFactory.createBasicXShapedMultidoku(size, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
new RandomSolver().solve(sudoku);
|
||||
JSONObject data = SudokuSerializer.serializeSudoku(sudoku);
|
||||
MultiDoku multiDoku = SudokuSerializer.deserializeSudoku(data);
|
||||
|
||||
assertTrue(data.toString().equals(SudokuSerializer.serializeSudoku(multiDoku).toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSerialize() {
|
||||
int testCount = 5;
|
||||
Random r = new Random();
|
||||
int testCount = 20;
|
||||
for (int i = 0; i < testCount; i++) {
|
||||
int blockWidth = r.nextInt(20) + 1;
|
||||
int blockHeight = r.nextInt(20) + 1;
|
||||
int blockWidth = r.nextInt(4) + 1;
|
||||
int blockHeight = r.nextInt(4) + 1;
|
||||
testSerializeWithSize(blockWidth, blockHeight);
|
||||
}
|
||||
for (int i = 0; i < testCount; i++) {
|
||||
int blockWidth = r.nextInt(4) + 1;
|
||||
int blockHeight = r.nextInt(4) + 1;
|
||||
testSaveWithSize(blockWidth, blockHeight);
|
||||
}
|
||||
for (int i = 0; i < testCount; i++) {
|
||||
int size = r.nextInt(2) + 2;
|
||||
testSerializeX(size);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,96 +1,106 @@
|
||||
package sudoku.solver;
|
||||
|
||||
import gui.Symbols;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import sudoku.io.SudokuPrinter;
|
||||
import sudoku.io.SudokuSerializer;
|
||||
import sudoku.structure.Cell;
|
||||
import sudoku.structure.MultiDoku;
|
||||
import sudoku.structure.Sudoku;
|
||||
import sudoku.structure.SudokuFactory;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
class SolverTest {
|
||||
|
||||
@Test
|
||||
void solveTest() {
|
||||
Random rand = new Random();
|
||||
@Test
|
||||
void solveTest() {
|
||||
Random rand = new Random();
|
||||
|
||||
MultiDoku dokuToTest = SudokuFactory.createBasicEmptySquareSudoku(3);
|
||||
MultiDoku dokuResult = SudokuFactory.createBasicEmptySquareSudoku(3);
|
||||
MultiDoku dokuToTest = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
MultiDoku dokuResult = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
|
||||
Sudoku sudokuToTest = dokuToTest.getSubGrid(0);
|
||||
Sudoku sudokuResult = dokuResult.getSubGrid(0);
|
||||
Sudoku sudokuToTest = dokuToTest.getSubGrid(0);
|
||||
Sudoku sudokuResult = dokuResult.getSubGrid(0);
|
||||
|
||||
int ns = Cell.NOSYMBOL;
|
||||
List<Integer> immutableCells = List.of(ns, ns, 0, ns, ns, 2, 8, ns, 1,
|
||||
ns, 3, ns, ns, 5, 6, 7, ns, ns,
|
||||
ns, ns, ns, 8, ns, 7, ns, ns, 6,
|
||||
0, ns, 1, ns, ns, ns, ns, ns, ns,
|
||||
4, 8, 7, 5, 1, ns, 6, ns, ns,
|
||||
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);
|
||||
int ns = Cell.NOSYMBOL;
|
||||
List<Integer> immutableCells = List.of(ns, ns, 0, ns, ns, 2, 8, ns, 1,
|
||||
ns, 3, ns, ns, 5, 6, 7, ns, ns,
|
||||
ns, ns, ns, 8, ns, 7, ns, ns, 6,
|
||||
0, ns, 1, ns, ns, ns, ns, ns, ns,
|
||||
4, 8, 7, 5, 1, ns, 6, ns, ns,
|
||||
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);
|
||||
|
||||
assert(sudokuToTest.setImmutableCellsSymbol(immutableCells));
|
||||
assert (sudokuToTest.setImmutableCellsSymbol(immutableCells));
|
||||
|
||||
//SudokuPrinter.printRectangleSudoku(dokuToTest.getSubGrid(0), 3, 3);
|
||||
SudokuPrinter.printMultiDoku(dokuToTest, 3, 3, Symbols.Numbers);
|
||||
|
||||
SudokuPrinter.printRectangleSudoku(dokuToTest.getSubGrid(0), 3, 3);
|
||||
List<Integer> correctCells = List.of(7, 6, 0, 3, 4, 2, 8, 5, 1,
|
||||
2, 3, 8, 1, 5, 6, 7, 0, 4,
|
||||
1, 4, 5, 8, 0, 7, 3, 2, 6,
|
||||
0, 2, 1, 6, 8, 3, 5, 4, 7,
|
||||
4, 8, 7, 5, 1, 0, 6, 3, 2,
|
||||
6, 5, 3, 2, 7, 4, 1, 8, 0,
|
||||
3, 1, 6, 0, 2, 8, 4, 7, 5,
|
||||
8, 0, 4, 7, 6, 5, 2, 1, 3,
|
||||
5, 7, 2, 4, 3, 1, 0, 6, 8);
|
||||
|
||||
sudokuResult.setCellsSymbol(correctCells);
|
||||
|
||||
List<Integer> correctCells = List.of(7, 6, 0, 3, 4, 2, 8, 5, 1,
|
||||
2, 3, 8, 1, 5, 6, 7, 0, 4,
|
||||
1, 4, 5, 8, 0, 7, 3, 2, 6,
|
||||
0, 2, 1, 6, 8, 3, 5, 4, 7,
|
||||
4, 8, 7, 5, 1, 0, 6, 3, 2,
|
||||
6, 5, 3, 2, 7, 4, 1, 8, 0,
|
||||
3, 1, 6, 0, 2, 8, 4, 7, 5,
|
||||
8, 0, 4, 7, 6, 5, 2, 1, 3,
|
||||
5, 7, 2, 4, 3, 1, 0, 6, 8);
|
||||
System.out.println("\n****************************Doku Control\n");
|
||||
SudokuPrinter.printRectangleSudoku(sudokuResult, 3, 3, Symbols.Russian);
|
||||
|
||||
sudokuResult.setCellsSymbol(correctCells);
|
||||
assert (dokuResult.isSolved());
|
||||
|
||||
new RandomSolver().solve(dokuToTest);
|
||||
|
||||
System.out.println("\n****************************Doku Control\n");
|
||||
SudokuPrinter.printRectangleSudoku(sudokuResult, 3, 3);
|
||||
System.out.println("\n****************************\nDoku solved");
|
||||
//SudokuPrinter.printRectangleSudoku(dokuToTest.getSubGrid(0), 3, 3);
|
||||
SudokuPrinter.printMultiDoku(dokuToTest, 3, 3, Symbols.Emojis);
|
||||
|
||||
assert (dokuToTest.isSolved());
|
||||
|
||||
assert(dokuResult.isValid());
|
||||
for (Cell cell : sudokuToTest.getCells()) {
|
||||
cell.setImmutable();
|
||||
}
|
||||
|
||||
Solver.solveRandom(dokuToTest, rand);
|
||||
for (Cell cell : sudokuResult.getCells()) {
|
||||
cell.setImmutable();
|
||||
}
|
||||
|
||||
assertEquals(SudokuSerializer.serializeSudoku(dokuResult).toString(),
|
||||
SudokuSerializer.serializeSudoku(dokuToTest).toString());
|
||||
|
||||
System.out.println("\n****************************\nDoku solved");
|
||||
SudokuPrinter.printRectangleSudoku(dokuToTest.getSubGrid(0), 3, 3);
|
||||
MultiDoku dokuToTest2 = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
Sudoku sudokuToTest2 = dokuToTest2.getSubGrid(0);
|
||||
|
||||
List<Integer> immutableCells2 = List.of(ns, ns, 0, ns, ns, 2, 8, ns, 1,
|
||||
1, 3, ns, ns, 5, 6, 7, ns, ns,
|
||||
ns, ns, ns, 8, ns, 7, ns, ns, 6,
|
||||
0, ns, 1, ns, ns, ns, ns, ns, ns,
|
||||
4, 8, 7, 5, 1, ns, 6, ns, ns,
|
||||
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);
|
||||
|
||||
assert(dokuToTest.isValid());
|
||||
boolean isSolved = new RandomSolver().solve(dokuToTest2);
|
||||
|
||||
assert(dokuToTest.equals(dokuResult));
|
||||
assert (!isSolved);
|
||||
|
||||
MultiDoku dokuToTest2 = SudokuFactory.createBasicEmptySquareSudoku(3);
|
||||
Sudoku sudokuToTest2 = dokuToTest2.getSubGrid(0);
|
||||
MultiDoku dokuToTest3 = SudokuFactory.createBasicEmptySquareDoku(3, SudokuFactory.DEFAULT_CONSTRAINTS);
|
||||
|
||||
List<Integer> immutableCells2 = List.of(ns, ns, 0, ns, ns, 2, 8, ns, 1,
|
||||
1, 3, ns, ns, 5, 6, 7, ns, ns,
|
||||
ns, ns, ns, 8, ns, 7, ns, ns, 6,
|
||||
0, ns, 1, ns, ns, ns, ns, ns, ns,
|
||||
4, 8, 7, 5, 1, ns, 6, ns, ns,
|
||||
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);
|
||||
new RandomSolver().solve(dokuToTest3);
|
||||
|
||||
boolean isSolved = Solver.solveRandom(dokuToTest2, rand);
|
||||
|
||||
assert(!isSolved);
|
||||
|
||||
MultiDoku dokuToTest3 = SudokuFactory.createBasicEmptySquareSudoku(3);
|
||||
|
||||
Solver.solveRandom(dokuToTest3, rand);
|
||||
|
||||
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