import java.awt.*;
import java.awt.event.*;
import java.lang.String;
public class Sudokusolver extends Frame {
// Anfang Variablen
/*private TextField textField1 = new TextField();
private TextField textField2 = new TextField();
private TextField textField3 = new TextField();
private TextField textField4 = new TextField();
private TextField textField5 = new TextField();
private TextField textField6 = new TextField();
private TextField textField7 = new TextField();
private TextField textField8 = new TextField();
private TextField textField9 = new TextField();
private TextField textField10 = new TextField();
private TextField textField11 = new TextField();
private TextField textField12 = new TextField();
private TextField textField13 = new TextField();
private TextField textField14 = new TextField();
private TextField textField15 = new TextField();
private TextField textField16 = new TextField();
private TextField textField17 = new TextField();
private TextField textField18 = new TextField();
private TextField textField19 = new TextField();
private TextField textField20 = new TextField();
private TextField textField21 = new TextField();
private TextField textField22 = new TextField();
private TextField textField23 = new TextField();
private TextField textField24 = new TextField();
private TextField textField25 = new TextField();
private TextField textField26 = new TextField();
private TextField textField27 = new TextField();
private TextField textField28 = new TextField();
private TextField textField29 = new TextField();
private TextField textField30 = new TextField();
private TextField textField31 = new TextField();
private TextField textField32 = new TextField();
private TextField textField33 = new TextField();
private TextField textField34 = new TextField();
private TextField textField35 = new TextField();
private TextField textField36 = new TextField();
private TextField textField37 = new TextField();
private TextField textField38 = new TextField();
private TextField textField39 = new TextField();
private TextField textField40 = new TextField();
private TextField textField41 = new TextField();
private TextField textField42 = new TextField();
private TextField textField43 = new TextField();
private TextField textField44 = new TextField();
private TextField textField45 = new TextField();
private TextField textField46 = new TextField();
private TextField textField47 = new TextField();
private TextField textField48 = new TextField();
private TextField textField49 = new TextField();
private TextField textField50 = new TextField();
private TextField textField51 = new TextField();
private TextField textField52 = new TextField();
private TextField textField53 = new TextField();
private TextField textField54 = new TextField();
private TextField textField55 = new TextField();
private TextField textField56 = new TextField();
private TextField textField57 = new TextField();
private TextField textField58 = new TextField();
private TextField textField59 = new TextField();
private TextField textField60 = new TextField();
private TextField textField61 = new TextField();
private TextField textField62 = new TextField();
private TextField textField63 = new TextField();
private TextField textField64 = new TextField();
private TextField textField65 = new TextField();
private TextField textField66 = new TextField();
private TextField textField67 = new TextField();
private TextField textField68 = new TextField();
private TextField textField69 = new TextField();
private TextField textField70 = new TextField();
private TextField textField71 = new TextField();
private TextField textField72 = new TextField();
private TextField textField73 = new TextField();
private TextField textField74 = new TextField();
private TextField textField75 = new TextField();
private TextField textField76 = new TextField();
private TextField textField77 = new TextField();
private TextField textField78 = new TextField();
private TextField textField79 = new TextField();
private TextField textField80 = new TextField();
private TextField textField81 = new TextField(); */ versuch ich durch folgendes zu ersetzen:
TextField[] textFields = new TextField[81];
for (int i = 0; i <= 81; i++){
textFields[i] = new TextField();
}
private Button button1 = new Button();
private Button button2 = new Button();
private Button button3 = new Button();
private TextField [][] matrix = {{textField1, textField2, textField3, textField4, textField5, textField6, textField7, textField8, textField9},
{textField10, textField11, textField12, textField13, textField14, textField15, textField16, textField17, textField18},
{textField19, textField20, textField21, textField22, textField23, textField24, textField25, textField26, textField27},
{textField28, textField29, textField30, textField31, textField32, textField33, textField34, textField35, textField36},
{textField37, textField38, textField39, textField40, textField41, textField42, textField43, textField44, textField45},
{textField46, textField47, textField48, textField49, textField50, textField51, textField52, textField53, textField54},
{textField55, textField56, textField57, textField58, textField59, textField60, textField61, textField62, textField63},
{textField64, textField65, textField66, textField67, textField68, textField69, textField70, textField71, textField72},
{textField73, textField74, textField75, textField76, textField77, textField78, textField79, textField80, textField81},
};
private String[][] vTab = new String[9][9];
private boolean aenderung;
private boolean aenderung2;
// Ende Variablen
public Sudokusolver(String title) {
// Frame-Initialisierung
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) { System.exit(0); }
});
int frameWidth = 400;
int frameHeight = 320;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2 ;
setLocation(x, y);
Panel cp = new Panel(null);
add(cp);
// Anfang Komponenten
textField1.setBounds(10, 10, 25, 25);
textField1.setText("");
cp.add(textField1);
textField2.setBounds(40, 10, 25, 25);
textField2.setText("");
cp.add(textField2);
textField3.setBounds(70, 10, 25, 25);
textField3.setText("");
cp.add(textField3);
textField4.setBounds(100, 10, 25, 25);
textField4.setText("");
cp.add(textField4);
textField5.setBounds(130, 10, 25, 25);
textField5.setText("");
cp.add(textField5);
textField6.setBounds(160, 10, 25, 25);
textField6.setText("");
cp.add(textField6);
textField7.setBounds(190, 10, 25, 25);
textField7.setText("");
cp.add(textField7);
textField8.setBounds(220, 10, 25, 25);
textField8.setText("");
cp.add(textField8);
textField9.setBounds(250, 10, 25, 25);
textField9.setText("");
cp.add(textField9);
textField10.setBounds(10, 40, 25, 25);
textField10.setText("");
cp.add(textField10);
textField11.setBounds(40, 40, 25, 25);
textField11.setText("");
cp.add(textField11);
textField12.setBounds(70, 40, 25, 25);
textField12.setText("");
cp.add(textField12);
textField13.setBounds(100, 40, 25, 25);
textField13.setText("");
cp.add(textField13);
textField14.setBounds(130, 40, 25, 25);
textField14.setText("");
cp.add(textField14);
textField15.setBounds(160, 40, 25, 25);
textField15.setText("");
cp.add(textField15);
textField16.setBounds(190, 40, 25, 25);
textField16.setText("");
cp.add(textField16);
textField17.setBounds(220, 40, 25, 25);
textField17.setText("");
cp.add(textField17);
textField18.setBounds(250, 40, 25, 25);
textField18.setText("");
cp.add(textField18);
textField19.setBounds(10, 70, 25, 25);
textField19.setText("");
cp.add(textField19);
textField20.setBounds(40, 70, 25, 25);
textField20.setText("");
cp.add(textField20);
textField21.setBounds(70, 70, 25, 25);
textField21.setText("");
cp.add(textField21);
textField22.setBounds(100, 70, 25, 25);
textField22.setText("");
cp.add(textField22);
textField23.setBounds(130, 70, 25, 25);
textField23.setText("");
cp.add(textField23);
textField24.setBounds(160, 70, 25, 25);
textField24.setText("");
cp.add(textField24);
textField25.setBounds(190, 70, 25, 25);
textField25.setText("");
cp.add(textField25);
textField26.setBounds(220, 70, 25, 25);
textField26.setText("");
cp.add(textField26);
textField27.setBounds(250, 70, 25, 25);
textField27.setText("");
cp.add(textField27);
textField28.setBounds(10, 100, 25, 25);
textField28.setText("");
cp.add(textField28);
textField29.setBounds(40, 100, 25, 25);
textField29.setText("");
cp.add(textField29);
textField30.setBounds(70, 100, 25, 25);
textField30.setText("");
cp.add(textField30);
textField31.setBounds(100, 100, 25, 25);
textField31.setText("");
cp.add(textField31);
textField32.setBounds(130, 100, 25, 25);
textField32.setText("");
cp.add(textField32);
textField33.setBounds(160, 100, 25, 25);
textField33.setText("");
cp.add(textField33);
textField34.setBounds(190, 100, 25, 25);
textField34.setText("");
cp.add(textField34);
textField35.setBounds(220, 100, 25, 25);
textField35.setText("");
cp.add(textField35);
textField36.setBounds(250, 100, 25, 25);
textField36.setText("");
cp.add(textField36);
textField37.setBounds(10, 130, 25, 25);
textField37.setText("");
cp.add(textField37);
textField38.setBounds(40, 130, 25, 25);
textField38.setText("");
cp.add(textField38);
textField39.setBounds(70, 130, 25, 25);
textField39.setText("");
cp.add(textField39);
textField40.setBounds(100, 130, 25, 25);
textField40.setText("");
cp.add(textField40);
textField41.setBounds(130, 130, 25, 25);
textField41.setText("");
cp.add(textField41);
textField42.setBounds(160, 130, 25, 25);
textField42.setText("");
cp.add(textField42);
textField43.setBounds(190, 130, 25, 25);
textField43.setText("");
cp.add(textField43);
textField44.setBounds(220, 130, 25, 25);
textField44.setText("");
cp.add(textField44);
textField45.setBounds(250, 130, 25, 25);
textField45.setText("");
cp.add(textField45);
textField46.setBounds(10, 160, 25, 25);
textField46.setText("");
cp.add(textField46);
textField47.setBounds(40, 160, 25, 25);
textField47.setText("");
cp.add(textField47);
textField48.setBounds(70, 160, 25, 25);
textField48.setText("");
cp.add(textField48);
textField49.setBounds(100, 160, 25, 25);
textField49.setText("");
cp.add(textField49);
textField50.setBounds(130, 160, 25, 25);
textField50.setText("");
cp.add(textField50);
textField51.setBounds(160, 160, 25, 25);
textField51.setText("");
cp.add(textField51);
textField52.setBounds(190, 160, 25, 25);
textField52.setText("");
cp.add(textField52);
textField53.setBounds(220, 160, 25, 25);
textField53.setText("");
cp.add(textField53);
textField54.setBounds(250, 160, 25, 25);
textField54.setText("");
cp.add(textField54);
textField55.setBounds(10, 190, 25, 25);
textField55.setText("");
cp.add(textField55);
textField56.setBounds(40, 190, 25, 25);
textField56.setText("");
cp.add(textField56);
textField57.setBounds(70, 190, 25, 25);
textField57.setText("");
cp.add(textField57);
textField58.setBounds(100, 190, 25, 25);
textField58.setText("");
cp.add(textField58);
textField59.setBounds(130, 190, 25, 25);
textField59.setText("");
cp.add(textField59);
textField60.setBounds(160, 190, 25, 25);
textField60.setText("");
cp.add(textField60);
textField61.setBounds(190, 190, 25, 25);
textField61.setText("");
cp.add(textField61);
textField62.setBounds(220, 190, 25, 25);
textField62.setText("");
cp.add(textField62);
textField63.setBounds(250, 190, 25, 25);
textField63.setText("");
cp.add(textField63);
textField64.setBounds(10, 220, 25, 25);
textField64.setText("");
cp.add(textField64);
textField65.setBounds(40, 220, 25, 25);
textField65.setText("");
cp.add(textField65);
textField66.setBounds(70, 220, 25, 25);
textField66.setText("");
cp.add(textField66);
textField67.setBounds(100, 220, 25, 25);
textField67.setText("");
cp.add(textField67);
textField68.setBounds(130, 220, 25, 25);
textField68.setText("");
cp.add(textField68);
textField69.setBounds(160, 220, 25, 25);
textField69.setText("");
cp.add(textField69);
textField70.setBounds(190, 220, 25, 25);
textField70.setText("");
cp.add(textField70);
textField71.setBounds(220, 220, 25, 25);
textField71.setText("");
cp.add(textField71);
textField72.setBounds(250, 220, 25, 25);
textField72.setText("");
cp.add(textField72);
textField73.setBounds(10, 250, 25, 25);
textField73.setText("");
cp.add(textField73);
textField74.setBounds(40, 250, 25, 25);
textField74.setText("");
cp.add(textField74);
textField75.setBounds(70, 250, 25, 25);
textField75.setText("");
cp.add(textField75);
textField76.setBounds(100, 250, 25, 25);
textField76.setText("");
cp.add(textField76);
textField77.setBounds(130, 250, 25, 25);
textField77.setText("");
cp.add(textField77);
textField78.setBounds(160, 250, 25, 25);
textField78.setText("");
cp.add(textField78);
textField79.setBounds(190, 250, 25, 25);
textField79.setText("");
cp.add(textField79);
textField80.setBounds(220, 250, 25, 25);
textField80.setText("");
cp.add(textField80);
textField81.setBounds(250, 250, 25, 25);
textField81.setText("");
cp.add(textField81);
button1.setBounds(288, 232, 100, 25);
button1.setLabel("Exit");
cp.add(button1);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button1ActionPerformed(evt);
}
});
button2.setBounds(288, 8, 100, 25);
button2.setLabel("Löse");
cp.add(button2);
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button2ActionPerformed(evt);
}
});
button3.setBounds(288, 48, 100, 25);
button3.setLabel("Clear");
cp.add(button3);
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button3ActionPerformed(evt);
}
});
// Ende Komponenten
setResizable(false);
setVisible(true);
}
// Anfang Ereignisprozeduren
public void button1ActionPerformed(ActionEvent evt) {
System.exit(1);
}
public void button2ActionPerformed(ActionEvent evt) {
fuellen ();
ausschlussEZ();
fillMatrix();
}
public void fuellen(){
String text = new String();
//Füllen leerer Zellen:
for (int a = 0; a <= 8; a++){
for (int b = 0; b <= 8; b++){
text = matrix[a][b].getText();
if (text.equals("")){
matrix[a][b].setText("X");
matrix[a][b].setBackground(Color.yellow);
vTab[a][b] = "123456789";
}else{
vTab[a][b] = text;
matrix[a][b].setBackground(Color.green);
}
matrix[a][b].setEditable(false);
}
}
}
public boolean ausschlussEZ(){
String text = new String();
String aktzahl = new String();
String kontrolle = new String();
do{
aenderung = false;
//Ausschluss eindeutiger Zahlen:
for (int x = 0; x <= 8; x++){
for (int y = 0; y <=8; y++){
aktzahl = vTab[x][y];
int zahl = Integer.valueOf(aktzahl).intValue();
if (zahl <= 9){
//Ausschluss Spalte:
for (int z = 0; z <= 8; z++){
text = vTab[z][y];
if (!text.equals(aktzahl)){
kontrolle = text;
text = text.replace(aktzahl, "");
vTab[z][y] = text;
if (!kontrolle.equals(text)){
aenderung = true;
}
}
}
//Ausschluss Zeile:
for (int z = 0; z <= 8; z++){
text = vTab[x][z];
if (!text.equals(aktzahl)){
kontrolle = text;
text = text.replace(aktzahl, "");
vTab[x][z] = text;
if (!kontrolle.equals(text)){
aenderung = true;
}
}
}
}
}
}
text = "";
}while(aenderung);
return aenderung;
}
public void fillMatrix(){
String text = new String();
for (int x = 0; x <= 8; x++){
for (int y = 0; y <= 8; y++){
text = vTab[x][y];
int zahl = Integer.valueOf(text).intValue();
if (zahl <= 9){
matrix[x][y].setText(text);
}else{
matrix[x][y].setBackground(Color.red);
}
}
}
}
public void button3ActionPerformed(ActionEvent evt) {
for (int a = 0; a <= 8; a++){
for (int b = 0; b <= 8; b++){
matrix[a][b].setText("");
matrix[a][b].setEditable(true);
matrix[a][b].setBackground(Color.white);
}
}
}
// Ende Ereignisprozeduren
public static void main(String[] args) {
new Sudokusolver("Sudokusolver");
}
}