2013-04-18 4 views
0

나는 자바로 Sudoku Solver 프로그램을 만들고있다.이 프로그램은 역 추적과 bruteforce를 사용하여 알고리즘을 해결한다. 내가 가지고있는 것 같은 문제는 내가 간단한 컨테이너에 올바른 값을 삽입하면 잘못된 값이 다른 클래스에서 액세스하려고 할 때 표시된다는 것입니다. 제가 System.out.println(tmp[i][j].getValue());Square[][] tmp = solutionsHash.get(count)과 다음 (a for(int i = 0 i < dim; i++) + for(int j = 0; j < dim; j++)으로) Sudokucontainer에 삽입있어서 내부를 인쇄 할 때HashMap에 관한 약간의 지침을 찾아서

HashMap의 정확한 위치에 정확한 값을 갖는다. 내가 바로 Square 클래스 'fillInnRemainingOfBoard() (Square[][] tmp = solutions.solutionsHash.get(count)와 같은 System.out.println 문 다음에 solutions.insert 문 다음에 대해-루프 같은를 사용할 때 그것은 또한 성공합니다.

나는 Board 클래스에서 동일한 작업을 수행하려고 할 때 문제가 발생합니다 , 그 클래스에서 내 test() 메서드에서 볼 수 있습니다. 다음 그냥 미리 정의 된 값을 인쇄합니다 (또는 보드가 비어 있으면 아무것도.) 유일한 차이점은 다음 ie ie Square[][] tmp = allSquares[0][0].solutionsHash.get(wantedNumber) 및 같은 사용합니다.하지만 앞에서 언급했듯이 solutionsHash이 정적으로 선언 된 경우 차이가 발생해야하는 이유는 알 수 없습니다.

임의의 h 엘프는 크게 appriciated 있습니다. 감사!

참고 : 들여 쓰기가 어떻게 든 엉망이 됐어, 미안해. 필자는 GUI 파트를 작성하지 않았기 때문에 여기에 올릴 수있는 권한이 없을 수도 있습니다. 또한 Board 클래스에서 같은 값이 인쇄되기 때문에 문제가 발생하기 전에 (Board 클래스에있을 수 있음) 발생합니다.

편집 : 일부 불필요한 코드가 제거되었습니다.

public class Square { 

static Sudokucontainer solutions; 
protected char value; 
protected int valueInt; 
protected static Square allSquares[][]; 
public int count = 0; 
protected boolean predefined = false; 
protected boolean lastSquare = false; 
protected Square next = null; 


public void setNext() { 
if(col.getNr() < col.dim) { 
    next = allSquares[row.getNr()-1][col.getNr()]; 
}else if(col.getNr() == col.dim) { 
    if(row.getNr() < row.dim) { 
    next = allSquares[row.getNr()][0]; 
    }else { 
    next = null; 
    lastSquare = true; 
    } 
} 
if(next != null) { 
    if(next.col.getNr() == col.dim && next.row.getNr() == col.dim && next.predefined == true) 
    lastSquare = true; 
} 
} 
public void fillInnRemainingOfBoard(Square[][] allSquares, int hd, int br) { 

    this.allSquares = allSquares; 
int highestNumber = col.dim; 
setNext(); 

if(value == '\u0020') { 
    for(int i = 1; i <= highestNumber; i++) { 
    if(row.isLegal[i-1] && col.isLegal[i-1] && box.isLegal[i-1]) { 
     String s1 = Integer.toString(i); 
     value = s1.charAt(0); 
     insert(value); 
     insertInt(i); 
     box.isLegal[i-1] = false; 
     col.isLegal[i-1] = false; 
     row.isLegal[i-1] = false; 
     if(!lastSquare) { 
     next.fillInnRemainingOfBoard(this.allSquares, hd, br); 
     }else{ 
     System.out.println("Solution found: " + count); 
     solutions.insert(allSquares, count, col.dim); 
     ++count; 
     } // Slutt paa else 
     box.isLegal[valueInt-1] = true; 
     col.isLegal[valueInt-1] = true; 
     row.isLegal[valueInt-1] = true; 
     value = '\u0020'; 
     } // Slutt paa if(row.isLegal[i-1] && col.isLegal[i-1] && box.isLegal[i-1])  
    } // Slutt paa if (value == '\u0020' || !predefined) 
}else {  
    if(!lastSquare) { 
    next.fillInnRemainingOfBoard(this.allSquares, hd, br); 
    } else { 
    System.out.println("Solution found"); 
    solutions.insert(allSquares, count, col.dim); 
    count++; 
    } 
} // Slutt paa else if(predefined)  
// Slutt paa for(int i = 1; i <= highestNumber; i++) 
}// Her returnerer vi til forrige metode 

public void insert(char value) { 
if(value != '.') { 
    this.value = value; 

}else{ 
    this.value = '\u0020'; // In other words; space(in unicode) 
} 
} 

public void insertInt(int value) { 
valueInt = value; 
} 



public char getValue() { 
return value; 
} 

} 

public class Sudokucontainer { 

int dim = 0; 
private int count = 0; 
static HashMap<Integer, Square[][]> solutionsHash = new HashMap<Integer, Square[][]>(); 

public void insert(Square[][] allSquares, int count, int dim) { 
this.count = count; 
this.dim = dim; 

if(count <= 499) { 
    solutionsHash.put(count,allSquares); 
} 

} 

public Square[][] get(int nr) { 
Square[][] tmp = solutionsHash.get(nr); 
return tmp; 
} 

public int getSolutionCount() { 
return solutionsHash.size(); 
} 

} 

public class Board { 

int dim, br, hd; 
char[][] charArray; 
char[] charArray1; 
static Square [][] allSquares; 
Row [] rows; 
Column[] columns; 
Box[] boxes; 

    public Board(int dim, int br, int hd, char[][] charArray) { 
this.dim = dim; 
this.br = br; 
this.hd = hd; 
this.charArray = charArray; 
charArray1 = new char[dim]; 
allSquares = new Square[dim][dim]; 
rows = new Row[dim]; 
columns = new Column[dim]; 
boxes = new Box[dim/br * dim/hd]; 
setRows(); 
setColumns(); 
setBoxes(); 
setSquares(); 
insertBoxInSquares(); 
fillInLegalValues(); 
welcome(); 
solve(); 
test(); 
System.out.println("Found solutions. Please refer to the graphical interface."); 
showGui(); 


} 


public void test() { 
    for(int i = 0; i < dim; i++) { 
     for(int j = 0; j < dim; j++) { 
     //Square[][] tmp = allSquares[0][0].solutions.solutionsHash.get(0); 
     //System.out.println(tmp[i][j].getValue()); 
     } 
    } 
} 

public void solve() { 

allSquares[0][0].fillInnRemainingOfBoard(allSquares, hd, br); 

} 

public void showGui() { 
new SudokuGUI(dim, hd, br, allSquares[0][0].solutions.solutions, false, 0, allSquares[0][0].solutions.solutionsHash); 
} 

} 
+1

많은 코드가 있습니다. – maba

+2

불필요한 코드를 제거하려고합니다. – Promille

+0

@ user2291525 : 불필요한 코드를 제거하면 도움이 될 것입니다. –

답변

0

나는 무엇이 문제인지는 알지만 문제의 원인을 알려줄 수 있습니다.

다른 개체를 인쇄하거나 개체와 인쇄물간에 값이 변경됩니다. 객체에 해시 코드 또는 toString() 호출을 인쇄하여 객체를 식별 할 수 있으므로 다른 경우에 동일한 객체인지 여부를 알 수 있습니다.

다른 경우에는 한 가지 경우에는 임시 범위가있는 개체를로드하지 않고 다음 경우에는 더 큰 범위로 개체를 인쇄하지 않는지 확인합니다. 그러나 다른 객체를 참조하는 것일 수도 있습니다. 내가 게시 한 코드에서 무슨 일이 벌어지고 있는지 말할 수 없다. 혼란 스러울 수도있다.

내 게시물의 제목이 내가 말하는 이유입니다. "해시 맵의 값이 올바르지 않습니다"라는 것은 현실적으로 불가능합니다. 프로그래밍에 익숙하지 않고 때로는 몇 년 후에 디버깅의 절반은 (1) 프로그래밍 언어 구조가 실제로 광고 된대로 작동한다는 것, (2) 어딘가에서 실수를 저질렀다고 믿는 것입니다.

+0

감사합니다. 객체는 해시 코드에 따라 동일하게 보입니다.왜 이런 일이 일어 났는지 파악할 수없는 것 같아요. 솔루션을 찾는 것이 재미 있다고 생각 하긴하지만 지금 당장은 시간이 없습니다. 그래서 다른 솔루션 (3D 배열)을 사용했습니다. 그래도 내가 배열에 대해 동일한 절차를 사용했기 때문에 (여전히 내가 볼 수있는 한) 매력이 있지만 매력적이었다. 제목이 오해의 소지가 있거나 잘못된 것입니다. 가능하다면 내가 바꿀거야. 다시 한번 감사드립니다. – Promille