Skip to content

Commit

Permalink
code changes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
1to9only committed Dec 22, 2023
1 parent fd2379c commit f13f82b
Show file tree
Hide file tree
Showing 8 changed files with 483 additions and 9 deletions.
70 changes: 70 additions & 0 deletions diuf/sudoku/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public class Settings {
private int iGridSize = 9;
private int iSaveFormat = 0;

private String s0 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private String s1 = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private String sA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

private EnumSet<SolvingTechnique> techniques;

private boolean isVertical = false; // generate dialog
Expand Down Expand Up @@ -80,6 +84,7 @@ public class Settings {
private boolean isCustom = false;
private String custom = null; // custom variant regions
private int count = 9; // custom regions
private int customconnect = 0; // custom connections

private boolean isNumbers = true; // is Numbers (else Alphas)

Expand Down Expand Up @@ -199,6 +204,16 @@ public int getSaveFormat() {
return iSaveFormat;
}

public String gets0() {
return s0;
}
public String gets1() {
return s1;
}
public String getsA() {
return sA;
}

public EnumSet<SolvingTechnique> getTechniques() {
return EnumSet.copyOf(this.techniques);
}
Expand Down Expand Up @@ -531,12 +546,33 @@ public boolean isCustom() {
return isCustom;
}

public void isCustomConnected() {
this.customconnect = 0;
for (int y=0; y<8; y++ ) {
for (int x=0; x<8; x++ ) {
int index = y*9+x;
char ch = this.custom.charAt(index);
if ( ch != '.' && ch != '0' ) {
char cri = this.custom.charAt(index+1);
if ( cri != '.' && cri != '0' && cri != ch ) {
this.customconnect = 1;
}
char cbe = this.custom.charAt(index+9);
if ( cbe != '.' && cbe != '0' && cbe != ch ) {
this.customconnect = 1;
}
}
}
}
}

public void setCustom(String custom) {
if ( this.custom == null || !(this.custom.equals(custom)) ) {
this.custom = custom.replace( "0", ".");
if ( this.isCustom != true ) {
this.isCustom = true;
}
isCustomConnected();
save();
}
}
Expand All @@ -556,6 +592,10 @@ public int getCount() {
return count;
}

public int getCustomConnect() {
return customconnect;
}

public void setNumbers(boolean isNumbers) {
if ( this.isNumbers != isNumbers ) {
this.isNumbers = isNumbers;
Expand Down Expand Up @@ -638,6 +678,31 @@ public void load() {
}
catch (NullPointerException e) { LoadError = 1; }

try {
s0 = (String)stgDetails.get("s0");
if ( s0 == null ) {
s0 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
LoadError = 1;
}
}
catch (NullPointerException e) { LoadError = 1; }
try {
s1 = (String)stgDetails.get("s1");
if ( s1 == null ) {
s1 = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
LoadError = 1;
}
}
catch (NullPointerException e) { LoadError = 1; }
try {
sA = (String)stgDetails.get("sA");
if ( sA == null ) {
sA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
LoadError = 1;
}
}
catch (NullPointerException e) { LoadError = 1; }

//generate dialog

try {
Expand Down Expand Up @@ -775,6 +840,7 @@ public void load() {
catch (NullPointerException e) { LoadError = 1; }
try {
custom = (String)stgDetails.get("custom");
isCustomConnected();
}
catch (NullPointerException e) { ; }
try {
Expand Down Expand Up @@ -852,6 +918,10 @@ public void save() {
stgDetails.put("iGridSize", ""+iGridSize);
stgDetails.put("iSaveFormat", ""+iSaveFormat);

stgDetails.put("s0", s0); // 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
stgDetails.put("s1", s1); // 123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
stgDetails.put("sA", sA); // ABCDEFGHIJKLMNOPQRSTUVWXYZ

// generate dialog

stgDetails.put("isVertical", isVertical?"true":"false");
Expand Down
6 changes: 6 additions & 0 deletions diuf/sudoku/gui/SolutionPath.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<body>
<h2>Solution Path</h2>
{0}
</body>
</html>
4 changes: 2 additions & 2 deletions diuf/sudoku/gui/SudokuFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ public String getDescription() {
private JMenuItem getMitLoad() {
if (mitLoad == null) {
mitLoad = new JMenuItem();
mitLoad.setText("Load...");
mitLoad.setText("Load grid...");
mitLoad.setMnemonic(java.awt.event.KeyEvent.VK_O);
mitLoad.setToolTipText("Open the file selector to load the grid from a file");
mitLoad.addActionListener(new java.awt.event.ActionListener() {
Expand Down Expand Up @@ -1038,7 +1038,7 @@ public void actionPerformed(java.awt.event.ActionEvent e) {
private JMenuItem getMitSave() {
if (mitSave == null) {
mitSave = new JMenuItem();
mitSave.setText("Save...");
mitSave.setText("Save grid...");
mitSave.setMnemonic(java.awt.event.KeyEvent.VK_S);
mitSave.setToolTipText("Open the file selector to save the grid to a file");
mitSave.addActionListener(new java.awt.event.ActionListener() {
Expand Down
12 changes: 11 additions & 1 deletion diuf/sudoku/gui/SudokuPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public class SudokuPanel extends JPanel {
private Color[] Pastel_Colors = { Pastel01, Pastel02, Pastel03, Pastel04, Pastel05, Pastel06, Pastel07, Pastel08, Pastel09, Pastel10, Pastel11, Pastel12, Pastel13, Pastel14, Pastel15, Pastel16, Pastel17, Pastel18, Pastel19, Pastel20, Pastel21, Pastel22, Pastel23, Pastel24, Pastel25, Pastel26, Pastel27, Pastel28, Pastel29, Pastel30, Pastel31, Pastel32, Pastel33};
private int[] Pastel_Index = { 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};

private static String DOTA = ".ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private String DOTA = "."+Settings.getInstance().getsA();

public SudokuPanel(SudokuFrame parent) {
super();
Expand Down Expand Up @@ -566,8 +566,13 @@ private void initFillColor(Graphics g, Cell cell) {
}
}
if (grid.isCustom() && grid.getCustomAt(cell.getX(),cell.getY())!=null) {
if ( Settings.getInstance().getCustomConnect() == 1 ) {
Grid.Custom custom = grid.getCustomAt(cell.getX(),cell.getY());
col = Pastel_Colors[ Pastel_Index[ custom.getCustomNum()+1]];
}
if ( Settings.getInstance().getCustomConnect() == 0 ) {
col = Pastel_Colors[ Pastel_Index[ 1]];
}
}
if (redCells != null && redCells.contains(cell))
col = Color.red;
Expand Down Expand Up @@ -615,8 +620,13 @@ private void init2FillColor(Graphics g, Cell cell) {
}
}
if (grid.isCustom() && grid.getCustomAt(cell.getX(),cell.getY())!=null) {
if ( Settings.getInstance().getCustomConnect() == 1 ) {
Grid.Custom custom = grid.getCustomAt(cell.getX(),cell.getY());
col = Pastel_Colors[ Pastel_Index[ custom.getCustomNum()+1]];
}
if ( Settings.getInstance().getCustomConnect() == 0 ) {
col = Pastel_Colors[ Pastel_Index[ 1]];
}
}
if (redCells != null && redCells.contains(cell))
col = Color.red;
Expand Down
17 changes: 12 additions & 5 deletions diuf/sudoku/io/SudokuIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ public class SudokuIO {
private static final String ERROR_MSG = "Unreadable Sudoku format";
private static final String WARNING_MSG = "Warning: the Sudoku format was not recognized.\nThe Sudoku may not have been read correctly";

private static String ZERZ = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static String DOT0 = ".0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static String DOT1 = ".123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static String DOTA = ".ABCDEFGHIJKLMNOPQRSTUVWXYZ";

private static int loadFromReader(Grid grid, Reader reader) throws IOException {
List<String> lines = new ArrayList<String>();
LineNumberReader lineReader = new LineNumberReader(reader);
Expand Down Expand Up @@ -286,6 +281,10 @@ private static String getSuffix(Grid grid) {
}

private static void saveToWriter81(Grid grid, Writer writer) throws IOException {
String ZERZ = Settings.getInstance().gets0();
String DOT0 = "."+Settings.getInstance().gets0();
String DOT1 = "."+Settings.getInstance().gets1();
String DOTA = "."+Settings.getInstance().getsA();
int pformat = Settings.getInstance().getPuzzleFormat();
int gSize = Settings.getInstance().getGridSize();
String text = ZERZ;
Expand Down Expand Up @@ -328,6 +327,10 @@ private static void saveToWriter81(Grid grid, Writer writer) throws IOException
}

private static void saveToWriter(Grid grid, Writer writer) throws IOException {
String ZERZ = Settings.getInstance().gets0();
String DOT0 = "."+Settings.getInstance().gets0();
String DOT1 = "."+Settings.getInstance().gets1();
String DOTA = "."+Settings.getInstance().getsA();
int pformat = Settings.getInstance().getPuzzleFormat();
int gSize = Settings.getInstance().getGridSize();
int sformat = Settings.getInstance().getSaveFormat();
Expand Down Expand Up @@ -629,6 +632,10 @@ private static void saveToWriter(Grid grid, Writer writer) throws IOException {
}

private static void savePencilMarksToWriter(Grid grid, Writer writer) throws IOException {
String ZERZ = Settings.getInstance().gets0();
String DOT0 = "."+Settings.getInstance().gets0();
String DOT1 = "."+Settings.getInstance().gets1();
String DOTA = "."+Settings.getInstance().getsA();
int pformat = Settings.getInstance().getPuzzleFormat();
int gSize = Settings.getInstance().getGridSize();
int sformat = Settings.getInstance().getSaveFormat();
Expand Down
52 changes: 52 additions & 0 deletions diuf/sudoku/solver/SmallestHintAccumulator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Project: Sudoku Explainer
* Copyright (C) 2023 1to9only
* Available under the terms of the Lesser General Public License (LGPL)
*/
package diuf.sudoku.solver;

import java.util.ArrayList;
import java.util.List;

/**
* Hints accumulator, that accumulates hints of the same rating.
*/
public class SmallestHintAccumulator implements HintsAccumulator {

private List<Hint> result = new ArrayList<Hint>();

private double lastDifficulty = 0.0;

public SmallestHintAccumulator() {
super();
}

public void add( Hint hint) throws InterruptedException {
double newDifficulty = ((Rule)hint).getDifficulty();
if ( lastDifficulty == 0.0 ) {
lastDifficulty = newDifficulty;
}
if ( lastDifficulty != newDifficulty ) {
throw new InterruptedException();
}
if ( !result.contains( hint) ) {
result.add( hint);
}
}

public List<Hint> getHints() {
return result;
}

public boolean hasHints()
{
return !result.isEmpty();
}

public void clear()
{
result.clear();
lastDifficulty = 0.0;
}

}
66 changes: 65 additions & 1 deletion diuf/sudoku/solver/Solver.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class Solver {

// private boolean isUsingAdvanced = false;

private static String ATA = "@ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private String ATA = "@"+Settings.getInstance().getsA();

private class DefaultHintsAccumulator implements HintsAccumulator {

Expand Down Expand Up @@ -510,6 +510,70 @@ public double analyseDifficulty(double min, double max) {
}
}

public void getBatchDifficulty() {
Grid backup = new Grid();
grid.copyTo(backup);
try {
difficulty = 0.0;
pearl = 0.0;
diamond = 0.0;
SmallestHintAccumulator accu = new SmallestHintAccumulator();
while (!isSolved()) {
/// SingleHintAccumulator accu = new SingleHintAccumulator();
try {
for (HintProducer producer : directHintProducers)
producer.getHints(grid, accu);
if ( accu.hasHints() ) throw new InterruptedException();
for (IndirectHintProducer producer : indirectHintProducers)
producer.getHints(grid, accu);
if ( accu.hasHints() ) throw new InterruptedException();
for (IndirectHintProducer producer : chainingHintProducers)
producer.getHints(grid, accu);
if ( accu.hasHints() ) throw new InterruptedException();
for (IndirectHintProducer producer : chainingHintProducers2)
producer.getHints(grid, accu);
if ( accu.hasHints() ) throw new InterruptedException();
for (IndirectHintProducer producer : advancedHintProducers)
producer.getHints(grid, accu);
if ( accu.hasHints() ) throw new InterruptedException();
for (IndirectHintProducer producer : experimentalHintProducers)
producer.getHints(grid, accu);
} catch (InterruptedException willHappen) {}
List<Hint> hints = accu.getHints();
if ( hints.isEmpty() ) {
difficulty = 20.0;
break;
}
//a assert hint instanceof Rule;
for ( Hint hint : hints ) {
Rule rule = (Rule)hint;
double ruleDiff = rule.getDifficulty();
if (ruleDiff > difficulty)
difficulty = ruleDiff;
hint.apply(grid);
if (pearl == 0.0) {
if (diamond == 0.0)
diamond = difficulty;
if (hint.getCell() != null) {
if (want == 'd' && difficulty > diamond) {
difficulty = 20.0;
break;
}
pearl = difficulty;
}
}
else if (want != 0 && difficulty > pearl) {
difficulty = 20.0;
break;
}
}
accu.clear();
}
} finally {
backup.copyTo(grid);
}
}

public void getDifficulty() {
Grid backup = new Grid();
grid.copyTo(backup);
Expand Down
Loading

0 comments on commit f13f82b

Please sign in to comment.