Skip to content

Commit 3d7912c

Browse files
committed
Equality checks
1 parent 0109211 commit 3d7912c

File tree

7 files changed

+138
-18
lines changed

7 files changed

+138
-18
lines changed

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,27 @@ public Puzzle(int[] cells) {
1616
this.cells = cells;
1717
}
1818

19+
@Override
20+
public int hashCode() {
21+
return Arrays.hashCode(cells);
22+
}
23+
24+
@Override
25+
public boolean equals(Object o) {
26+
if (this == o) return true;
27+
if (o == null || getClass() != o.getClass()) return false;
28+
29+
Puzzle puzzle = (Puzzle) o;
30+
31+
return Arrays.equals(cells, puzzle.cells);
32+
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return Arrays.toString(cells);
38+
}
39+
1940
public boolean isSolved() {
2041
for (int i = 0; i < solution.length; i++) {
2142
if (solution[i] != cells[i]) {
@@ -36,7 +57,9 @@ public int getBlankIndex() {
3657

3758
/**
3859
* Create a copy of the puzzle where the blank swapped with the value in the target position
39-
* @param target move the blank to this location, and move the value from this location to the current blank location
60+
*
61+
* @param target
62+
* move the blank to this location, and move the value from this location to the current blank location
4063
* @return A copy of the puzzle with the blank and target swapped.
4164
*/
4265
public Puzzle swapBlank(int target) {
@@ -46,9 +69,4 @@ public Puzzle swapBlank(int target) {
4669
copy[target] = 8;
4770
return new Puzzle(copy);
4871
}
49-
50-
@Override
51-
public String toString() {
52-
return Arrays.toString(cells);
53-
}
5472
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ public PuzzleState solve() {
3737
if (!state.isSolution()) {
3838
this.search(state);
3939
state = getFrontier().remove();
40-
}else{
41-
continue;
4240
}
4341
} while (!state.isSolution());
4442

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,26 @@ public int compare(PuzzleState o1, PuzzleState o2) {
8888
}
8989

9090
@Override
91-
public int compareTo(PuzzleState o) {
91+
public int compareTo( PuzzleState o) {
9292
return compare(this, o);
9393
}
9494

95+
@Override
96+
public boolean equals(Object o) {
97+
if (this == o) return true;
98+
if (o == null || getClass() != o.getClass()) return false;
99+
100+
PuzzleState that = (PuzzleState) o;
101+
102+
return puzzle.equals(that.puzzle);
103+
104+
}
105+
106+
@Override
107+
public int hashCode() {
108+
return puzzle.hashCode();
109+
}
110+
95111
public enum Direction {
96112
Left(-1), Right(1), Up(-3), Down(3);
97113

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.teachingextensions.logo;
22

33
import org.junit.Test;
4+
import org.teachingextensions.approvals.lite.Approvals;
45
import org.teachingextensions.approvals.lite.reporters.UseReporter;
56
import org.teachingextensions.approvals.lite.reporters.macosx.BeyondCompareReporter;
67

@@ -27,4 +28,17 @@ public void solve_puzzle() throws Exception {
2728
public void solve_longer_puzzle() throws Exception {
2829
verifyLongSolution();
2930
}
31+
32+
/**
33+
* Solve jumbo puzzle
34+
*/
35+
@Test
36+
public void solve_jumbo_puzzle() throws Exception {
37+
int[] cells = {8, 0, 1, 2, 3, 4, 5, 6, 7};
38+
Puzzle p = new Puzzle(cells);
39+
PuzzlePlayer b = getPlayer(p);
40+
41+
PuzzleState s = b.solve();
42+
Approvals.verifyAll("From " + p + " to " + s.getPuzzle(), s.getHistory());
43+
}
3044
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
From [8, 0, 1, 2, 3, 4, 5, 6, 7] to [0, 1, 2, 3, 4, 5, 6, 7, 8]
2+
3+
4+
{Right = 1}
5+
{Down = 3}
6+
{Left = -1}
7+
{Down = 3}
8+
{Right = 1}
9+
{Right = 1}
10+
{Up = -3}
11+
{Left = -1}
12+
{Left = -1}
13+
{Down = 3}
14+
{Right = 1}
15+
{Up = -3}
16+
{Up = -3}
17+
{Right = 1}
18+
{Down = 3}
19+
{Down = 3}
20+
{Left = -1}
21+
{Left = -1}
22+
{Up = -3}
23+
{Right = 1}
24+
{Right = 1}
25+
{Down = 3}

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
import java.util.Stack;
99

10-
import static org.junit.Assert.assertEquals;
11-
import static org.junit.Assert.assertTrue;
10+
import static org.junit.Assert.*;
1211

1312
@UseReporter(BeyondCompareReporter.class)
1413
public class PuzzleStateTest {
@@ -98,4 +97,36 @@ public void actual_cost_is_actual_steps() {
9897
PuzzleState s = new PuzzleState(getSolvedPuzzle(), history);
9998
assertEquals(2, s.getActualCost());
10099
}
100+
101+
/**
102+
* Is not equal to another state when the puzzles are different
103+
*/
104+
@Test
105+
public void not_equal_to_state_with_different_puzzle() {
106+
PuzzleState a = new PuzzleState(getSolvedPuzzle());
107+
PuzzleState b = new PuzzleState(getPuzzle(2));
108+
assertNotEquals(a, b);
109+
}
110+
111+
/**
112+
* Is equal to another state when the puzzles are the same
113+
*/
114+
@Test
115+
public void equal_to_state_with_same_puzzle() {
116+
PuzzleState a = new PuzzleState(getPuzzle(1));
117+
PuzzleState b = new PuzzleState(getPuzzle(1));
118+
assertTrue(a.equals(b));
119+
}
120+
121+
/**
122+
* Is equal to state with same puzzle even when history differs
123+
*/
124+
@Test
125+
public void equal_even_with_different_history() {
126+
Stack<PuzzleState.Direction> history = new Stack<>();
127+
history.add(PuzzleState.Direction.Right);
128+
PuzzleState a = new PuzzleState(getPuzzle(3), history);
129+
PuzzleState b = new PuzzleState(getPuzzle(3));
130+
assertEquals(a, b);
131+
}
101132
}

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
import java.util.Arrays;
66

7-
import static org.junit.Assert.assertEquals;
8-
import static org.junit.Assert.assertFalse;
9-
import static org.junit.Assert.assertTrue;
7+
import static org.junit.Assert.*;
108

119
public class PuzzleTest {
1210
private static int[] getSolution() {
@@ -46,7 +44,7 @@ public void puzzle_not_solved() {
4644
* A puzzle has a blank
4745
*/
4846
@Test
49-
public void puzzle_has_blank(){
47+
public void puzzle_has_blank() {
5048
Puzzle p = new Puzzle(getSolution());
5149
assertEquals(8, p.getBlankIndex());
5250
}
@@ -55,7 +53,7 @@ public void puzzle_has_blank(){
5553
* Blank can be anywhere
5654
*/
5755
@Test
58-
public void blank_can_be_anywhere(){
56+
public void blank_can_be_anywhere() {
5957
int[] cells = swap(getSolution(), 8, 2);
6058
Puzzle p = new Puzzle(cells);
6159
assertEquals(2, p.getBlankIndex());
@@ -65,7 +63,7 @@ public void blank_can_be_anywhere(){
6563
* Puzzle can swap blank for you
6664
*/
6765
@Test
68-
public void puzzle_can_swap_blank(){
66+
public void puzzle_can_swap_blank() {
6967
Puzzle p = new Puzzle(getSolution());
7068
Puzzle c = p.swapBlank(4);
7169
assertEquals(4, c.getBlankIndex());
@@ -75,10 +73,30 @@ public void puzzle_can_swap_blank(){
7573
* Puzzle can swap blank for you
7674
*/
7775
@Test
78-
public void blank_swap_leaves_original_as_is(){
76+
public void blank_swap_leaves_original_as_is() {
7977
Puzzle p = new Puzzle(getSolution());
8078
p.swapBlank(4);
8179
assertEquals(8, p.getBlankIndex());
8280
}
8381

82+
/**
83+
* Puzzle is not equal to a puzzle with different cells.
84+
*/
85+
@Test
86+
public void different_cells_means_different_puzzle() {
87+
Puzzle a = new Puzzle(getSolution());
88+
Puzzle b = new Puzzle(swap(getSolution(), 8, 2));
89+
assertNotEquals(a, b);
90+
}
91+
92+
/**
93+
* Puzzle is equal to a puzzle with the same cells.
94+
*/
95+
@Test
96+
public void same_cells_means_same_puzzle() {
97+
Puzzle a = new Puzzle(swap(getSolution(), 8, 3));
98+
Puzzle b = new Puzzle(swap(getSolution(), 8, 3));
99+
assertEquals(a, b);
100+
}
101+
84102
}

0 commit comments

Comments
 (0)