From 2bc7d9e76c812aeee0e5c4b401a3af404636b55a Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Fri, 4 Oct 2024 18:32:40 +0200 Subject: [PATCH] Make generator better --- src/main/java/eu/m724/Generator.java | 89 +++++++++++++++++++++------- 1 file changed, 67 insertions(+), 22 deletions(-) diff --git a/src/main/java/eu/m724/Generator.java b/src/main/java/eu/m724/Generator.java index 2dd78a6..bd6209a 100644 --- a/src/main/java/eu/m724/Generator.java +++ b/src/main/java/eu/m724/Generator.java @@ -1,45 +1,90 @@ package eu.m724; +import java.time.Duration; +import java.time.LocalTime; import java.util.HashSet; import java.util.Set; import java.util.concurrent.ThreadLocalRandom; public class Generator { - public static Set generate(int width, int height, String solution, Word[] words) { - ThreadLocalRandom random = ThreadLocalRandom.current(); - Set placedWords = new HashSet<>(words.length); + private int width, height; + private String solution; + private Word[] words; + private char[][] charArray; + private Set placedWords; + + private ThreadLocalRandom random = ThreadLocalRandom.current(); + + private Generator(int width, int height, String solution, Word[] words) { + this.width = width; + this.height = height; + this.solution = solution; + this.words = words; + + this.charArray = new char[width][height]; + this.placedWords = new HashSet<>(words.length); + } + + public static Set generate(int width, int height, String solution, Word[] words) { + return new Generator(width, height, solution, words).generate(); + } + + private Set generate() { System.out.printf("Generator running with %d words\n", words.length); + long start = System.nanoTime(); boolean vertical = random.nextBoolean(); - for (int i=0; i placedWords) { - for (PlacedWord placedWord : placedWords) { - if (placedWord == null) continue; + private void placeWord(PlacedWord word) { + int x = word.x(); + int y = word.y(); - if (placedWord.vertical()) { - if (placedWord.x() == x) { - if (y >= placedWord.y() && y <= placedWord.y() + placedWord.wordLength() - 1) { - return placedWord; - } - } - } else { - if (placedWord.y() == y) { - if (x >= placedWord.x() && x <= placedWord.y() + placedWord.wordLength() - 1) { - return placedWord; - } - } + placedWords.add(word); + + for (int i=0; i