Make generator better
This commit is contained in:
parent
c2002c155a
commit
2bc7d9e76c
1 changed files with 67 additions and 22 deletions
|
@ -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<PlacedWord> generate(int width, int height, String solution, Word[] words) {
|
||||
ThreadLocalRandom random = ThreadLocalRandom.current();
|
||||
Set<PlacedWord> placedWords = new HashSet<>(words.length);
|
||||
private int width, height;
|
||||
private String solution;
|
||||
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);
|
||||
long start = System.nanoTime();
|
||||
|
||||
boolean vertical = random.nextBoolean();
|
||||
for (int i=0; i<words.length; i++) {
|
||||
Word word = words[i];
|
||||
placedWords.add(new PlacedWord(0, i, false, word));
|
||||
for (Word word : words) {
|
||||
PlacedWord placedWord;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
System.out.printf("Generation took %fms\n", (System.nanoTime() - start) / 1000000.0);
|
||||
|
||||
return placedWords;
|
||||
}
|
||||
|
||||
private static PlacedWord getWordAt(int x, int y, Set<PlacedWord> 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<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) {
|
||||
charArray[x][y] = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 null;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue