2014-01-18 38 views
2

我一直在努力尋找這個wordsearch項目幾天了,只是試圖讓水平搜索工作。它意味着在所有8個可能的方向上工作(水平,垂直,對角線)。這是我目前的代碼。Java字搜索字符排列

現在我只是擔心水平,因爲我懷疑如果我比較下來,其餘的會更簡單。

我的意思是寫代碼,找到板陣列中包含的單詞,並將這些字符輸出到與板陣列大小相同的另一個陣列(因此,輸出陣列是板陣列的解決方案) 。

到目前爲止,我所有的代碼遍歷整個電路板,然後檢查它是否與單詞表的第一個字符匹配,如果是,那麼這個字符被分配在輸出數組上,最終打印在輸出數組中結束到控制檯供用戶查看。

我的問題是,我怎樣才能轉發我的搜索迭代wordlist以及?我的方法錯了嗎?例如,如果board char與wordlist中的char相匹配,則繼續按照指定的方向(在我的情況下,我擔心水平方向)並找到該單詞。

另外,方法'過濾器'是爲了處理exceptionOutofBounds以防萬一搜索失敗。

歡迎任何想法或方法。

+0

你有那都應該是在網格中發現某處,或者這是一個「查找所有有效的話」對一個非常大的字典的字的簡短列表有效的單詞? –

+0

@TeresaCarrigan是的,我有一個應該在網格上找到的單詞的簡短列表。網格和單詞列表都在同一個文件中。 – fzxt

+1

解決問題後,請不要刪除您的問題。相反,請標記適當的答案。 –

回答

5

這裏是搜索在不同的方向上字的網格的例子。我已經實施了其中三項,並讓其中三項完成。按照我個人的喜好,我會使用一個字符串數組來代替wordList的鋸齒數組,但是我選擇了OP的實現方式。我做了一個簡單的版本,使用4 x 4網格和3個單詞列表。請注意,我在輸出板上調用fillWithSpaces()。這對格式化至關重要。

我有一個名爲 「board.txt」

dcat 
aoiq 
eigk 
snur 

的文本文件和文本文件「字樣。TXT」

dog 
cat 
runs 

這裏是程序的輸出:

DCAT 
-O-- 
--G- 
SNUR 

我的策略是搜索板一個單詞的第一個字母。一旦我找到它,我更新的靜態字段foundRow和foundColumn。當我使用不同的單詞時,我更新靜態字段currentWord。當我找到匹配的字母時,我有六種不同的方法,checkForwards()checkBackwards()等等(還有其他方法可以做到這一點,但我我試圖讓這個例子儘可能的清晰。

這裏我是向後檢查的方法。由於我已經知道第一個字母匹配,所以我從第二個字符開始(索引1)。對於每個新字符,我都會在比較這些值之前檢查它是否在板上。 (還有更好的方法來做到這一點)。如果有任何失敗,我會回來。如果所有字符匹配,我一次只複製一個字符。

static void checkBackwards() 
{ 
    for(int i = 1; i < wordList[currentWord].length; i++) 
    { 
     if(foundColumn - i < 0) return; 
     if(wordList[currentWord][i] != board[foundRow][foundColumn - i]) return; 
    } 
    //if we got to here, update the output 
    for(int i = 0; i < wordList[currentWord].length; i++) 
    { 
     output[foundRow][foundColumn - i] = wordList[currentWord][i]; 
    } 
    return; 
} 

這裏是源代碼:

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

public class Wordsearch 
{ 
    static Scanner input; 
    static char[][] wordList; 
    static char[][] board; 
    static char[][] output; 

    static int foundRow; 
    static int foundColumn; 
    static int currentWord; 

    public static void main(String[] args) throws FileNotFoundException 
    { 
     File wordInput = new File("words.txt"); 
     File boardInput = new File("board.txt"); 
     if(!wordInput.exists() || !boardInput.exists()) 
     { 
      System.out.println("Files do not exist."); 
      System.exit(1); 
     } 

     wordList = new char[3][]; //word list matrix 
     board = new char[4][4]; //board or grid matrix 
     output= new char[4][4]; //solved puzzle 

     fillWithSpaces(output); 

     input = new Scanner(wordInput); 
     for(int i = 0; i < wordList.length; i++) 
     { 
      wordList[i] = input.nextLine().toUpperCase().toCharArray(); 
     } 

     input = new Scanner(boardInput); 
     for(int i = 0; i < board[0].length; i++) 
     { 
      board[i] = input.nextLine().toUpperCase().toCharArray(); 
     } 

     for(int i = 0; i < wordList.length; i++) 
     { 
      currentWord = i; 
      if(findFirstLetter()) 
      { 
       checkEachDirection(); 
      } 
     } 

     print(output); 
    } 

    static boolean findFirstLetter() 
    { 
     for(int r = 0; r < board.length; r++) 
     { 
      for(int c = 0; c < board.length; c++) 
      { 
       if(wordList[currentWord][0] == board[r][c]) 
       { 
        foundRow = r; 
        foundColumn = c; 
        return true; 
       } 
      } 
     } 
     return false; 
    } 

    static void checkEachDirection() 
    { 
     checkForwards(); 
     checkBackwards(); 
     //checkUp(); 
     //checkDown(); 
     checkDiagonalDown(); 
     //checkDiagonalUp(); 
    } 

    static void checkForwards() 
    { 
     for(int i = 1; i < wordList[currentWord].length; i++) 
     { 
      if(foundColumn + i > board.length - 1) return; 
      if(wordList[currentWord][i] != board[foundRow][foundColumn + i]) return; 
     } 
     //if we got to here, update the output 
     for(int i = 0; i < wordList[currentWord].length; i++) 
     { 
      output[foundRow][foundColumn + i] = wordList[currentWord][i]; 
     } 
     return; 
    } 

    static void checkBackwards() 
    { 
     for(int i = 1; i < wordList[currentWord].length; i++) 
     { 
      if(foundColumn - i < 0) return; 
      if(wordList[currentWord][i] != board[foundRow][foundColumn - i]) return; 
     } 
     //if we got to here, update the output 
     for(int i = 0; i < wordList[currentWord].length; i++) 
     { 
      output[foundRow][foundColumn - i] = wordList[currentWord][i]; 
     } 
     return; 
    } 

    static void checkDiagonalDown() 
    { 
     for(int i = 1; i < wordList[currentWord].length; i++) 
     { 
      if(foundColumn + i > board.length - 1) return; 
      if(foundRow + i > board.length - 1) return; 
      if(wordList[currentWord][i] != board[foundRow + i][foundColumn + i]) return; 
     } 
     //if we got to here, update the output 
     for(int i = 0; i < wordList[currentWord].length; i++) 
     { 
      output[foundRow + i][foundColumn + i] = wordList[currentWord][i]; 
     } 
     return; 
    } 

    static void print(char[][] board) 
    { 
     for(int i = 0; i < board.length; i++) 
     { 
      for(int j = 0; j < board.length; j++) 
      { 
       System.out.print(board[i][j]); 
      } 
      System.out.println(); 
     } 
     System.out.println(); 
    } 

    static void fillWithSpaces(char[][] board) 
    { 
     for(int i = 0; i < board.length; i++) 
     { 
      for(int j = 0; j < board.length; j++) 
      { 
       board[i][j] = '-'; 
      } 
     } 
    } 
} 
+0

我試圖實現這個確切的方法,只是不知道從哪裏開始。非常感謝。 – fzxt

+0

我很高興它有幫助! –

1

考慮下面的程序:

import java.util.ArrayList; 

public class WordSearch { 

    static char[][] board; 
    static int board_x, board_y; 
    static ArrayList<String> search_words; 

    public static void main(String args[]) 
    { 
     board = new char[][]{ 
      { 's', 't', 'a', 'c', 'k' }, 
      { 'x', 'f', 'l', 'o', 'w' }, 
      { 'x', 'x', 'x', 'v', 'x' }, 
      { 'x', 'x', 'x', 'e', 'x' }, 
      { 'x', 'x', 'x', 'r', 'x' }, 
     }; 
     // You could also get these from board.size, etc 
     board_x = 5;  
     board_y = 5; 

     search_words = new ArrayList<String>(); 
     search_words.add("stack"); 
     search_words.add("over"); 
     search_words.add("flow"); 
     search_words.add("not"); 

     for(String word : search_words){ 
      find(word); 
     } 
    } 

    public static void find(String word) 
    { 
     // Search for the word laid out horizontally 
     for(int r=0; r<board_y; r++){ 
      for(int c=0; c<=(board_x - word.length()); c++){ 
       // The pair (r,c) will always be where we start checking from 
       boolean match = true; 
       for(int i=0; i<word.length(); i++){ 
        if(board[r][c + i] != word.charAt(i)){ 
         match = false; 
         System.out.format(" '%s' not found starting at (%d,%d) -- first failure at %d\n", word, r, c, i); 
         break; 
        } 
       } 

       if(match){ 
        System.out.format("Found match (horizontal) for '%s' starting at (%d,%d)\n", word, r, c); 
       } 
      } 
     } 
    } 
} 

該板是2維字符數組和您要搜索的單詞列表稱爲search_words一個ArrayList。

經過板子和search_words列表的一些簡單的樣例初始化後,它遍歷列表中的單詞,搜索每個單詞是否水平放置。

這個想法可以擴展到垂直或對角搜索以及一些調整。

這裏的邏輯是你應該從示例程序中拿走,而不一定是結構。如果我這樣做是因爲任何嚴重的事情,我可能會有一個Board類,可能有一個.find(word)方法。

最後,詳細的輸出是:

Found match (horizontal) for 'stack' starting at (0,0) 
    'stack' not found starting at (1,0) -- first failure at 0 
    'stack' not found starting at (2,0) -- first failure at 0 
    'stack' not found starting at (3,0) -- first failure at 0 
    'stack' not found starting at (4,0) -- first failure at 0 
    'over' not found starting at (0,0) -- first failure at 0 
    'over' not found starting at (0,1) -- first failure at 0 
    'over' not found starting at (1,0) -- first failure at 0 
    'over' not found starting at (1,1) -- first failure at 0 
    'over' not found starting at (2,0) -- first failure at 0 
    'over' not found starting at (2,1) -- first failure at 0 
    'over' not found starting at (3,0) -- first failure at 0 
    'over' not found starting at (3,1) -- first failure at 0 
    'over' not found starting at (4,0) -- first failure at 0 
    'over' not found starting at (4,1) -- first failure at 0 
    'flow' not found starting at (0,0) -- first failure at 0 
    'flow' not found starting at (0,1) -- first failure at 0 
    'flow' not found starting at (1,0) -- first failure at 0 
Found match (horizontal) for 'flow' starting at (1,1) 
    'flow' not found starting at (2,0) -- first failure at 0 
    'flow' not found starting at (2,1) -- first failure at 0 
    'flow' not found starting at (3,0) -- first failure at 0 
    'flow' not found starting at (3,1) -- first failure at 0 
    'flow' not found starting at (4,0) -- first failure at 0 
    'flow' not found starting at (4,1) -- first failure at 0 
    'not' not found starting at (0,0) -- first failure at 0 
    'not' not found starting at (0,1) -- first failure at 0 
    'not' not found starting at (0,2) -- first failure at 0 
    'not' not found starting at (1,0) -- first failure at 0 
    'not' not found starting at (1,1) -- first failure at 0 
    'not' not found starting at (1,2) -- first failure at 0 
    'not' not found starting at (2,0) -- first failure at 0 
    'not' not found starting at (2,1) -- first failure at 0 
    'not' not found starting at (2,2) -- first failure at 0 
    'not' not found starting at (3,0) -- first failure at 0 
    'not' not found starting at (3,1) -- first failure at 0 
    'not' not found starting at (3,2) -- first failure at 0 
    'not' not found starting at (4,0) -- first failure at 0 
    'not' not found starting at (4,1) -- first failure at 0 
    'not' not found starting at (4,2) -- first failure at 0 
0

,你可以嘗試一下本作水平搜索 串詞= 「」; 爲(字符串關鍵字:字){

 // STEP1: Find *************IF ******************* The word is in 
     // the Array 
     // CODE HERE 
     boolean found = false; 
     for (int i = 0; i < puzzle.length; i++) { 
      String rowString = ""; 
      for (int j = 0; j < puzzle[i].length; j++) { 
       rowString += puzzle[i][j]; 
       if (rowString.contains(keyWord) && !found) { 

        System.out.println(keyWord); 
        int index = rowString.indexOf(keyWord); 
        rowString.indexOf(keyWord); 
        // int length = keyWord.length(); 
        for (int ii = 0; ii < keyWord.length(); ii++) { 
         solutionArray[i][index + ii] = keyWord.charAt(ii); 
         rowString += puzzle[i][j]; 
         System.out.println(); 
         // length--; 
        } 
        found = true; 
       } 

      } 
     }