Skip to content

Commit ee837f6

Browse files
committed
Branch generation
1 parent 7264159 commit ee837f6

12 files changed

+238
-89
lines changed

src/main/java/org/teachingextensions/approvals/lite/Approvals.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@ public static void verify(String response) throws Exception {
1919
}
2020

2121
public static <T> void verifyAll(String header, T[] values) {
22-
Approvals.verifyAll(header, Arrays.asList(values), new Function1<T, String>() {
22+
Approvals.verifyAll(header, Arrays.asList(values));
23+
}
24+
25+
public static <T> void verifyAll(String header, Iterable<T> values) {
26+
Approvals.verifyAll(header, values, new Function1<T, String>() {
2327
@Override
2428
public String call(T i) {
2529
return i + "";
2630
}
2731
});
32+
2833
}
2934

3035
public static <T> void verifyAll(String header, T[] values,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.teachingextensions.approvals.lite.reporters.macosx;
2+
3+
import org.teachingextensions.approvals.lite.reporters.GenericDiffReporter;
4+
5+
import java.text.MessageFormat;
6+
7+
public class BeyondCompareReporter extends GenericDiffReporter {
8+
public static final BeyondCompareReporter INSTANCE = new BeyondCompareReporter();
9+
static final String DIFF_PROGRAM = "/usr/local/bin/bcompare";
10+
static final String MESSAGE = MessageFormat.format(
11+
"Unable to find Beyond Compare at {0}", DIFF_PROGRAM);
12+
13+
public BeyondCompareReporter() {
14+
super(DIFF_PROGRAM, MESSAGE);
15+
}
16+
}

src/main/java/org/teachingextensions/approvals/lite/reporters/macosx/DiffMergeReporter.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import java.text.MessageFormat;
66

77
public class DiffMergeReporter extends GenericDiffReporter {
8-
private static final String DIFF_PROGRAM = "/Applications/DiffMerge.app/Contents/MacOS/DiffMerge";
9-
static final String MESSAGE = MessageFormat.format("Unable to find DiffMerge at {0}",
10-
DIFF_PROGRAM);
11-
public static final DiffMergeReporter INSTANCE = new DiffMergeReporter();
8+
public static final DiffMergeReporter INSTANCE = new DiffMergeReporter();
9+
private static final String DIFF_PROGRAM = "/Applications/DiffMerge.app/Contents/MacOS/DiffMerge";
10+
static final String MESSAGE = MessageFormat.format("Unable to find DiffMerge at {0}",
11+
DIFF_PROGRAM);
1212

13-
public DiffMergeReporter() {
14-
super(DIFF_PROGRAM, GenericDiffReporter.STANDARD_ARGUMENTS, MESSAGE, GenericDiffReporter.TEXT_FILE_EXTENSIONS);
15-
}
13+
public DiffMergeReporter() {
14+
super(DIFF_PROGRAM, GenericDiffReporter.STANDARD_ARGUMENTS, MESSAGE, GenericDiffReporter.TEXT_FILE_EXTENSIONS);
15+
}
1616
}

src/main/java/org/teachingextensions/logo/Puzzle.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,9 @@ public Puzzle swapBlank(int target) {
4646
copy[target] = 8;
4747
return new Puzzle(copy);
4848
}
49+
50+
@Override
51+
public String toString() {
52+
return Arrays.toString(cells);
53+
}
4954
}

src/main/java/org/teachingextensions/logo/PuzzleState.java

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,80 @@
11
package org.teachingextensions.logo;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Stack;
6+
37
/**
48
* Represents a node in the puzzle-solving graph. Keeps track of the current puzzle arrangement and the actions
59
* required to arrive at the current arrangement from the starting arrangement.
610
*/
711
public class PuzzleState {
8-
public enum Directions {
12+
private final Puzzle puzzle;
13+
private final Stack<Direction> history;
14+
15+
public PuzzleState(Puzzle puzzle) {
16+
this(puzzle, new Stack<Direction>());
17+
}
18+
19+
public PuzzleState(Puzzle puzzle, Stack<Direction> history) {
20+
this.puzzle = puzzle;
21+
this.history = history;
22+
}
23+
24+
public boolean isSolution() {
25+
return puzzle.isSolved();
26+
}
27+
28+
@Override
29+
public String toString() {
30+
StringBuilder b = new StringBuilder();
31+
if (!history.isEmpty()) {
32+
b.append(history.peek());
33+
b.append(" to ");
34+
}
35+
36+
b.append(puzzle);
37+
return b.toString();
38+
}
39+
40+
41+
public List<PuzzleState> getBranches() {
42+
List<PuzzleState> branches = new ArrayList<>(4);
43+
int blank = puzzle.getBlankIndex();
44+
int x = blank % 3;
45+
int y = blank / 3;
46+
for (Direction d : Direction.values()) {
47+
if (d == Direction.Left && x == 0) {
48+
continue;
49+
}
50+
51+
if (d == Direction.Right && x == 2) {
52+
continue;
53+
}
54+
55+
if (d == Direction.Up && y == 0) {
56+
continue;
57+
}
58+
59+
if (d == Direction.Down && y == 2) {
60+
continue;
61+
}
62+
63+
Stack<Direction> h = new Stack<>();
64+
h.addAll(history);
65+
h.push(d);
66+
branches.add(new PuzzleState(puzzle.swapBlank(blank + d.getValue()), h));
67+
}
68+
69+
return branches;
70+
}
71+
72+
public enum Direction {
973
Left(-1), Right(1), Up(-3), Down(3);
1074

1175
private final int value;
1276

13-
private Directions(int i) {
77+
private Direction(int i) {
1478
this.value = i;
1579
}
1680

src/test/java/org/teachingextensions/logo/PuzzleStateTest.java

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,76 @@
22

33
import org.junit.Test;
44
import org.teachingextensions.approvals.lite.Approvals;
5-
import org.teachingextensions.approvals.lite.reporters.FileLauncherReporter;
65
import org.teachingextensions.approvals.lite.reporters.UseReporter;
6+
import org.teachingextensions.approvals.lite.reporters.macosx.BeyondCompareReporter;
77

8-
@UseReporter(FileLauncherReporter.class)
8+
import static org.junit.Assert.assertTrue;
9+
10+
@UseReporter(BeyondCompareReporter.class)
911
public class PuzzleStateTest {
1012

13+
private static Puzzle getSolvedPuzzle() {
14+
int[] solved = {0, 1, 2, 3, 4, 5, 6, 7, 8};
15+
return new Puzzle(solved);
16+
}
17+
18+
private static Puzzle getPuzzle(int blank) {
19+
return getSolvedPuzzle().swapBlank(blank);
20+
}
21+
1122
/**
1223
* The state can tell you what moves are allowed from cell to cell
1324
*/
1425
@Test
15-
public void state_has_move_directions(){
16-
StringBuilder b = new StringBuilder();
17-
Approvals.verifyAll("Directions", PuzzleState.Directions.values());
26+
public void state_has_move_directions() {
27+
Approvals.verifyAll("Directions", PuzzleState.Direction.values());
28+
}
29+
30+
/**
31+
* The state can tell you if it is a solution to the puzzle
32+
*/
33+
@Test
34+
public void state_can_be_solution() {
35+
Puzzle p = getSolvedPuzzle();
36+
PuzzleState s = new PuzzleState(p);
37+
assertTrue(s.isSolution());
38+
}
39+
40+
/**
41+
* The state can tell you all possible next moves for its puzzle.
42+
*/
43+
@Test
44+
public void tells_you_next_moves() {
45+
PuzzleState s = new PuzzleState(getSolvedPuzzle());
46+
Approvals.verifyAll("Branches for " + s, s.getBranches());
47+
}
48+
49+
/**
50+
* Or we can see the next moves from the top right corner
51+
*/
52+
@Test
53+
public void moves_from_top_right() {
54+
Puzzle p = getPuzzle(2);
55+
PuzzleState s = new PuzzleState(p);
56+
Approvals.verifyAll("Branches for " + s, s.getBranches());
57+
}
58+
59+
/**
60+
* Or we can see the next moves from the center
61+
*/
62+
@Test
63+
public void moves_from_center(){
64+
Puzzle p = getPuzzle(4);
65+
PuzzleState s = new PuzzleState(p);
66+
Approvals.verifyAll("Branches for " + s, s.getBranches());
67+
}
68+
69+
/**
70+
* Finally, lets see what moves we can do from top center.
71+
*/
72+
@Test
73+
public void moves_from_top_center(){
74+
PuzzleState s = new PuzzleState(getPuzzle(1));
75+
Approvals.verifyAll("Branches for " + s, s.getBranches());
1876
}
1977
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Branches for [0, 1, 2, 3, 8, 5, 6, 7, 4]
2+
3+
4+
{Left = -1} to [0, 1, 2, 8, 3, 5, 6, 7, 4]
5+
{Right = 1} to [0, 1, 2, 3, 5, 8, 6, 7, 4]
6+
{Up = -3} to [0, 8, 2, 3, 1, 5, 6, 7, 4]
7+
{Down = 3} to [0, 1, 2, 3, 7, 5, 6, 8, 4]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Branches for [0, 8, 2, 3, 4, 5, 6, 7, 1]
2+
3+
4+
{Left = -1} to [8, 0, 2, 3, 4, 5, 6, 7, 1]
5+
{Right = 1} to [0, 2, 8, 3, 4, 5, 6, 7, 1]
6+
{Down = 3} to [0, 4, 2, 3, 8, 5, 6, 7, 1]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Branches for [0, 1, 8, 3, 4, 5, 6, 7, 2]
2+
3+
4+
{Left = -1} to [0, 8, 1, 3, 4, 5, 6, 7, 2]
5+
{Down = 3} to [0, 1, 5, 3, 4, 8, 6, 7, 2]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Branches for [0, 1, 2, 3, 4, 5, 6, 7, 8]
2+
3+
4+
{Left = -1} to [0, 1, 2, 3, 4, 5, 6, 8, 7]
5+
{Up = -3} to [0, 1, 2, 3, 4, 8, 6, 7, 5]

0 commit comments

Comments
 (0)