http://www.math.grin.edu/~rebelsky/Courses/397/98S/
Puzzle object that supports
reset() reset the puzzle to its initial state
legalMoves() list the legal moves
makeMove(Move m) make a legal move
// As long as we haven't found a solution // Pick a legal move that doesn't lead to a board we've already seen // Make that move
// As long as we haven't found a solution AND // there is a board state + legal move combination we haven't tried // Pick one of those combinations // Make the move
/**
Find a solution to a puzzle using a brute-force method.
pre: The puzzle has been initialized.
post: The solution is printed.
*/
public void solve(Puzzle p) {
Set checked = new Set(); // The boards we've already checked
MoveList moves_to_make; // The moves from current pos.
Linear todo = new Linear(); // The boards we still need to check
Puzzle current; // The current state of the puzzle
Puzzle next; // The next state of the puzzle
// Start at the beginning
current = p;
current.reset();
checked.add(current);
todo.add(current);
// Keep going until we find a solution (exit within loop) or
// run out of things to try
while (!todo.empty()) {
// Get the next board position and moves used to generate that position
current = todo.remove();
// Is it a solution?
if (current.solved()) {
print "Solved the puzzle. Guess how?"
return;
}
// If not, consider all alternatives
moves_to_make = current.legalMoves();
for each move in moves_to_make { // This is not legal Java
// Make the move
next = current;
next.makeMove(move);
// If we haven't seen the position, add it as something to consider
if (!checked.contains(next)) {
checked.add(next);
todo.add(next);
} # if we haven't seen this position yet
} // for each
} // while
print "The puzzle had no solution.";
} // solve
/**
Find a solution to a puzzle using a brute-force method.
pre: The puzzle has been initialized.
post: The solution is printed.
*/
public void solve(Puzzle p) {
...
MoveList moves_made; // The moves made to reach the current state
Move move; // One move
Pair p; // A board/moves pair,
...
// Start at the beginning
...
todo.add(new Pair(current, new MoveList());
// Keep going until we find a solution (exit within loop) or
// run out of things to try
while (!todo.empty()) {
// Get the next board position and moves used to generate that position
p = todo.remove();
current = p.first();
moves_made = p.second();
// Is it a solution?
if (current.solved()) {
moves_made.printMoves();
return;
}
// If not, consider all alternatives
moves_to_make = current.legalMoves();
for each move in moves_to_make { // This is not legal Java
// Make the move
...
// If we haven't seen the position, add it as something to consider
if (!checked.contains(next)) {
checked.add(next);
todo.add(new Pair(next,new MoveList(move,moves_made)));
} # if we haven't seen this position yet
} // for each
} // while
print "The puzzle had no solution.";
} // solve
for each move in legalMoves strategy, which we would then need
to implement more precisely for each type of puzzle.
/**
Compute the minimum in a group of integers.
pre: The group is nonempty.
post: The group is unaffected.
*/
public int min(Group g) {
int guess; // Current guess as to the minimum
int val; // The next value;
// Get a start guess
guess = g.someElement();
// Compare to each other element, reducing our guess whenever
// possible.
foreach val in g {
if val < guess
guess = val;
}
// We're done
return val;
} // min
Enumerations and Iterators are structures that give
you the elements of some structure, one by one, and thereby support these
types of algorithms. They maintain a state that helps them keep track
of which elements they have and have not returned.
Enumeration is a standard Java interface, defined in
java.util.
Iterator is Bailey's refinement of Enumeration.
hasMoreElements() which returns true if there are more elements
to enumerate.
nextElement() which returns the next element in the list.
/**
Compute the smallest element in an enumeration.
pre: The enumeration is nonempty.
pre: No elements have been removed from the enumeration.
pre: All elements in the enumeration are comparable.
post: The smallest element is returned.
*/
public Object min(Enumeration stuff) {
Object guess; // Current guess as to the minimum
Object val; // The next value in the enumeration
// Get a start guess
guess = stuff.nextElement();
// Compare to each other element, reducing our guess whenever
// possible.
while (stuff.hasMoreElements()) {
val = stuff.nextElement();
if (val.lessthan(guess))
guess = val;
} // while
// We're done
return val;
} // min
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
Source text last modified Mon Nov 3 09:31:50 1997.
This page generated on Wed Nov 5 12:38:27 1997 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu