Permalink
Browse files

Small changes

  • Loading branch information...
mariuszgromada committed Apr 9, 2016
1 parent eb9383c commit ca9af229b24a8864b01246e320ab8e624a70fcd3
@@ -48,8 +48,7 @@
package org.mariuszgromada.math.janetsudoku;
import java.util.ArrayList;
import org.mariuszgromada.janetutils.DateTimeX;
import org.mariuszgromada.math.janetsudoku.utils.DateTimeX;
/**
* Sudoku generator.
*
@@ -703,7 +702,7 @@ private void addMessage(String msg, int msgType) {
mt = "(error)";
lastErrorMessage = msg;
}
messages = messages + "\n" + vdt + mt + " " + msg;
messages = messages + SudokuStore.NEW_LINE_SEPARATOR + vdt + mt + " " + msg;
lastMessage = msg;
}
/**
@@ -49,8 +49,8 @@
import java.util.ArrayList;
import java.util.Stack;
import org.mariuszgromada.janetutils.ArrayX;
import org.mariuszgromada.janetutils.DateTimeX;
import org.mariuszgromada.math.janetsudoku.utils.ArrayX;
import org.mariuszgromada.math.janetsudoku.utils.DateTimeX;
/**
* Sudoku board, with predefined Sudoku examples and possibility to load
@@ -1087,7 +1087,7 @@ private void addMessage(String msg, int msgType) {
mt = "(error)";
lastErrorMessage = msg;
}
messages = messages + "\n" + vdt + mt + " " + msg;
messages = messages + SudokuStore.NEW_LINE_SEPARATOR + vdt + mt + " " + msg;
lastMessage = msg;
}
/**
@@ -1292,8 +1292,8 @@ private String boardStateToString() {
boardStateStr = boardStateStr + "ready";
break;
}
boardStateStr = boardStateStr + "\n" + "Initial empty cells: " + emptyCellsNumber;
boardStateStr = boardStateStr + "\n" + "Solving : ";
boardStateStr = boardStateStr + SudokuStore.NEW_LINE_SEPARATOR + "Initial empty cells: " + emptyCellsNumber;
boardStateStr = boardStateStr + SudokuStore.NEW_LINE_SEPARATOR + "Solving : ";
switch(solvingState) {
case SOLVING_STATE_NOT_STARTED:
boardStateStr = boardStateStr + "not started";
@@ -1315,21 +1315,21 @@ private String boardStateToString() {
* @return Board and empty cells representation.
*/
public String boardAndEmptyCellsToString() {
return SudokuStore.boardAndEmptyCellsToString(sudokuBoard, getEmptyCells()) + boardStateToString() + "\n";
return SudokuStore.boardAndEmptyCellsToString(sudokuBoard, getEmptyCells()) + boardStateToString() + SudokuStore.NEW_LINE_SEPARATOR;
}
/**
* Returns string board (only) representation.
* @return Board (only) representation.
*/
public String boardToString() {
return SudokuStore.boardToString(sudokuBoard) + boardStateToString() + "\n";
return SudokuStore.boardToString(sudokuBoard) + boardStateToString() + SudokuStore.NEW_LINE_SEPARATOR;
}
/**
* Returns string empty cells (only) representation.
* @return Empty cells (only) representation.
*/
public String emptyCellsToString() {
return SudokuStore.emptyCellsToString( getEmptyCells() ) + boardStateToString() + "\n";
return SudokuStore.emptyCellsToString( getEmptyCells() ) + boardStateToString() + SudokuStore.NEW_LINE_SEPARATOR;
}
/**
* Return string representation of cells that lead to
@@ -1,5 +1,5 @@
/*
* @(#)SudokuStore.java 1.0.0 2016-03-19
* @(#)SudokuStore.java 1.1.0 2016-04-09
*
* You may use this software under the condition of "Simplified BSD License"
*
@@ -49,9 +49,10 @@
import java.util.ArrayList;
import org.mariuszgromada.janetutils.ArrayX;
import org.mariuszgromada.janetutils.io.FileX;
import org.mariuszgromada.janetutils.DateTimeX;
import org.mariuszgromada.math.janetsudoku.utils.ArrayX;
import org.mariuszgromada.math.janetsudoku.utils.DateTimeX;
import org.mariuszgromada.math.janetsudoku.utils.FileX;
/**
* Storehouse for various things used in library, i.e. sudoku board examples.
@@ -67,7 +68,7 @@
* <a href="http://bitbucket.org/mariuszgromada/mxparser/" target="_blank">mXparser on Bitbucket</a><br>
* <a href="http://mxparser.codeplex.com/" target="_blank">mXparser on CodePlex</a><br>
*
* @version 1.0.0
* @version 1.1.0
*/
public final class SudokuStore {
/**
@@ -114,6 +115,10 @@
* @see SudokuStore#seqOfRandomBoardTransf(int[][], int);
*/
public static final int DEFAULT_RND_TRANSF_SEQ_LENGTH = 1000;
/**
* System specific new line separator
*/
public static final String NEW_LINE_SEPARATOR = System.getProperty("line.separator");
/**
* Threads number.
*/
@@ -138,6 +143,15 @@
public static final int[][] getPuzzleExample(int exampleNumber) {
return SudokuPuzzles.getPuzzleExample(exampleNumber);
}
/**
* Gets Sudoku example for the Sudoku Store.
* @return Sudoku example is exists, otherwise null.
* @see SudokuPuzzles#NUMBER_OF_PUZZLE_EXAMPLES
* @see SudokuPuzzles#getPuzzleExample(int)
*/
public static final int[][] getPuzzleExample() {
return SudokuPuzzles.getPuzzleExample( SudokuStore.randomIndex(SudokuPuzzles.NUMBER_OF_PUZZLE_EXAMPLES) );
}
/**
* Returns pre-calculated puzzle example difficulty rating based on
* the average number of steps-back performed while recursive
@@ -1342,12 +1356,12 @@ private static final String convBoardToString(int[][] sudokuBoard, String headCo
String boardStr = "";
if (headComment != null)
if (headComment.length() > 0)
boardStr = boardStr + "# " + headComment + "\n\n";
boardStr = boardStr + "# " + headComment + NEW_LINE_SEPARATOR + NEW_LINE_SEPARATOR;
if (sudokuBoard == null) return "NULL sudoku board.";
boardStr = boardStr + "+-------+-------+-------+\n";
boardStr = boardStr + "+-------+-------+-------+" + NEW_LINE_SEPARATOR;
for (int i = 0; i < SudokuBoard.BOARD_SIZE; i ++) {
if ((i > 0) && (i < SudokuBoard.BOARD_SIZE) && (i % SudokuBoard.BOARD_SUB_SQURE_SIZE == 0))
boardStr = boardStr + "+-------+-------+-------+\n" ;
boardStr = boardStr + "+-------+-------+-------+" + NEW_LINE_SEPARATOR ;
boardStr = boardStr + "| ";
for (int j = 0; j < SudokuBoard.BOARD_SIZE; j++) {
if ((j > 0) && (j < SudokuBoard.BOARD_SIZE) && (j % SudokuBoard.BOARD_SUB_SQURE_SIZE == 0))
@@ -1357,9 +1371,9 @@ private static final String convBoardToString(int[][] sudokuBoard, String headCo
else
boardStr = boardStr + ". ";
}
boardStr = boardStr + "|\n";
boardStr = boardStr + "|" + NEW_LINE_SEPARATOR;
}
boardStr = boardStr + "+-------+-------+-------+\n\n";
boardStr = boardStr + "+-------+-------+-------+" + NEW_LINE_SEPARATOR + NEW_LINE_SEPARATOR;
if (tailComment != null)
if (tailComment.length() > 0)
boardStr = boardStr + "# " + tailComment;
@@ -1398,11 +1412,11 @@ public static final String boardToString(int[][] sudokuBoard, String headComment
* @return Empty cells (only) string representation.
*/
public static final String emptyCellsToString(int[][] emptyCells) {
String boardStr = "Number of free digits\n";
boardStr = boardStr + "=====================\n";
String boardStr = "Number of free digits" + NEW_LINE_SEPARATOR;
boardStr = boardStr + "=====================" + NEW_LINE_SEPARATOR;
for (int i = 0; i < SudokuBoard.BOARD_SIZE; i ++) {
if ((i > 0) && (i < SudokuBoard.BOARD_SIZE) && (i % SudokuBoard.BOARD_SUB_SQURE_SIZE == 0))
boardStr = boardStr + "---------------------\n" ;
boardStr = boardStr + "---------------------" + NEW_LINE_SEPARATOR ;
for (int j = 0; j < SudokuBoard.BOARD_SIZE; j++) {
if ((j > 0) && (j < SudokuBoard.BOARD_SIZE) && (j % SudokuBoard.BOARD_SUB_SQURE_SIZE == 0))
boardStr = boardStr + "| ";
@@ -1411,9 +1425,9 @@ public static final String emptyCellsToString(int[][] emptyCells) {
else
boardStr = boardStr + ". ";
}
boardStr = boardStr + "\n";
boardStr = boardStr + NEW_LINE_SEPARATOR;
}
boardStr = boardStr + "=====================\n";
boardStr = boardStr + "=====================" + NEW_LINE_SEPARATOR;
return boardStr;
}
/**
@@ -1423,11 +1437,11 @@ public static final String emptyCellsToString(int[][] emptyCells) {
* @return Board and empty cells representation.
*/
public static final String boardAndEmptyCellsToString(int[][] sudokuBoard, int[][] emptyCells) {
String boardStr = " Sudoku board Number of free digits\n";
boardStr = boardStr + "===================== =====================\n";
String boardStr = " Sudoku board Number of free digits" + NEW_LINE_SEPARATOR;
boardStr = boardStr + "===================== =====================" + NEW_LINE_SEPARATOR;
for (int i = 0; i < SudokuBoard.BOARD_SIZE; i ++) {
if ((i > 0) && (i < SudokuBoard.BOARD_SIZE) && (i % SudokuBoard.BOARD_SUB_SQURE_SIZE == 0))
boardStr = boardStr + "--------------------- ---------------------\n" ;
boardStr = boardStr + "--------------------- ---------------------" + NEW_LINE_SEPARATOR ;
for (int j = 0; j < SudokuBoard.BOARD_SIZE; j++) {
if ((j > 0) && (j < SudokuBoard.BOARD_SIZE) && (j % SudokuBoard.BOARD_SUB_SQURE_SIZE == 0))
boardStr = boardStr + "| ";
@@ -1445,9 +1459,9 @@ public static final String boardAndEmptyCellsToString(int[][] sudokuBoard, int[]
else
boardStr = boardStr + ". ";
}
boardStr = boardStr + "\n";
boardStr = boardStr + NEW_LINE_SEPARATOR;
}
boardStr = boardStr + "===================== =====================\n";
boardStr = boardStr + "===================== =====================" + NEW_LINE_SEPARATOR;
return boardStr;
}
/**
@@ -1457,18 +1471,19 @@ public static final String boardAndEmptyCellsToString(int[][] sudokuBoard, int[]
*/
public static final String solutionPathToString(BoardCell[] solutionBoardCells) {
String solutionPath = "";
solutionPath = solutionPath + " --------------- \n";
solutionPath = solutionPath + "| id | i, j | d |\n";
solutionPath = solutionPath + "|----|----- |---|\n";
solutionPath = solutionPath + " --------------- " + NEW_LINE_SEPARATOR;
solutionPath = solutionPath + "| id | i, j | d |" + NEW_LINE_SEPARATOR;
solutionPath = solutionPath + "|----|----- |---|" + NEW_LINE_SEPARATOR;
if (solutionBoardCells != null)
for (int i = 0; i < solutionBoardCells.length; i++) {
BoardCell b = solutionBoardCells[i];
if (i + 1 < 10) solutionPath = solutionPath + "| ";
else solutionPath = solutionPath + "| ";
solutionPath = solutionPath + (i+1) + " | " + (b.rowIndex+1) + ", " + (b.colIndex + 1) + " | " + b.digit + " |\n";
solutionPath = solutionPath + (i+1) + " | " + (b.rowIndex+1) + ", " + (b.colIndex + 1) + " | " + b.digit + " |" + NEW_LINE_SEPARATOR;
}
solutionPath = solutionPath + " --------------- \n";
solutionPath = solutionPath + " --------------- " + NEW_LINE_SEPARATOR;
return solutionPath;
}
/**
* Prints Sudoku board to the console.
@@ -47,8 +47,9 @@
*/
package org.mariuszgromada.math.janetsudoku.regtests;
import org.mariuszgromada.janetutils.DateTimeX;
import org.mariuszgromada.math.janetsudoku.SudokuStore;
import org.mariuszgromada.math.janetsudoku.utils.DateTimeX;
import org.mariuszgromada.math.janetsudoku.*;
/**
* Starts all regression tests.
@@ -1,5 +1,5 @@
/*
* @(#)RegTestsApi.java 1.0.0 2016-03-19
* @(#)RegTestsApi.java 1.1.0 2016-04-09
*
* You may use this software under the condition of "Simplified BSD License"
*
@@ -48,13 +48,14 @@
package org.mariuszgromada.math.janetsudoku.regtests;
import java.util.ArrayList;
import org.mariuszgromada.janetutils.DateTimeX;
import org.mariuszgromada.math.janetsudoku.BoardCell;
import org.mariuszgromada.math.janetsudoku.SudokuBoard;
import org.mariuszgromada.math.janetsudoku.SudokuGenerator;
import org.mariuszgromada.math.janetsudoku.SudokuPuzzles;
import org.mariuszgromada.math.janetsudoku.SudokuSolver;
import org.mariuszgromada.math.janetsudoku.SudokuStore;
import org.mariuszgromada.math.janetsudoku.utils.DateTimeX;
import org.mariuszgromada.math.janetsudoku.utils.FileX;
/**
* Regression tests for public API provided by Janet-Sudoku.
*
@@ -69,7 +70,7 @@
* <a href="http://bitbucket.org/mariuszgromada/mxparser/" target="_blank">mXparser on Bitbucket</a><br>
* <a href="http://mxparser.codeplex.com/" target="_blank">mXparser on CodePlex</a><br>
*
* @version 1.0.0
* @version 1.1.0
*
* @see SudokuSolver
*/
@@ -445,6 +446,43 @@ static boolean runTest(int testId, int threadId) {
}
}
break;
case 6:
testDesc = "SudokuGenerator -> generate -> save -> load -> compare";
{
String filePath = FileX.getTmpDir() + FileX.genRndFileName(20, "txt");
SudokuGenerator g = new SudokuGenerator(SudokuGenerator.PARAM_GEN_RND_BOARD);
int[][] generated = g.generate();
g.saveBoard(filePath, "generated", "saved");
int[][] loaded = SudokuStore.loadBoard(filePath);
FileX.removeFile(filePath);
if (SudokuStore.boardsAreEqual(generated, loaded) == false)
testResult = false;
if ( testResult == true ) {
resultDesc = "Expecting equal - are equal.";
} else {
resultDesc = "Expecting equal - are not equal.";
}
}
break;
case 7:
testDesc = "SudokuSolver -> solve -> save -> load -> compare";
{
String filePath = FileX.getTmpDir() + FileX.genRndFileName(20, "txt");
SudokuSolver s = new SudokuSolver(SudokuStore.getPuzzleExample());
s.solve();
int[][] solved = s.getSolvedBoard();
s.saveSolvedBoard(filePath, "solved", "saved");
int[][] loaded = SudokuStore.loadBoard(filePath);
FileX.removeFile(filePath);
if (SudokuStore.boardsAreEqual(solved, loaded) == false)
testResult = false;
if ( testResult == true ) {
resultDesc = "Expecting equal - are equal.";
} else {
resultDesc = "Expecting equal - are not equal.";
}
}
break;
}
if (testResult == true)
SudokuStore.consolePrintln("(Thread: " + threadId + ") " + "Test: " + testId + " " + testDesc + " " + resultDesc + " >>> ApiTests, result: OK");
@@ -455,5 +493,5 @@ static boolean runTest(int testId, int threadId) {
/**
* Number of regression tests;
*/
static final int NUMBER_OF_TESTS = 6;
static final int NUMBER_OF_TESTS = 8;
}
@@ -47,12 +47,13 @@
*/
package org.mariuszgromada.math.janetsudoku.regtests;
import org.mariuszgromada.janetutils.DateTimeX;
import org.mariuszgromada.math.janetsudoku.ErrorCodes;
import org.mariuszgromada.math.janetsudoku.SudokuPuzzles;
import org.mariuszgromada.math.janetsudoku.SudokuSolver;
import org.mariuszgromada.math.janetsudoku.SudokuGenerator;
import org.mariuszgromada.math.janetsudoku.SudokuStore;
import org.mariuszgromada.math.janetsudoku.utils.DateTimeX;
/**
* Regression tests for the SudokuGenerator class.
@@ -47,13 +47,12 @@
*/
package org.mariuszgromada.math.janetsudoku.regtests;
import org.mariuszgromada.janetutils.DateTimeX;
import org.mariuszgromada.math.janetsudoku.ErrorCodes;
import org.mariuszgromada.math.janetsudoku.SudokuBoard;
import org.mariuszgromada.math.janetsudoku.SudokuPuzzles;
import org.mariuszgromada.math.janetsudoku.SudokuSolver;
import org.mariuszgromada.math.janetsudoku.SudokuStore;
import org.mariuszgromada.math.janetsudoku.utils.DateTimeX;
/**
* Regression tests for the SudokuSolver class.
*
@@ -46,10 +46,10 @@
* "Yes, up to isomorphism."
*/
package org.mariuszgromada.math.janetsudoku.regtests;
import org.mariuszgromada.janetutils.DateTimeX;
import org.mariuszgromada.math.janetsudoku.SudokuPuzzles;
import org.mariuszgromada.math.janetsudoku.SudokuStore;
import org.mariuszgromada.math.janetsudoku.utils.DateTimeX;
/**
* Regression tests for the SudokuStore class.
*
@@ -51,6 +51,7 @@
import org.mariuszgromada.math.janetsudoku.SudokuPuzzles;
import org.mariuszgromada.math.janetsudoku.SudokuSolver;
import org.mariuszgromada.math.janetsudoku.SudokuStore;
import org.mariuszgromada.math.janetsudoku.utils.FileX;
/**
* Basic Janet-Sudoku Tutorial.
*
@@ -78,7 +79,7 @@
* @param args No arguments are considered.
*/
public static void main(String[] args) {
String tmpDir = System.getProperty("java.io.tmpdir");
String tmpDir = FileX.getTmpDir();
{
/*
* Simple sudoku generation.
Oops, something went wrong.

0 comments on commit ca9af22

Please sign in to comment.