Move Class
Move class.
Puzzle and
Move interface.
Puzzle interface, you'll need to support the
functions that we've used in our generic puzzle solver routines.
/** A simple puzzle. */
public interface Puzzle {
/** List all the legal moves from the current state. */
public Iterator moves();
/** Make a legal move */
public void makeMove(Move m) throws Exception;
/** Reset the puzzle to its initial state */
public void reset();
/** Check if the puzzle is solved. */
} // Puzzle
Move interface? Well, since
moves are puzzle-specific, perhaps nothing.
makeMove(Move m) in our Puzzle interface.
package coinpuzzle;
...
public class CoinPuzzleMove implements Move {
/** The position of the coin that we're moving. */
int position;
/** The direction in which we're moving it.
int direction;
} // CoinPuzzleMove
CoinPuzzle class
package coinpuzzle; // Permit direct access to the fields of the move
...
public class CoinPuzzle implements Puzzle {
...
public void makeMove(Move m) throws Exception {
CoinPuzzleMove cpm;
// Convert the move
if (m instanceof CoinPuzzleMove) {
cpm = (CoinPuzzleMove) m;
}
else {
throw new Exception("Illegal type of move");
}
// Verify that the move is legal
if (!legal(cpm)) {
throw new Exception("Illegal move");
}
// Make the move
coin = topCoin(cpm.position);
removeTopCoin(cpm.position);
placeCoin(coin,newPosition(cpm.position,cpm.direction));
} // makeMove
...
} // CoinPuzzle
implements instead of extedns).
Stack
class that works on Objects, we can use it for any
subclass of Object.
sort() routine that
works for any list of comparable objects, we can use it for any
comparable objects.
static before the callee?
static!
Integer class, but
doesn't have an associated Integer object.
main() method is associated with the
class as a whole, but not with particular objects in the class.
static keyword.
value(). What value are you requesting?
The value of the last object you used? The value of some random object?
Something completely different.
removeLeast() as efficient as possible.
insert() as efficient as possible.
OrderedVector class.
removeLeast() removes the
first element.
insert() is then an O(n) operation as we may have to
shift all the elements to insert an element in the "correct" place.
removeLeast() is also an O(n) operation, as we need to
shift everything left after the removal.
removeLeast() removes the
last element.
insert() is still an O(n) operation as we may still have
to shift.
removeLeast() becomes an O(1) operation.
min() routine to find the smallest element.
insert() is either O(1) or O(n), depending on whether or
not we have to grow the vector.
removeLeast() is now an O(n) operation.
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 Wed Nov 12 14:26:34 1997.
This page generated on Wed Nov 12 14:28:21 1997 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu