Skip to content

Commit

Permalink
add form to generate function input to specify which form will be use…
Browse files Browse the repository at this point in the history
…d to generate
  • Loading branch information
nguyennd97 committed Jun 13, 2022
1 parent 63dcb5b commit 7c76596
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 25 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 11 additions & 1 deletion src/vn/com/dangnguyendota/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
35 changes: 24 additions & 11 deletions src/vn/com/dangnguyendota/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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");
}
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions src/vn/com/dangnguyendota/Puzzle4.java
Original file line number Diff line number Diff line change
Expand Up @@ -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[][]{
Expand Down
4 changes: 4 additions & 0 deletions src/vn/com/dangnguyendota/Puzzle5.java
Original file line number Diff line number Diff line change
Expand Up @@ -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[][]{
Expand Down
4 changes: 4 additions & 0 deletions src/vn/com/dangnguyendota/Puzzle6.java
Original file line number Diff line number Diff line change
Expand Up @@ -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[][]{
Expand Down
4 changes: 4 additions & 0 deletions src/vn/com/dangnguyendota/Puzzle7.java
Original file line number Diff line number Diff line change
Expand Up @@ -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[][]{
Expand Down
4 changes: 4 additions & 0 deletions src/vn/com/dangnguyendota/Puzzle8.java
Original file line number Diff line number Diff line change
Expand Up @@ -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[]{};
Expand Down
4 changes: 4 additions & 0 deletions src/vn/com/dangnguyendota/Puzzle9.java
Original file line number Diff line number Diff line change
Expand Up @@ -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[]{};
Expand Down
54 changes: 41 additions & 13 deletions src/vn/com/dangnguyendota/PuzzleFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 7c76596

Please sign in to comment.