This commit is contained in:
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,46 +1,91 @@
|
||||
package gui;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Random;
|
||||
|
||||
import gui.ColorGenerator.Color;
|
||||
import imgui.ImGui;
|
||||
import imgui.ImVec2;
|
||||
import imgui.ImVec4;
|
||||
import imgui.app.Application;
|
||||
import imgui.app.Configuration;
|
||||
import imgui.flag.ImGuiCol;
|
||||
import sudoku.Block;
|
||||
import sudoku.Cell;
|
||||
import sudoku.MultiDoku;
|
||||
import sudoku.Sudoku;
|
||||
import sudoku.SudokuFactory;
|
||||
|
||||
public class Main extends Application {
|
||||
|
||||
// temp thing
|
||||
private static int[] values = new int[9 * 9];
|
||||
private Sudoku sudoku;
|
||||
private int currentIndex = -1;
|
||||
private Map<Block, Color> colorPalette = new HashMap<>();
|
||||
private static final int SUDOKU_SIZE = 5;
|
||||
|
||||
@Override
|
||||
protected void configure(Configuration config) {
|
||||
config.setTitle("Let's play sudoku!");
|
||||
}
|
||||
|
||||
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++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initImGui(Configuration config) {
|
||||
super.initImGui(config);
|
||||
ImGui.getIO().getFonts().addFontFromFileTTF("comic.ttf", 50.0f);
|
||||
Random r = new Random();
|
||||
for (int i = 0; i < 9 * 9; i++) {
|
||||
values[i] = r.nextInt(9) + 1;
|
||||
MultiDoku doku = SudokuFactory.createBasicEmptySquareSudoku(SUDOKU_SIZE);
|
||||
sudoku = doku.getSubGrid(0);
|
||||
initColors();
|
||||
}
|
||||
|
||||
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 process() {
|
||||
ImGui.begin("Sudoku Window");
|
||||
for (int y = 0; y < 9; y++) {
|
||||
for (int x = 0; x < 9; x++) {
|
||||
for (int y = 0; y < sudoku.getSize(); y++) {
|
||||
for (int x = 0; x < sudoku.getSize(); x++) {
|
||||
if (x > 0)
|
||||
ImGui.sameLine();
|
||||
ImGui.pushID(y * 9 + x);
|
||||
ImGui.selectable(Integer.toString(values[y * 9 + x]), false, 0, new ImVec2(50, 50));
|
||||
ImGui.popID();
|
||||
int index = y * sudoku.getSize() + x;
|
||||
Cell cell = sudoku.getCell(x, y);
|
||||
int symbol = cell.getSymbolIndex();
|
||||
Color blockColor = colorPalette.get(cell.getBlock());
|
||||
ImGui.pushStyleColor(ImGuiCol.Header, new ImVec4(blockColor.r, blockColor.g, blockColor.b, 1.0f));
|
||||
if (ImGui.selectable(Integer.toString(symbol + 1) + "##" + index, true, 0, new ImVec2(50, 50))) {
|
||||
ImGui.openPopup("editPopup");
|
||||
currentIndex = index;
|
||||
}
|
||||
ImGui.popStyleColor();
|
||||
}
|
||||
}
|
||||
renderPopup();
|
||||
ImGui.end();
|
||||
// ImGui.showDemoWindow();
|
||||
ImGui.showDemoWindow();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -3,17 +3,9 @@
|
||||
*/
|
||||
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.List;
|
||||
|
||||
import sudoku.io.SudokuPrinter;
|
||||
|
||||
public class Main {
|
||||
public String getGreeting() {
|
||||
|
||||
@@ -54,16 +54,15 @@ public class Sudoku {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < values.size(); i++) {
|
||||
int x = i%this.blocks.size();
|
||||
int y = (i-x)/this.blocks.size();
|
||||
int x = i % this.blocks.size();
|
||||
int y = (i - x) / this.blocks.size();
|
||||
int value = values.get(i);
|
||||
if (!this.setCellSymbol(x, y, value)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Cell getCell(int x, int y) {
|
||||
int index = y * getSize() + x;
|
||||
@@ -83,6 +82,12 @@ public class Sudoku {
|
||||
return this.blocks.size();
|
||||
}
|
||||
|
||||
public boolean isValid(List<IConstraint> constraints) {
|
||||
// not implemented
|
||||
// for eachcase check contraintes
|
||||
return false;
|
||||
}
|
||||
|
||||
public List<Cell> getCells() {
|
||||
return this.cells;
|
||||
}
|
||||
@@ -149,6 +154,7 @@ public class Sudoku {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Sudoku {");
|
||||
|
||||
Reference in New Issue
Block a user