Domanda

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
      }
È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top