Algorithm
Development Kit 1.0

algs.model.problems.fifteenpuzzle
Class SlideMove

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

public class SlideMove
extends java.lang.Object
implements IMove

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

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 fromC, int fromR, int toC, int toR)
          Move from (fromC, fromR) -> (toC, toR)
 
Method Summary
 boolean execute(INode staten)
          Execute the move on the given board state.
 boolean isValid(INode staten)
          Determine if move is valid for the given state.
 java.lang.String toString()
          Reasonable implementation.
 boolean undo(INode staten)
          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.


fromC

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


fromR

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


toC

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


toR

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

Constructor Detail

SlideMove

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

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

execute

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

Specified by:
execute in interface IMove
Parameters:
staten - state on which to execute the move.
Throws:
java.lang.IllegalArgumentException - if staten is not a FifteenPuzzleNode

isValid

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

Specified by:
isValid in interface IMove
Parameters:
staten - state on which to execute the move.
Throws:
java.lang.IllegalArgumentException - if staten is not a FifteenPuzzleNode

undo

public boolean undo(INode staten)
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:
staten - state on which to execute the move.
Throws:
java.lang.IllegalArgumentException - if staten is not a FifteenPuzzleNode

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.