Ich habe ein kleines Problem mit einem Programm von mir, das Sudokus lösen soll. Wenn ich allerdings fürs erste nur die Erste Zeile mit allen Zahlen, außer der 1 ausfülle, und dann auf start drücke, bekomme ich: Exception in thread "Thread-11" java.lang.ArrayIndexOutOfBoundsException: 0
at Sudoku$1.run(Sudoku.java:104)
Danach hängt sich das JFrame auf. Falls die Leere Zelle an der Position 0/0 liegt, wir diese Zelle noch mit der 1 ausgefüllt, anderenfalls bleibt die leere Zelle leer, und das Fenster hängt sich auf. Wenn ich versuche, in dem Thread des start-Buttons zu debuggen, hängt sich mein PC komplett auf. Das Problem liegt vermutlich in der start-Methode der Sudoku-Klasse ab der while-schleife:
at Sudoku$1.run(Sudoku.java:104)
Danach hängt sich das JFrame auf. Falls die Leere Zelle an der Position 0/0 liegt, wir diese Zelle noch mit der 1 ausgefüllt, anderenfalls bleibt die leere Zelle leer, und das Fenster hängt sich auf. Wenn ich versuche, in dem Thread des start-Buttons zu debuggen, hängt sich mein PC komplett auf. Das Problem liegt vermutlich in der start-Methode der Sudoku-Klasse ab der while-schleife:
Java:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.HashSet;
public class Sudoku {
private Gui gui;
private FileWriter protocolStream;
private String lineSeparator;
private Cell[][] cells;
private Cell[][][] cellBlocks;
private int cellsLeft;
private boolean finished;
public static void main(String[] args) {
new Sudoku();
}
public Sudoku() {
try {
protocolStream = new FileWriter(new File(("C:"
+ System.getenv("HOMEPATH") + "/documents/sudoku_protocol"
+ new Date().getTime() + ".txt").replace(" ", "_")), true);
} catch (IOException e) {
e.printStackTrace();
}
lineSeparator = System.getProperty("line.separator");
gui = new Gui(this);
cells = gui.createCells();
Cell[][][] cellBlocksSwap = {
{
{ cells[0][0], cells[0][1], cells[0][2], cells[1][0],
cells[1][1], cells[1][2], cells[2][0],
cells[2][1], cells[2][2] },
{ cells[3][0], cells[3][1], cells[3][2], cells[4][0],
cells[4][1], cells[4][2], cells[5][0],
cells[5][1], cells[5][2] },
{ cells[6][0], cells[6][1], cells[6][2], cells[7][0],
cells[7][1], cells[7][2], cells[8][0],
cells[8][1], cells[8][2] } },
{
{ cells[0][3], cells[0][4], cells[0][5], cells[1][3],
cells[1][4], cells[1][5], cells[2][3],
cells[2][4], cells[2][5] },
{ cells[3][3], cells[3][4], cells[3][5], cells[4][3],
cells[4][4], cells[4][5], cells[5][3],
cells[5][4], cells[5][5] },
{ cells[6][3], cells[6][4], cells[6][5], cells[7][3],
cells[7][4], cells[7][5], cells[8][3],
cells[8][4], cells[8][5] } },
{
{ cells[0][6], cells[0][7], cells[0][8], cells[1][6],
cells[1][7], cells[1][8], cells[2][6],
cells[2][7], cells[2][8] },
{ cells[3][6], cells[3][7], cells[3][8], cells[4][6],
cells[4][7], cells[4][8], cells[5][6],
cells[5][7], cells[5][8] },
{ cells[6][6], cells[6][7], cells[6][8], cells[7][6],
cells[7][7], cells[7][8], cells[8][6],
cells[8][7], cells[8][8] } } };
cellBlocks = cellBlocksSwap;
}
public void start() {
cellsLeft = 9 * 9;
for (int x = 0; x < 9; x++) {
for (int y = 0; y < 9; y++) {
cells[x][y].initializeValue();
if (cells[x][y].getValue() != 0) {
cellsLeft--;
}
}
}
writeSudoku();
for (int x = 0; x < 9; x++) {
for (int y = 0; y < 9; y++) {
Cell cellChecking = cells[x][y];
if (cellChecking.getValue() == 0) {
HashSet<Cell> influencingCells = new HashSet<Cell>();
influencingCells = getInfluencingCells(cellChecking);
HashSet<Integer> impossibleNumbers = new HashSet<Integer>();
for (Cell cell : influencingCells) {
if (cell.getValue() != 0) {
impossibleNumbers.add(cell.getValue());
}
}
for (int i : impossibleNumbers) {
cellChecking.removePossibleNumber(i);
}
}
}
}
while (!finished) {
for (int x = 0; x < 9; x++) {
for (int y = 0; y < 9; y++) {
if (cells[x][y].possibleNumbers.size() == 1) {
SetValueThread thread = new SetValueThread(x, y) {
@Override
public void run() {
int i = 0;
while (i == 0) {
cells[posX][posY]
.setValue((Integer) cells[posX][posY].possibleNumbers
.toArray()[0]);
i++;
}
}
};
thread.start();
}
}
}
}
}
public void writeProtocol(String s) {
/*
* s += lineSeparator; try { protocolStream.write(s); } catch
* (IOException e) { e.printStackTrace(); }
*/
}
private void writeSudoku() {
for (int x = 0; x < 9; x++) {
String s = "";
for (int y = 0; y < 9; y++) {
s += cells[x][y];
}
writeProtocol(s);
}
}
private HashSet<Cell> getLine(Cell cell) {
HashSet<Cell> set = new HashSet<Cell>();
for (int i = 0; i < 9; i++) {
set.add(cells[i][cell.getY()]);
}
return set;
}
private HashSet<Cell> getCol(Cell cell) {
HashSet<Cell> set = new HashSet<Cell>();
for (int i = 0; i < 9; i++) {
set.add(cells[cell.getX()][i]);
}
return set;
}
private HashSet<Cell> getBlock(Cell cell) {
HashSet<Cell> set = new HashSet<Cell>();
for (int i = 0; i < 9; i++) {
set.add(cellBlocks[cell.getX() / 3][cell.getY() / 3][i]);
}
return set;
}
private HashSet<Cell> getInfluencingCells(Cell cell) {
HashSet<Cell> set = new HashSet<Cell>();
set.addAll(getLine(cell));
set.addAll(getCol(cell));
set.addAll(getBlock(cell));
return set;
}
public void cellSolved(Cell cell) {
cellsLeft--;
if (cellsLeft == 0) {
finish();
} else {
HashSet<Cell> influencingCells = new HashSet<Cell>();
influencingCells.addAll(getInfluencingCells(cell));
for (Cell c : influencingCells) {
c.removePossibleNumber(cell.getValue());
}
}
}
private void finish() {
finished = true;
try {
writeProtocol("finished");
protocolStream.flush();
protocolStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Gui extends JFrame{
private Sudoku sudoku;
private JButton button;
public Gui(final Sudoku sudoku) {
super();
this.sudoku = sudoku;
setLayout(null);
setBounds(100, 100, 250, 325);
button = new JButton("start");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent Event) {
sudoku.start();
}
});
button.setBounds(75, 230, 80, 40);
add(button);
}
public Cell[][] createCells() {
Cell[][] cells = new Cell[9][9];
for(int x = 0; x < 9; x++) {
for(int y = 0; y < 9; y++) {
cells[x][y] = new Cell(x, y, sudoku);
add(cells[x][y].getTextField());
}
}
setVisible(true);
return cells;
}
}
Java:
import java.awt.Color;
import java.util.HashSet;
import javax.swing.JTextField;
public class Cell {
private Sudoku sudoku;
private JTextField textField;
private int value;
private int x;
private int y;
public HashSet<Integer> possibleNumbers;
public Cell(int x, int y, Sudoku sudoku) {
this.sudoku = sudoku;
this.x = x;
this.y = y;
possibleNumbers = new HashSet<Integer>();
for (int i = 1; i <= 9; i++) {
possibleNumbers.add(i);
}
x *= 20;
y *= 20;
if (x >= 60) {
x += 5;
if (x >= 110) {
x += 5;
}
}
if (y >= 60) {
y += 5;
if (y >= 110) {
y += 5;
}
}
textField = new JTextField();
textField.setBounds(x + 20, y + 20, 20, 20);
}
@Override
public String toString() {
if (value == 0) {
return "?";
}
return "" + x + "" + y;
}
public JTextField getTextField() {
return textField;
}
public void initializeValue() {
textField.setEditable(false);
if (textField.getText().isEmpty()) {
value = 0;
return;
}
value = Integer.parseInt(textField.getText());
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void removePossibleNumber(int i) {
if (possibleNumbers.remove(i)) {
sudoku.writeProtocol("" + i
+ " removed as possible number for Cell[" + x + ", " + y
+ "]");
}
}
public int getValue() {
return value;
}
public void setValue(int i) {
value = i;
textField.setForeground(Color.BLUE);
textField.setText("" + i);
sudoku.writeProtocol("" + i + "was set as number for Cell[" + x + ", "
+ y + "] because it was the only possible number");
sudoku.cellSolved(this);
}
}
Java:
public class SetValueThread extends Thread {
protected int posX;
protected int posY;
public SetValueThread(int x, int y) {
super();
posX = x;
posY = y;
}
}
Zuletzt bearbeitet von einem Moderator: