Permalink
Browse files

[tdd-diamond] Refactor: generate positions for each letter

  • Loading branch information...
michaelszymczak committed Apr 9, 2017
1 parent 52368fa commit b66b302f6a5b19e96687a31515de3f7867518943
@@ -1,5 +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.*;

@@ -18,46 +20,24 @@ private Diamond(Letter letter) {
public String rendered() {
if (letter == A)
{
Layout layout = Layout.forLastLetterBeing(A);
return new Board(
new PositionedLetter(ofYX(layout.yOfTop(A),layout.xOfLeft(A)), A),
new PositionedLetter(ofYX(layout.yOfTop(A),layout.xOfRight(A)), A),
new PositionedLetter(ofYX(layout.yOfBottom(A),layout.xOfLeft(A)), A),
new PositionedLetter(ofYX(layout.yOfBottom(A),layout.xOfRight(A)), A)
).toString();
return new Board(Layout.forLastLetterBeing(A).positioned(A)).toString();
}
if (letter == B)
{
Layout layout = Layout.forLastLetterBeing(B);
return new Board(
new PositionedLetter(ofYX(layout.yOfTop(A),layout.xOfLeft(A)), A),
new PositionedLetter(ofYX(layout.yOfTop(A),layout.xOfRight(A)), A),
new PositionedLetter(ofYX(layout.yOfBottom(A),layout.xOfLeft(A)), A),
new PositionedLetter(ofYX(layout.yOfBottom(A),layout.xOfRight(A)), A),

new PositionedLetter(ofYX(layout.yOfTop(B),layout.xOfLeft(B)), B),
new PositionedLetter(ofYX(layout.yOfTop(B),layout.xOfRight(B)), B),
new PositionedLetter(ofYX(layout.yOfBottom(B),layout.xOfLeft(B)), B),
new PositionedLetter(ofYX(layout.yOfBottom(B),layout.xOfRight(B)), 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 PositionedLetter(ofYX(layout.yOfTop(A),layout.xOfLeft(A)), A),
new PositionedLetter(ofYX(layout.yOfTop(A),layout.xOfRight(A)), A),
new PositionedLetter(ofYX(layout.yOfBottom(A),layout.xOfLeft(A)), A),
new PositionedLetter(ofYX(layout.yOfBottom(A),layout.xOfRight(A)), A),

new PositionedLetter(ofYX(layout.yOfTop(B),layout.xOfLeft(B)), B),
new PositionedLetter(ofYX(layout.yOfTop(B),layout.xOfRight(B)), B),
new PositionedLetter(ofYX(layout.yOfBottom(B),layout.xOfLeft(B)), B),
new PositionedLetter(ofYX(layout.yOfBottom(B),layout.xOfRight(B)), B),

new PositionedLetter(ofYX(layout.yOfTop(C),layout.xOfLeft(C)), C),
new PositionedLetter(ofYX(layout.yOfTop(C),layout.xOfRight(C)), C),
new PositionedLetter(ofYX(layout.yOfBottom(C),layout.xOfLeft(C)), C),
new PositionedLetter(ofYX(layout.yOfBottom(C),layout.xOfRight(C)), C)
return new Board(new ImmutableSet.Builder<PositionedLetter>()
.addAll(layout.positioned(A))
.addAll(layout.positioned(B))
.addAll(layout.positioned(C))
.build()
).toString();

}
@@ -1,5 +1,12 @@
package com.michaelszymczak.diamond;

import com.google.common.collect.ImmutableSet;

import java.util.Set;

import static com.michaelszymczak.diamond.Coordinates.ofYX;
import static com.michaelszymczak.diamond.Letter.A;

public class Layout {

private final Letter lastLetter;
@@ -28,4 +35,12 @@ public int xOfRight(Letter letter) {
return lastLetter.ordinal() + letter.ordinal();
}

public Set<PositionedLetter> positioned(Letter letter) {
return ImmutableSet.of(
new PositionedLetter(ofYX(yOfTop(letter),xOfLeft(letter)), letter),
new PositionedLetter(ofYX(yOfTop(letter),xOfRight(letter)), letter),
new PositionedLetter(ofYX(yOfBottom(letter),xOfLeft(letter)), letter),
new PositionedLetter(ofYX(yOfBottom(letter),xOfRight(letter)), letter)
);
}
}
@@ -2,6 +2,7 @@ package com.michaelszymczak.diamond

import spock.lang.Specification

import static com.michaelszymczak.diamond.Coordinates.ofYX
import static com.michaelszymczak.diamond.Layout.forLastLetterBeing
import static com.michaelszymczak.diamond.Letter.A
import static com.michaelszymczak.diamond.Letter.B
@@ -53,4 +54,21 @@ class LayoutShould extends Specification {
layout.xOfRight(C) == 5
layout.xOfRight(D) == 6
}

def "generate coordinates for each letter based on last letter"() {
expect:
forLastLetterBeing(A).positioned(A) == [new PositionedLetter(ofYX(0,0), A)] as Set
forLastLetterBeing(B).positioned(A) == [
new PositionedLetter(ofYX(0,1), A),
new PositionedLetter(ofYX(2,1), A)
] as Set
forLastLetterBeing(B).positioned(B) == [
new PositionedLetter(ofYX(1,0), B),
new PositionedLetter(ofYX(1,2), B)
] as Set
forLastLetterBeing(C).positioned(B) == [
new PositionedLetter(ofYX(1,1), B), new PositionedLetter(ofYX(1,3), B),
new PositionedLetter(ofYX(3,1), B), new PositionedLetter(ofYX(3,3), B),
] as Set
}
}

0 comments on commit b66b302

Please sign in to comment.