Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Boggle not working?

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Boggle not working?

    I'm making a program that plays Boggle. It has a problem, however. When it checks the input to see if it is a valid word, it generates an IndexOutOfBoundsException. Here's my code, what's the deal?
    import java.util.*;
     import java.lang.*;
     import java.io.*;
    public class MyBoggle2{
        static  final int ROWS = 4, COLS = 4;
    	static final int LOW = 'A', HIGH = 'Z';
    	static char[][] board = new char[4][4];
        public static void main(String[] args)  throws IOException, FileNotFoundException{
        	ArrayList<String> found = new ArrayList<String>();
    		ArrayList<String> wrong = new ArrayList<String>();
    		ArrayList<String> real = new ArrayList<String>();
    		String temp;
    		String word;
    		int size;
    		int words;
    		int index = 0;
    		char restart = 'Y';	
    		Scanner input = new Scanner(System.in);
        	File dic = new File ("bogdict.txt");
    		FileReader in = new FileReader(dic);
    		BufferedReader readFile = new BufferedReader(in);
     
        	while (restart != 'N') {
        	word = " ";
        	makeBoard();
        	System.out.println("Let's play a game of Boggle: ");
        	printBoard();
        	real = new ArrayList<String>();
        	wrong = new ArrayList<String>();
        	found = new ArrayList<String>();
        	temp = readFile.readLine();
        	while(temp != null) {
        		if (checkWord(temp) == true) {
        			real.add(temp);
        		}
        		temp = readFile.readLine();
        	}
        	size = real.size();
        	System.out.println("Enter words or ZZ to quit: ");
        	while (word.equals("zz") == false){
        		word = input.next().toLowerCase();
        		System.out.print("");
        		index = 0;
        		if (!word.equals("zz"))
        		while (index < size && real.get(index) != word){
        			if (word.equals(real.get(index))) {
        				found.add(real.get(index));
        				real.set(index, "");
        			}
        			index += 1;
        		}	
        	}
        	System.out.println("Words you guessed correctly: ");
        	for (String i : found) {
        		System.out.println(i);
        	}
        	words = real.size() - found.size();
        	System.out.println("Words the computer found that you could have guessed: " + words);
        	System.out.println("Words you could have guessed: ");
        	for (String i : real) {
        		if (!i.equals("")) System.out.println(i);
        	}
     
        }
        in.close();
        readFile.close();
        }
        public static void printBoard() {
        	for (int y = 0; y <= 3; y++) {
        		for (int x = 0; x <= 3; x++) {
        			System.out.print(board[y][x] + " ");
        		}
        		System.out.println("");
        	}
        }
        public static void makeBoard() {
        	for (int y = 0; y <= 3; y++) {
        		for (int x = 0; x <= 3; x++) {
        			board[y][x]=(char)((int)'a'+Math.random()*((int)'z'-(int)'a'+1));
        		}
        	}
        }
        private static boolean findWord(int row, int col, String word) {
                    if (word.length() == 0) {return true;}
                    if (row < 0 || row >= ROWS) {return false;}
                    if (col < 0 || col >= COLS) {return false;}
                    if (word.charAt(0) != board[row][col]) {return false;}
                    char saveChar = board[row][col];
                    board[row][col] = ' ';
                    for (int r = row - 1; r <= row + 1; r++) {
                            for (int c = col - 1; c <= col + 1; c++) {
                                    if (findWord(r, c, word.substring(1))) {
                                    	board[row][col] = saveChar;
                                    	return true;
                            }
                    }
            }
            board[row][col] = saveChar;
            return (false);
        }
            private static boolean checkWord(String word) {
                    for (int row = 0; row < ROWS; row++) {
                            for (int col = 0; col < COLS; col++) {
                                    if (findWord(row, col, word)) return true;
                            }
                    }
                    return (false);
            }
    }


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Boggle not working?

    It would help if you posted the full text of the error message, which will say exactly where the problem occurs, and if you supplied a sample dictionary and input that will demonstrate the problem.

    It would also help you if you used your ROWS and COLS constants when creating and wherever you access the board in a loop.

Similar Threads

  1. Object as Reference not working
    By jassi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 9th, 2010, 09:47 AM
  2. My KeyListener is not Working!!
    By DarrenReeder in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2010, 05:18 PM
  3. Working with MAC .DMG files
    By mdv2000 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 21st, 2010, 12:31 PM
  4. Working with Methods
    By duckman in forum Object Oriented Programming
    Replies: 3
    Last Post: November 9th, 2009, 08:27 PM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM