5 * $Id: Puzzle.java,v 1.1 2008/03/31 03:59:18 rmh3093 Exp rmh3093 $
8 * $Log: Puzzle.java,v $
9 * Revision 1.1 2008/03/31 03:59:18 rmh3093
16 import java
.io
.FileNotFoundException
;
17 import java
.io
.IOException
;
18 import java
.util
.Scanner
;
22 * This class represents word search puzzles.
28 private char[][] puzzleGrid
;
31 * Load a puzzle from a puzzle file. If any errors occur during the
32 * processing of the puzzle file, the appropriate exceptions will be thrown.
34 * @param puzzleFile the name of the file containing the puzzle to load.
36 public Puzzle(String puzzleFile
) throws PuzzleFileFormatException
,
37 IOException
, FileNotFoundException
{
38 setPuzzle(puzzleFile
);
42 * Replace the puzzle currently stored in this object with the puzzle
43 * contained in the given file. If any error occurs during the processing of
44 * the puzzle file, the puzzle will be set to null and the appropriate
45 * exception will be thrown.
48 * @throws java.io.FileNotFoundException
49 * @throws java.io.IOException
50 * @throws PuzzleFileFormatException
52 public void setPuzzle(String puzzleFile
) throws FileNotFoundException
,
53 IOException
, PuzzleFileFormatException
{
55 // Throw FileNotFoundException if puzzle file does not exist
56 File pz
= new File(puzzleFile
);
58 //throw new PuzzleFileFormatException("WordSearch: cannot open " + puzzleFile);
62 Scanner input
= new Scanner(pz
);
64 int rows
= Integer
.parseInt(input
.nextLine());
65 int cols
= Integer
.parseInt(input
.nextLine());
66 if (!input
.hasNext()) {
68 throw new PuzzleFileFormatException("WordSearch: invalid puzzle file");
70 puzzleGrid
= new char[rows
][cols
];
71 for (int i
=0; i
<rows
; i
++) {
72 char[] row
= input
.nextLine().toCharArray();
73 for (int j
=0; j
<cols
; j
++) {
74 if (Character
.isLetterOrDigit(row
[j
])) {
75 puzzleGrid
[i
][j
] = row
[j
];
77 throw new PuzzleFileFormatException("WordSearch: invalid puzzle file");
82 } catch (NumberFormatException e
) {
84 throw new PuzzleFileFormatException("WordSearch: invalid puzzle file");
86 } catch (FileNotFoundException e
) {
87 throw new PuzzleFileFormatException("WordSearch: cannot open " + puzzleFile
);
92 * Print the puzzle contained in this object to standard output.
95 int rows
= puzzleGrid
.length
;
96 int cols
= puzzleGrid
[0].length
;
97 for (int i
=0; i
<rows
; i
++) {
98 for (int j
=0; j
<cols
; j
++) {
99 System
.out
.print(puzzleGrid
[i
][j
]);
101 System
.out
.println();
107 private Location
searchN(char[] target
, int r
, int c
, Location location
) {
110 for (int i
=1; i
<target
.length
; i
++) {
112 row
= puzzleGrid
.length
- 1;
116 if (puzzleGrid
[row
][col
] != target
[i
]) {
119 if (i
== target
.length
- 1) {
120 location
= new Location(r
,c
,row
,col
);
127 private Location
searchS(char[] target
, int r
, int c
, Location location
) {
130 for (int i
=1; i
<target
.length
; i
++) {
131 if ( row
== puzzleGrid
.length
- 1 ) {
136 if (puzzleGrid
[row
][col
] != target
[i
]) {
139 if (i
== target
.length
- 1) {
140 location
= new Location(r
,c
,row
,col
);
147 private Location
searchW(char[] target
, int r
, int c
, Location location
) {
150 for (int i
=1; i
<target
.length
; i
++) {
152 col
= puzzleGrid
[0].length
- 1;
156 if (puzzleGrid
[row
][col
] != target
[i
]) {
159 if (i
== target
.length
- 1) {
160 location
= new Location(r
,c
,row
,col
);
167 private Location
searchE(char[] target
, int r
, int c
, Location location
) {
170 for (int i
=1; i
<target
.length
; i
++) {
171 if ( col
== puzzleGrid
[0].length
- 1 ) {
176 if (puzzleGrid
[row
][col
] != target
[i
]) {
179 if (i
== target
.length
- 1) {
180 location
= new Location(r
,c
,row
,col
);
187 private Location
searchNE(char[] target
, int r
, int c
, Location location
) {
190 for (int i
=1; i
<target
.length
; i
++) {
191 if ( col
== puzzleGrid
[0].length
- 1 ) {
197 row
= puzzleGrid
.length
- 1;
201 if (puzzleGrid
[row
][col
] != target
[i
]) {
204 if (i
== target
.length
- 1) {
205 location
= new Location(r
,c
,row
,col
);
212 private Location
searchSE(char[] target
, int r
, int c
, Location location
) {
215 for (int i
=1; i
<target
.length
; i
++) {
216 if ( col
== puzzleGrid
[0].length
- 1 ) {
221 if ( row
== puzzleGrid
.length
- 1 ) {
226 if (puzzleGrid
[row
][col
] != target
[i
]) {
229 if (i
== target
.length
- 1) {
230 location
= new Location(r
,c
,row
,col
);
237 private Location
searchNW(char[] target
, int r
, int c
, Location location
) {
240 for (int i
=1; i
<target
.length
; i
++) {
242 col
= puzzleGrid
[0].length
- 1;
247 row
= puzzleGrid
.length
- 1;
251 if (puzzleGrid
[row
][col
] != target
[i
]) {
254 if (i
== target
.length
- 1) {
255 location
= new Location(r
,c
,row
,col
);
262 private Location
searchSW(char[] target
, int r
, int c
, Location location
) {
265 for (int i
=1; i
<target
.length
; i
++) {
267 col
= puzzleGrid
[0].length
- 1;
271 if ( row
== puzzleGrid
.length
- 1 ) {
276 if (puzzleGrid
[row
][col
] != target
[i
]) {
279 if (i
== target
.length
- 1) {
280 location
= new Location(r
,c
,row
,col
);
288 * Search the puzzle for the given string. The search is not case sensitive
289 * and any non-alphabetic characters in the string will be ignored. If the
290 * string is found in the puzzle a location object, describing where the
291 * string was found will be returned. If the string is not found in the
292 * puzzle, null will be returned.
294 * @param word the string containing the word to search for.
295 * @return the location of the word if found and null otherwise.
297 public Location
find(String word
) {
298 char[] target
= word
.toCharArray();
299 Location location
= null;
300 int rows
= puzzleGrid
.length
;
301 int cols
= puzzleGrid
[0].length
;
302 for (int i
=0; i
<rows
; i
++) {
303 for (int j
=0; j
<cols
; j
++) {
304 if ( puzzleGrid
[i
][j
] == target
[0]) {
305 location
= searchN(target
,
307 if (location
== null) {
308 location
= searchS(target
,
310 if (location
== null ) {
311 location
= searchE(target
,
313 if (location
== null) {
314 location
= searchW(target
,
316 if (location
== null) {
317 location
= searchNW(target
,
319 if (location
== null) {
320 location
= searchSW(target
,
322 if (location
== null) {
323 location
= searchNE(target
,
325 if (location
== null) {
326 location
= searchSE(target
,