Make generator better

This commit is contained in:
Minecon724 2024-10-04 18:32:40 +02:00
parent c2002c155a
commit 2bc7d9e76c
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8

View file

@ -1,45 +1,90 @@
package eu.m724; package eu.m724;
import java.time.Duration;
import java.time.LocalTime;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
public class Generator { public class Generator {
public static Set<PlacedWord> generate(int width, int height, String solution, Word[] words) { private int width, height;
ThreadLocalRandom random = ThreadLocalRandom.current(); private String solution;
Set<PlacedWord> placedWords = new HashSet<>(words.length); private Word[] words;
private char[][] charArray;
private Set<PlacedWord> 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<PlacedWord> generate(int width, int height, String solution, Word[] words) {
return new Generator(width, height, solution, words).generate();
}
private Set<PlacedWord> generate() {
System.out.printf("Generator running with %d words\n", words.length); System.out.printf("Generator running with %d words\n", words.length);
long start = System.nanoTime();
boolean vertical = random.nextBoolean(); boolean vertical = random.nextBoolean();
for (int i=0; i<words.length; i++) { for (Word word : words) {
Word word = words[i]; PlacedWord placedWord;
placedWords.add(new PlacedWord(0, i, false, word));
do {
int x = random.nextInt(1, width - (!vertical ? word.length() : 0));
int y = random.nextInt(1, height - (vertical ? word.length() : 0));
placedWord = new PlacedWord(x, y, vertical, word);
} while (!canPlace(placedWord));
placeWord(placedWord); // TODO rename
vertical = !vertical; vertical = !vertical;
} }
System.out.printf("Generation took %fms\n", (System.nanoTime() - start) / 1000000.0);
return placedWords; return placedWords;
} }
private static PlacedWord getWordAt(int x, int y, Set<PlacedWord> placedWords) { private void placeWord(PlacedWord word) {
for (PlacedWord placedWord : placedWords) { int x = word.x();
if (placedWord == null) continue; int y = word.y();
if (placedWord.vertical()) { placedWords.add(word);
if (placedWord.x() == x) {
if (y >= placedWord.y() && y <= placedWord.y() + placedWord.wordLength() - 1) { for (int i=0; i<word.length(); i++) {
return placedWord; if (word.vertical()) { y++; } else { x++; }
}
} char c = word.word().word().charAt(i); // TODO
} else {
if (placedWord.y() == y) { if (charArray[x][y] == 0 || charArray[x][y] == c) {
if (x >= placedWord.x() && x <= placedWord.y() + placedWord.wordLength() - 1) { charArray[x][y] = c;
return placedWord;
}
} }
} }
} }
return null; private boolean canPlace(PlacedWord word) {
int x = word.x();
int y = word.y();
for (int i=0; i<word.length(); i++) {
if (word.vertical()) { y++; } else { x++; }
char c = word.word().word().charAt(i); // TODO
if (charArray[x][y] != 0 && charArray[x][y] != c) {
return false;
}
}
return true;
} }
} }