1

i made this boggle-type game, and so far it works very well. This method here is for searching the 10x10 grid to see if the word the user entered is correct. The problem i have is that this code only works for words that appear left to right, and top to bottom. Now i don't expect to do diagonal, but i would like to make it to where the words that appear in reverse order are also accepted by the system. I've been told that the word that you enter would have to be flipped (reversed) in order to see if it matches properly (which makes sense. After all, you're looking for a word that is backwords). So exactly how would i achieve this? Now, i'm not very good with coding, so please, if you have time then write down what code i would have to use. Thank you.

public static boolean wordSearch (char[][] table, String search)
       {
           int  ctr = search.length();
           String temp = ""; 

           // LEFT TO RIGHT / X-AXIS
           for (int row = 0; row < 10; row++) //Checks each row (x) one by one
           {
                   for (int a = 0; a <= (10 - ctr); a++)
                   {
                    StringBuilder s = new StringBuilder(10-ctr);//Does... something
                        for (int x = a; x <= (a+ctr-1); x++) //Checks every possibility in the row
                         {
                            s.append(table[row][x]);
                            temp = s.toString();
                            if (temp.equals(search))
                            {
                                return true;
                            }
                         }
                   }
           }
           // TOP TO BOTTOM / Y-AXIS
           for (int column = 0; column < 10; column++)
           {
                   for (int b = 0; b <= (10 - ctr); b++)
                   {
                    StringBuilder v = new StringBuilder(10-ctr);
                        for (int y = b; y <= (b+ctr-1); y++)//checks every possibility in grid
                         {
                            v.append(table[y][column]);
                            temp = v.toString();
                            if (temp.equals(search))
                            {
                                return true;
                            }
                         }
                   }
           }
            return false;//if word isn't in grid it returns as false, which then displays an error message
      }
Floris
  • 45,857
  • 6
  • 70
  • 122
  • http://stackoverflow.com/questions/198199/how-do-you-reverse-a-string-in-place-in-c-or-c shows you how to reverse a string - and you're right, that would be the cleanest way of checking for words that go right-to-left or bottom-to-top. – Floris Jan 25 '13 at 05:04
  • Of all the solutions the above link gives, I like this one the best: `#include std::reverse(str.begin(), str.end());` – Floris Jan 25 '13 at 05:08
  • The above was a C++ answer. I have retagged the question with Java tag, and modified my original answer to be Java also. – Floris Jan 26 '13 at 04:57

1 Answers1

1

EDIT ... the Java version!!! (I have no Java compiler handy right now but I think this is right...)

At the second line of your code, we compute the reverse string; there are thousands of ways to do this, but this one is pretty self-explanatory (I am assuming there is no white space in your string...):

int ls = search.length();  // length of initial string
StringBuilder sb = new StringBuilder(); // temporary place to store growing string
for(int ii=ls-1;ii>=0; ii--) {
        sb.append(search.charAt(ii)); // build the string one character at a time
    }
hcraes = sb.toString(); // convert to "regular" string

Now, at every point where you have the line

if (temp.equals(search))

change that line to

if (temp.equals(search) || temp.equals(hcraes))

that should do the trick.

Floris
  • 45,857
  • 6
  • 70
  • 122
  • What is "std::reverse" ? It doesn't exactly seem to be working. It says it's an illegal start of expression. Can you clarify it please?? – user2009772 Jan 25 '13 at 23:29
  • Is this Java or C++ ... maybe I was reading your code too quickly. At first glance it could be either? But std:reverse() is c++. Tell me if you need a Java answer. Probably good to tag your question with the language... – Floris Jan 26 '13 at 00:56
  • Yeah, sorry. I just signed up for this. Yeah, i probably should have specified. Its for Java. – user2009772 Jan 26 '13 at 03:18
  • OK no wonder you are confused. Give me a second... I'll give you the Java version. – Floris Jan 26 '13 at 03:19
  • @user2009772 - it's there now. I did find a compiler that ran this code no problem. If this works for you please "accept" the answer (check box to the left), and welcome to SO! – Floris Jan 26 '13 at 03:48