2013-03-26 1 views
0

그래서 2 차원 배열 격자를 사용하여 단어 검색 프로그램을 작성해야합니다.Word Search Java에서 2 차원 배열을 사용하여 - 두 번째 문자 과거를 얻으십시오

내 코드는 그리드에서 2 글자 단어를 모두 찾을 수 있지만 길이가 2 글자를 넘고 코드가 건너 뜁니다. 나는 2am에 현재 죽었다. 그래서 그 명백한 무엇인가 명백한 im는 놓친다. 그러나 그것이 isnt하면, 나를 도와 줘라?

현재 검색 코드 :

/** 
    * Searches for a word in this LetterGrid using traditional WordSearch 
    * rules. 
    * 
    * @param word 
    *   the word to look for 
    */ 
    public boolean wordSearch(String word) 
    { 
      // Check each letter in grid 
      for (int row = 0; row < this.noOfRows; row++) 
      { 
        for (int col = 0; col < this.rowLength; col++) 
        { 
          if (grid[row][col] == word.charAt(0) && word.length() > 1) 
          { 
            return gridCheck(row, col, word, 1); 
          } 
          else if (grid[row][col] == word.charAt(0)) 
          { 
            return true; 
          } 
        } 

      } 
      return false; 
    } 

    public boolean gridCheck(int row, int col, String word, int charToFind) 
    { 

      if (charToFind == word.length() - 1) 
      { 
        return true; 
      } 
      else if (charToFind < word.length() - 1) 
      { 
        // Where is the letter being checked? -contingency check- 
        // if letter being checked is not touching any edge [most likely] 
        if (row > 0 && row < this.noOfRows && col > 0 
            && col < this.rowLength) 
        { 
          // FOR CODES SEE CHECKPLACES.TXT 
          // A 
          if (grid[row - 1][col - 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row - 1, col - 1, word, 
                word.charAt(charToFind + 1)); 
          } 
          // B 
          else if (grid[row - 1][col] == word.charAt(charToFind)) 
          { 
            gridCheck(row - 1, col, word, word.charAt(charToFind + 1)); 
          } 
          // C 
          else if (grid[row - 1][col + 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row - 1, col + 1, word, 
                word.charAt(charToFind + 1)); 
          } 
          // D 
          else if (grid[row][col - 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row, col - 1, word, word.charAt(charToFind + 1)); 
          } 
          // E 
          else if (grid[row][col + 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row, col + 1, word, word.charAt(charToFind + 1)); 
          } 
          // F 
          else if (grid[row + 1][col - 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row + 1, col - 1, word, 
                word.charAt(charToFind + 1)); 
          } 
          // G 
          else if (grid[row + 1][col] == word.charAt(charToFind)) 
          { 
            gridCheck(row + 1, col, word, word.charAt(charToFind + 1)); 
          } 
          // H 
          else if (grid[row + 1][col + 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row + 1, col + 1, word, 
                word.charAt(charToFind + 1)); 
          } 

        } 
        // Letter is touching top left corner 
        if (row == 0 && col == 0) 
        { 
          // E 
          if (grid[row][col + 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row, col + 1, word, word.charAt(charToFind + 1)); 
          } 
          // H 
          else if (grid[row + 1][col + 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row + 1, col + 1, word, 
                word.charAt(charToFind + 1)); 
          } 
          // G 
          else if (grid[row + 1][col] == word.charAt(charToFind)) 
          { 
            gridCheck(row + 1, col, word, word.charAt(charToFind + 1)); 
          } 
        } 
        // Letter is touching top Right corner 
        if (row == 0 && col == this.rowLength) 
        { 
          // D 
          if (grid[row][col - 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row, col - 1, word, word.charAt(charToFind + 1)); 
          } 
          // F 
          else if (grid[row + 1][col - 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row + 1, col - 1, word, 
                word.charAt(charToFind + 1)); 
          } 
          // G 
          else if (grid[row + 1][col] == word.charAt(charToFind)) 
          { 
            gridCheck(row + 1, col, word, word.charAt(charToFind + 1)); 
          } 

        } 
        // Letter is Touching bottom Left Corner 
        if (row == this.noOfRows && col == 0) 
        { 
          // B 
          if (grid[row - 1][col] == word.charAt(charToFind)) 
          { 
            gridCheck(row - 1, col, word, word.charAt(charToFind + 1)); 
          } 
          // C 
          else if (grid[row - 1][col + 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row - 1, col + 1, word, 
                word.charAt(charToFind + 1)); 
          } 

          // E 
          else if (grid[row][col + 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row, col + 1, word, word.charAt(charToFind + 1)); 
          } 

        } 
        // Letter is touching bottom right corner 
        if (row == this.noOfRows && col == this.rowLength) 
        { 
          // A 
          if (grid[row - 1][col - 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row - 1, col - 1, word, 
                word.charAt(charToFind + 1)); 
          } 
          // B 
          else if (grid[row - 1][col] == word.charAt(charToFind)) 
          { 
            gridCheck(row - 1, col, word, word.charAt(charToFind + 1)); 
          } 
          // D 
          if (grid[row][col - 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row, col - 1, word, word.charAt(charToFind + 1)); 
          } 
        } 
        // letter is on top row of grid 
        if (row == 0 && col > 0 && col < this.rowLength) 
        { 
          // D 
          if (grid[row][col - 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row, col - 1, word, word.charAt(charToFind + 1)); 
          } 
          // E 
          else if (grid[row][col + 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row, col + 1, word, word.charAt(charToFind + 1)); 
          } 
          // F 
          else if (grid[row + 1][col - 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row + 1, col - 1, word, 
                word.charAt(charToFind + 1)); 
          } 
          // G 
          else if (grid[row + 1][col] == word.charAt(charToFind)) 
          { 
            gridCheck(row + 1, col, word, word.charAt(charToFind + 1)); 
          } 
          // H 
          else if (grid[row + 1][col + 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row + 1, col + 1, word, 
                word.charAt(charToFind + 1)); 
          } 
        } 
        // Letter is on bottom row of grid 
        if (row == this.noOfRows && col > 0 && col < this.rowLength) 
        { 
          // FOR CODES SEE CHECKPLACES.TXT 
          // A 
          if (grid[row - 1][col - 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row - 1, col - 1, word, 
                word.charAt(charToFind + 1)); 
          } 
          // B 
          else if (grid[row - 1][col] == word.charAt(charToFind)) 
          { 
            gridCheck(row - 1, col, word, word.charAt(charToFind + 1)); 
          } 
          // C 
          else if (grid[row - 1][col + 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row - 1, col + 1, word, 
                word.charAt(charToFind + 1)); 
          } 
          // D 
          else if (grid[row][col - 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row, col - 1, word, word.charAt(charToFind + 1)); 
          } 
          // E 
          else if (grid[row][col + 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row, col + 1, word, word.charAt(charToFind + 1)); 
          } 
        } 
        // Letter is on Leftmost column of grid 
        if (col == 0 && row > 0 && row < this.noOfRows) 
        { 
          // B 
          if (grid[row - 1][col] == word.charAt(charToFind)) 
          { 
            gridCheck(row - 1, col, word, word.charAt(charToFind + 1)); 
          } 
          // C 
          else if (grid[row - 1][col + 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row - 1, col + 1, word, 
                word.charAt(charToFind + 1)); 
          } 
          // E 
          else if (grid[row][col + 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row, col + 1, word, word.charAt(charToFind + 1)); 
          } 
          // G 
          else if (grid[row + 1][col] == word.charAt(charToFind)) 
          { 
            gridCheck(row + 1, col, word, word.charAt(charToFind + 1)); 
          } 
          // H 
          else if (grid[row + 1][col + 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row + 1, col + 1, word, 
                word.charAt(charToFind + 1)); 
          } 
        } 
        // Letter is on rightmost column of grid 
        if (col == this.rowLength && row > 0 && row < this.noOfRows) 
        { 
          // A 
          if (grid[row - 1][col - 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row - 1, col - 1, word, 
                word.charAt(charToFind + 1)); 
          } 
          // B 
          else if (grid[row - 1][col] == word.charAt(charToFind)) 
          { 
            gridCheck(row - 1, col, word, word.charAt(charToFind + 1)); 
          } 
          // D 
          else if (grid[row][col - 1] == word.charAt(charToFind)) 
          { 
            gridCheck(row, col - 1, word, word.charAt(charToFind + 1)); 
          } 

        } 
      } 

      // If word is not found 
      return false; 
    } 

다음과 같이 그리드 오브젝트 [우리가 그것을 필요로 넣다]의 생성자입니다 -

/** 
    * Constructs a new LetterGrid Object 
    * 
    * @param letterGridFile 
    *   the file to use 
    */ 
    public LetterGrid(String letterGridFile) 
    { 
      try 
      { 
        // Init Scanner 
        Scanner fileIn = new Scanner(new File(letterGridFile)); 
        // Init ArrayList 
        ArrayList<String> nextLine = new ArrayList<String>(10); 
        // Read Data 
        while (fileIn.hasNextLine()) 
        { 
          nextLine.add(fileIn.nextLine()); 
          noOfRows++; 
        } 
        fileIn.close(); 

        rowLength = nextLine.size(); 
        grid = new char[noOfRows][rowLength]; 
        // Add data to grid 
        for (int rowCount = 0; rowCount < noOfRows; rowCount++) 
        { 

          grid[rowCount] = (nextLine.get(rowCount).toCharArray()); 
        } 
      } 
      // In case file name is mistyped or nonexistent 
      catch (IOException exp) 
      { 
        System.out.println("Oops, something went wrong."); 
        System.out.println("--> File Not Found"); 
      } 

    } 

을 마지막 참조가 내가 검색을 사용하고 있습니다 :

Places to check    the X = current letter to check around 
Row Col Code    A B C 
-1 -1 A 
-1  0 B    D X E 
-1  1 C 
0 -1 D    F G H 
0  1 E 
1 -1 F 
1  0 G 
1  1 H 

여러분 모두에게 감사합니다 =)

내가 볼

-Apok

답변

0

그럼 우선 나는 당신의 코드는 다음에 단어 "컴퓨터"를 계산합니다 생각입니다 :는 X의 임의의 문자입니다

xxxxxxcxxxxxx 
xxxxxoxxxxxxx 
xxxxxmxxxxxxx 
xxxxxxpuxxxxx 
xxxxxretxxxxx 

.

나는 당신이 전화 사이 검색의 방향을 보존 뭔가를 구현해야한다고 생각합니다.

예를 들어 격자의 한 지점에서 "컴퓨터"라는 단어의 첫 글자를 찾은 다음 북서쪽으로 'o'를 찾으면 코드는 계속 NW 방향으로 검색해야합니다 단어에없는 문자를 발견 할 때까지 (또는 물론 "벽"을 치는 경우)

0

범위 내에서 검색을 제한하기 위해 추가 코드를 추가해야하지만 이렇게하는 것이 트릭 일 수 있습니다. 그리드의 (또한 이것은 C#은 자바가 아니지만 거의 동일합니다)

private Point FindWordInWordSeacrh(String word) 
    { 
     char[,] grid = new char[10, 10]; 


     for (int x = 0; x < 10; x++) 
     { 
      for (int y = 0; y < 10; y++) 
      { 
       int iWOrdCharIndex = 0; 
       if (grid[x,y] == word[iWOrdCharIndex]) 
       { 
        // if the first letter of the word is found 
        iWOrdCharIndex++; 

        // Set the direction vector to continue the search 
        for (int xDir = -1; xDir <= 1; xDir++) 
        { 
         for (int yDir = -1; yDir <= 1; yDir++) 
         { 
          // Diretion vector set so check for remaining letters of word 
          for (int iCharPos = 1; iCharPos < word.Length; iCharPos++) 
          { 
           // Check the next letters of the word along this direction vector 
           if (grid[x+xDir, y+yDir] != word[iCharPos]) 
           { 
            // break loop and chnage direction vector if looking in wrong direction 
            break; 
           } 
           else if (iCharPos == word.Length) 
           { 
            // retun the starting point if word found 
            return new Point(x, y); 
           } 
          } 
         } 
        } 
       } 

      } 
     } 
     return null; 
    }