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
.Scanner
;
25 * This class represents word search puzzles.
31 private char[][] puzzleGrid
;
34 * Load a puzzle from a puzzle file. If any errors occur during the
35 * processing of the puzzle file, the appropriate exceptions will be thrown.
37 * @param puzzleFile the name of the file containing the puzzle to load.
39 public Puzzle(String puzzleFile
) throws PuzzleFileFormatException
,
40 IOException
, FileNotFoundException
{
41 setPuzzle(puzzleFile
);
45 * Replace the puzzle currently stored in this object with the puzzle
46 * contained in the given file. If any error occurs during the processing of
47 * the puzzle file, the puzzle will be set to null and the appropriate
48 * exception will be thrown.
51 * @throws java.io.FileNotFoundException
52 * @throws java.io.IOException
53 * @throws PuzzleFileFormatException
55 public void setPuzzle(String puzzleFile
) throws FileNotFoundException
,
56 IOException
, PuzzleFileFormatException
{
58 // Throw FileNotFoundException if puzzle file does not exist
59 File pz
= new File(puzzleFile
);
61 //throw new PuzzleFileFormatException("WordSearch: cannot open " + puzzleFile);
65 Scanner input
= new Scanner(pz
);
67 int rows
= Integer
.parseInt(input
.nextLine());
68 int cols
= Integer
.parseInt(input
.nextLine());
69 if (!input
.hasNext()) {
71 throw new PuzzleFileFormatException("WordSearch: invalid puzzle file");
73 puzzleGrid
= new char[rows
][cols
];
74 for (int i
=0; i
<rows
; i
++) {
75 char[] row
= input
.nextLine().toCharArray();
76 for (int j
=0; j
<cols
; j
++) {
77 if (Character
.isLetterOrDigit(row
[j
])) {
78 puzzleGrid
[i
][j
] = row
[j
];
80 throw new PuzzleFileFormatException("WordSearch: invalid puzzle file");
85 } catch (NumberFormatException e
) {
87 throw new PuzzleFileFormatException("WordSearch: invalid puzzle file");
89 } catch (FileNotFoundException e
) {
90 throw new PuzzleFileFormatException("WordSearch: cannot open " + puzzleFile
);
95 * Print the puzzle contained in this object to standard output.
98 int rows
= puzzleGrid
.length
;
99 int cols
= puzzleGrid
[0].length
;
100 for (int i
=0; i
<rows
; i
++) {
101 for (int j
=0; j
<cols
; j
++) {
102 System
.out
.print(puzzleGrid
[i
][j
]);
104 System
.out
.println();
110 private Location
searchN(char[] target
, int r
, int c
, Location location
) {
113 for (int i
=1; i
<target
.length
; i
++) {
115 row
= puzzleGrid
.length
- 1;
119 if (puzzleGrid
[row
][col
] != target
[i
]) {
122 if (i
== target
.length
- 1) {
123 location
= new Location(r
,c
,row
,col
);
130 private Location
searchS(char[] target
, int r
, int c
, Location location
) {
133 for (int i
=1; i
<target
.length
; i
++) {
134 if ( row
== puzzleGrid
.length
- 1 ) {
139 if (puzzleGrid
[row
][col
] != target
[i
]) {
142 if (i
== target
.length
- 1) {
143 location
= new Location(r
,c
,row
,col
);
150 private Location
searchW(char[] target
, int r
, int c
, Location location
) {
153 for (int i
=1; i
<target
.length
; i
++) {
155 col
= puzzleGrid
[0].length
- 1;
159 if (puzzleGrid
[row
][col
] != target
[i
]) {
162 if (i
== target
.length
- 1) {
163 location
= new Location(r
,c
,row
,col
);
170 private Location
searchE(char[] target
, int r
, int c
, Location location
) {
173 for (int i
=1; i
<target
.length
; i
++) {
174 if ( col
== puzzleGrid
[0].length
- 1 ) {
179 if (puzzleGrid
[row
][col
] != target
[i
]) {
182 if (i
== target
.length
- 1) {
183 location
= new Location(r
,c
,row
,col
);
190 private Location
searchNE(char[] target
, int r
, int c
, Location location
) {
193 for (int i
=1; i
<target
.length
; i
++) {
194 if ( col
== puzzleGrid
[0].length
- 1 ) {
200 row
= puzzleGrid
.length
- 1;
204 if (puzzleGrid
[row
][col
] != target
[i
]) {
207 if (i
== target
.length
- 1) {
208 location
= new Location(r
,c
,row
,col
);
215 private Location
searchSE(char[] target
, int r
, int c
, Location location
) {
218 for (int i
=1; i
<target
.length
; i
++) {
219 if ( col
== puzzleGrid
[0].length
- 1 ) {
224 if ( row
== puzzleGrid
.length
- 1 ) {
229 if (puzzleGrid
[row
][col
] != target
[i
]) {
232 if (i
== target
.length
- 1) {
233 location
= new Location(r
,c
,row
,col
);
240 private Location
searchNW(char[] target
, int r
, int c
, Location location
) {
243 for (int i
=1; i
<target
.length
; i
++) {
245 col
= puzzleGrid
[0].length
- 1;
250 row
= puzzleGrid
.length
- 1;
254 if (puzzleGrid
[row
][col
] != target
[i
]) {
257 if (i
== target
.length
- 1) {
258 location
= new Location(r
,c
,row
,col
);
265 private Location
searchSW(char[] target
, int r
, int c
, Location location
) {
268 for (int i
=1; i
<target
.length
; i
++) {
270 col
= puzzleGrid
[0].length
- 1;
274 if ( row
== puzzleGrid
.length
- 1 ) {
279 if (puzzleGrid
[row
][col
] != target
[i
]) {
282 if (i
== target
.length
- 1) {
283 location
= new Location(r
,c
,row
,col
);
291 * Search the puzzle for the given string. The search is not case sensitive
292 * and any non-alphabetic characters in the string will be ignored. If the
293 * string is found in the puzzle a location object, describing where the
294 * string was found will be returned. If the string is not found in the
295 * puzzle, null will be returned.
297 * @param word the string containing the word to search for.
298 * @return the location of the word if found and null otherwise.
300 public Location
find(String word
) {
301 char[] target
= word
.toCharArray();
302 Location location
= null;
303 int rows
= puzzleGrid
.length
;
304 int cols
= puzzleGrid
[0].length
;
305 for (int i
=0; i
<rows
; i
++) {
306 for (int j
=0; j
<cols
; j
++) {
307 if ( puzzleGrid
[i
][j
] == target
[0]) {
308 location
= searchN(target
,
310 if (location
== null) {
311 location
= searchS(target
,
313 if (location
== null ) {
314 location
= searchE(target
,
316 if (location
== null) {
317 location
= searchW(target
,
319 if (location
== null) {
320 location
= searchNW(target
,
322 if (location
== null) {
323 location
= searchSW(target
,
325 if (location
== null) {
326 location
= searchNE(target
,
328 if (location
== null) {
329 location
= searchSE(target
,