update console interface
All checks were successful
Linux arm64 / Build (push) Successful in 37s

This commit is contained in:
Janet-Doe
2025-02-01 15:51:45 +01:00
parent 2b3581a400
commit 140d37fbd9
4 changed files with 73 additions and 1172 deletions

View File

@@ -4,14 +4,12 @@ import gui.RenderableMultidoku;
import gui.Symbols;
import sudoku.constraint.*;
import sudoku.solver.RandomSolver;
import sudoku.solver.Solver;
import sudoku.structure.Difficulty;
import sudoku.structure.MultiDoku;
import sudoku.structure.SudokuFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class ConsoleInterface {
@@ -20,25 +18,35 @@ public class ConsoleInterface {
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.");
start();
System.out.println("Do you have a save sudoku you would like to continue? (y/n, default n)");
if (reader.next().equalsIgnoreCase("y")){
useSavedDoku();
}
else {
createDoku();
}
}
public void start(){
private void useSavedDoku() {
System.out.println("What save should we use? Please enter the save number.");
MultiDoku md = saveChoice();
int blockWidth = md.getSubGrid(0).getBlockWidth();
int blockHeight = md.getSubGrid(0).getBlocks().getFirst().getCells().size() / blockWidth;
List<String> listSymbols = pickSymbols(blockWidth*blockHeight);
System.out.println("This is the saved sudoku:");
showMultidoku(md, listSymbols, blockWidth, blockHeight);
}
public void createDoku(){
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 {
listSymbols = Symbols.Numbers.getSymbols();
}
List<String> listSymbols = pickSymbols(numberOfSymbols);
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), " +
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') ?");
MultiDoku doku;
if (reader.next().equalsIgnoreCase("multi")) {
@@ -47,11 +55,11 @@ public class ConsoleInterface {
else {
doku = SudokuFactory.createBasicEmptyRectangleDoku(width, height, listConstraints);
}
RenderableMultidoku rm = RenderableMultidoku.fromMultidoku(doku);
System.out.println("Your sudoku will look like this:");
SudokuPrinter.printMultiDoku(rm, listSymbols, width, height);
showMultidoku(doku, listSymbols, 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))");
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);
@@ -60,7 +68,21 @@ public class ConsoleInterface {
generatePartialDoku(doku, difficulty);
}
System.out.println("Here's your sudoku !");
SudokuPrinter.printMultiDoku(rm, listSymbols, width, height);
showMultidoku(doku, listSymbols, width, height);
}
private MultiDoku saveChoice() {
int nbSave;
MultiDoku md = null;
do {
nbSave = reader.nextInt();
try {
md = SudokuSerializer.getSavedMultiDoku(nbSave);
} catch (Exception e) {
System.out.println("There seems to be a problem with this save, please try again.");
}
} while (md == null);
return md;
}
public int getBlockWidth() {
@@ -91,18 +113,30 @@ public class ConsoleInterface {
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();
private List<String> pickSymbols(int numberOfSymbols) {
System.out.println("Would you like to pick the " + numberOfSymbols + " symbols from the sudoku? (y/n, default 'no' will use numbers)");
if (reader.next().equalsIgnoreCase("y")) {
List<String> listSymbols = new ArrayList<>();
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);
}
listSymbols.add(newSymbol);
System.out.println("You chose the symbols: " + listSymbols.toString());
return listSymbols;
} else {
System.out.println("What existing sets of symbols do you want to use? Numbers ('n', default), letters ('l'), or emojis ('e')?");
return switch (reader.next().toLowerCase()) {
case "l" -> Symbols.Letters.getSymbols();
case "e" -> Symbols.Emojis.getSymbols();
default -> Symbols.Numbers.getSymbols();
};
}
System.out.println("You chose the symbols: " + listSymbols.toString());
}
private List<Constraint> getListConstraints() {
@@ -133,4 +167,12 @@ public class ConsoleInterface {
new RandomSolver().solve(doku);
}
private void showMultidoku(MultiDoku doku, List<String> listSymbols, int width, int height){
showMultiDoku(RenderableMultidoku.fromMultidoku(doku), listSymbols, width, height);
}
private void showMultiDoku(RenderableMultidoku doku, List<String> listSymbols, int width, int height){
SudokuPrinter.printMultiDoku(doku, listSymbols, width, height);
}
}