From 7c76596e7e239edbdfe92086405edc79f57826d1 Mon Sep 17 00:00:00 2001 From: nguyennd Date: Mon, 13 Jun 2022 11:05:18 +0700 Subject: [PATCH] add form to generate function input to specify which form will be used to generate --- README.md | 28 ++++++++++ src/vn/com/dangnguyendota/Example.java | 12 ++++- src/vn/com/dangnguyendota/Generator.java | 35 +++++++++---- src/vn/com/dangnguyendota/Puzzle4.java | 4 ++ src/vn/com/dangnguyendota/Puzzle5.java | 4 ++ src/vn/com/dangnguyendota/Puzzle6.java | 4 ++ src/vn/com/dangnguyendota/Puzzle7.java | 4 ++ src/vn/com/dangnguyendota/Puzzle8.java | 4 ++ src/vn/com/dangnguyendota/Puzzle9.java | 4 ++ src/vn/com/dangnguyendota/PuzzleFactory.java | 54 +++++++++++++++----- 10 files changed, 128 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index e0bfe2c..dceb78b 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,34 @@ System.out.println(game.getAnswer()); // use game.getQuestion().isClassicForm() to check classic form. game.getQuestion().getForm(); ``` + +Using specified form: +```java +// generate 7x7 puzzle within 5 seconds with minimum score of 10 and maximum score of 1.000.000, specify which form will be use +Game game = Generator.generate(7, 5 * 1000, 10, 1000000, new int[][]{ + {0, 0, 1, 1, 1, 1, 2}, + {0, 0, 0, 1, 1, 1, 2}, + {3, 0, 0, 4, 4, 2, 2}, + {3, 3, 4, 4, 4, 2, 2}, + {3, 3, 4, 4, 6, 6, 2}, + {3, 5, 5, 5, 6, 6, 6}, + {3, 5, 5, 5, 5, 6, 6} + }); +// or we can use one of default forms +Game game = Generator.generate(7, 5 * 1000, 10, 1000000, Puzzle7.FORM2.toArray()); + +// score of generated puzzle +System.out.println("Score: " + game.getScore()); +// puzzle +System.out.println("Puzzle"); +System.out.println(game.getQuestion()); +// solved puzzle +System.out.println("Solved"); +System.out.println(game.getAnswer()); +// Form of puzzle (return null if classic form) +// use game.getQuestion().isClassicForm() to check classic form. +game.getQuestion().getForm(); +``` ## Solver diff --git a/src/vn/com/dangnguyendota/Example.java b/src/vn/com/dangnguyendota/Example.java index a5894cc..21f9fc8 100644 --- a/src/vn/com/dangnguyendota/Example.java +++ b/src/vn/com/dangnguyendota/Example.java @@ -61,7 +61,17 @@ static void generateAndSaveToFile() { } static void generate() { - Game game = Generator.generate(7, 5 * 1000, 10, 1000000); +// Game game = Generator.generate(7, 5 * 1000, 10, 1000000, Puzzle7.FORM2.toArray()); +// Game game = Generator.generate(7, 5 * 1000, 10, 1000000); + Game game = Generator.generate(7, 5 * 1000, 10, 1000000, new int[][]{ + {0, 0, 1, 1, 1, 1, 2}, + {0, 0, 0, 1, 1, 1, 2}, + {3, 0, 0, 4, 4, 2, 2}, + {3, 3, 4, 4, 4, 2, 2}, + {3, 3, 4, 4, 6, 6, 2}, + {3, 5, 5, 5, 6, 6, 6}, + {3, 5, 5, 5, 5, 6, 6} + }); System.out.println("score: " + game.getScore()); System.out.println("Question"); System.out.println(game.getQuestion()); diff --git a/src/vn/com/dangnguyendota/Generator.java b/src/vn/com/dangnguyendota/Generator.java index 93ef223..eb1b76f 100644 --- a/src/vn/com/dangnguyendota/Generator.java +++ b/src/vn/com/dangnguyendota/Generator.java @@ -8,24 +8,35 @@ public class Generator { private static final Random random = new Random(); + public static Game generate(int size, long time, int minScore, int maxScore) { + return generate(size, time, minScore, maxScore, null); + } + + public static Game generate(int size, long time, int maxScore) { + return generate(size, time, maxScore, null); + } + + /** * generate random game contain solved puzzle, puzzle and score of puzzle - * @param size the type of puzzle (example: puzzle 9x9 size = 9, 7x7 size = 7) - * @param time maximum thinking time (millisecond) + * + * @param size the type of puzzle (example: puzzle 9x9 size = 9, 7x7 size = 7) + * @param time maximum thinking time (millisecond) * @param minScore the minimum of puzzle score * @param maxScore the maximum of puzzle score + * @param form the form of puzzle, set null if using a random registered form * @return Game */ - public static Game generate(int size, long time, int minScore, int maxScore) { - if(minScore > maxScore) throw new IllegalArgumentException("Min score is bigger than max score"); + public static Game generate(int size, long time, int minScore, int maxScore, int[][] form) { + if (minScore > maxScore) throw new IllegalArgumentException("Min score is bigger than max score"); long from = System.currentTimeMillis(); while (true) { - Game game = Generator.generate(size, time, maxScore); + Game game = Generator.generate(size, time, maxScore, form); // only accept puzzle with score between minScore and maxScore // if score is not as expected, try again - if(game.getScore() >= minScore && game.getScore() <= maxScore) { + if (game.getScore() >= minScore && game.getScore() <= maxScore) { return game; } @@ -37,14 +48,16 @@ public static Game generate(int size, long time, int minScore, int maxScore) { /** * generate random game contain solved puzzle, puzzle and score of puzzle - * @param size the type of puzzle (example: puzzle 9x9 size = 9, 7x7 size = 7) - * @param time maximum thinking time (millisecond) + * + * @param size the type of puzzle (example: puzzle 9x9 size = 9, 7x7 size = 7) + * @param time maximum thinking time (millisecond) * @param maxScore the maximum of puzzle score + * @param form the form of puzzle, set null if using a random registered form * @return Game */ - public static Game generate(int size, long time, int maxScore) { + public static Game generate(int size, long time, int maxScore, int[][] form) { // random answer first - Puzzle answer = PuzzleFactory.newSolvedPuzzle(size); + Puzzle answer = PuzzleFactory.newSolvedPuzzle(size, form); if (answer == null) { throw new IllegalArgumentException("invalid input"); } @@ -88,7 +101,7 @@ public static Game generate(int size, long time, int maxScore) { // if current score is greater than old score // set question back to old question and break the loop int score = question.difficultyScore(answer); - if(score > maxScore) { + if (score > maxScore) { question.set(row, col, value); break; } diff --git a/src/vn/com/dangnguyendota/Puzzle4.java b/src/vn/com/dangnguyendota/Puzzle4.java index 2e57771..e39884a 100644 --- a/src/vn/com/dangnguyendota/Puzzle4.java +++ b/src/vn/com/dangnguyendota/Puzzle4.java @@ -19,6 +19,10 @@ public Form(String form) { public Form(int[][] form) { this.form = form; } + + public int[][] toArray() { + return form; + } } public static Form FORM1 = new Form(new int[][]{ diff --git a/src/vn/com/dangnguyendota/Puzzle5.java b/src/vn/com/dangnguyendota/Puzzle5.java index f11b302..2107959 100644 --- a/src/vn/com/dangnguyendota/Puzzle5.java +++ b/src/vn/com/dangnguyendota/Puzzle5.java @@ -9,6 +9,10 @@ public static class Form { public Form(int[][] form) { this.form = form; } + + public int[][] toArray() { + return form; + } } public static Form FORM1 = new Form(new int[][]{ diff --git a/src/vn/com/dangnguyendota/Puzzle6.java b/src/vn/com/dangnguyendota/Puzzle6.java index bd9cf42..c561272 100644 --- a/src/vn/com/dangnguyendota/Puzzle6.java +++ b/src/vn/com/dangnguyendota/Puzzle6.java @@ -9,6 +9,10 @@ public static class Form { public Form(int[][] form) { this.form = form; } + + public int[][] toArray() { + return form; + } } public static Form FORM1 = new Form(new int[][]{ diff --git a/src/vn/com/dangnguyendota/Puzzle7.java b/src/vn/com/dangnguyendota/Puzzle7.java index 49a0897..5ea3c55 100644 --- a/src/vn/com/dangnguyendota/Puzzle7.java +++ b/src/vn/com/dangnguyendota/Puzzle7.java @@ -10,6 +10,10 @@ public static class Form { public Form(int[][] form) { this.form = form; } + + public int[][] toArray() { + return form; + } } public static Form FORM1 = new Form(new int[][]{ diff --git a/src/vn/com/dangnguyendota/Puzzle8.java b/src/vn/com/dangnguyendota/Puzzle8.java index 7d6323d..e4e6745 100644 --- a/src/vn/com/dangnguyendota/Puzzle8.java +++ b/src/vn/com/dangnguyendota/Puzzle8.java @@ -10,6 +10,10 @@ public static class Form { public Form(int[][] form) { this.form = form; } + + public int[][] toArray() { + return form; + } } public static Form[] FORMS = new Form[]{}; diff --git a/src/vn/com/dangnguyendota/Puzzle9.java b/src/vn/com/dangnguyendota/Puzzle9.java index fc99599..918f93c 100644 --- a/src/vn/com/dangnguyendota/Puzzle9.java +++ b/src/vn/com/dangnguyendota/Puzzle9.java @@ -9,6 +9,10 @@ public static class Form { public Form(int[][] form) { this.form = form; } + + public int[][] toArray() { + return form; + } } public static Form[] FORMS = new Form[]{}; diff --git a/src/vn/com/dangnguyendota/PuzzleFactory.java b/src/vn/com/dangnguyendota/PuzzleFactory.java index 62a32f9..c01b1a8 100644 --- a/src/vn/com/dangnguyendota/PuzzleFactory.java +++ b/src/vn/com/dangnguyendota/PuzzleFactory.java @@ -3,37 +3,65 @@ import java.util.Random; public class PuzzleFactory { - public static Puzzle newSolvedPuzzle(int size) { + public static Puzzle newSolvedPuzzle(int size, int[][] form) { Puzzle puzzle; int r; switch (size) { case 4: - r = (new Random()).nextInt(Puzzle4.FORMS.length + 1); - if (r == Puzzle4.FORMS.length) { - puzzle = new Puzzle4(); + if (form == null) { + r = (new Random()).nextInt(Puzzle4.FORMS.length + 1); + if (r == Puzzle4.FORMS.length) { + puzzle = new Puzzle4(); + } else { + puzzle = new Puzzle4(Puzzle4.FORMS[r]); + } } else { - puzzle = new Puzzle4(Puzzle4.FORMS[r]); + puzzle = new Puzzle4(new Puzzle4.Form(form)); } + break; case 5: - puzzle = new Puzzle5(Puzzle5.FORMS[(new Random()).nextInt(Puzzle5.FORMS.length)]); + puzzle = form == null ? new Puzzle5(Puzzle5.FORMS[(new Random()).nextInt(Puzzle5.FORMS.length)]) : new Puzzle5(new Puzzle5.Form(form)); break; case 6: - r = (new Random()).nextInt(Puzzle6.FORMS.length + 1); - if (r == Puzzle6.FORMS.length) { - puzzle = new Puzzle6(); + if (form == null) { + r = (new Random()).nextInt(Puzzle6.FORMS.length + 1); + if (r == Puzzle6.FORMS.length) { + puzzle = new Puzzle6(); + } else { + puzzle = new Puzzle6(Puzzle6.FORMS[r]); + } } else { - puzzle = new Puzzle6(Puzzle6.FORMS[r]); + puzzle = new Puzzle6(new Puzzle6.Form(form)); } + break; case 7: - puzzle = new Puzzle7(Puzzle7.FORMS[(new Random()).nextInt(Puzzle7.FORMS.length)]); + puzzle = form == null ? new Puzzle7(Puzzle7.FORMS[(new Random()).nextInt(Puzzle7.FORMS.length)]) : new Puzzle7(new Puzzle7.Form(form)); break; case 8: - puzzle = new Puzzle8(); + if (form == null) { + r = (new Random()).nextInt(Puzzle8.FORMS.length + 1); + if (r == Puzzle8.FORMS.length) { + puzzle = new Puzzle8(); + } else { + puzzle = new Puzzle8(Puzzle8.FORMS[r]); + } + } else { + puzzle = new Puzzle8(new Puzzle8.Form(form)); + } break; case 9: - puzzle = new Puzzle9(); + if (form == null) { + r = (new Random()).nextInt(Puzzle9.FORMS.length + 1); + if (r == Puzzle9.FORMS.length) { + puzzle = new Puzzle9(); + } else { + puzzle = new Puzzle9(Puzzle9.FORMS[r]); + } + } else { + puzzle = new Puzzle9(new Puzzle9.Form(form)); + } break; case 12: puzzle = new Puzzle12();