asdjsa
[rmh3093.git] / project1 / Puzzle.java
blobd04295f2cf0f77f7c264890f5f8a3c5c2b2731d5
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.NoSuchElementException;
22 import java.util.Scanner;
25 /**
26 * This class represents word search puzzles.
28 * @author rmh3093
30 public class Puzzle {
32 private char[][] puzzleGrid;
34 /**
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);
45 /**
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.
51 * @param puzzleFile
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);
62 if (!pz.exists()) {
63 //throw new PuzzleFileFormatException("WordSearch: cannot open " + puzzleFile);
66 try {
67 Scanner input = new Scanner(pz);
68 try {
69 int rows = Integer.parseInt(input.nextLine());
70 int cols = Integer.parseInt(input.nextLine());
71 if (!input.hasNext()) {
72 input.close();
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];
81 } else {
82 throw new PuzzleFileFormatException("WordSearch: invalid puzzle file");
86 input.close();
87 } catch (NumberFormatException e) {
88 input.close();
89 throw new PuzzleFileFormatException("WordSearch: invalid puzzle file");
90 } catch (NoSuchElementException e) {
91 input.close();
92 throw new PuzzleFileFormatException("WordSearch: invalid puzzle file");
93 } catch (ArrayIndexOutOfBoundsException e) {
94 input.close();
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]);
111 if (j == (cols-1)) {
112 System.out.println();
118 private Location searchE(char[] target, int r, int c, Location location) {
119 int col = c;
120 int row = r;
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);
125 break;
126 } else {
127 if ( col == puzzleGrid[0].length - 1 ) {
128 col = 0;
129 } else {
130 col++;
133 } else {
134 break;
137 return location;
140 private Location searchW(char[] target, int r, int c, Location location) {
141 int col = c;
142 int row = r;
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);
147 break;
148 } else {
149 if ( col == 0 ) {
150 col = puzzleGrid[0].length - 1;
151 } else {
152 col--;
155 } else {
156 break;
159 return location;
162 private Location searchN(char[] target, int r, int c, Location location) {
163 int col = c;
164 int row = r;
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);
169 break;
170 } else {
171 if ( row == 0 ) {
172 row = puzzleGrid.length - 1;
173 } else {
174 row--;
177 } else {
178 break;
181 return location;
184 private Location searchS(char[] target, int r, int c, Location location) {
185 int col = c;
186 int row = r;
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);
191 break;
192 } else {
193 if ( row == puzzleGrid.length - 1 ) {
194 row = 0;
195 } else {
196 row++;
199 } else {
200 break;
203 return location;
206 private Location searchSE(char[] target, int r, int c, Location location) {
207 int col = c;
208 int row = r;
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);
213 break;
214 } else {
215 if ( row == puzzleGrid.length - 1 ) {
216 row = 0;
217 } else {
218 row++;
220 if ( col == puzzleGrid.length - 1 ) {
221 col = 0;
222 } else {
223 col++;
226 } else {
227 break;
230 return location;
233 private Location searchSW(char[] target, int r, int c, Location location) {
234 int col = c;
235 int row = r;
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);
240 break;
241 } else {
242 if ( row == puzzleGrid.length - 1 ) {
243 row = 0;
244 } else {
245 row++;
247 if ( col == 0 ) {
248 col = puzzleGrid.length - 1;
249 } else {
250 col--;
253 } else {
254 break;
257 return location;
260 private Location searchNE(char[] target, int r, int c, Location location) {
261 int col = c;
262 int row = r;
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);
267 break;
268 } else {
269 if ( row == 0 ) {
270 row = puzzleGrid.length - 1;
271 } else {
272 row--;
274 if ( col == puzzleGrid.length - 1 ) {
275 col = 0;
276 } else {
277 col++;
280 } else {
281 break;
284 return location;
287 private Location searchNW(char[] target, int r, int c, Location location) {
288 int col = c;
289 int row = r;
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);
294 break;
295 } else {
296 if ( row == 0 ) {
297 row = puzzleGrid.length - 1;
298 } else {
299 row--;
301 if ( col == 0 ) {
302 col = puzzleGrid.length - 1;
303 } else {
304 col--;
307 } else {
308 break;
311 return location;
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(" ");
326 String temp2 = "";
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);
352 return location;