Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
next up is optimizations and randomization to produce different tables with more variety (not all abc words) renamed: src/main/resources/fungoes/lexiku/sowpods.txt -> data/sowpods.txt new file: data/template_1.txt new file: data/template_2.txt modified: pom.xml modified: src/main/java/fungoes/lexiku/BoardBuilder.java new file: src/main/java/fungoes/lexiku/Lexiku.java modified: src/test/java/fungoes/lexiku/BoardBuilderTest.java
- Loading branch information
1 parent
f15e8aa
commit 31391d5
Showing
7 changed files
with
230 additions
and
4 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
...main/resources/fungoes/lexiku/sowpods.txt → data/sowpods.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,29 @@ | ||
a | ||
b | ||
c | ||
d | ||
e | ||
f | ||
g | ||
h | ||
i | ||
j | ||
k | ||
l | ||
m | ||
n | ||
o | ||
p | ||
q | ||
r | ||
s | ||
t | ||
u | ||
v | ||
w | ||
x | ||
y | ||
z | ||
aa | ||
aah | ||
aahed | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
OX | ||
OO | ||
OO | ||
OX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
XOOOXXOOOX | ||
OOXOOOOXOO | ||
OXXXOOXXXO | ||
OOXOOOOXOO | ||
XOOOXOOOOX | ||
XOOOOXOOOX | ||
OOXOOOOXOO | ||
OXXXOOXXXO | ||
OOXOOOOXOO | ||
XOOOXXOOOX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,106 @@ | ||
package fungoes.lexiku; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
|
||
public class BoardBuilder { | ||
|
||
public List<String[][]> build(Dictionary dictionary, Template template) { | ||
|
||
List<String[][]> completedBoards = new ArrayList<String[][]>(); | ||
Space[][] spaces = emptySpaces(template); | ||
List<StartingPoint> startingPoints = template.horizontalStartingPoints(); | ||
int currentStartingPoints = 0; | ||
Set<String> usedWords = new HashSet<String>(startingPoints.size() * 5); | ||
|
||
fillSpaces(spaces, startingPoints, currentStartingPoints, dictionary, template, completedBoards, usedWords); | ||
|
||
return completedBoards; | ||
} | ||
|
||
private void fillSpaces(Space[][] spaces, | ||
List<StartingPoint> startingPoints, int currentStartingPoint, | ||
Dictionary dictionary, Template template, | ||
List<String[][]> completedBoards, Set<String> usedWords) { | ||
|
||
if(currentStartingPoint == startingPoints.size()) { | ||
String[][] completedBoard = new String[template.getWidth()][template.getHeight()]; | ||
|
||
for(int x = 0; x < template.getWidth(); x++) { | ||
for(int y = 0; y < template.getHeight(); y++) { | ||
completedBoard[x][y] = spaces[x][y].letter; | ||
} | ||
} | ||
|
||
Lexiku.printBoard(template, completedBoards); | ||
|
||
completedBoards.add(completedBoard); | ||
} | ||
else { | ||
StartingPoint sPoint = startingPoints.get(currentStartingPoint++); | ||
|
||
for(String candidateWord : dictionary.byLengthIterable(sPoint.length)) { | ||
if(candidateWord.length() == 1 || !usedWords.contains(candidateWord)) { | ||
boolean works = true; | ||
|
||
if(sPoint.y > 0) { | ||
for(int i = 0; i < sPoint.length && works; i++) { | ||
int dx = sPoint.x + i; | ||
int dy = sPoint.y - 1; | ||
|
||
String prefix = spaces[dx][dy].verticalWordSoFar + String.valueOf(candidateWord.charAt(i)); | ||
int verticalWordLength = template.verticalWordLengthFor(dx, sPoint.y); | ||
|
||
if(prefix.length() == verticalWordLength) { | ||
works = !prefix.equals(candidateWord) && (prefix.length() == 1 || !usedWords.contains(prefix)) && dictionary.isWordPresent(prefix); | ||
} | ||
else { | ||
works = dictionary.containsPrefix(prefix, verticalWordLength); | ||
} | ||
} | ||
} | ||
|
||
if(works) { | ||
for(int i = 0; i < sPoint.length; i++) { | ||
int dx = sPoint.x + i; | ||
int dy = sPoint.y - 1; | ||
|
||
spaces[dx][sPoint.y].letter = String.valueOf(candidateWord.charAt(i)); | ||
|
||
if(sPoint.y > 0) { | ||
spaces[dx][sPoint.y].verticalWordSoFar = spaces[dx][dy].verticalWordSoFar + String.valueOf(candidateWord.charAt(i)); | ||
} | ||
else { | ||
spaces[dx][sPoint.y].verticalWordSoFar = spaces[dx][sPoint.y].letter; | ||
} | ||
} | ||
|
||
usedWords.add(candidateWord); | ||
fillSpaces(spaces, startingPoints, currentStartingPoint, dictionary, template, completedBoards, usedWords); | ||
usedWords.remove(candidateWord); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
private Space[][] emptySpaces(Template template) { | ||
Space[][] spaces = new Space[template.getWidth()][template.getHeight()]; | ||
for(int x = 0; x < template.getWidth(); x++) { | ||
for(int y = 0; y < template.getHeight(); y++) { | ||
spaces[x][y] = new Space(); | ||
} | ||
} | ||
return spaces; | ||
} | ||
|
||
private static class Space { | ||
public String letter = "-"; | ||
public String verticalWordSoFar = ""; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package fungoes.lexiku; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public class Lexiku { | ||
|
||
public static void main(String[] args) throws FileNotFoundException, IOException { | ||
if(args.length != 2) { | ||
System.out.println("2 Arguments Required. (dictionary file, template file)"); | ||
return; | ||
} | ||
|
||
Dictionary dictionary = new Dictionary(); | ||
dictionary.loadWords(new FileReader(new File(args[0]))); | ||
|
||
Template template = new Template(new FileReader(new File(args[1]))); | ||
|
||
BoardBuilder builder = new BoardBuilder(); | ||
|
||
List<String[][]> boards = builder.build(dictionary, template); | ||
|
||
System.out.println("Boards Found: " + boards.size()); | ||
|
||
//printBoard(template, boards); | ||
} | ||
|
||
public static void printBoard(Template template, List<String[][]> boards) { | ||
for(String[][] board : boards) { | ||
for(int y = 0; y < template.getHeight(); y++) { | ||
for(int x = 0; x < template.getWidth(); x++) { | ||
System.out.print(board[x][y]); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
System.out.println(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,41 @@ | ||
package fungoes.lexiku; | ||
|
||
import org.junit.Assert; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.StringReader; | ||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
|
||
|
||
public class BoardBuilderTest { | ||
|
||
// TODO some work | ||
@Test | ||
public void emptyDictionaryYieldsNoBoards() throws Exception { | ||
Dictionary dictionary = new Dictionary(); | ||
Template template = new Template(new StringReader("OO\nOO")); | ||
|
||
List<String[][]> boards = new BoardBuilder().build(dictionary, template); | ||
|
||
assertEquals(0, boards.size()); | ||
} | ||
|
||
|
||
@Test | ||
public void name() throws Exception { | ||
Assert.assertEquals(4, 2 + 2); | ||
public void fourByFourTemplateProducesSingleBoard() throws Exception { | ||
Dictionary dictionary = new Dictionary(); | ||
dictionary.loadWords(new StringReader("ab\nbc\nad\ndc")); | ||
|
||
Template template = new Template(new StringReader("OO\nOO")); | ||
|
||
BoardBuilder builder = new BoardBuilder(); | ||
List<String[][]> boards = builder.build(dictionary, template); | ||
|
||
assertEquals(2, boards.size()); | ||
|
||
String[][] board = boards.get(0); | ||
assertEquals(2, board.length); | ||
assertEquals(2, board[0].length); | ||
|
||
} | ||
} |