jhdsf
[rmh3093.git] / project1 / Puzzle.java
blob394d2d418c56909f45d3cb704f770d448946c881
1 /*
2 * Puzzle.java
4 * Version:
5 * $Id: Puzzle.java,v 1.2 2008/04/11 17:53:32 rmh3093 Exp rmh3093 $
7 * Revisions:
8 * $Log: Puzzle.java,v $
9 * Revision 1.2 2008/04/11 17:53:32 rmh3093
10 * finding words works
12 * Revision 1.1 2008/03/31 03:59:18 rmh3093
13 * Initial revision
18 import java.io.File;
19 import java.io.FileNotFoundException;
20 import java.io.IOException;
21 import java.util.Scanner;
24 /**
25 * This class represents word search puzzles.
27 * @author rmh3093
29 public class Puzzle {
31 private char[][] puzzleGrid;
33 /**
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);
44 /**
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.
50 * @param puzzleFile
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);
60 if (!pz.exists()) {
61 //throw new PuzzleFileFormatException("WordSearch: cannot open " + puzzleFile);
64 try {
65 Scanner input = new Scanner(pz);
66 try {
67 int rows = Integer.parseInt(input.nextLine());
68 int cols = Integer.parseInt(input.nextLine());
69 if (!input.hasNext()) {
70 input.close();
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];
79 } else {
80 throw new PuzzleFileFormatException("WordSearch: invalid puzzle file");
84 input.close();
85 } catch (NumberFormatException e) {
86 input.close();
87 throw new PuzzleFileFormatException("WordSearch: invalid puzzle file");
89 } catch (FileNotFoundException e) {
90 throw new PuzzleFileFormatException("WordSearch: cannot open " + puzzleFile);
94 /**
95 * Print the puzzle contained in this object to standard output.
97 public void print() {
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]);
103 if (j == (cols-1)) {
104 System.out.println();
110 private Location searchN(char[] target, int r, int c, Location location) {
111 int col = c;
112 int row = r;
113 for (int i=1; i<target.length; i++) {
114 if ( row == 0 ) {
115 row = puzzleGrid.length - 1;
116 } else {
117 row = row - 1;
119 if (puzzleGrid[row][col] != target[i]) {
120 break;
121 } else {
122 if (i == target.length - 1) {
123 location = new Location(r,c,row,col);
127 return location;
130 private Location searchS(char[] target, int r, int c, Location location) {
131 int col = c;
132 int row = r;
133 for (int i=1; i<target.length; i++) {
134 if ( row == puzzleGrid.length - 1 ) {
135 row = 0;
136 } else {
137 row = row + 1;
139 if (puzzleGrid[row][col] != target[i]) {
140 break;
141 } else {
142 if (i == target.length - 1) {
143 location = new Location(r,c,row,col);
147 return location;
150 private Location searchW(char[] target, int r, int c, Location location) {
151 int col = c;
152 int row = r;
153 for (int i=1; i<target.length; i++) {
154 if ( col == 0 ) {
155 col = puzzleGrid[0].length - 1;
156 } else {
157 col = col - 1;
159 if (puzzleGrid[row][col] != target[i]) {
160 break;
161 } else {
162 if (i == target.length - 1) {
163 location = new Location(r,c,row,col);
167 return location;
170 private Location searchE(char[] target, int r, int c, Location location) {
171 int col = c;
172 int row = r;
173 for (int i=1; i<target.length; i++) {
174 if ( col == puzzleGrid[0].length - 1 ) {
175 col = 0;
176 } else {
177 col = col + 1;
179 if (puzzleGrid[row][col] != target[i]) {
180 break;
181 } else {
182 if (i == target.length - 1) {
183 location = new Location(r,c,row,col);
187 return location;
190 private Location searchNE(char[] target, int r, int c, Location location) {
191 int col = c;
192 int row = r;
193 for (int i=1; i<target.length; i++) {
194 if ( col == puzzleGrid[0].length - 1 ) {
195 col = 0;
196 } else {
197 col = col + 1;
199 if ( row == 0 ) {
200 row = puzzleGrid.length - 1;
201 } else {
202 row = row - 1;
204 if (puzzleGrid[row][col] != target[i]) {
205 break;
206 } else {
207 if (i == target.length - 1) {
208 location = new Location(r,c,row,col);
212 return location;
215 private Location searchSE(char[] target, int r, int c, Location location) {
216 int col = c;
217 int row = r;
218 for (int i=1; i<target.length; i++) {
219 if ( col == puzzleGrid[0].length - 1 ) {
220 col = 0;
221 } else {
222 col = col + 1;
224 if ( row == puzzleGrid.length - 1 ) {
225 row = 0;
226 } else {
227 row = row + 1;
229 if (puzzleGrid[row][col] != target[i]) {
230 break;
231 } else {
232 if (i == target.length - 1) {
233 location = new Location(r,c,row,col);
237 return location;
240 private Location searchNW(char[] target, int r, int c, Location location) {
241 int col = c;
242 int row = r;
243 for (int i=1; i<target.length; i++) {
244 if ( col == 0 ) {
245 col = puzzleGrid[0].length - 1;
246 } else {
247 col = col - 1;
249 if ( row == 0 ) {
250 row = puzzleGrid.length - 1;
251 } else {
252 row = row - 1;
254 if (puzzleGrid[row][col] != target[i]) {
255 break;
256 } else {
257 if (i == target.length - 1) {
258 location = new Location(r,c,row,col);
262 return location;
265 private Location searchSW(char[] target, int r, int c, Location location) {
266 int col = c;
267 int row = r;
268 for (int i=1; i<target.length; i++) {
269 if ( col == 0 ) {
270 col = puzzleGrid[0].length - 1;
271 } else {
272 col = col - 1;
274 if ( row == puzzleGrid.length - 1 ) {
275 row = 0;
276 } else {
277 row = row + 1;
279 if (puzzleGrid[row][col] != target[i]) {
280 break;
281 } else {
282 if (i == target.length - 1) {
283 location = new Location(r,c,row,col);
287 return location;
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,
309 i, j, location);
310 if (location == null) {
311 location = searchS(target,
312 i, j, location);
313 if (location == null ) {
314 location = searchE(target,
315 i, j, location);
316 if (location == null) {
317 location = searchW(target,
318 i, j, location);
319 if (location == null) {
320 location = searchNW(target,
321 i, j, location);
322 if (location == null) {
323 location = searchSW(target,
324 i, j, location);
325 if (location == null) {
326 location = searchNE(target,
327 i, j, location);
328 if (location == null) {
329 location = searchSE(target,
330 i, j, location);
341 return location;