Permalink
Browse files

[tdd-diamond] Done: generate every diamong based on the last letter

  • Loading branch information...
michaelszymczak committed Apr 9, 2017
1 parent b66b302 commit 169792fb3a003f46f991007097d22b4aebf79e71
@@ -1,9 +1,7 @@
package com.michaelszymczak.diamond;

import com.google.common.collect.ImmutableSet;

import static com.michaelszymczak.diamond.Coordinates.ofYX;
import static com.michaelszymczak.diamond.Letter.*;
import java.util.List;
import java.util.stream.Collectors;

public class Diamond {

@@ -18,27 +16,14 @@ private Diamond(Letter letter) {
}

public String rendered() {
if (letter == A)
{
return new Board(Layout.forLastLetterBeing(A).positioned(A)).toString();
}
if (letter == B)
{
Layout layout = Layout.forLastLetterBeing(B);
return new Board(new ImmutableSet.Builder<PositionedLetter>()
.addAll(layout.positioned(A))
.addAll(layout.positioned(B))
.build()
).toString();
}

Layout layout = Layout.forLastLetterBeing(C);
return new Board(new ImmutableSet.Builder<PositionedLetter>()
.addAll(layout.positioned(A))
.addAll(layout.positioned(B))
.addAll(layout.positioned(C))
.build()
).toString();
return new Board(allPositionedLetters()).toString();
}

private List<PositionedLetter> allPositionedLetters() {
final Layout layout = Layout.forLastLetterBeing(letter);

return letter.inclusiveSequence().stream()
.flatMap(l -> layout.positioned(l).stream())
.collect(Collectors.toList());
}
}
@@ -1,5 +1,16 @@
package com.michaelszymczak.diamond;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public enum Letter {
A, B, C, D
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;

public List<Letter> inclusiveSequence() {
return Stream.of(values())
.filter(l -> l.ordinal() <= ordinal())
.sorted((l1, l2) -> Integer.compare(l1.ordinal(), l2.ordinal()))
.collect(Collectors.toList());
}
}
@@ -27,5 +27,59 @@ class DiamondAcceptanceTest extends Specification {
" A "
}


}
def "uses all letters if possible"() {
expect:
Diamond.of(Letter.Z).rendered() == "" +
" A " + "\n" +
" B B " + "\n" +
" C C " + "\n" +
" D D " + "\n" +
" E E " + "\n" +
" F F " + "\n" +
" G G " + "\n" +
" H H " + "\n" +
" I I " + "\n" +
" J J " + "\n" +
" K K " + "\n" +
" L L " + "\n" +
" M M " + "\n" +
" N N " + "\n" +
" O O " + "\n" +
" P P " + "\n" +
" Q Q " + "\n" +
" R R " + "\n" +
" S S " + "\n" +
" T T " + "\n" +
" U U " + "\n" +
" V V " + "\n" +
" W W " + "\n" +
" X X " + "\n" +
" Y Y " + "\n" +
"Z Z" + "\n" +
" Y Y " + "\n" +
" X X " + "\n" +
" W W " + "\n" +
" V V " + "\n" +
" U U " + "\n" +
" T T " + "\n" +
" S S " + "\n" +
" R R " + "\n" +
" Q Q " + "\n" +
" P P " + "\n" +
" O O " + "\n" +
" N N " + "\n" +
" M M " + "\n" +
" L L " + "\n" +
" K K " + "\n" +
" J J " + "\n" +
" I I " + "\n" +
" H H " + "\n" +
" G G " + "\n" +
" F F " + "\n" +
" E E " + "\n" +
" D D " + "\n" +
" C C " + "\n" +
" B B " + "\n" +
" A "
}
}
@@ -2,13 +2,22 @@ package com.michaelszymczak.diamond

import spock.lang.Specification

import static com.michaelszymczak.diamond.Letter.*

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
A.ordinal() == 0
B.ordinal() == 1
C.ordinal() == 2
D.ordinal() == 3
}

def "return all letters up to the specified one"() {
expect:
A.inclusiveSequence() == [A]
B.inclusiveSequence() == [A,B]
D.inclusiveSequence() == [A,B,C,D]
}
}

0 comments on commit 169792f

Please sign in to comment.