3

I am trying to create a simple word search for a class assignment, and I have managed to figure out how to search east (from left to right) and west(right to left). But I am having trouble trying to figure out how to search south (top to bottom).

The code that I have works for one file that I read in but the second file returns an ArrayIndexOutOfBoundsException. Is there anything that is specific in my code that would make it un-scalable?

My corrected code looks like this:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

import wordSeek.GameBoard;

public class WordGame {

    private char[][] letters;
    GameBoard gb;

    public static void main(String[] args) {
        WordGame wg = new WordGame();
        wg.play();

    }

    public WordGame() {
        letters = readLettersFromFile();
        gb = new GameBoard(letters);

    }

    private void play() {
        Scanner s = new Scanner(System.in);
        String word;

        do {
            System.out.println("Enter word to find: ");
            word = s.next();

            // reset all highlighted tiles
            gb.reset();

            search(word);

        } while (!word.equals("QUIT"));

        gb.dispose();
    }

    // Nothing to be done above
    // Complete all the methods below

    private char[][] readLettersFromFile() {
        // From the data in the file Letters.txt determine the size (number of
        // rows and number of columns) for the letters array

        int rowCount = 0;
        int colCount = 0;
        char c;

        File file = new File("resources/Places.txt");

        Scanner fileScanner = null;
        try {
            fileScanner = new Scanner(file);
            } 
        catch (FileNotFoundException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        ArrayList<String> data = new ArrayList<String>();

        while(fileScanner.hasNextLine())
        {
            String line = fileScanner.nextLine();
            data.add(line);
        }

        fileScanner.close();
        rowCount = data.size();
        colCount = data.get(0).trim().length()/2+1;

        // Instantiate a two dimensional array of characters of the appropriate
        // size

        letters = new char [rowCount][colCount];

        // Populate the array with the letters in Letters.txt
        for (int i = 0; i < rowCount; i++) {
            String line = data.get(i);
            int pos = 0;
            for (int j = 0; j < colCount; j++) {
                letters[i][j] = line.charAt(pos);
                pos += 2;
            }

        }

        // return the array

        return letters;

    }

    private void search(String word) {
        System.out.println("Searching for " + word);

        //Call the other search methods below as needed
        searchIterativeEast(word);
        searchIterativeWest(word);
        searchIterativeSouth(word);
        searchIterativeNorth(word);
    }

    //The following four methods must employ ITERATION to search the game board
    private boolean searchIterativeEast(String word) {
        int k = 0;

        for (int i = 0; i < letters.length; i++) 
        {
            for (int j = 0; j < letters[i].length; j++) {
                if (word.charAt(k) == letters[i][j]) {
                    k++;
                } 
                else {
                    k = 0;
                }
                if (k == word.length()) {
                    for (int col = j - k + 1; col <= j; col++) {
                        gb.highlight(i, col);
                    }

                    return true;
                }
            }
        }

        return false;
    }

    private boolean searchIterativeWest(String word) {

            String reversedWord = "";
            for (int i = word.length() - 1; i != -1; i--)
            {
                reversedWord += word.charAt(i);
            }

            int k = 0;

            for (int i = 0; i < letters.length; i++) 
            {
                for (int j = 0; j < letters[i].length; j++) {
                    if (reversedWord.charAt(k) == letters[i][j]) {
                        k++;
                    } 
                    else {
                        k = 0;
                    }
                    if (k == reversedWord.length()) {
                        for (int col = j - k + 1; col <= j; col++) {
                            gb.highlight(i, col);
                        }

                        return true;
                    }
                }
            }

            return false;

    }

    private boolean searchIterativeSouth(String word) {
        int k = 0;
        int store = letters[0].length;

        for (int j = 0; j < letters[store].length; j++)
        {
            for (int i = 0; i < letters.length; i++)
            {
                if (word.charAt(k) == letters[i][j])
                {
                    k++;
                }
                else
                {
                    k = 0;
                }
                if (k == word.length())
                {
                    for(int row = i-k+1 ; row <= i; row++)
                    {
                        gb.highlight(row, j);
                    }
                    return true;
                }
            }
        }

        return false;
    }

    private boolean searchIterativeNorth(String word) {

        String reversedWord = "";
        for (int i = word.length() - 1; i != -1; i--)
        {
            reversedWord += word.charAt(i);
        }

        int k = 0;
        int store = 0;

        for(int i = 0; i < letters.length; i++)
        {
            store = letters[i].length;
        }

        for (int j = 0; j < letters[store].length; j++)
        {
            for (int i = 0; i < letters.length; i++)
            {
                if (reversedWord.charAt(k) == letters[i][j])
                {
                    k++;
                }
                else
                {
                    k = 0;
                }
                if (k == reversedWord.length())
                {
                    for(int row = i-k+1 ; row <= i; row++)
                    {
                        gb.highlight(row, j);
                    }
                    return true;
                }
            }
        }

        return false;
    }

The Gameboard for the first file (Animals.txt) looks like: 5X4 2d Array

X C A T
P A L Q
I R B U
G P X N
G O D W

Output highlights CARP.

The Gameboard for the second file (Places.txt) looks like: 11x15 2d Array

O M J G D A X V C S Q N K I F 
D A X V T Q O M J H A A H F C 
A Y W U R P N L F E I T A L Y
J N H N E T H E R L A N D S F 
D B I Z X V T O A R Q O A Y K 
M K I A H F K R N O D B N N I
N Z Y W P H T V C G C T A A N 
R A Q O T S N L E K K I C M G 
I H P U U F D C A Z D O X R D 
X W O A L E U Z E N E V N E O 
V S U S J R Q L I Z A R B G M 
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
Wes Johnson
  • 69
  • 1
  • 1
  • 7
  • What is letters? Is it a n X n matrix? Or just an array of array? – Rohit Jain Feb 15 '13 at 05:19
  • 1
    Ok, here's a hint. You outer loop should iterate through columns, and inner loop should iterate through rows. It's just the reverse of how you normally iterate through an array of array. So, just try and change `letters[i][j]` to `letters[j][i]`. And I'm sure you will get an exception then. You need to resolve that exception. – Rohit Jain Feb 15 '13 at 05:23
  • Alright I will play around with that and see if I can get it to work thank you! – Wes Johnson Feb 15 '13 at 05:26
  • I managed to figure out a algorithm that works for the initial char elements; but now I have no idea what is preventing it from being scalable. – Wes Johnson Feb 15 '13 at 09:18
  • Show your current code and full stack trace. – Rohit Jain Feb 15 '13 at 09:21
  • I updated my original post with the stack trace and my full code. The specific method i am working on is the searchIterativeSouth method. – Wes Johnson Feb 15 '13 at 09:27
  • 1
    Change `letters[store].length` to `store` in the outer loop condition. – Rohit Jain Feb 15 '13 at 09:33
  • You're welcome :) You can accept any of the answers below that you think is closer to the actual solution. – Rohit Jain Feb 15 '13 at 09:37

2 Answers2

1
for (int j = 0; j < letters[i].length; j++)

instead of

for (int j = 0; j < letters.length; j++)
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
  • that fixed the ArrayIndexOutOfBoundsException error, I have been playing around with the code so much that it caused the error. – Wes Johnson Feb 15 '13 at 05:25
  • 1
    @Achintya. This alone will not solve the issue. There is something more in the problem than just changing the loop condition. But, I hope OP to figure it out. – Rohit Jain Feb 15 '13 at 05:28
1

The return statement goes outside of the for loop, otherwise you will only highlight the first letter.

...
for (int row = j-k+1; row <=i; row++ )
    {
        //gb.highlight() just calls a method that    highlights the letters.
        gb.highlight(row, j);
        //return true;
    }
return true;
...
Denis
  • 148
  • 10