Permalink
Browse files

[tdd-diamond] Calculate top and bottom rows of each letter

  • Loading branch information...
michaelszymczak committed Apr 9, 2017
1 parent 4352724 commit 5bb73a9f6782f08e0082c512c3bb1355cae36f86
@@ -16,26 +16,28 @@ private Diamond(Letter letter) {
}

public String rendered() {
final Layout layout = new Layout();
if (letter == A)
{
return new Board(new PositionedLetter(ofYX(layout.yOfTopLeft(A),0), A)).toString();
Layout layout = Layout.forLastLetterBeing(A);
return new Board(new PositionedLetter(ofYX(layout.yOfTop(A),0), A)).toString();
}
if (letter == B)
{
Layout layout = Layout.forLastLetterBeing(B);
return new Board(
new PositionedLetter(ofYX(layout.yOfTopLeft(A),1), A),
new PositionedLetter(ofYX(layout.yOfTopLeft(B),0), B), new PositionedLetter(ofYX(layout.yOfTopRight(B),2), B),
new PositionedLetter(ofYX(2,1), A)
new PositionedLetter(ofYX(layout.yOfTop(A),1), A),
new PositionedLetter(ofYX(layout.yOfTop(B),0), B), new PositionedLetter(ofYX(layout.yOfTop(B),2), B),
new PositionedLetter(ofYX(layout.yOfBottom(A),1), A)
).toString();
}

Layout layout = Layout.forLastLetterBeing(C);
return new Board(
new PositionedLetter(ofYX(layout.yOfTopLeft(A),2), A),
new PositionedLetter(ofYX(layout.yOfTopLeft(B),1), B), new PositionedLetter(ofYX(1,3), B),
new PositionedLetter(ofYX(layout.yOfTopLeft(C),0), C), new PositionedLetter(ofYX(layout.yOfTopRight(C),4), C),
new PositionedLetter(ofYX(3,1), B), new PositionedLetter(ofYX(3,3), B),
new PositionedLetter(ofYX(4,2), A)
new PositionedLetter(ofYX(layout.yOfTop(A),2), A),
new PositionedLetter(ofYX(layout.yOfTop(B),1), B), new PositionedLetter(ofYX(layout.yOfTop(B),3), B),
new PositionedLetter(ofYX(layout.yOfTop(C),0), C), new PositionedLetter(ofYX(layout.yOfTop(C),4), C),
new PositionedLetter(ofYX(layout.yOfBottom(B),1), B), new PositionedLetter(ofYX(layout.yOfBottom(B),3), B),
new PositionedLetter(ofYX(layout.yOfBottom(A),2), A)
).toString();

}
@@ -2,12 +2,21 @@

public class Layout {

private final Letter lastLetter;

public int yOfTopLeft(Letter letter) {
return letter.ordinal();
public static Layout forLastLetterBeing(Letter lastLetter) {
return new Layout(lastLetter);
}

private Layout(Letter lastLetter) {
this.lastLetter = lastLetter;
}

public int yOfTopRight(Letter letter) {
public int yOfTop(Letter letter) {
return letter.ordinal();
}

public int yOfBottom(Letter letter) {
return lastLetter.ordinal() * 2 - letter.ordinal();
}
}
@@ -5,12 +5,25 @@ import spock.lang.Specification
class LayoutShould extends Specification {
def "let the top letter to be in ordinal number distance from the top"() {
given:
def layout = new Layout()
def layout = layout()

expect:
layout.yOfTopLeft(Letter.A) == 0
layout.yOfTopLeft(Letter.C) == 2
layout.yOfTopRight(Letter.A) == 0
layout.yOfTopRight(Letter.C) == 2
layout.yOfTop(Letter.A) == 0
layout.yOfTop(Letter.C) == 2
}

def "let the bottom letter to be in ordinal number distance from the bottom which is twice the ordinal number of the max letter"() {
given:
def layout = Layout.forLastLetterBeing(Letter.D)

expect:
layout.yOfBottom(Letter.D) == 3
layout.yOfBottom(Letter.C) == 4
layout.yOfBottom(Letter.B) == 5
layout.yOfBottom(Letter.A) == 6
}

private static Layout layout() {
Layout.forLastLetterBeing(Letter.C)
}
}

0 comments on commit 5bb73a9

Please sign in to comment.