Permalink
Browse files

[tdd-diamond] Calculate top left and right row of each letter

  • Loading branch information...
michaelszymczak committed Apr 9, 2017
1 parent ab42e6d commit 43527243bb5fcc2767c660a5d3ec0a1d37af008d
@@ -16,23 +16,24 @@ private Diamond(Letter letter) {
}

public String rendered() {
final Layout layout = new Layout();
if (letter == A)
{
return new Board(new PositionedLetter(ofYX(0,0), A)).toString();
return new Board(new PositionedLetter(ofYX(layout.yOfTopLeft(A),0), A)).toString();
}
if (letter == B)
{
return new Board(
new PositionedLetter(ofYX(0,1), A),
new PositionedLetter(ofYX(1,0), B), new PositionedLetter(ofYX(1,2), B),
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)
).toString();
}

return new Board(
new PositionedLetter(ofYX(0,2), A),
new PositionedLetter(ofYX(1,1), B), new PositionedLetter(ofYX(1,3), B),
new PositionedLetter(ofYX(2,0), C), new PositionedLetter(ofYX(2,4), C),
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)
).toString();
@@ -0,0 +1,13 @@
package com.michaelszymczak.diamond;

public class Layout {


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

public int yOfTopRight(Letter letter) {
return letter.ordinal();
}
}
@@ -0,0 +1,16 @@
package com.michaelszymczak.diamond

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()

expect:
layout.yOfTopLeft(Letter.A) == 0
layout.yOfTopLeft(Letter.C) == 2
layout.yOfTopRight(Letter.A) == 0
layout.yOfTopRight(Letter.C) == 2
}
}
@@ -0,0 +1,14 @@
package com.michaelszymczak.diamond

import spock.lang.Specification

class LetterShould extends Specification {

def "have position in the alphabet as its ordinal number"() {
expect:
Letter.A.ordinal() == 0
Letter.B.ordinal() == 1
Letter.C.ordinal() == 2
Letter.D.ordinal() == 3
}
}

0 comments on commit 4352724

Please sign in to comment.