-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add clue columns after each poem for #69.
- Loading branch information
Showing
3 changed files
with
168 additions
and
21 deletions.
There are no files selected for viewing
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
86 changes: 86 additions & 0 deletions
86
core/src/com/github/donkirkby/vograbulary/poemsorting/PoemDisplay.java
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,86 @@ | ||
package com.github.donkirkby.vograbulary.poemsorting; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class PoemDisplay { | ||
private int width; | ||
private int clueLineCount; | ||
private List<String> bodyLines = new ArrayList<String>(); | ||
private List<String> clueColumns = new ArrayList<String>(); | ||
|
||
public PoemDisplay(Poem poem, int width) { | ||
List<String> poemLines = new ArrayList<String>(); | ||
Poem sortedPoem = poem.sortWords(); | ||
final int poemLineCount = poem.getLines().size(); | ||
for (int poemLineIndex = 0; poemLineIndex < poemLineCount; poemLineIndex++) { | ||
String poemLine = poem.getLines().get(poemLineIndex); | ||
String sortedLine = sortedPoem.getLines().get(poemLineIndex); | ||
String indent = ""; | ||
int start = 0; | ||
int lastBreak = 0; | ||
for (int charIndex = 0; charIndex < poemLine.length(); charIndex++) { | ||
boolean shouldAdd; | ||
if (charIndex == poemLine.length() - 1) { | ||
shouldAdd = true; | ||
lastBreak = poemLine.length(); | ||
} | ||
else { | ||
if (poemLine.charAt(charIndex) == ' ') { | ||
lastBreak = charIndex; | ||
} | ||
shouldAdd = charIndex - start >= width; | ||
} | ||
if (shouldAdd) { | ||
poemLines.add(indent + poemLine.substring(start, lastBreak)); | ||
bodyLines.add(indent + sortedLine.substring(start, lastBreak)); | ||
start = lastBreak+1; | ||
indent = " "; | ||
} | ||
} | ||
} | ||
this.width = 0; | ||
for (String line : bodyLines) { | ||
this.width = Math.max(this.width, line.length()); | ||
} | ||
char column[] = new char[bodyLines.size()]; | ||
for (int charIndex = 0; charIndex < this.width; charIndex++) { | ||
int letterCount = 0; | ||
for (int lineIndex = 0; lineIndex < poemLines.size(); lineIndex++) { | ||
final String line = poemLines.get(lineIndex); | ||
char c = charIndex < line.length() | ||
? Character.toLowerCase(line.charAt(charIndex)) | ||
: ' '; | ||
if ('a' <= c && c <= 'z') { | ||
column[letterCount++] = c; | ||
} | ||
} | ||
Arrays.sort(column, 0, letterCount); | ||
clueColumns.add(new String(column, 0, letterCount)); | ||
clueLineCount = Math.max(clueLineCount, letterCount); | ||
} | ||
} | ||
|
||
public int getWidth() { | ||
return width; | ||
} | ||
|
||
public int getBodyLineCount() { | ||
return bodyLines.size(); | ||
} | ||
|
||
public char getBody(int lineIndex, int charIndex) { | ||
String line = bodyLines.get(lineIndex); | ||
return charIndex < line.length() ? line.charAt(charIndex) : ' '; | ||
} | ||
|
||
public int getClueLineCount() { | ||
return clueLineCount; | ||
} | ||
|
||
public char getClue(int lineIndex, int charIndex) { | ||
String line = charIndex < clueColumns.size() ? clueColumns.get(charIndex) : ""; | ||
return lineIndex < line.length() ? line.charAt(lineIndex) : ' '; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
vograbulary-test/src/com/github/donkirkby/vograbulary/poemsorting/PoemDisplayTest.java
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,63 @@ | ||
package com.github.donkirkby.vograbulary.poemsorting; | ||
|
||
import static org.hamcrest.Matchers.*; | ||
import static org.junit.Assert.*; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
|
||
public class PoemDisplayTest { | ||
@Test | ||
public void display() { | ||
List<Poem> poems = Poem.load( | ||
"Guess I'm a poet,", | ||
"I didn't know it."); | ||
int width = 15; | ||
String expectedDisplay = | ||
"egssu i'm a \n" + | ||
" eopt, \n" + | ||
"i ddin't know\n" + | ||
" it. \n" + | ||
"gudidnetmkaow\n" + | ||
"i esioit n \n" + | ||
" pt \n" + | ||
" s \n"; | ||
PoemDisplay display = new PoemDisplay(poems.get(0), width); | ||
|
||
assertThat("display", buildTextDisplay(display), is(expectedDisplay)); | ||
} | ||
|
||
@Test | ||
public void displayBlankColumn() { | ||
List<Poem> poems = Poem.load( | ||
"Guess I'm a poet,", | ||
"I did not know it."); | ||
int width = 20; | ||
String expectedDisplay = | ||
"egssu i'm a eopt, \n" + | ||
"i ddi not know it.\n" + | ||
"gudid iom anooeit \n" + | ||
"i ess n t k pw t \n"; | ||
PoemDisplay display = new PoemDisplay(poems.get(0), width); | ||
|
||
assertThat("display", buildTextDisplay(display), is(expectedDisplay)); | ||
} | ||
|
||
private String buildTextDisplay(PoemDisplay display) { | ||
StringBuilder builder = new StringBuilder(); | ||
for (int i = 0; i < display.getBodyLineCount(); i++) { | ||
for (int j = 0; j < display.getWidth(); j++) { | ||
builder.append(display.getBody(i, j)); | ||
} | ||
builder.append('\n'); | ||
} | ||
for (int i = 0; i < display.getClueLineCount(); i++) { | ||
for (int j = 0; j < display.getWidth(); j++) { | ||
builder.append(display.getClue(i, j)); | ||
} | ||
builder.append('\n'); | ||
} | ||
return builder.toString(); | ||
} | ||
} |