2014-04-10 3 views
2

나는 스도쿠 해결 프로그램을 쓰고 있어요,하지만 나는 다음과 같은 오류가 발생하고 있습니다를 찾을 수 없습니다. 내 프로그램의 코드는 다음과 같습니다 :는 실수 (StackOverflowError가)

package code; 

import java.awt.Color;           //Import nötiger Klassen 
import javax.swing.*; 
import javax.swing.border.Border; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 




public class SOLVE implements ActionListener{ 

private int[][] matrix;       //Eingelesenes Sudokufeld 
private boolean[][]input;      //Überfrüfungsfeld zur bestimmung von eingegebenen Zahlen 

private JFrame fenster;       //GUI Fenster 
private JTextArea field[][];     //GUI Text Sudoku Feld 
private JButton start;       //Start Button 
private JButton reset; 


public void gui(){           //Erstellt die Grafische Oberfläche 
    Border b1 = BorderFactory.createLineBorder(Color.black); 

    //Erstellt Fenster 
    fenster = new JFrame(); 
    fenster.setLocation(100, 100); 
    fenster.setSize(500, 320); 
    fenster.getContentPane().setLayout(null); 
    fenster.setTitle("Sudoku Solver"); 

    //Erstellt Sudoku TextFelder 
    field = new JTextArea[9][9]; 
    int x = 0;  //x-Koordinate 
    for(int i=0;i<9;i++){ 
     int y = 0;  //y-Koordinate 
     for(int j=0;j<9;j++){ 
      field[i][j] = new JTextArea(); 
      field[i][j].setLocation((200+x),(30+y)); 
      field[i][j].setSize(20, 20); 
      field[i][j].setText("0"); 
      field[i][j].setBorder(b1); 
      field[i][j].setVisible(true); 
      fenster.add(field[i][j]); 
      if(j==2||j==5){ 
       y=y+32; 
      } 
      else{ 
       y=y+25; 
      } 
     } 
     if(i==2||i==5){ 
      x=x+32; 
     } 
     else{ 
      x=x+25; 
     } 
    } 

    //Erstellt Start Button 
    start = new JButton(); 
    start.setText("Solve"); 
    start.setSize(150,50); 
    start.setLocation(30,30); 
    start.addActionListener(this); 

    //Erstellt Reset Button 
    reset = new JButton(); 
    reset.setText("Reset"); 
    reset.setSize(150,50); 
    reset.setLocation(30,100); 
    reset.addActionListener(this); 


    //Fügt Komponenten zum Fenster hinzu 
    fenster.add(reset); 
    fenster.add(start); 
    fenster.setVisible(true); 
} 

public SOLVE(){            //Startet Klasse 
    gui(); 
    matrix = new int[9][9]; 
    input = new boolean[9][9]; 
} 

public void copy(){           //Einlesen der TextFelder in die Matrix une Überprüfungs Matrix 
    for(int i=0;i<9;i++){ 
     for(int j=0;j<9;j++){ 
      matrix[i][j]=Integer.parseInt(field[i][j].getText()); 
      if(matrix[i][j]!=0){ 
       input[i][j]=false; 
      } 
      else{ 
       input[i][j] = true; 
      } 
     } 
    } 
} 

public boolean check(int x,int y, int solution){    //Überprüfung ob Lösung möglich ist für eine bestimmte Zahl bei einem bestimmten Feld 
    boolean moeglich = false; //Insgesammt möglich 
    boolean wmoeglich = true; //Waagrecht möglich 
    boolean smoeglich = true; //Senkrecht möglich 
    boolean fmoeglich = true; //Feld möglich 

    //Überprüft waagrecht und senkrecht ob möglich 
    for(int i=0;i<9;i++){ 
     if(matrix[x][i]==solution){ 
      wmoeglich = false; 
     } 
     if(matrix[i][y]==solution){ 
      smoeglich = false; 
     } 
    } 
    //Sucht nötiges Feld und überprüft dieses 

    //Oberste Reihe 
    if(x<3){ 
     //linkes Feld 
     if(y<3){ 
      for(int i=0;i<3;i++){ 
       for(int j=0;j<3;j++){ 
        if(matrix[i][j]==solution){ 
         fmoeglich = false; 
        } 
       } 
      } 
     } 
     //mittleres Feld 
     else if(y<6&& y>2){ 
      for(int i=0;i<3;i++){ 
       for(int j=3;j<6;j++){ 
        if(matrix[i][j]==solution){ 
         fmoeglich = false; 
        } 
       } 
      } 
     } 
     //rechtes Feld 
     else if(y<9 && y>5){ 
      for(int i=0;i<3;i++){ 
       for(int j=6;j<9;j++){ 
        if(matrix[i][j]==solution){ 
         fmoeglich = false; 
        } 
       } 
      } 
     } 
    } 
    //mittlere Reihe 
    else if(x<6 && x>2){ 
     //linkes Feld 
     if(y<3){ 
      for(int i=3;i<6;i++){ 
       for(int j=0;j<3;j++){ 
        if(matrix[i][j]==solution){ 
         fmoeglich = false; 
        } 
       } 
      } 
     } 
     //mittleres Feld 
     else if(y<6 && y>2){ 
      for(int i=3;i<6;i++){ 
       for(int j=3;j<6;j++){ 
        if(matrix[i][j]==solution){ 
         fmoeglich = false; 
        } 
       } 
      } 
     } 
     //rechtes Feld 
     else if(y<9 && y>5){ 
      for(int i=3;i<6;i++){ 
       for(int j=6;j<9;j++){ 
        if(matrix[i][j]==solution){ 
         fmoeglich = false; 
        } 
       } 
      } 
     } 
    } 
    //unterste Reihe 
    else if(x<9 && x>5){ 
     //linkes Feld 
     if(y<3){ 
      for(int i=6;i<9;i++){ 
       for(int j=0;j<3;j++){ 
        if(matrix[i][j]==solution){ 
         fmoeglich = false; 
        } 
       } 
      } 
     } 
     //mittleres Feld 
     else if(y<6 && y>2){ 
      for(int i=6;i<9;i++){ 
       for(int j=3;j<6;j++){ 
        if(matrix[i][j]==solution){ 
         fmoeglich = false; 
        } 
       } 
      } 
     } 
     //rechtes Feld 
     else if(y<9 && y>5){ 
      for(int i=6;i<9;i++){ 
       for(int j=6;j<9;j++){ 
        if(matrix[i][j]==solution){ 
         fmoeglich = false; 
        } 
       } 
      } 
     } 
    } 

    //wenn eine Überprüfung fehlgeschlagen Lösung nicht möglich 
    if(smoeglich == true && wmoeglich == true && fmoeglich == true){ 
     moeglich = true; 
    } 
    return moeglich; 
} 

public void search(int i, int j){        //Lösen des Sudokus (Eigentlich Rekursiv) 
    if(input[i][j]==true){ //Wenn keine menschliche eingabe 
     int l = 1; 
     for(l=1;l<10;l++){ 
      if(check(i,j,l)==true){ //Wenn Überprüfung erfolgreich war 
        matrix[i][j]=l; //Schreibt Lösung in Matrix 
        field[i][j].setText(""+l); //Und Schreibt Lösung in TextFeld 

        //Nachfolger suchen 
        if(i<=7){ 
         search(i+1,j); 
        } 
        else if(i==8 && j<=7){ 
         search(i-8,j+1); 
        } 
        else{ 
         break; 
        } 
      } 
      //Da Rekursion nicht funktionierte hier den Schritt zurück 
      else if(check(i,j,l)==false && l==9){ 
       matrix[i][j]=0; 
       if(i>0){ 
        search(i-1,j); 
       } 
       else if(i==0 && j>0){ 
        search(i+8,j-1); 
       } 
       else{ 
        break; 
       } 
      } 
     } 
    } 
    else{ 
     //Nachfolger suchen 
     if(i<=7){ 
      search(i+1,j);  
     } 
     else if(i==8 && j<=7){ 
      search(i-8,j+1); 
     } 
     else{} 
    } 
} 

public void reset(){           //Resettet Fenster und Matrix 
    for(int i=0;i<9;i++){ 
     for(int j=0;j<9;j++){ 
      field[i][j].setText("0"); 
      matrix[i][j]=0; 
      input[i][j]=true; 
     } 
    } 
} 

public void master(){           //Master Methode (Führt die nötigen Methoden nacheinander aus) 
    System.gc(); 
    copy(); 
    search(0,0);  
} 

public static void main(String[] args) {      //Main Methode (Start Methode) 
    new SOLVE(); 
} 

public void actionPerformed(ActionEvent evt){     //Button Abfrage 
    Object source = evt.getSource(); 

    if(source==start){ //Wenn Button 'start' keklckt wird Methode main ausführen 
     master(); 
    } 
    else if(source == reset){ 
     reset(); 
    } 
} 
} 

이 오류의 원인은 무엇입니까? 코드의 주석이 독일어로되어있어서 유감이지만, 원한다면 영어로 번역 할 수 있습니다.

+0

문제를 진단하기에 충분한 stacktrace를 게시하지 않았습니다. StackOverflowErrors를 사용하면 추적에서 전체 사이클을 찾아야합니다. –

+0

'search()'가 재귀 적으로 자신을 호출하는 것처럼 보입니다. 내 추측은 너무 깊어서 스스로를 호출하고 스택 메모리를 소모한다는 것입니다. –

+0

@stvcisco 아니, 그게 틀림 없어. stacktrace를보십시오. –

답변

0

"AWT-EventQueue-0"예외는 HashMap과 비동기적인 작업을 수행 할 때의 예외입니다. 고도은 모델에서보기를 분리하는 것이 좋습니다! 필요한 경우에,뿐만 아니라 컨트롤러를 분리 :

http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

당신이 실행 중이 야 줄 것을에서 번호를받을 수 있나요? 이것이 실제로 문제가 시작되는 지점을 식별하는 데 도움이됩니다. 또한 코드에서 디버깅을 할 수 있습니다. 나는 Eclipse (Indigo)를 추천한다. 좋은 디버거가 내장되어 있고, 다른 많은 것들이 많이있다. 그것은 당신에게서 자랍니다! 질문 양식 다음


시도 :

https://stackoverflow.com/questions/how-to-ask

는 전혀 관련이 코드의 3 페이지를 떠날 때 정말 열심히 때문에! (언어 정보, 독일어의 주석과 변수 이름은 이해하기가 어렵지 않습니다.) 내 말은 내가 알고있는 것입니다. 무엇을 당신은 모든 사람들을 위해 이름이 "asdf"또는 "foobar" 나는 걱정한다.)

또한 클래스 변수의 감속 후에 첫 번째 메소드로 클래스의 생성자를 넣으십시오. 이렇게하면 신속하게 식별하기가 훨씬 쉬워집니다.

+0

AWT-EventQueue-0는 이벤트 발송 스레드의 이름이며 GUI 이벤트 또는 이벤트 대기열에 게시 된 실행 파일의 모든 추적이 표시됩니다. 그게 "HashMap과 비동기적인 일을하는 것"과 무슨 관련이 있습니까? 그게 무슨 뜻이야? –

+0

네가 맞아. 내 잘못이야! 나는 빠른 검색을했고 제대로 읽지 않았다. – Lurvas777