2013-10-02 3 views
1

저는 크로스 워드 퍼즐 솔버를 프로그래밍하고 있으며, 거의 98 %의 코드가 1 글리치를 제외하고 작동합니다. 나는 파일에서 1 단어를 읽은 다음 그 문자열을 문자 배열과 비교하여 배열에서 그 문자열이 어디에 있는지 찾아냅니다. 지금까지는 모든 단어를 읽고 그 위치를 찾았지만 내 문제는 그것이 내 목록에서 3 단어를 건너 뛴다는 것입니다. 나는 단어가 char 배열에 있다는 것을 알고 있지만 어떤 이유인지 그들은 선택되지 않았다. 어떤 아이디어?루프는 모든 것을 처리하지 않습니다

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


    public class Test { 

     public static void main(String[] args) throws Exception{ 
      File file = new File("puzzle.txt"); 
      Scanner sc = new Scanner(file); 
      int row = sc.nextInt(); 
      int col = sc.nextInt(); 
      sc.nextLine(); 

      //Make an array to hold the puzzle 
      char[][] puzzle = new char[row][col]; 


      //Read in the strings from the file into the array. 
      for (int i=0; i<row; i++){ 
       String getChar = (new String(sc.next())); 
       for(int j = 0;j<col; j++){ 
        puzzle[i][j] = getChar.charAt(j); 
        } 
       } 

      //Read the number of words and move to the next line  
      int numwords = sc.nextInt(); 
      sc.nextLine(); 


      //look for each word 
      for(int i=0; i<numwords; i++){ 
       String word = new String(); 
       word = sc.nextLine(); 
       System.out.printf("This is word: %s\n", word); 

       //arrays to hold the direction. 
       int [] movx ={-1, -1, -1, 0, 0, 1, 1, 1}; 
       int [] movy ={-1, 0, 1, -1, 1, -1, 0, 1}; 

       //this variable will hold if we found or not the string in the puzzle 
       boolean found = false; 

       //find the words in the puzzle 
       for(int m = 0; m < puzzle.length; m++) { 
        for(int n = 0; n < puzzle[0].length; n++) { 
         if(puzzle[m][n] == word.charAt(0)){ 
          for (int o = 0; o < 8; o++){ 
           if(check(m, n, word, puzzle, movx[o], movy[o])){ 
            System.out.printf("%s found at position (%d, %d)\n\n", word, m, n); 
            found = true; 
            break; 
            } 
          } 

         } 
        } 
       } 
       if (!found){ 
        System.out.printf("%s was not found\n\n", word); 
       } 
      } 

     //Close the scanner  
      sc.close();   
     } 
     //This is your generic-direction function 
     public static boolean check(int row, int col, String word, char[][] puzzle, int offsetx, int offsety){ 

      //start with the current position 
      int x = row; 
      int y = col; 

      for (int i = 0; i < word.length(); i++){ 
       char c = word.charAt(i); 

       //Is not equal 
       if (puzzle[x][y] != c) return false; 

       x += offsetx; 
       y += offsety; 

       //check the boundaries, if we go out then we didn't find the word; 
       if (x < 0 || x >= puzzle.length || y < 0 || y >= puzzle[x].length) return false; 
      } 

      return true; 
     } 

    } 

(편집 영역) 인쇄 문 내 진단에서

, 내가 3, 8, 9 경우, wordsearch 단어를 찾을 수 없습니다 :

여기 내 코드입니다. 그리고 나는이 루프 이후에 있다고 생각한다 :

이 루프 이전에는 나의 이전 루프가 모든 단어를 통과하기 때문에. 그리고이 루프 안에서 단어가 루프 안으로 전달됩니다. 배열의 모든 지점을 통해 모든 수표를 검사하고 퍼즐에 있더라도 거짓으로 전달합니다.

10 10 
    WVERTICALL 
    ROOAFFLSAB 
    ACRILIATOA 
    NDODKONWDC 
    DRKESOODDK 
    OEEPZEGLIW 
    MSIIHOAERA 
    ALRKRRIRER 
    KODIDEDRCD 
    HELWSLEUTH 
    10 
    WEEK 
    FIND 
    RANDOM 
    SLEUTH 
    BACKWARD 
    VERTICAL 
    DIAGONAL 
    WIKIPEDIA 
    HORIZONTAL 
    WORDSEARCH 

이 (WEEK가 아닌가 인쇄 무엇 :

(/ 편집 영역)

이 루프 전에, 나는

내가에서 읽고 있어요 파일 테스트 퍼즐) :

This is word: WEEK and this is i 0 
    WEEK was not found 

    This is word: FIND and this is i 1 
    FIND found at position (1, 4) 

    This is word: RANDOM and this is i 2 
    RANDOM found at position (1, 0) 

    This is word: SLEUTH and this is i 3 
    SLEUTH was not found 

    This is word: BACKWARD and this is i 4 
    BACKWARD found at position (1, 9) 

    This is word: VERTICAL and this is i 5 
    VERTICAL found at position (0, 1) 

    This is word: DIAGONAL and this is i 6 
    DIAGONAL found at position (8, 6) 

    This is word: WIKIPEDIA and this is i 7 
    WIKIPEDIA found at position (9, 3) 

    This is word: HORIZONTAL and this is i 8 
    HORIZONTAL was not found 

    This is word: WORDSEARCH and this is i 9 
    WORDSEARCH was not found 

이 그것을 찾을 수없는 단어는 다음과 같습니다

SLEUTH is at position (9,4) 
    HORIZONTAL is at position (9,0) 
    WORDSEARCH is at position (0,0) 

팁, 트릭 또는 아이디어는 매우 높이 평가됩니다! 수표 방법에

+0

이후에 if (i == word.length() - 1) return true;을 입력하십시오. 왜 char 비교로 char을 수행합니까? String 클래스에서 제공하는 메서드를 살펴보십시오. 'equals' –

+0

당신의'movx'와'movy' 값은 그 단어들에 대해 올바르지 않습니다. –

+0

@DavidB 내 수정 사항을 확인하십시오. – Drieke

답변

4

:

//check the boundaries, if we go out then we didn't find the word; 
if (x < 0 || x >= puzzle.length || y < 0 || y >= puzzle[x].length) return false; 

하면, y 값을 사용자의 X를 증가 한 후 퍼즐 경계를 초과했습니다 있는지 확인하고 있습니다. 문제는 발견 할 수없는 세 단어에 대한 것이고, 마지막 문자를 찾고, 카운터를 증가시키고, 퍼즐의 가장자리를 벗어 났으며, 이미 마지막 문자를 찾았음에도 false를 반환한다고 결정합니다.

는 다음과 같이, 루프의 시작 상태를 이동하십시오 :없는 무엇

for (int i = 0; i < word.length(); i++){ 
    //check the boundaries, if we go out then we didn't find the word; 
    if (x < 0 || x >= puzzle.length || y < 0 || y >= puzzle[x].length) return false; 

    char c = word.charAt(i); 

    //Is not equal 
    if (puzzle[x][y] != c) return false; 

    x += offsetx; 
    y += offsety; 
} 
+0

감사합니다! 그 트릭을 :) – Drieke

1

check 방법의 조건이다. 단어의 마지막 문자가 가장자리에있을 때 다음 이웃 (바운드 검사)을 찾으려고하기 때문에 실패합니다. 이 조건을 추가하십시오 if (puzzle[x][y] != c) return false;

관련 문제