Permalink
Browse files

Working on demo app :-)

  • Loading branch information...
mariuszgromada committed Apr 13, 2016
1 parent 9c4118b commit ff164ade86e9dc42a6f1d7ded1e8549a6a84580e
Binary file not shown.
@@ -364,6 +364,74 @@ public void run() {
}
return sudokuBoard;
}
/**
* Load Sudoku board from one string line ('0' and' '.' treated as empty cell).
* Any other char than '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.'
* is being filtered out.
*
* @param boardDefinition Board definition in one string line containing
* row after row.
* @return Loaded board if data is sufficient to fill 81 cells
* (including empty cells), otherwise null.
*/
public static final int[][] loadBoardFromStringLine(String boardDefinition) {
final int lastInex = BOARD_SIZE - 1;
if (boardDefinition == null) return null;
if (boardDefinition.length() < SudokuBoard.BOARD_CELLS_NUMBER) return null;
int [][] board = boardCopy(SudokuPuzzles.PUZZLE_EMPTY);
int cellNum = 0;
int i = 0;
int j = -1;
char[] line = boardDefinition.toCharArray();
for (int k = 0; k < line.length; k++) {
char c = line[k];
int d = -1;
if (c == '1') d = 1;
else if (c == '2') d = 2;
else if (c == '3') d = 3;
else if (c == '4') d = 4;
else if (c == '5') d = 5;
else if (c == '6') d = 6;
else if (c == '7') d = 7;
else if (c == '8') d = 8;
else if (c == '9') d = 9;
else if (c == '0') d = 0;
else if (c == '.') d = 0;
if ( (d >= 0) && (cellNum < SudokuBoard.BOARD_CELLS_NUMBER) ) {
j++;
cellNum++;
board[i][j] = d;
if ( j == lastInex) {
i++;
j = -1;
}
}
}
if (cellNum == SudokuBoard.BOARD_CELLS_NUMBER) return board;
else return null;
}
/**
* Loads Sudoku board from variadic list of strings (each string as a
* one row)
*
* Format:
* Any character different than '1-9' and '.' is being removed.
* Any line starting with '#' is being removed.
* Any empty line is being removed.
* Any final line having less than 9 characters is being removed.
*
* If number of final lines is less then 9 then null is returned.
*
* Finally 9 starting characters for first 9 lines is the
* loaded board definition.
*
* @param boardDefinition Board definition (variadic list of strings).
* @return Array representing loaded Sudoku board,
* null - if problem occurred while loading.
*/
public static final int[][] loadBoardFromStrings(String... boardDefinition) {
return loadBoard(boardDefinition);
}
/**
* Saves board to the text file.
*
@@ -1356,6 +1424,8 @@ public static final boolean boardsAreEqual(int[][] board1, int[][] board2) {
*/
private static final String convBoardToString(int[][] sudokuBoard, String headComment, String tailComment) {
String boardStr = "";
String oneLineDefDot = "";
String oneLineDefZero = "";
if (headComment != null)
if (headComment.length() > 0)
boardStr = boardStr + "# " + headComment + NEW_LINE_SEPARATOR + NEW_LINE_SEPARATOR;
@@ -1368,17 +1438,25 @@ private static final String convBoardToString(int[][] sudokuBoard, String headCo
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 + "| ";
if (sudokuBoard[i][j] != BoardCell.EMPTY)
if (sudokuBoard[i][j] != BoardCell.EMPTY) {
boardStr = boardStr + sudokuBoard[i][j] + " ";
else
oneLineDefDot = oneLineDefDot + sudokuBoard[i][j];
oneLineDefZero = oneLineDefZero + sudokuBoard[i][j];
} else {
boardStr = boardStr + ". ";
oneLineDefDot = oneLineDefDot + '.';
oneLineDefZero = oneLineDefZero + '0';
}
}
boardStr = boardStr + "|" + NEW_LINE_SEPARATOR;
}
boardStr = boardStr + "+-------+-------+-------+" + NEW_LINE_SEPARATOR + NEW_LINE_SEPARATOR;
boardStr = boardStr + "One line definitions:" + NEW_LINE_SEPARATOR;
boardStr = boardStr + oneLineDefDot + NEW_LINE_SEPARATOR;
boardStr = boardStr + oneLineDefZero + NEW_LINE_SEPARATOR + NEW_LINE_SEPARATOR;
if (tailComment != null)
if (tailComment.length() > 0)
boardStr = boardStr + "# " + tailComment;
boardStr = NEW_LINE_SEPARATOR + NEW_LINE_SEPARATOR + boardStr + "# " + tailComment;
return boardStr;
}
/**
@@ -34,6 +34,9 @@ static int consoleReadInt() {
}
return consoleInt;
}
static String consoleReadLine() {
return console.nextLine();
}
private void loopMenuMain() {
Menu menu = new Menu(MenuData.MAIN_TITLE, MenuData.MAIN_CONTENT, this);
int selItem;
@@ -42,7 +45,7 @@ private void loopMenuMain() {
switch(selItem) {
case MenuData.LOAD: loopMenuLoad(); break;
case MenuData.GENERATE: loopMenuGenerate(); break;
case MenuData.INPUT: inputPuzzleFromKeyboard(); break;
case MenuData.INPUT: loopMenuInput(); break;
case MenuData.MODIFY: loopMenuModify(); break;
case MenuData.EVALUATE: loopMenuEvaluate(); break;
case MenuData.SOLVE: loopMenuSolve(); break;
@@ -60,7 +63,7 @@ private void loopMenuLoad() {
do {
selItem = menu.getItem();
switch(selItem) {
case MenuData.LOAD_FROM_FILE: ; break;
case MenuData.LOAD_FROM_FILE: loadFromFile(); break;
case MenuData.LOAD_EXAMPLE: loadFromExample(); break;
case MenuData.LOAD_EMPTY_PUZZLE: puzzle = SudokuStore.boardCopy(SudokuPuzzles.PUZZLE_EMPTY); break;
default: incorrectSelection();
@@ -73,13 +76,27 @@ private void loopMenuGenerate() {
do {
selItem = menu.getItem();
switch(selItem) {
case MenuData.GENERATE_RANDOM: ; break;
case MenuData.GENERATE_RANDOM: generateRandomPuzzle(); break;
case MenuData.GENERATE_BASED_ON_EXAMPLE: ; break;
case MenuData.GENERATE_BASED_ON_CURRENT_PUZZLE: ; break;
default: incorrectSelection();
}
} while ( selItem != MenuData.RETURN );
}
private void loopMenuInput() {
int selItem;
Menu menu = new Menu(MenuData.INPUT_TITLE, MenuData.INPUT_CONTENT, this);
do {
selItem = menu.getItem();
switch(selItem) {
case MenuData.INPUT_ONE_LINE: inputPuzzleFromKeyboard1Line(); break;
case MenuData.INPUT_9ROWS: inputPuzzleFromKeyboard9rows(); break;
case MenuData.INPUT_11ROWS: inputPuzzleFromKeyboard11rows(); break;
case MenuData.INPUT_13ROWS: inputPuzzleFromKeyboard13rows(); break;
default: incorrectSelection();
}
} while ( selItem != MenuData.RETURN );
}
private void loopMenuEvaluate() {
int selItem;
Menu menu = new Menu(MenuData.EVALUATE_TITLE, MenuData.EVALUATE_CONTENT, this);
@@ -139,6 +156,9 @@ private void loopMenuOptions() {
default: incorrectSelection();
}
} while ( selItem != MenuData.RETURN );
}
private void loadFromFile() {
}
private void loadFromExample() {
System.out.println();
@@ -152,7 +172,63 @@ private void loadFromExample() {
}
}
private void inputPuzzleFromKeyboard() {
private void inputPuzzleFromKeyboard1Line() {
System.out.print("One line definition: ");
String line = consoleReadLine();
int[][] parsedPuzzle = SudokuStore.loadBoardFromStringLine(line);
if (parsedPuzzle != null) puzzle = parsedPuzzle;
else System.out.println(">>> !!! Error - incorrect puzzle definition !!! <<<");
}
private void inputPuzzleFromKeyboard9rows() {
System.out.println("You will be asked for inputting 9 rows.");
System.out.print("Row 1/9: "); String r1 = consoleReadLine();
System.out.print("Row 2/9: "); String r2 = consoleReadLine();
System.out.print("Row 3/9: "); String r3 = consoleReadLine();
System.out.print("Row 4/9: "); String r4 = consoleReadLine();
System.out.print("Row 5/9: "); String r5 = consoleReadLine();
System.out.print("Row 6/9: "); String r6 = consoleReadLine();
System.out.print("Row 7/9: "); String r7 = consoleReadLine();
System.out.print("Row 8/9: "); String r8 = consoleReadLine();
System.out.print("Row 9/9: "); String r9 = consoleReadLine();
int[][] parsedPuzzle = SudokuStore.loadBoardFromStrings(r1, r2, r3, r4, r5, r6, r7, r8, r9);
if (parsedPuzzle != null) puzzle = parsedPuzzle;
else System.out.println(">>> !!! Error - incorrect puzzle definition !!! <<<");
}
private void inputPuzzleFromKeyboard11rows() {
System.out.println("You will be asked for inputting 11 rows (2 supporting).");
System.out.print("Row 1/11: "); String r1 = consoleReadLine();
System.out.print("Row 2/11: "); String r2 = consoleReadLine();
System.out.print("Row 3/11: "); String r3 = consoleReadLine();
System.out.print("Row 4/11: "); String r4 = consoleReadLine();
System.out.print("Row 5/11: "); String r5 = consoleReadLine();
System.out.print("Row 6/11: "); String r6 = consoleReadLine();
System.out.print("Row 7/11: "); String r7 = consoleReadLine();
System.out.print("Row 8/11: "); String r8 = consoleReadLine();
System.out.print("Row 9/11: "); String r9 = consoleReadLine();
System.out.print("Row 10/11: "); String r10 = consoleReadLine();
System.out.print("Row 11/11: "); String r11 = consoleReadLine();
int[][] parsedPuzzle = SudokuStore.loadBoardFromStrings(r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11);
if (parsedPuzzle != null) puzzle = parsedPuzzle;
else System.out.println(">>> !!! Error - incorrect puzzle definition !!! <<<");
}
private void inputPuzzleFromKeyboard13rows() {
System.out.println("You will be asked for inputting 13 rows (4 supporting).");
System.out.print("Row 1/13: "); String r1 = consoleReadLine();
System.out.print("Row 2/13: "); String r2 = consoleReadLine();
System.out.print("Row 3/13: "); String r3 = consoleReadLine();
System.out.print("Row 4/13: "); String r4 = consoleReadLine();
System.out.print("Row 5/13: "); String r5 = consoleReadLine();
System.out.print("Row 6/13: "); String r6 = consoleReadLine();
System.out.print("Row 7/13: "); String r7 = consoleReadLine();
System.out.print("Row 8/13: "); String r8 = consoleReadLine();
System.out.print("Row 9/13: "); String r9 = consoleReadLine();
System.out.print("Row 10/13: "); String r10 = consoleReadLine();
System.out.print("Row 11/13: "); String r11 = consoleReadLine();
System.out.print("Row 12/13: "); String r12 = consoleReadLine();
System.out.print("Row 13/13: "); String r13 = consoleReadLine();
int[][] parsedPuzzle = SudokuStore.loadBoardFromStrings(r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13);
if (parsedPuzzle != null) puzzle = parsedPuzzle;
else System.out.println(">>> !!! Error - incorrect puzzle definition !!! <<<");
}
private void solveFindFirst() {
solver = new SudokuSolver(puzzle);
@@ -165,6 +241,15 @@ private void solveFindFirst() {
System.out.println(solver.getMessages());
}
}
private void generateRandomPuzzle() {
generator = new SudokuGenerator(SudokuGenerator.PARAM_GEN_RND_BOARD);
int[][] generated = generator.generate();
if (generator.getGeneratorState() == SudokuGenerator.GENERATOR_GEN_FINISHED) puzzle = generated;
else {
System.out.println(">>> !!! Error while generating random puzzle !!! <<<");
System.out.println(generator.getMessages());
}
}
private void savePuzzle() {
}
private void displayAboutInto() {
@@ -21,9 +21,9 @@ void consolePrintMenue() {
System.out.println("----- " + title + " -----");
for (int i = 1; i <= itemsNum; i++) {
if ( (itemsNum >= 10) && (i < 10) ) System.out.print(" ");
System.out.println(i + ". " + content[i] + ".");
System.out.println(i + ". " + content[i]);
}
System.out.println("0. " + content[0] + ".");
System.out.println("0. " + content[0]);
System.out.println("----- " + title + " -----");
}
int getItem() {
@@ -4,14 +4,14 @@
static final String MAIN_TITLE = "Main menu";
static final String[] MAIN_CONTENT = {
/* 0 */ "Quit from application"
/* 1 */ ,"Load"
/* 2 */ ,"Generate"
/* 3 */ ,"Input"
/* 4 */ ,"Modify"
/* 5 */ ,"Evaluate"
/* 6 */ ,"Solve"
/* 1 */ ,"Load ..."
/* 2 */ ,"Generate ..."
/* 3 */ ,"Input ..."
/* 4 */ ,"Modify ..."
/* 5 */ ,"Evaluate ..."
/* 6 */ ,"Solve ..."
/* 7 */ ,"Save"
/* 8 */ ,"Options"
/* 8 */ ,"Options ..."
/* 9 */ ,"About"
};
@@ -40,8 +40,17 @@
};
static final int INPUT = 3;
static final String INPUT_TITLE = "";
static final String INPUT_TITLE = "Puzzle input";
static final int INPUT_ONE_LINE = 1;
static final int INPUT_9ROWS = 2;
static final int INPUT_11ROWS = 3;
static final int INPUT_13ROWS = 4;
static final String[] INPUT_CONTENT = {
/* 0 */ "Return to main menu"
/* 1 */ ,"One line string"
/* 2 */ ,"Separate strings for each row - 9 rows"
/* 3 */ ,"Separate strings for each row - 11 rows"
/* 4 */ ,"Separate strings for each row - 13 rows"
};
static final int MODIFY = 4;

0 comments on commit ff164ad

Please sign in to comment.