Class EightPuzzle

java.lang.Object
  extended by EightPuzzle

public class EightPuzzle
extends java.lang.Object


Constructor Summary
EightPuzzle()
          Constructor for EightPuzzle.
 
Method Summary
 void addAMove(int row, int column)
          Moves the number at the given row and column to the current empty row and column.
 void displayBoard()
          Displays the board in this form (for example):
 boolean empty(int row, int column)
          Returns: true, if the square at row, column is empty.
 boolean gameWon()
          Returns: True if there has been a win.
 int getMoves()
          Returns: the number of moves.
 void help()
          Provides "on-line" help to the user.
 void initBoard()
          Initializes the board to numbers descending from 8 down to 1 in this configuration:
 boolean legalMove(int row, int column)
          Returns: true, if there is an adjacent square that is empty.
 int newColumn()
          Returns: The current empty column (the "new" column that a piece would move to).
 int newRow()
          Returns: The current empty row (the "new" row that a piece would move to).
 java.lang.String numberAt(int row, int column)
          Returns: the number at the row and column, as a string.
 void resetGame()
          This re-initializes the board, and resets any necessary instance variables.
 int toInt(char ch)
          Returns: The integer version of the character digit.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EightPuzzle

public EightPuzzle()
Constructor for EightPuzzle. Creates and initializes the board.

Method Detail

addAMove

public void addAMove(int row,
                     int column)
Moves the number at the given row and column to the current empty row and column. It does nothing if the current empty row and column is not adjacent to the given square (i.e., if the intended move is not a legal one).


displayBoard

public void displayBoard()
Displays the board in this form (for example):
 The current board:
  8 | 7 | 6
  ----------
  5 | 1 | 4
  ----------
  2 |   | 3
 


empty

public boolean empty(int row,
                     int column)
Returns: true, if the square at row, column is empty.


gameWon

public boolean gameWon()
Returns: True if there has been a win.
Precondition: The board has been initialized.


getMoves

public int getMoves()
Returns: the number of moves.


help

public void help()
Provides "on-line" help to the user.
E.g., like this:
 After the prompt "eightpuzzle>":
 Enter m directly followed by two digits (each 0, 1, or 2) to move.
 Enter q to quit.
 Enter n to start a new game.
 Enter h for help.
   


initBoard

public void initBoard()
Initializes the board to numbers descending from 8 down to 1 in this configuration:
   8    7    6
   5    4    3
   2    1
 

Comment: This really should be a private helper method.


legalMove

public boolean legalMove(int row,
                         int column)
Returns: true, if there is an adjacent square that is empty.


newColumn

public int newColumn()
Returns: The current empty column (the "new" column that a piece would move to).
Comment: Useful for the GUI.


newRow

public int newRow()
Returns: The current empty row (the "new" row that a piece would move to).
Comment: Useful for the GUI.


numberAt

public java.lang.String numberAt(int row,
                                 int column)
Returns: the number at the row and column, as a string. Empty string if the square is empty.
Comment: Useful in the GUI part.


resetGame

public void resetGame()
This re-initializes the board, and resets any necessary instance variables.


toInt

public int toInt(char ch)
Returns: The integer version of the character digit.
Precondition: ch is a character digit, like '6'.
Comment: Useful in gameWon() and in the driver program.