Algorithm
Development Kit 1.0

algs.model.problems.eightpuzzle
Class SlideMove

java.lang.Object
  extended by algs.model.problems.eightpuzzle.SlideMove
All Implemented Interfaces:
IMove

public class SlideMove
extends java.lang.Object
implements IMove

Slide a numbered tile from (r,c) to (r', c').

Since:
1.0
Version:
1.0, 6/15/08
Author:
George Heineman

Field Summary
 int fromC
          column coordinate of the move's source.
 int fromR
          row coordinate of the move's source.
 int tile
          tile being moved.
 int toC
          column coordinate of the move's destination.
 int toR
          row coordinate of the move's destination.
 
Constructor Summary
SlideMove(int tile, int fromR, int fromC, int toR, int toC)
          Move from (fromC, fromR) -> (toC, toR)
 
Method Summary
 boolean execute(INode n)
          Execute the move on the given board state.
 boolean isValid(INode n)
          Determine if move is valid for the given state.
 java.lang.String toString()
          Reasonable implementation.
 boolean undo(INode n)
          Assume move had been valid, so the undo is a straightforward swap.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

tile

public final int tile
tile being moved.


fromR

public final int fromR
row coordinate of the move's source.


fromC

public final int fromC
column coordinate of the move's source.


toR

public final int toR
row coordinate of the move's destination.


toC

public final int toC
column coordinate of the move's destination.

Constructor Detail

SlideMove

public SlideMove(int tile,
                 int fromR,
                 int fromC,
                 int toR,
                 int toC)
Move from (fromC, fromR) -> (toC, toR)

Parameters:
tile - tile to move.
fromR - row coordinate of the move's source.
fromC - column coordinate of the move's source.
toR - row coordinate of the move's destination
toC - column coordinate of the move's destination
Method Detail

execute

public boolean execute(INode n)
Execute the move on the given board state.

Specified by:
execute in interface IMove
Parameters:
n - state on which to execute the move.

isValid

public boolean isValid(INode n)
Determine if move is valid for the given state.

Specified by:
isValid in interface IMove
Parameters:
n -
Throws:
java.lang.IllegalArgumentException - if n is not an EightPuzzleNode

undo

public boolean undo(INode n)
Assume move had been valid, so the undo is a straightforward swap. Note that we simply swap the fromC/fromR and toC/toR in the invocation.

Specified by:
undo in interface IMove
Parameters:
n - game state whose move is to be undone.

toString

public java.lang.String toString()
Reasonable implementation.

Overrides:
toString in class java.lang.Object

Algorithm Development Kit 1.0

This code supports the Algorithms in a Nutshell book, published by O'Reilly Media, Inc. in November 2008. Please visit the book web page to learn of any changes to the code repository or to record a potential defect.