Write a program of wordSearch puzzle that use the following
Write a program of wordSearch puzzle that use the following text file as an input. The output should be like this: PIXEL found (left) at (0,9). ( Use JAVA Array ) .Please do not use arrylist and the likes! (solve for searchRight() only )
Hints
 • The puzzle can be represented as a right-sized two-dimensional array of characters (char).
 • A String can be converted into a right-sized array of characters via the String method toCharArray.
. • Consider writing separate methods such as:
 private boolean searchRight ( int x, int y, String word ) {
 that look for a word starting from position (x,y) in the puzzle and proceeding
 in the designated direction (right in this case). The method returns true if the
 word is found in the designated direction starting from the indicated position.
text file
18 18
 T P I R C S A V A J L E X I P I G E
 L I A M E M O R Y M M O U S E N I L
 C R A B K S A T X I N U Y H S T F G
 D N D I R E C T O R Y E T A O E O O
 P O W E R S U P P L Y N I R F R L O
 U C O A S A E V A S S C R E T N D G
 K I R O P K T Y P S H R U W W E E L
 C D D E C P R E E A H Y C A A T R M
 A N R I M A L L T D R P E R R E A T
 B O L E N M E I E K E T S E E P H H
 R C K I P R A F C V R I I R S U L M
 E E B E I A R R I A B O O T M B O R
 N S T W R A P R G R T N W B I N G O
 N O O S G N D L O O D I N T I O I S
 A N G M A K A U L A R A O T E A N R
 C A E A S P T L T A I P O N R N D U
 S N F I R E W A L L W R E I K O O C
 T F D P R D H T O O T E U L B Y T E
 JAVASCRIPT
 PIXEL
 INTERNET
 GIF
 GOOGLE
 LCD
 EMAIL
 MEMORY
 MOUSE
 SHAREWARE
 TASKBAR
 UNIX
 SECURITY
 SOFTWARE
 FOLDER
 ICON
 DIRECTORY
Solution
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
// WordSearch class interface: solve word search puzzle
//
// CONSTRUCTION: with no initializer
// ******************PUBLIC OPERATIONS******************
// int solvePuzzle( ) --> Print all words found in the
// puzzle; return number of matches
public class WordSearch
{
/**
* Constructor for WordSearch class.
* Prompts for and reads puzzle and dictionary files.
*/
public WordSearch( ) throws IOException
{
puzzleStream = openFile( \"Enter puzzle file\" );
wordStream = openFile( \"Enter dictionary name\" );
System.out.println( \"Reading files...\" );
readPuzzle( );
readWords( );
}
/**
* Routine to solve the word search puzzle.
* Performs checks in all eight directions.
* @return number of matches
*/
public int solvePuzzle( )
{
int matches = 0;
for( int r = 0; r < rows; r++ )
for( int c = 0; c < columns; c++ )
for( int rd = -1; rd <= 1; rd++ )
for( int cd = -1; cd <= 1; cd++ )
if( rd != 0 || cd != 0 )
matches += solveDirection( r, c, rd, cd );
return matches;
}
/**
* Search the grid from a starting point and direction.
* @return number of matches
*/
private int solveDirection( int baseRow, int baseCol, int rowDelta, int colDelta )
{
String charSequence = \"\";
int numMatches = 0;
int searchResult;
charSequence += theBoard[ baseRow ][ baseCol ];
for( int i = baseRow + rowDelta, j = baseCol + colDelta;
i >= 0 && j >= 0 && i < rows && j < columns;
i += rowDelta, j += colDelta )
{
charSequence += theBoard[ i ][ j ];
searchResult = prefixSearch( theWords, charSequence );
if( searchResult == theWords.length )
break;
if( !((String)theWords[ searchResult ]).startsWith( charSequence ) )
break;
if( theWords[ searchResult ].equals( charSequence ) )
{
numMatches++;
System.out.println( \"Found \" + charSequence + \" at \" +
baseRow + \" \" + baseCol + \" to \" +
i + \" \" + j );
}
}
return numMatches;
}
/**
* Performs the binary search for word search.
* @param a the sorted array of strings.
* @param x the string to search for.
* @return last position examined;
* this position either matches x, or x is
* a prefix of the mismatch, or there is no
* word for which x is a prefix.
*/
private static int prefixSearch( Object [ ] a, String x )
{
int idx = Arrays.binarySearch( a, x );
if( idx < 0 )
return -idx - 1;
else
return idx;
}
/**
* Print a prompt and open a file.
* Retry until open is successful.
* Program exits if end of file is hit.
*/
private BufferedReader openFile( String message )
{
String fileName = \"\";
FileReader theFile;
BufferedReader fileIn = null;
do
{
System.out.println( message + \": \" );
try
{
fileName = in.readLine( );
if( fileName == null )
System.exit( 0 );
theFile = new FileReader( fileName );
fileIn = new BufferedReader( theFile );
}
catch( IOException e )
{ System.err.println( \"Cannot open \" + fileName ); }
} while( fileIn == null );
System.out.println( \"Opened \" + fileName );
return fileIn;
}
/**
* Routine to read the grid.
* Checks to ensure that the grid is rectangular.
* Checks to make sure that capacity is not exceeded is omitted.
*/
private void readPuzzle( ) throws IOException
{
String oneLine;
List puzzleLines = new ArrayList( );
if( ( oneLine = puzzleStream.readLine( ) ) == null )
throw new IOException( \"No lines in puzzle file\" );
columns = oneLine.length( );
puzzleLines.add( oneLine );
while( ( oneLine = puzzleStream.readLine( ) ) != null )
{
if( oneLine.length( ) != columns )
System.err.println( \"Puzzle is not rectangular; skipping row\" );
else
puzzleLines.add( oneLine );
}
rows = puzzleLines.size( );
theBoard = new char[ rows ][ columns ];
Iterator itr = puzzleLines.iterator( );
for( int r = 0; r < rows; r++ )
{
String theLine = (String) itr.next( );
theBoard[ r ] = theLine.toCharArray( );
}
}
/**
* Routine to read the dictionary.
* Error message is printed if dictionary is not sorted.
*/
private void readWords( ) throws IOException
{
List words = new ArrayList( );
String lastWord = null;
String thisWord;
while( ( thisWord = wordStream.readLine( ) ) != null )
{
if( lastWord != null && thisWord.compareTo( lastWord ) < 0 )
{
System.err.println( \"Dictionary is not sorted... skipping\" );
continue;
}
words.add( thisWord );
lastWord = thisWord;
}
theWords = words.toArray( );
}
// Cheap main
public static void main( String [ ] args )
{
WordSearch p = null;
try
{
p = new WordSearch( );
}
catch( IOException e )
{
System.out.println( \"IO Error: \" );
e.printStackTrace( );
return;
}
System.out.println( \"Solving...\" );
p.solvePuzzle( );
}
private int rows;
private int columns;
private char [ ][ ] theBoard;
private Object [ ] theWords;
private BufferedReader puzzleStream;
private BufferedReader wordStream;
private BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
}







