我想要完成的事情:
while(there are empty squares)
get a random number of the remaining numbers
if(the number hasn't been tried before)
if(it fits)
place it and remove from available
next square
if(all numbers have been tried)
forget it
move back a square and try a different number
这个循环在availableNumbers = 64,有时是37,有时是81等停止。什么阻止它一直走?为什么它有时会被卡住而有时会退回?
while(availableNumbers.size()>0){
rand = rn.nextInt(availableNumbers.size());
number = availableNumbers.get(rand);
if(number != triedNumbers[y][x][number-1]){ // If the number hasn't been tried in this square
if(!checkConflict(number)){
squares[y][x].setText(""+number);
availableNumbers.remove(new Integer(number));
addedNumbers[y][x] = number;
triedNumbers[y][x][number-1] = number;
moveAhead();
}
}
if(Arrays.equals(triedNumbers[y][x],refArray)){ // If all numbers have been tried in that square
emptySquare(); // Forget all numbers tried in current square
moveBack();
}
}
emptySquare,moveBack,moveAhead和checkConflicts所有工作都像他们应该的那样(根据我的测试)。以下是他们的要点:https://gist.github.com/WQvist/ac4296fb3a86fdc3d713
编辑:我终于解决了。谢谢你的帮助。看到我的回答。答案 0 :(得分:1)
您的psudeocode不正确。它应该是:
while(there are empty squares)
get a random number of the remaining numbers
if(the number hasn't been tried before)
if(it fits)
place it and remove from available
next square
while(all numbers have been tried)
forget it
move back a square
try a different number
你有时候必须回到一个以上的方格去到一个没有尝试过所有数字的广场。
答案 1 :(得分:0)
解决了!
while(currentSquare<81){
rand = rn.nextInt(availableNumbers.size());
number = availableNumbers.get(rand);
if(square[currentSquare].contains(number)){
square[currentSquare].remove(new Integer(number));
if(!checkConflict(number)){
squares[y][x].setText(""+number);
addedNumbers[y][x] = number;
availableNumbers.remove(rand);
moveAhead();
currentSquare++;
}
}
if(currentSquare < 81){
while(square[currentSquare].isEmpty()){
square[currentSquare].addAll(refList);
addedNumbers[y][x] = 0;
availableNumbers.addAll(refList);
moveBack();
if(currentSquare>=1){
currentSquare--;
}
}
}
}