5 * $Id: Puzzle.java,v 1.2 2008/04/11 17:53:32 rmh3093 Exp rmh3093 $
8 * $Log: Puzzle.java,v $
9 * Revision 1.2 2008/04/11 17:53:32 rmh3093
12 * Revision 1.1 2008/03/31 03:59:18 rmh3093
19 import java
.io
.FileNotFoundException
;
20 import java
.io
.IOException
;
21 import java
.util
.NoSuchElementException
;
22 import java
.util
.Scanner
;
26 * This class represents word search puzzles.
32 private char[][] puzzleGrid
;
35 * Load a puzzle from a puzzle file. If any errors occur during the
36 * processing of the puzzle file, the appropriate exceptions will be thrown.
38 * @param puzzleFile the name of the file containing the puzzle to load.
40 public Puzzle(String puzzleFile
) throws PuzzleFileFormatException
,
41 IOException
, FileNotFoundException
{
42 setPuzzle(puzzleFile
);
46 * Replace the puzzle currently stored in this object with the puzzle
47 * contained in the given file. If any error occurs during the processing of
48 * the puzzle file, the puzzle will be set to null and the appropriate
49 * exception will be thrown.
52 * @throws java.io.FileNotFoundException
53 * @throws java.io.IOException
54 * @throws PuzzleFileFormatException
56 public void setPuzzle(String puzzleFile
) throws FileNotFoundException
,
57 IOException
, PuzzleFileFormatException
, NoSuchElementException
,
58 ArrayIndexOutOfBoundsException
{
60 // Throw FileNotFoundException if puzzle file does not exist
61 File pz
= new File(puzzleFile
);
63 //throw new PuzzleFileFormatException("WordSearch: cannot open " + puzzleFile);
67 Scanner input
= new Scanner(pz
);
69 int rows
= Integer
.parseInt(input
.nextLine());
70 int cols
= Integer
.parseInt(input
.nextLine());
71 if (!input
.hasNext()) {
73 throw new PuzzleFileFormatException("WordSearch: invalid puzzle file");
75 puzzleGrid
= new char[rows
][cols
];
76 for (int i
=0; i
<rows
; i
++) {
77 char[] row
= input
.nextLine().toCharArray();
78 for (int j
=0; j
<cols
; j
++) {
79 if (Character
.isLetterOrDigit(row
[j
])) {
80 puzzleGrid
[i
][j
] = row
[j
];
82 throw new PuzzleFileFormatException("WordSearch: invalid puzzle file");
87 } catch (NumberFormatException e
) {
89 throw new PuzzleFileFormatException("WordSearch: invalid puzzle file");
90 } catch (NoSuchElementException e
) {
92 throw new PuzzleFileFormatException("WordSearch: invalid puzzle file");
93 } catch (ArrayIndexOutOfBoundsException e
) {
95 throw new PuzzleFileFormatException("WordSearch: invalid puzzle file");
97 } catch (FileNotFoundException e
) {
98 throw new PuzzleFileFormatException("WordSearch: cannot open " + puzzleFile
);
103 * Print the puzzle contained in this object to standard output.
105 public void print() {
106 int rows
= puzzleGrid
.length
;
107 int cols
= puzzleGrid
[0].length
;
108 for (int i
=0; i
<rows
; i
++) {
109 for (int j
=0; j
<cols
; j
++) {
110 System
.out
.print(puzzleGrid
[i
][j
]);
112 System
.out
.println();
118 private Location
searchE(char[] target
, int r
, int c
, Location location
) {
121 for (int i
=0; i
<target
.length
; i
++) {
122 if (Character
.toUpperCase(target
[i
]) == Character
.toUpperCase(puzzleGrid
[row
][col
])) {
123 if (i
== target
.length
- 1) {
124 location
= new Location(r
,c
,row
,col
);
127 if ( col
== puzzleGrid
[0].length
- 1 ) {
140 private Location
searchW(char[] target
, int r
, int c
, Location location
) {
143 for (int i
=0; i
<target
.length
; i
++) {
144 if (Character
.toUpperCase(target
[i
]) == Character
.toUpperCase(puzzleGrid
[row
][col
])) {
145 if (i
== target
.length
- 1) {
146 location
= new Location(r
,c
,row
,col
);
150 col
= puzzleGrid
[0].length
- 1;
162 private Location
searchN(char[] target
, int r
, int c
, Location location
) {
165 for (int i
=0; i
<target
.length
; i
++) {
166 if (Character
.toUpperCase(target
[i
]) == Character
.toUpperCase(puzzleGrid
[row
][col
])) {
167 if (i
== target
.length
- 1) {
168 location
= new Location(r
,c
,row
,col
);
172 row
= puzzleGrid
.length
- 1;
184 private Location
searchS(char[] target
, int r
, int c
, Location location
) {
187 for (int i
=0; i
<target
.length
; i
++) {
188 if (Character
.toUpperCase(target
[i
]) == Character
.toUpperCase(puzzleGrid
[row
][col
])) {
189 if (i
== target
.length
- 1) {
190 location
= new Location(r
,c
,row
,col
);
193 if ( row
== puzzleGrid
.length
- 1 ) {
206 private Location
searchSE(char[] target
, int r
, int c
, Location location
) {
209 for (int i
=0; i
<target
.length
; i
++) {
210 if (Character
.toUpperCase(target
[i
]) == Character
.toUpperCase(puzzleGrid
[row
][col
])) {
211 if (i
== target
.length
- 1) {
212 location
= new Location(r
,c
,row
,col
);
215 if ( row
== puzzleGrid
.length
- 1 ) {
220 if ( col
== puzzleGrid
.length
- 1 ) {
233 private Location
searchSW(char[] target
, int r
, int c
, Location location
) {
236 for (int i
=0; i
<target
.length
; i
++) {
237 if (Character
.toUpperCase(target
[i
]) == Character
.toUpperCase(puzzleGrid
[row
][col
])) {
238 if (i
== target
.length
- 1) {
239 location
= new Location(r
,c
,row
,col
);
242 if ( row
== puzzleGrid
.length
- 1 ) {
248 col
= puzzleGrid
.length
- 1;
260 private Location
searchNE(char[] target
, int r
, int c
, Location location
) {
263 for (int i
=0; i
<target
.length
; i
++) {
264 if (Character
.toUpperCase(target
[i
]) == Character
.toUpperCase(puzzleGrid
[row
][col
])) {
265 if (i
== target
.length
- 1) {
266 location
= new Location(r
,c
,row
,col
);
270 row
= puzzleGrid
.length
- 1;
274 if ( col
== puzzleGrid
.length
- 1 ) {
287 private Location
searchNW(char[] target
, int r
, int c
, Location location
) {
290 for (int i
=0; i
<target
.length
; i
++) {
291 if (Character
.toUpperCase(target
[i
]) == Character
.toUpperCase(puzzleGrid
[row
][col
])) {
292 if (i
== target
.length
- 1) {
293 location
= new Location(r
,c
,row
,col
);
297 row
= puzzleGrid
.length
- 1;
302 col
= puzzleGrid
.length
- 1;
315 * Search the puzzle for the given string. The search is not case sensitive
316 * and any non-alphabetic characters in the string will be ignored. If the
317 * string is found in the puzzle a location object, describing where the
318 * string was found will be returned. If the string is not found in the
319 * puzzle, null will be returned.
321 * @param word the string containing the word to search for.
322 * @return the location of the word if found and null otherwise.
324 public Location
find(String word
) {
325 String
[] temp1
= word
.split(" ");
327 for (String s
: temp1
) temp2
= temp2
+ s
;
328 char[] target
= temp2
.toCharArray();
330 Location location
= null;
331 int rows
= puzzleGrid
.length
;
332 int cols
= puzzleGrid
[0].length
;
333 for (int i
=0; i
<rows
; i
++) {
334 for (int j
=0; j
<cols
; j
++) {
335 location
= searchE(target
, i
, j
, location
);
336 if (location
== null)
337 location
= searchW(target
, i
, j
, location
);
338 if (location
== null)
339 location
= searchN(target
, i
, j
, location
);
340 if (location
== null)
341 location
= searchS(target
, i
, j
, location
);
342 if (location
== null)
343 location
= searchSE(target
, i
, j
, location
);
344 if (location
== null)
345 location
= searchSW(target
, i
, j
, location
);
346 if (location
== null)
347 location
= searchNE(target
, i
, j
, location
);
348 if (location
== null)
349 location
= searchNW(target
, i
, j
, location
);