Compare commits
12 Commits
e19a9c7b27
...
b05351bc20
| Author | SHA1 | Date | |
|---|---|---|---|
| b05351bc20 | |||
| 7ab1173fb3 | |||
| 749f5c831d | |||
| 48fc88d8ab | |||
| 6c8bfad18d | |||
| ba65cb9ff5 | |||
| 396abb30b3 | |||
| ea5afa66df | |||
| a8359d2f69 | |||
| 2d980ac816 | |||
|
|
42d5bdc8d6 | ||
| 121c60ddef |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -7,4 +7,6 @@ build
|
|||||||
.vscode
|
.vscode
|
||||||
.idea
|
.idea
|
||||||
|
|
||||||
output.puml
|
output.puml
|
||||||
|
|
||||||
|
imgui.ini
|
||||||
@@ -29,11 +29,13 @@ dependencies {
|
|||||||
implementation 'org.json:json:20250107'
|
implementation 'org.json:json:20250107'
|
||||||
|
|
||||||
implementation 'com.fasterxml.jackson.core:jackson-databind:2.18.2'
|
implementation 'com.fasterxml.jackson.core:jackson-databind:2.18.2'
|
||||||
|
|
||||||
|
implementation "io.github.spair:imgui-java-app:1.88.0"
|
||||||
}
|
}
|
||||||
|
|
||||||
application {
|
application {
|
||||||
// Define the main class for the application.
|
// Define the main class for the application.
|
||||||
mainClass = 'sudoku.Main'
|
mainClass = 'gui.Main'
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.named('test') {
|
tasks.named('test') {
|
||||||
|
|||||||
BIN
app/comic.ttf
Normal file
BIN
app/comic.ttf
Normal file
Binary file not shown.
70
app/src/main/java/gui/ColorGenerator.java
Normal file
70
app/src/main/java/gui/ColorGenerator.java
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
package gui;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ColorGenerator {
|
||||||
|
|
||||||
|
public static class Color {
|
||||||
|
public float r, g, b;
|
||||||
|
|
||||||
|
public Color(float r, float g, float b) {
|
||||||
|
this.r = r;
|
||||||
|
this.g = g;
|
||||||
|
this.b = b;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Color> greatPalette(int colorCount) {
|
||||||
|
List<Color> colors = greatScheme(colorCount);
|
||||||
|
List<Color> newOrder = new ArrayList<>();
|
||||||
|
int newIndex = 0;
|
||||||
|
while (!colors.isEmpty()) {
|
||||||
|
int randomIndex = newIndex % colors.size();
|
||||||
|
newOrder.add(colors.get(randomIndex));
|
||||||
|
colors.remove(randomIndex);
|
||||||
|
newIndex += Math.sqrt(colorCount) + 1;
|
||||||
|
}
|
||||||
|
return newOrder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Color> greatScheme(int colorCount) {
|
||||||
|
List<Color> colors = new ArrayList<>();
|
||||||
|
for (int i = 0; i < colorCount; i++) {
|
||||||
|
colors.add(hslToRgb((float) (i) / (float) colorCount, 0.9f, 0.4f));
|
||||||
|
}
|
||||||
|
return colors;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Color hslToRgb(float h, float s, float l){
|
||||||
|
float r, g, b;
|
||||||
|
|
||||||
|
if (s == 0f) {
|
||||||
|
r = g = b = l; // achromatic
|
||||||
|
} else {
|
||||||
|
float q = l < 0.5f ? l * (1 + s) : l + s - l * s;
|
||||||
|
float p = 2 * l - q;
|
||||||
|
r = hueToRgb(p, q, h + 1f/3f);
|
||||||
|
g = hueToRgb(p, q, h);
|
||||||
|
b = hueToRgb(p, q, h - 1f/3f);
|
||||||
|
}
|
||||||
|
return new Color(r, g, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Helper method that converts hue to rgb */
|
||||||
|
public static float hueToRgb(float p, float q, float t) {
|
||||||
|
if (t < 0f)
|
||||||
|
t += 1f;
|
||||||
|
if (t > 1f)
|
||||||
|
t -= 1f;
|
||||||
|
if (t < 1f / 6f)
|
||||||
|
return p + (q - p) * 6f * t;
|
||||||
|
if (t < 1f / 2f)
|
||||||
|
return q;
|
||||||
|
if (t < 2f / 3f)
|
||||||
|
return p + (q - p) * (2f / 3f - t) * 6f;
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
34
app/src/main/java/gui/Main.java
Normal file
34
app/src/main/java/gui/Main.java
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package gui;
|
||||||
|
|
||||||
|
import gui.menu.MainMenu;
|
||||||
|
import gui.menu.StateMachine;
|
||||||
|
import imgui.ImGui;
|
||||||
|
import imgui.app.Application;
|
||||||
|
import imgui.app.Configuration;
|
||||||
|
|
||||||
|
public class Main extends Application {
|
||||||
|
|
||||||
|
private final StateMachine stateMachine = new StateMachine();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void configure(Configuration config) {
|
||||||
|
config.setTitle("Let's play sudoku!");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void initImGui(Configuration config) {
|
||||||
|
super.initImGui(config);
|
||||||
|
ImGui.getIO().getFonts().addFontFromFileTTF("comic.ttf", 50.0f);
|
||||||
|
stateMachine.pushState(new MainMenu(stateMachine));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void process() {
|
||||||
|
stateMachine.render();
|
||||||
|
// ImGui.showDemoWindow();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
launch(new Main());
|
||||||
|
}
|
||||||
|
}
|
||||||
79
app/src/main/java/gui/SudokuRenderer.java
Normal file
79
app/src/main/java/gui/SudokuRenderer.java
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
package gui;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import gui.ColorGenerator.Color;
|
||||||
|
import imgui.ImGui;
|
||||||
|
import imgui.ImVec2;
|
||||||
|
import imgui.ImVec4;
|
||||||
|
import imgui.flag.ImGuiCol;
|
||||||
|
import imgui.flag.ImGuiStyleVar;
|
||||||
|
import sudoku.Block;
|
||||||
|
import sudoku.Cell;
|
||||||
|
import sudoku.Sudoku;
|
||||||
|
|
||||||
|
public class SudokuRenderer {
|
||||||
|
|
||||||
|
private final Sudoku sudoku;
|
||||||
|
private int currentIndex = -1;
|
||||||
|
private final Map<Block, Color> colorPalette;
|
||||||
|
|
||||||
|
public SudokuRenderer(Sudoku sudoku) {
|
||||||
|
this.sudoku = sudoku;
|
||||||
|
this.colorPalette = new HashMap<>();
|
||||||
|
initColors();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initColors() {
|
||||||
|
List<Color> colors = ColorGenerator.greatPalette(sudoku.getSize());
|
||||||
|
int index = 0;
|
||||||
|
for (Block block : sudoku.getBlocks()) {
|
||||||
|
colorPalette.put(block, colors.get(index));
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void renderPopup() {
|
||||||
|
if (ImGui.beginPopup("editPopup")) {
|
||||||
|
for (int i = 1; i < sudoku.getSize() + 1; i++) {
|
||||||
|
if (i % (int) (Math.sqrt(sudoku.getSize())) != 1)
|
||||||
|
ImGui.sameLine();
|
||||||
|
if (ImGui.button(Integer.toString(i), new ImVec2(50, 50))) {
|
||||||
|
this.sudoku.setCellSymbol(currentIndex % sudoku.getSize(), currentIndex / sudoku.getSize(), i - 1);
|
||||||
|
ImGui.closeCurrentPopup();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ImGui.endPopup();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void render() {
|
||||||
|
ImGui.begin("Sudoku Window");
|
||||||
|
for (int y = 0; y < sudoku.getSize(); y++) {
|
||||||
|
for (int x = 0; x < sudoku.getSize(); x++) {
|
||||||
|
if (x > 0)
|
||||||
|
ImGui.sameLine();
|
||||||
|
int index = y * sudoku.getSize() + x;
|
||||||
|
Cell cell = sudoku.getCell(x, y);
|
||||||
|
int symbol = cell.getSymbolIndex();
|
||||||
|
Color blockColor = colorPalette.get(cell.getBlock());
|
||||||
|
ImGui.pushStyleVar(ImGuiStyleVar.SelectableTextAlign, new ImVec2(0.5f, 0.5f));
|
||||||
|
ImGui.pushStyleColor(ImGuiCol.Header, new ImVec4(blockColor.r, blockColor.g, blockColor.b, 1.0f));
|
||||||
|
String cellText = "";
|
||||||
|
if (symbol != -1)
|
||||||
|
cellText += Integer.toString(symbol + 1);
|
||||||
|
if (ImGui.selectable(cellText + "##" + index, true, 0, new ImVec2(50, 50))) {
|
||||||
|
ImGui.openPopup("editPopup");
|
||||||
|
currentIndex = index;
|
||||||
|
}
|
||||||
|
ImGui.popStyleVar();
|
||||||
|
ImGui.popStyleColor();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
renderPopup();
|
||||||
|
ImGui.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
25
app/src/main/java/gui/menu/BaseView.java
Normal file
25
app/src/main/java/gui/menu/BaseView.java
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package gui.menu;
|
||||||
|
|
||||||
|
import imgui.ImGui;
|
||||||
|
|
||||||
|
public abstract class BaseView {
|
||||||
|
|
||||||
|
protected final StateMachine stateMachine;
|
||||||
|
|
||||||
|
public BaseView(StateMachine stateMachine) {
|
||||||
|
this.stateMachine = stateMachine;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void render();
|
||||||
|
|
||||||
|
public void closeMenu() {
|
||||||
|
this.stateMachine.popState();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void renderReturnButton() {
|
||||||
|
if (ImGui.button("Retour")) {
|
||||||
|
closeMenu();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
17
app/src/main/java/gui/menu/CreateGameMenu.java
Normal file
17
app/src/main/java/gui/menu/CreateGameMenu.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package gui.menu;
|
||||||
|
|
||||||
|
import imgui.ImGui;
|
||||||
|
|
||||||
|
public class CreateGameMenu extends BaseView {
|
||||||
|
|
||||||
|
public CreateGameMenu(StateMachine stateMachine) {
|
||||||
|
super(stateMachine);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void render() {
|
||||||
|
ImGui.text("Créer");
|
||||||
|
renderReturnButton();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
17
app/src/main/java/gui/menu/JoinGameMenu.java
Normal file
17
app/src/main/java/gui/menu/JoinGameMenu.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package gui.menu;
|
||||||
|
|
||||||
|
import imgui.ImGui;
|
||||||
|
|
||||||
|
public class JoinGameMenu extends BaseView {
|
||||||
|
|
||||||
|
public JoinGameMenu(StateMachine stateMachine) {
|
||||||
|
super(stateMachine);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void render() {
|
||||||
|
ImGui.text("Rejoindre");
|
||||||
|
renderReturnButton();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
37
app/src/main/java/gui/menu/MainMenu.java
Normal file
37
app/src/main/java/gui/menu/MainMenu.java
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package gui.menu;
|
||||||
|
|
||||||
|
import imgui.ImGui;
|
||||||
|
import imgui.ImVec2;
|
||||||
|
|
||||||
|
public class MainMenu extends BaseView {
|
||||||
|
|
||||||
|
public MainMenu(StateMachine stateMachine) {
|
||||||
|
super(stateMachine);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void render() {
|
||||||
|
var displaySize = ImGui.getIO().getDisplaySize();
|
||||||
|
ImVec2 buttonSize = new ImVec2(500, 150);
|
||||||
|
int paddingHeight = 20;
|
||||||
|
|
||||||
|
float centerX = displaySize.x / 2.0f - buttonSize.x / 2.0f;
|
||||||
|
float centerY = displaySize.y / 2.0f - buttonSize.y / 2.0f;
|
||||||
|
|
||||||
|
ImGui.setCursorPos(centerX, centerY - buttonSize.y - paddingHeight);
|
||||||
|
if (ImGui.button("Mode histoire", buttonSize)) {
|
||||||
|
this.stateMachine.pushState(new SoloMenu(this.stateMachine));
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.setCursorPos(centerX, centerY);
|
||||||
|
if (ImGui.button("Multijoueur", buttonSize)) {
|
||||||
|
this.stateMachine.pushState(new MultiMenu(this.stateMachine));
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.setCursorPos(centerX, centerY + buttonSize.y + paddingHeight);
|
||||||
|
if (ImGui.button("Options", buttonSize)) {
|
||||||
|
this.stateMachine.pushState(new OptionsMenu(this.stateMachine));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
56
app/src/main/java/gui/menu/MultiMenu.java
Normal file
56
app/src/main/java/gui/menu/MultiMenu.java
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
package gui.menu;
|
||||||
|
|
||||||
|
import imgui.ImGui;
|
||||||
|
import imgui.ImVec2;
|
||||||
|
import imgui.type.ImInt;
|
||||||
|
import imgui.type.ImString;
|
||||||
|
|
||||||
|
public class MultiMenu extends BaseView {
|
||||||
|
|
||||||
|
private final ImInt port = new ImInt(25565);
|
||||||
|
private final ImString address = new ImString("localhost");
|
||||||
|
|
||||||
|
public MultiMenu(StateMachine stateMachine) {
|
||||||
|
super(stateMachine);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void renderCreate() {
|
||||||
|
ImVec2 displaySize = ImGui.getIO().getDisplaySize();
|
||||||
|
ImGui.beginChild("##CreateGame", new ImVec2(displaySize.x / 2.0f, displaySize.y * 8.0f / 9.0f));
|
||||||
|
ImGui.inputInt("Port", port);
|
||||||
|
if (ImGui.button("Créer")) {
|
||||||
|
// TODO: create game
|
||||||
|
}
|
||||||
|
ImGui.endChild();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void renderJoin() {
|
||||||
|
ImVec2 displaySize = ImGui.getIO().getDisplaySize();
|
||||||
|
ImGui.beginChild("##JoinGame", new ImVec2(displaySize.x / 2.0f, displaySize.y * 8.0f / 9.0f));
|
||||||
|
ImGui.inputText("Adresse", address);
|
||||||
|
ImGui.inputInt("Port", port);
|
||||||
|
if (ImGui.button("Rejoindre")) {
|
||||||
|
// TODO: join game
|
||||||
|
}
|
||||||
|
ImGui.endChild();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void render() {
|
||||||
|
|
||||||
|
renderCreate();
|
||||||
|
ImGui.sameLine();
|
||||||
|
renderJoin();
|
||||||
|
|
||||||
|
ImVec2 displaySize = ImGui.getIO().getDisplaySize();
|
||||||
|
ImVec2 buttonSize = new ImVec2(500, 70.0f);
|
||||||
|
|
||||||
|
float centerX = displaySize.x / 2.0f - buttonSize.x / 2.0f;
|
||||||
|
|
||||||
|
ImGui.setCursorPosX(centerX);
|
||||||
|
if (ImGui.button("Retour", buttonSize)) {
|
||||||
|
closeMenu();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
18
app/src/main/java/gui/menu/OptionsMenu.java
Normal file
18
app/src/main/java/gui/menu/OptionsMenu.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package gui.menu;
|
||||||
|
|
||||||
|
import imgui.ImGui;
|
||||||
|
|
||||||
|
public class OptionsMenu extends BaseView {
|
||||||
|
|
||||||
|
public OptionsMenu(StateMachine stateMachine) {
|
||||||
|
super(stateMachine);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void render() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
ImGui.text("Options");
|
||||||
|
renderReturnButton();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
47
app/src/main/java/gui/menu/SoloMenu.java
Normal file
47
app/src/main/java/gui/menu/SoloMenu.java
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package gui.menu;
|
||||||
|
|
||||||
|
import imgui.ImGui;
|
||||||
|
import imgui.type.ImInt;
|
||||||
|
|
||||||
|
public class SoloMenu extends BaseView {
|
||||||
|
|
||||||
|
private final ImInt sudokuType = new ImInt(0);
|
||||||
|
private static final String[] sudokuTypes = { "Carré", "Rectangle" };
|
||||||
|
private static final int SQUARE = 0, RECTANGLE = 1;
|
||||||
|
|
||||||
|
private final ImInt sudokuSize = new ImInt(3);
|
||||||
|
|
||||||
|
private final ImInt sudokuWidth = new ImInt(3);
|
||||||
|
private final ImInt sudokuHeight = new ImInt(3);
|
||||||
|
|
||||||
|
public SoloMenu(StateMachine stateMachine) {
|
||||||
|
super(stateMachine);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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")) {
|
||||||
|
this.stateMachine.pushState(new SudokuView(stateMachine, sudokuSize.get(), sudokuSize.get()));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case RECTANGLE:
|
||||||
|
ImGui.inputInt("Largeur", sudokuHeight);
|
||||||
|
ImGui.inputInt("Longueur", sudokuWidth);
|
||||||
|
if (ImGui.button("Résoudre un sudoku")) {
|
||||||
|
this.stateMachine.pushState(new SudokuView(stateMachine, sudokuWidth.get(), sudokuHeight.get()));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
renderReturnButton();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
43
app/src/main/java/gui/menu/StateMachine.java
Normal file
43
app/src/main/java/gui/menu/StateMachine.java
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package gui.menu;
|
||||||
|
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
|
import imgui.ImGui;
|
||||||
|
import imgui.ImVec2;
|
||||||
|
import imgui.flag.ImGuiKey;
|
||||||
|
import imgui.flag.ImGuiWindowFlags;
|
||||||
|
|
||||||
|
public class StateMachine {
|
||||||
|
|
||||||
|
private final Stack<BaseView> menus;
|
||||||
|
|
||||||
|
public StateMachine() {
|
||||||
|
this.menus = new Stack<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void pushState(BaseView menu) {
|
||||||
|
menus.add(menu);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void popState() {
|
||||||
|
menus.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkEscape() {
|
||||||
|
if (ImGui.isKeyPressed(ImGuiKey.Escape) && menus.size() > 1) {
|
||||||
|
popState();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void render() {
|
||||||
|
var displaySize = ImGui.getIO().getDisplaySize();
|
||||||
|
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.getLast().render();
|
||||||
|
ImGui.end();
|
||||||
|
checkEscape();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
87
app/src/main/java/gui/menu/SudokuView.java
Normal file
87
app/src/main/java/gui/menu/SudokuView.java
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
package gui.menu;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import gui.ColorGenerator;
|
||||||
|
import gui.ColorGenerator.Color;
|
||||||
|
import imgui.ImGui;
|
||||||
|
import imgui.ImVec2;
|
||||||
|
import imgui.ImVec4;
|
||||||
|
import imgui.flag.ImGuiCol;
|
||||||
|
import imgui.flag.ImGuiStyleVar;
|
||||||
|
import sudoku.Block;
|
||||||
|
import sudoku.Cell;
|
||||||
|
import sudoku.MultiDoku;
|
||||||
|
import sudoku.Sudoku;
|
||||||
|
import sudoku.SudokuFactory;
|
||||||
|
|
||||||
|
public class SudokuView extends BaseView {
|
||||||
|
|
||||||
|
private final Sudoku sudoku;
|
||||||
|
private int currentIndex = -1;
|
||||||
|
private final Map<Block, Color> colorPalette;
|
||||||
|
|
||||||
|
public SudokuView(StateMachine stateMachine, int width, int height) {
|
||||||
|
super(stateMachine);
|
||||||
|
MultiDoku doku = SudokuFactory.createBasicEmptyRectangleSudoku(width, height);
|
||||||
|
this.sudoku = doku.getSubGrid(0);
|
||||||
|
this.colorPalette = new HashMap<>();
|
||||||
|
initColors();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initColors() {
|
||||||
|
List<Color> colors = ColorGenerator.greatPalette(sudoku.getSize());
|
||||||
|
int index = 0;
|
||||||
|
for (Block block : sudoku.getBlocks()) {
|
||||||
|
colorPalette.put(block, colors.get(index));
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void renderPopup() {
|
||||||
|
if (ImGui.beginPopup("editPopup")) {
|
||||||
|
for (int i = 1; i < sudoku.getSize() + 1; i++) {
|
||||||
|
if (i % (int) (Math.sqrt(sudoku.getSize())) != 1)
|
||||||
|
ImGui.sameLine();
|
||||||
|
if (ImGui.button(Integer.toString(i), new ImVec2(50, 50))) {
|
||||||
|
this.sudoku.setCellSymbol(currentIndex % sudoku.getSize(), currentIndex / sudoku.getSize(), i - 1);
|
||||||
|
ImGui.closeCurrentPopup();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ImGui.endPopup();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void render() {
|
||||||
|
for (int y = 0; y < sudoku.getSize(); y++) {
|
||||||
|
for (int x = 0; x < sudoku.getSize(); x++) {
|
||||||
|
if (x > 0)
|
||||||
|
ImGui.sameLine();
|
||||||
|
int index = y * sudoku.getSize() + x;
|
||||||
|
Cell cell = sudoku.getCell(x, y);
|
||||||
|
int symbol = cell.getSymbolIndex();
|
||||||
|
Color blockColor = colorPalette.get(cell.getBlock());
|
||||||
|
ImGui.pushStyleVar(ImGuiStyleVar.SelectableTextAlign, new ImVec2(0.5f, 0.5f));
|
||||||
|
ImGui.pushStyleColor(ImGuiCol.Header, new ImVec4(blockColor.r, blockColor.g, blockColor.b, 1.0f));
|
||||||
|
String cellText = "";
|
||||||
|
if (symbol != -1)
|
||||||
|
cellText += Integer.toString(symbol + 1);
|
||||||
|
if (ImGui.selectable(cellText + "##" + index, true, 0, new ImVec2(50, 50))) {
|
||||||
|
ImGui.openPopup("editPopup");
|
||||||
|
currentIndex = index;
|
||||||
|
}
|
||||||
|
ImGui.popStyleVar();
|
||||||
|
ImGui.popStyleColor();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
renderPopup();
|
||||||
|
if (ImGui.button("Résoudre")) {
|
||||||
|
// TODO: solve
|
||||||
|
}
|
||||||
|
renderReturnButton();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -3,17 +3,9 @@
|
|||||||
*/
|
*/
|
||||||
package sudoku;
|
package sudoku;
|
||||||
|
|
||||||
import sudoku.constraint.BlockConstraint;
|
|
||||||
import sudoku.constraint.ColumnConstraint;
|
|
||||||
import sudoku.constraint.IConstraint;
|
|
||||||
import sudoku.constraint.LineConstraint;
|
|
||||||
import sudoku.io.SudokuPrinter;
|
|
||||||
import sudoku.io.SudokuSerializer;
|
|
||||||
import sudoku.solver.Solver;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
|
||||||
|
import sudoku.io.SudokuPrinter;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public String getGreeting() {
|
public String getGreeting() {
|
||||||
|
|||||||
@@ -54,16 +54,15 @@ public class Sudoku {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < values.size(); i++) {
|
for (int i = 0; i < values.size(); i++) {
|
||||||
int x = i%this.blocks.size();
|
int x = i % this.blocks.size();
|
||||||
int y = (i-x)/this.blocks.size();
|
int y = (i - x) / this.blocks.size();
|
||||||
int value = values.get(i);
|
int value = values.get(i);
|
||||||
if (!this.setCellSymbol(x, y, value)) {
|
if (!this.setCellSymbol(x, y, value)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Cell getCell(int x, int y) {
|
public Cell getCell(int x, int y) {
|
||||||
int index = y * getSize() + x;
|
int index = y * getSize() + x;
|
||||||
@@ -83,6 +82,12 @@ public class Sudoku {
|
|||||||
return this.blocks.size();
|
return this.blocks.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isValid(List<IConstraint> constraints) {
|
||||||
|
// not implemented
|
||||||
|
// for eachcase check contraintes
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public List<Cell> getCells() {
|
public List<Cell> getCells() {
|
||||||
return this.cells;
|
return this.cells;
|
||||||
}
|
}
|
||||||
@@ -149,6 +154,7 @@ public class Sudoku {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("Sudoku {");
|
sb.append("Sudoku {");
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ public class ColumnConstraint implements IConstraint {
|
|||||||
@Override
|
@Override
|
||||||
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
|
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
|
||||||
for (int i = 0; i < s.getSize(); i++) {
|
for (int i = 0; i < s.getSize(); i++) {
|
||||||
if (s.getCell(x, newSymbolIndex).getSymbolIndex() == newSymbolIndex)
|
if (s.getCell(x, i).getSymbolIndex() == newSymbolIndex)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ public class LineConstraint implements IConstraint {
|
|||||||
@Override
|
@Override
|
||||||
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
|
public boolean canBePlaced(final Sudoku s, int x, int y, int newSymbolIndex) {
|
||||||
for (int i = 0; i < s.getSize(); i++) {
|
for (int i = 0; i < s.getSize(); i++) {
|
||||||
if (s.getCell(newSymbolIndex, y).getSymbolIndex() == newSymbolIndex)
|
if (s.getCell(i, y).getSymbolIndex() == newSymbolIndex)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Reference in New Issue
Block a user