semi-functionning console interface
All checks were successful
Linux arm64 / Build (push) Successful in 38s

This commit is contained in:
Janet-Doe
2025-01-30 11:51:17 +01:00
parent d4beaec8a8
commit f1d963e546
7 changed files with 191 additions and 951 deletions

View File

@@ -3,10 +3,7 @@
*/
package sudoku;
import sudoku.io.SudokuPrinter;
import sudoku.structure.SudokuFactory;
import java.util.Arrays;
import sudoku.io.ConsoleInterface;
public class Main {
public String getGreeting() {
@@ -14,33 +11,7 @@ public class Main {
}
public static void main(String[] args) {
System.out.println(new Main().getGreeting());
int blockWidth = 2;
int blockHeight = 2;
var multidoku = SudokuFactory.createBasicEmptyRectangleDoku(blockWidth, blockHeight,
SudokuFactory.DEFAULT_CONSTRAINTS);
var sudoku = multidoku.getSubGrid(0);
if (!sudoku.setCellsSymbol(Arrays.asList(0, 1, 2, 3, 2, 3, 1, 1, 1, 0, 3, 2, 3, 2, 1, 1))) {
System.out.println("At least one of those values does not respect the constraints.");
}
// sudoku.setCellSymbol(8,3,0);
SudokuPrinter.printRectangleSudoku(multidoku.getSubGrid(0), blockWidth, blockHeight);
/*
* Solver solver = new Solver();
* ArrayList<IConstraint> constraints = new ArrayList<>();
* constraints.add(new LineConstraint());
* constraints.add(new ColumnConstraint());
* constraints.add(new BlockConstraint());
* try {
* solver.solve(multidoku, constraints);
* } catch (Exception e) {
* System.out.println(e);
* }
*/
ConsoleInterface console = new ConsoleInterface();
console.start();
}
}

View File

@@ -0,0 +1,139 @@
package sudoku.io;
import sudoku.constraint.*;
import sudoku.solver.Solver;
import sudoku.structure.Difficulty;
import sudoku.structure.MultiDoku;
import sudoku.structure.Sudoku;
import sudoku.structure.SudokuFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class ConsoleInterface {
public Scanner reader = new Scanner(System.in);
public void start(){
welcome();
System.out.println("First of all, you need to tell me the size of the sudoku you want to generate.");
int width = getBlockWidth();
int height = getBlockHeight();
System.out.println("Your sudoku will have blocks of a " + width + " x " + height + " format.");
int numberOfSymbols = width * height;
System.out.println("Would you like to pick the " + numberOfSymbols + " symbols from the sudoku? (y/n, default 'no' will use numbers)" );
List<String> listSymbols = new ArrayList<>();
if(reader.next().equalsIgnoreCase("y")){
pickSymbols(listSymbols, numberOfSymbols);
}
else {
// TODO
System.out.println("Simon doit finir sa partie.");
assert false;
}
List<Constraint> listConstraints = getListConstraints();
System.out.println("Now that we have the size of our sudoku, would you rather have a single grid ('one', default), " +
"or a a multidoku composed of 5 subgrids ('multi') ?");
List<Sudoku> subGrids = new ArrayList<>();
MultiDoku doku;
if (reader.next().equalsIgnoreCase("multi")) {
doku = SudokuFactory.createBasicEmptyRectangleDoku(width, height, listConstraints);
}
else {
doku = SudokuFactory.createBasicXShapedMultidoku(width, height, listConstraints);
}
System.out.println("Your sudoku will look like this:");
// TODO printMultiDoku method not yet implemented
SudokuPrinter.printMultiDoku(doku, width, height);
System.out.println("We now will fill this sudoku.");
System.out.println("What level of difficulty would you like? ('very easy', 'easy', 'medium' (default), 'hard', 'full' (sudoku fully completed))");
String difficulty = reader.next().toLowerCase();
if (difficulty.equals("full")) {
generateFullDoku(doku);
}
else {
generatePartialDoku(doku, difficulty);
}
System.out.println("Here's your sudoku !");
SudokuPrinter.printMultiDoku(doku, width, height);
}
public void welcome(){
System.out.println("Welcome to our Sudoku Solver!");
System.out.println("This is the project of Melvyn Bauvent, Lilas Grenier and Simon Priblyski.");
}
public int getBlockWidth() {
System.out.println("Width of a block: ");
int widthBlock = reader.nextInt();
checkValidSize(widthBlock);
while (!checkValidSize(widthBlock)) {
System.out.println("That is not a valid width for a block. Try again:");
widthBlock = reader.nextInt();
}
System.out.println("You have chose a width of " + widthBlock + ".");
return widthBlock;
}
public int getBlockHeight() {
System.out.println("Height of a block: ");
int heightBlock = reader.nextInt();
checkValidSize(heightBlock);
while (!checkValidSize(heightBlock)) {
System.out.println("That is not a valid height for a block. Try again:");
heightBlock = reader.nextInt();
}
System.out.println("You have chose a height of " + heightBlock + ".");
return heightBlock;
}
private Boolean checkValidSize(int size) {
return (size > 0);
}
private void pickSymbols(List<String> listSymbols, int numberOfSymbols) {
System.out.println("You have chosen to pick your own symbols.");
for (int i = 0; i < numberOfSymbols; i++) {
System.out.println("Choose for the symbol number " + i + ": ");
String newSymbol = reader.next();
while (listSymbols.contains(newSymbol)) {
System.out.println("This symbol has already been given. Try again:");
newSymbol = reader.next();
}
listSymbols.add(newSymbol);
}
System.out.println("You chose the symbols: " + listSymbols.toString());
}
private List<Constraint> getListConstraints() {
List<Constraint> listConstraints = SudokuFactory.DEFAULT_CONSTRAINTS;
System.out.println("The sudoku have constraints of blocks, lines and columns. Would you like to add the diagonal constraints ? (y/n, default 'no')");
if(reader.next().equalsIgnoreCase("y")){
listConstraints.add(Constraint.Diagonal);
}
return listConstraints;
}
private void generatePartialDoku(MultiDoku doku, String difficultyName) {
Difficulty difficulty;
switch (difficultyName){
case "very easy": difficulty = Difficulty.VeryEasy;
case "easy": difficulty = Difficulty.Easy;
case "hard": difficulty = Difficulty.Hard;
default: difficulty = Difficulty.Medium;
}
try {
SudokuFactory.fillDoku(doku, difficulty);
} catch (Exception e) {
System.out.println("There seems to be a problem with those settings. Let's start again.");
}
}
private void generateFullDoku(MultiDoku doku) {
Solver.solveRandom(doku, new Random());
}
}

View File

@@ -1,5 +1,6 @@
package sudoku.io;
import sudoku.structure.MultiDoku;
import sudoku.structure.Sudoku;
public class SudokuPrinter {
@@ -43,4 +44,8 @@ public class SudokuPrinter {
}
return result.toString();
}
public static void printMultiDoku(final MultiDoku doku, int blockWidth, int blockHeight){
// TODO
}
}

View File

@@ -246,6 +246,38 @@ public class SudokuFactory {
return new MultiDoku(Arrays.asList(sudoku1, sudoku2, sudoku3, sudoku4, sudoku5));
}
/**
* Créée un MultiDoku de Blocks rectangulaires de forme width par height composé de cinq Sudokus,
* dont un central qui partage chacun de ses Blocks d'angle avec un autre
* Sudoku.
*
* @param width int, largeur des Blocks unitraires des Sudokus à crééer.
* @param height int, hauteur des Blocks unitraires des Sudokus à crééer.
* @return MultiDoku, MultiDoku de forme X.
*/
public static MultiDoku createBasicXShapedMultidoku(int width, int height, List<Constraint> constraints) {
assert (width > 1 && height > 1);
/*
* 2 3
* 1
* 4 5
*/
Sudoku sudoku1 = createRectangleSudoku(width, height, constraints);
Sudoku sudoku2 = createRectangleSudoku(width, height, constraints);
Sudoku sudoku3 = createRectangleSudoku(width, height, constraints);
Sudoku sudoku4 = createRectangleSudoku(width, height, constraints);
Sudoku sudoku5 = createRectangleSudoku(width, height, constraints);
linkSquareSudokus(sudoku1, sudoku2, new Coordinate(1 - width, 1 - height));
linkSquareSudokus(sudoku1, sudoku3, new Coordinate(width - 1, 1 - height));
linkSquareSudokus(sudoku1, sudoku4, new Coordinate(1 - width, height - 1));
linkSquareSudokus(sudoku1, sudoku5, new Coordinate(width - 1, height - 1));
return new MultiDoku(Arrays.asList(sudoku1, sudoku2, sudoku3, sudoku4, sudoku5));
}
public static void fillDoku(MultiDoku doku, Difficulty difficulty) throws Exception {
Solver.solveRandom(doku, random);
int nbCellsToEmpty = (int) (difficulty.getFactor() * doku.getNbCells());

View File

@@ -7,22 +7,25 @@ import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import sudoku.io.SudokuSerializer;
import sudoku.solver.Solver;
import sudoku.structure.MultiDoku;
import sudoku.structure.SudokuFactory;
public class SudokuSerializerTest {
void testSerializeWithSize(int blockWidth, int blockHeight) {
var sudoku = SudokuFactory.createBasicEmptyRectangleDoku(blockWidth, blockHeight,
void testSerializeWithSize(int blockWidth, int blockHeight, Random r) {
MultiDoku sudoku = SudokuFactory.createBasicEmptyRectangleDoku(blockWidth, blockHeight,
SudokuFactory.DEFAULT_CONSTRAINTS);
Solver.solveRandom(sudoku, r);
JSONObject data = SudokuSerializer.serializeSudoku(sudoku);
MultiDoku multiDoku = SudokuSerializer.deserializeSudoku(data);
assert (data.toString().equals(SudokuSerializer.serializeSudoku(multiDoku).toString()));
}
void testSaveWithSize(int blockWidth, int blockHeight) {
void testSaveWithSize(int blockWidth, int blockHeight, Random r) {
MultiDoku doku = SudokuFactory.createBasicEmptyRectangleDoku(blockWidth, blockHeight,
SudokuFactory.DEFAULT_CONSTRAINTS);
Solver.solveRandom(doku, r);
String savePath = SudokuSerializer.saveMultiDoku(doku);
MultiDoku otherDoku = null;
try {
@@ -41,17 +44,21 @@ public class SudokuSerializerTest {
@Test
void testSerialize() {
Random r = new Random();
testSerializeWithSize(3, 3, r);
/**
int testCount = 5;
for (int i = 0; i < testCount; i++) {
int blockWidth = r.nextInt(10) + 1;
int blockHeight = r.nextInt(10) + 1;
testSerializeWithSize(blockWidth, blockHeight);
testSerializeWithSize(blockWidth, blockHeight, r);
}
for (int i = 0; i < testCount; i++) {
int blockWidth = r.nextInt(10) + 1;
int blockHeight = r.nextInt(10) + 1;
testSaveWithSize(blockWidth, blockHeight);
testSaveWithSize(blockWidth, blockHeight, r);
}
*/
}
}