Skip to content

Commit bde115d

Browse files
committed
Breadth First Player
1 parent ee837f6 commit bde115d

File tree

6 files changed

+104
-0
lines changed

6 files changed

+104
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,7 @@ public static ApprovalNamer createApprovalNamer() {
9191
return new JUnitStackTraceNamer();
9292
}
9393

94+
public static void verify(Object o) throws Exception {
95+
Approvals.verify(o + "");
96+
}
9497
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.teachingextensions.logo;
2+
3+
import java.util.*;
4+
5+
/**
6+
* A player who solves puzzles using breadth-first search.
7+
*/
8+
public class BreadthFirstPlayer {
9+
private final Puzzle puzzle;
10+
private final Set<PuzzleState> visited = new HashSet<>();
11+
private final Queue<PuzzleState> frontier = new ArrayDeque<>();
12+
13+
public BreadthFirstPlayer(Puzzle puzzle) {
14+
15+
this.puzzle = puzzle;
16+
}
17+
18+
public PuzzleState solve() {
19+
PuzzleState state = new PuzzleState(this.puzzle);
20+
do {
21+
visited.add(state);
22+
if (!state.isSolution()){
23+
this.search(state);
24+
}
25+
state = frontier.remove();
26+
} while (!state.isSolution());
27+
28+
return state;
29+
}
30+
31+
private void search(PuzzleState state) {
32+
List<PuzzleState> branches = state.getBranches();
33+
for(PuzzleState b : branches){
34+
if (!visited.contains(b)){
35+
frontier.add(b);
36+
}
37+
}
38+
}
39+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ public List<PuzzleState> getBranches() {
6969
return branches;
7070
}
7171

72+
public Iterable<Direction> getHistory() {
73+
return this.history;
74+
}
75+
76+
public Puzzle getPuzzle() {
77+
return this.puzzle;
78+
}
79+
7280
public enum Direction {
7381
Left(-1), Right(1), Up(-3), Down(3);
7482

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.teachingextensions.logo;
2+
3+
import org.junit.Test;
4+
import org.teachingextensions.approvals.lite.Approvals;
5+
import org.teachingextensions.approvals.lite.reporters.UseReporter;
6+
import org.teachingextensions.approvals.lite.reporters.macosx.BeyondCompareReporter;
7+
8+
@UseReporter(BeyondCompareReporter.class)
9+
public class BreadthFirstPlayerTest {
10+
/**
11+
* Produces a puzzle solution
12+
*/
13+
@Test
14+
public void solve_puzzle() throws Exception {
15+
int[] cells = {0, 1, 2, 3, 4, 5, 6, 8, 7};
16+
Puzzle p = new Puzzle(cells);
17+
BreadthFirstPlayer b = new BreadthFirstPlayer(p);
18+
PuzzleState s = b.solve();
19+
Approvals.verifyAll("Solve " + p, s.getHistory());
20+
}
21+
22+
/**
23+
* Solve longer puzzle
24+
*/
25+
@Test
26+
public void solve_longer_puzzle() throws Exception {
27+
int[] cells = {0, 1, 2, 3, 4, 8, 5, 6, 7};
28+
Puzzle p = new Puzzle(cells);
29+
BreadthFirstPlayer b = new BreadthFirstPlayer(p);
30+
31+
PuzzleState s = b.solve();
32+
Approvals.verifyAll("From " + p + " to " + s.getPuzzle(), s.getHistory());
33+
}
34+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
From [0, 1, 2, 3, 4, 8, 5, 6, 7] to [0, 1, 2, 3, 4, 5, 6, 7, 8]
2+
3+
4+
{Down = 3}
5+
{Left = -1}
6+
{Left = -1}
7+
{Up = -3}
8+
{Right = 1}
9+
{Down = 3}
10+
{Right = 1}
11+
{Up = -3}
12+
{Left = -1}
13+
{Left = -1}
14+
{Down = 3}
15+
{Right = 1}
16+
{Right = 1}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Solve [0, 1, 2, 3, 4, 5, 6, 8, 7]
2+
3+
4+
{Right = 1}

0 commit comments

Comments
 (0)