new seed
[rmh3093.git] / project1 / Puzzle.java
blob296d83c48704b2a25df887ca8c66a13b002b0374
1 /*
2 * Puzzle.java
4 * Version:
5 * $Id: Puzzle.java,v 1.1 2008/03/31 03:59:18 rmh3093 Exp rmh3093 $
7 * Revisions:
8 * $Log: Puzzle.java,v $
9 * Revision 1.1 2008/03/31 03:59:18 rmh3093
10 * Initial revision
15 import java.io.File;
16 import java.io.FileNotFoundException;
17 import java.io.IOException;
18 import java.util.Scanner;
21 /**
22 * This class represents word search puzzles.
24 * @author rmh3093
26 public class Puzzle {
28 private char[][] puzzleGrid;
30 /**
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);
41 /**
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.
47 * @param puzzleFile
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);
57 if (!pz.exists()) {
58 //throw new PuzzleFileFormatException("WordSearch: cannot open " + puzzleFile);
61 try {
62 Scanner input = new Scanner(pz);
63 try {
64 int rows = Integer.parseInt(input.nextLine());
65 int cols = Integer.parseInt(input.nextLine());
66 if (!input.hasNext()) {
67 input.close();
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];
76 } else {
77 throw new PuzzleFileFormatException("WordSearch: invalid puzzle file");
81 input.close();
82 } catch (NumberFormatException e) {
83 input.close();
84 throw new PuzzleFileFormatException("WordSearch: invalid puzzle file");
86 } catch (FileNotFoundException e) {
87 throw new PuzzleFileFormatException("WordSearch: cannot open " + puzzleFile);
91 /**
92 * Print the puzzle contained in this object to standard output.
94 public void print() {
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]);
100 if (j == (cols-1)) {
101 System.out.println();
107 private Location searchN(char[] target, int r, int c, Location location) {
108 int col = c;
109 int row = r;
110 for (int i=1; i<target.length; i++) {
111 if ( row == 0 ) {
112 row = puzzleGrid.length - 1;
113 } else {
114 row = row - 1;
116 if (puzzleGrid[row][col] != target[i]) {
117 break;
118 } else {
119 if (i == target.length - 1) {
120 location = new Location(r,c,row,col);
124 return location;
127 private Location searchS(char[] target, int r, int c, Location location) {
128 int col = c;
129 int row = r;
130 for (int i=1; i<target.length; i++) {
131 if ( row == puzzleGrid.length - 1 ) {
132 row = 0;
133 } else {
134 row = row + 1;
136 if (puzzleGrid[row][col] != target[i]) {
137 break;
138 } else {
139 if (i == target.length - 1) {
140 location = new Location(r,c,row,col);
144 return location;
147 private Location searchW(char[] target, int r, int c, Location location) {
148 int col = c;
149 int row = r;
150 for (int i=1; i<target.length; i++) {
151 if ( col == 0 ) {
152 col = puzzleGrid[0].length - 1;
153 } else {
154 col = col - 1;
156 if (puzzleGrid[row][col] != target[i]) {
157 break;
158 } else {
159 if (i == target.length - 1) {
160 location = new Location(r,c,row,col);
164 return location;
167 private Location searchE(char[] target, int r, int c, Location location) {
168 int col = c;
169 int row = r;
170 for (int i=1; i<target.length; i++) {
171 if ( col == puzzleGrid[0].length - 1 ) {
172 col = 0;
173 } else {
174 col = col + 1;
176 if (puzzleGrid[row][col] != target[i]) {
177 break;
178 } else {
179 if (i == target.length - 1) {
180 location = new Location(r,c,row,col);
184 return location;
187 private Location searchNE(char[] target, int r, int c, Location location) {
188 int col = c;
189 int row = r;
190 for (int i=1; i<target.length; i++) {
191 if ( col == puzzleGrid[0].length - 1 ) {
192 col = 0;
193 } else {
194 col = col + 1;
196 if ( row == 0 ) {
197 row = puzzleGrid.length - 1;
198 } else {
199 row = row - 1;
201 if (puzzleGrid[row][col] != target[i]) {
202 break;
203 } else {
204 if (i == target.length - 1) {
205 location = new Location(r,c,row,col);
209 return location;
212 private Location searchSE(char[] target, int r, int c, Location location) {
213 int col = c;
214 int row = r;
215 for (int i=1; i<target.length; i++) {
216 if ( col == puzzleGrid[0].length - 1 ) {
217 col = 0;
218 } else {
219 col = col + 1;
221 if ( row == puzzleGrid.length - 1 ) {
222 row = 0;
223 } else {
224 row = row + 1;
226 if (puzzleGrid[row][col] != target[i]) {
227 break;
228 } else {
229 if (i == target.length - 1) {
230 location = new Location(r,c,row,col);
234 return location;
237 private Location searchNW(char[] target, int r, int c, Location location) {
238 int col = c;
239 int row = r;
240 for (int i=1; i<target.length; i++) {
241 if ( col == 0 ) {
242 col = puzzleGrid[0].length - 1;
243 } else {
244 col = col - 1;
246 if ( row == 0 ) {
247 row = puzzleGrid.length - 1;
248 } else {
249 row = row - 1;
251 if (puzzleGrid[row][col] != target[i]) {
252 break;
253 } else {
254 if (i == target.length - 1) {
255 location = new Location(r,c,row,col);
259 return location;
262 private Location searchSW(char[] target, int r, int c, Location location) {
263 int col = c;
264 int row = r;
265 for (int i=1; i<target.length; i++) {
266 if ( col == 0 ) {
267 col = puzzleGrid[0].length - 1;
268 } else {
269 col = col - 1;
271 if ( row == puzzleGrid.length - 1 ) {
272 row = 0;
273 } else {
274 row = row + 1;
276 if (puzzleGrid[row][col] != target[i]) {
277 break;
278 } else {
279 if (i == target.length - 1) {
280 location = new Location(r,c,row,col);
284 return location;
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,
306 i, j, location);
307 if (location == null) {
308 location = searchS(target,
309 i, j, location);
310 if (location == null ) {
311 location = searchE(target,
312 i, j, location);
313 if (location == null) {
314 location = searchW(target,
315 i, j, location);
316 if (location == null) {
317 location = searchNW(target,
318 i, j, location);
319 if (location == null) {
320 location = searchSW(target,
321 i, j, location);
322 if (location == null) {
323 location = searchNE(target,
324 i, j, location);
325 if (location == null) {
326 location = searchSE(target,
327 i, j, location);
338 return location;