cmsc433.p3
Class Grid

java.lang.Object
  extended by cmsc433.p3.Grid

public class Grid
extends java.lang.Object

The Grid class is a matrix of letters which are intended to form words. Words are constructed as paths through the matrix. A solution to a grid is simply a list of (nonoverlapping) paths that correspond to words in a dictionary. The score of a solution depends on the length of the word, and the frequency of words of that length in the given dictionary. This class is immutable, and so is thread-safe.


Method Summary
 void display(Solution sol)
          This method displays the grid using the Swing GUI library, along with a depiction of the given solution.
 char get(int row, int col)
          Provides the character at the given row,col coordinates
 java.lang.String getWord(Path path)
          Returns the word corresponding to the given path.
 java.awt.Color intToColor(int id)
           
static Grid makeGrid(java.lang.String file)
          This factory method constructs a Grid object from the contents of the given file.
 int numCols()
          Provides the number of columns in this grid
 int numRows()
          Provides the number of rows in this grid.
 int score(Dictionary dict, Solution sol)
          Computes the score of the given Solution.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

makeGrid

public static Grid makeGrid(java.lang.String file)
This factory method constructs a Grid object from the contents of the given file. The file is expected to formatted as a series of rows of characters; each row in the file corresponds to a row in the grid.

Parameters:
file - - a text file containing (equal-length) rows of characters
Returns:
a Grid object representing the contents of the file

numRows

public int numRows()
Provides the number of rows in this grid.

Returns:
the number of rows in the grid

numCols

public int numCols()
Provides the number of columns in this grid

Returns:
the number of columns in the grid

get

public char get(int row,
                int col)
Provides the character at the given row,col coordinates

Parameters:
row - - the row of the character
col - - the column of the character
Returns:
the character at (row,col)

getWord

public java.lang.String getWord(Path path)
Returns the word corresponding to the given path. Assumes that the path consists of adjacent coordinates (must be checked in caller).

Parameters:
path - - a Path object
Returns:
the word that corresponds to that path in this grid

score

public int score(Dictionary dict,
                 Solution sol)
Computes the score of the given Solution. It does this by extracting each path from the solution and ensuring that (a) it is a legal path (i.e., all points are adjacent), (b) that it does not overlap with any other paths already processed, and (c) that it corresponds to an actual word in the dictionary. If these conditions are met, the word is scored according the dictionary's score() method and added to the total score.

Parameters:
dict - - the dictionary of words to use for scoring
sol - - the solution to score
Returns:
the integer score value

display

public void display(Solution sol)
This method displays the grid using the Swing GUI library, along with a depiction of the given solution.

Parameters:
sol -

intToColor

public java.awt.Color intToColor(int id)