Skip to content

Commit 0332c44

Browse files
committed
EightPuzzle classes polished.
1 parent f88f786 commit 0332c44

File tree

2 files changed

+39
-49
lines changed

2 files changed

+39
-49
lines changed

aima-core/src/main/java/aima/core/environment/eightpuzzle/EightPuzzleBoard.java

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package aima.core.environment.eightpuzzle;
22

33
import java.util.ArrayList;
4+
import java.util.Arrays;
45
import java.util.List;
56

67
import aima.core.agent.Action;
@@ -11,7 +12,7 @@
1112
* @author Ravi Mohan
1213
* @author Ruediger Lunde
1314
*/
14-
public class EightPuzzleBoard {
15+
public class EightPuzzleBoard implements Cloneable {
1516

1617
public static Action LEFT = new DynamicAction("Left");
1718
public static Action RIGHT = new DynamicAction("Right");
@@ -27,13 +28,8 @@ public EightPuzzleBoard() {
2728
state = new int[] { 5, 4, 0, 6, 1, 8, 7, 3, 2 };
2829
}
2930

30-
public EightPuzzleBoard(int[] state) {
31-
this.state = new int[state.length];
32-
System.arraycopy(state, 0, this.state, 0, state.length);
33-
}
34-
35-
public EightPuzzleBoard(EightPuzzleBoard copyBoard) {
36-
this(copyBoard.getState());
31+
public EightPuzzleBoard(int[] state) { state.clone();
32+
this.state = state.clone();
3733
}
3834

3935
public int[] getState() {
@@ -45,8 +41,8 @@ public int getValueAt(XYLocation loc) {
4541
}
4642

4743
public XYLocation getLocationOf(int val) {
48-
int absPos = getPositionOf(val);
49-
return new XYLocation(getXCoord(absPos), getYCoord(absPos));
44+
int pos = getPositionOf(val);
45+
return new XYLocation(getXCoord(pos), getYCoord(pos));
5046
}
5147

5248
public void moveGapRight() {
@@ -93,8 +89,8 @@ public void moveGapUp() {
9389
public List<XYLocation> getPositions() {
9490
ArrayList<XYLocation> result = new ArrayList<>(9);
9591
for (int i = 0; i < 9; i++) {
96-
int absPos = getPositionOf(i);
97-
result.add(new XYLocation(getXCoord(absPos), getYCoord(absPos)));
92+
int pos = getPositionOf(i);
93+
result.add(new XYLocation(getXCoord(pos), getYCoord(pos)));
9894
}
9995
return result;
10096
}
@@ -107,75 +103,69 @@ public void setBoard(List<XYLocation> locs) {
107103

108104
public boolean canMoveGap(Action action) {
109105
boolean result = true;
110-
int absPos = getPositionOf(0);
106+
int pos = getPositionOf(0);
111107
if (action.equals(LEFT))
112-
result = (getXCoord(absPos) != 0);
108+
result = (getXCoord(pos) != 0);
113109
else if (action.equals(RIGHT))
114-
result = (getXCoord(absPos) != 2);
110+
result = (getXCoord(pos) != 2);
115111
else if (action.equals(UP))
116-
result = (getYCoord(absPos) != 0);
112+
result = (getYCoord(pos) != 0);
117113
else if (action.equals(DOWN))
118-
result = (getYCoord(absPos) != 2);
114+
result = (getYCoord(pos) != 2);
119115
return result;
120116
}
121117

122118
@Override
123119
public boolean equals(Object o) {
124120
if (o != null && getClass() == o.getClass()) {
125121
EightPuzzleBoard aBoard = (EightPuzzleBoard) o;
126-
for (int i = 0; i < 9; i++) {
127-
if (state[i] != aBoard.state[i])
128-
return false;
129-
}
130-
return true;
122+
return Arrays.equals(state, aBoard.state);
131123
}
132124
return false;
133125
}
134126

135127
@Override
136128
public int hashCode() {
137-
int result = 17;
138-
for (int i = 0; i < 8; i++) {
139-
int position = this.getPositionOf(i);
140-
result = 37 * result + position;
141-
}
142-
return result;
129+
return Arrays.hashCode(state);
143130
}
144131

145132
@Override
146133
public String toString() {
147134
return state[0] + " " + state[1] + " " + state[2] + "\n"
148-
+ state[3] + " " + state[4] + " " + state[5] + " " + "\n"
135+
+ state[3] + " " + state[4] + " " + state[5] + "\n"
149136
+ state[6] + " " + state[7] + " " + state[8];
150137
}
151138

139+
@Override
140+
public EightPuzzleBoard clone() {
141+
EightPuzzleBoard result = null;
142+
try {
143+
result = (EightPuzzleBoard) super.clone();
144+
result.state = result.state.clone();
145+
} catch (CloneNotSupportedException e) {
146+
e.printStackTrace(); // should never happen...
147+
}
148+
return result;
149+
}
150+
152151
//
153152
// PRIVATE METHODS
154153
//
155154

156-
/**
157-
* Note: The graphic representation maps x values on row numbers (x-axis in
158-
* vertical direction).
159-
*/
160-
private int getXCoord(int absPos) {
161-
return absPos % 3;
155+
private int getXCoord(int pos) {
156+
return pos % 3;
162157
}
163-
164-
/**
165-
* Note: The graphic representation maps y values on column numbers (y-axis
166-
* in horizontal direction).
167-
*/
168-
private int getYCoord(int absPos) {
169-
return absPos / 3;
158+
159+
private int getYCoord(int pos) {
160+
return pos / 3;
170161
}
171162

172-
private int getAbsPosition(int x, int y) {
163+
private int getPosition(int x, int y) {
173164
return x + 3 * y;
174165
}
175166

176167
private int getValueAt(int x, int y) {
177-
// refactor this use either case or a div/mod soln
178-
return state[getAbsPosition(x, y)];
168+
return state[getPosition(x, y)];
179169
}
180170

181171
private int getGapPosition() {
@@ -190,7 +180,7 @@ private int getPositionOf(int val) {
190180
}
191181

192182
private void setValue(int x, int y, int val) {
193-
int absPos = getAbsPosition(x, y);
194-
state[absPos] = val;
183+
int pos = getPosition(x, y);
184+
state[pos] = val;
195185
}
196186
}

aima-core/src/main/java/aima/core/environment/eightpuzzle/EightPuzzleFunctions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public static List<Action> getActions(EightPuzzleBoard state) {
2222
}
2323

2424
public static EightPuzzleBoard getResult(EightPuzzleBoard state, Action action) {
25-
EightPuzzleBoard result = new EightPuzzleBoard(state);
25+
EightPuzzleBoard result = state.clone();
2626

27-
if (state.canMoveGap(action)) {
27+
if (result.canMoveGap(action)) {
2828
if (action == EightPuzzleBoard.UP)
2929
result.moveGapUp();
3030
else if (action == EightPuzzleBoard.DOWN)

0 commit comments

Comments
 (0)