Ich bin dabei ein TicTacToe Spiel zu programmieren.
Mei Problem ist das ich kein richtigen Ansatz habe, wie ich denn Gegner (denn Computer) programmieren soll das er anständig funktioniert.
Ich habe ein Test Ansatz ausprobiert (der Auskommentiert ist), der sein Zug immer auf ein Random ausgelostetes Feld setzt, auch wenn das Feld schon belegt ist.
Die Folgenden Programme sind 2 Klassen die miteinander verknüpft sind.
----------------------------------------------------------------------
Tut mir Leid wenn man mein Programm oder die Art und weiße wie ich das Problem beschrieben habe nicht direkt oder auch gar nicht versteht.
Ich freu mich auf jede Art von Vorschlägen.
----------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Mei Problem ist das ich kein richtigen Ansatz habe, wie ich denn Gegner (denn Computer) programmieren soll das er anständig funktioniert.
Ich habe ein Test Ansatz ausprobiert (der Auskommentiert ist), der sein Zug immer auf ein Random ausgelostetes Feld setzt, auch wenn das Feld schon belegt ist.
Die Folgenden Programme sind 2 Klassen die miteinander verknüpft sind.
----------------------------------------------------------------------
Tut mir Leid wenn man mein Programm oder die Art und weiße wie ich das Problem beschrieben habe nicht direkt oder auch gar nicht versteht.
Ich freu mich auf jede Art von Vorschlägen.
----------------------------------------------------------------------
Code:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
class MyCanvas extends JComponent {
public void paint(Graphics g) {
g.setColor(Color.DARK_GRAY);
g.fill3DRect(40, 40, 200, 200, true);
}
}
public class TicTacToe {
public static void main(String[] args) {
Interface();
Buttons internetObjekt = new Buttons();
internetObjekt.verbinden();
}
public static void Interface() {
JFrame frame = new JFrame("TicTacToe");
//Frame Interface
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(30, 30, 300, 350);
// frame.setLayout(null);
// frame.setDefaultLookAndFeelDecorated(true);
//Visibility
frame.setVisible(true);
frame.add(Buttons.F1);
frame.add(Buttons.F2);
frame.add(Buttons.button1);
frame.add(Buttons.button2);
frame.add(Buttons.button3);
//
frame.add(Buttons.button4);
frame.add(Buttons.button5);
frame.add(Buttons.button6);
//
frame.add(Buttons.button7);
frame.add(Buttons.button8);
frame.add(Buttons.button9);
frame.add(Buttons.buttonNeu);
frame.getContentPane().add(new MyCanvas());
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Java:
import jdk.jfr.Enabled;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import static java.awt.Color.BLACK;
public class Buttons {
static JButton button1 = new JButton("");
static JButton button2 = new JButton("");
static JButton button3 = new JButton("");
//
static JButton button4 = new JButton("");
static JButton button5 = new JButton("");
static JButton button6 = new JButton("");
//
static JButton button7 = new JButton("");
static JButton button8 = new JButton("");
static JButton button9 = new JButton("");
static JButton buttonNeu = new JButton("Neustart");
static JLabel F1 = new JLabel("");
static JLabel F2 = new JLabel("");
static boolean status = false;
public void verbinden() {
System.out.println("willkommen!!!");
System.out.println("Status: " + status);
F1.setBounds(80, 1, 110, 60);
F1.setVisible(true);
F1.setText("X ist an der Reihe!!!");
F2.setBounds(80, 1, 110, 60);
// F2.setVisible(false);
//Button Größe + Position
button1.setBounds(40, 40, 60, 60);
button2.setBounds(110, 40, 60, 60);
button3.setBounds(180, 40, 60, 60);
//
button4.setBounds(40, 110, 60, 60);
button5.setBounds(110, 110, 60, 60);
button6.setBounds(180, 110, 60, 60);
//
button7.setBounds(40, 180, 60, 60);
button8.setBounds(110, 180, 60, 60);
button9.setBounds(180, 180, 60, 60);
buttonNeu.setBounds(40, 260, 200, 30);
//Fokus deaktiviert
button1.setFocusPainted(false);
button2.setFocusPainted(false);
button3.setFocusPainted(false);
button4.setFocusPainted(false);
button5.setFocusPainted(false);
button6.setFocusPainted(false);
button7.setFocusPainted(false);
button8.setFocusPainted(false);
button9.setFocusPainted(false);
buttonNeu.setFocusPainted(false);
button1.setBackground(Color.lightGray);
button2.setBackground(Color.lightGray);
button3.setBackground(Color.lightGray);
//
button4.setBackground(Color.lightGray);
button5.setBackground(Color.lightGray);
button6.setBackground(Color.lightGray);
//
button7.setBackground(Color.lightGray);
button8.setBackground(Color.lightGray);
button9.setBackground(Color.lightGray);
buttonNeu.setBackground(Color.lightGray);
//
//
button1.setBorder(new LineBorder(BLACK));
button2.setBorder(new LineBorder(BLACK));
button3.setBorder(new LineBorder(BLACK));
//
button4.setBorder(new LineBorder(BLACK));
button5.setBorder(new LineBorder(BLACK));
button6.setBorder(new LineBorder(BLACK));
//
button7.setBorder(new LineBorder(BLACK));
button8.setBorder(new LineBorder(BLACK));
button9.setBorder(new LineBorder(BLACK));
buttonNeu.setBorder(new LineBorder(BLACK));
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
button1.setFont(button1.getFont().deriveFont((float) 32));
button2.setFont(button2.getFont().deriveFont((float) 32));
button3.setFont(button3.getFont().deriveFont((float) 32));
//
button4.setFont(button4.getFont().deriveFont((float) 32));
button5.setFont(button5.getFont().deriveFont((float) 32));
button6.setFont(button6.getFont().deriveFont((float) 32));
//
button7.setFont(button7.getFont().deriveFont((float) 32));
button8.setFont(button8.getFont().deriveFont((float) 32));
button9.setFont(button9.getFont().deriveFont((float) 32));
// buttonNeu.setFont(button9.getFont().deriveFont((float) 22));
Object clicked = e.getSource();
//
if (buttonNeu == clicked){
status = false;
F1.setText("X ist an der Reihe!!!");
F2.setText("");
F1.setVisible(true);
F2.setVisible(false);
button1.setText("");
button2.setText("");
button3.setText("");
//
button4.setText("");
button5.setText("");
button6.setText("");
//
button7.setText("");
button8.setText("");
button9.setText("");
button1.setEnabled(true);
button2.setEnabled(true);
button3.setEnabled(true);
//
button4.setEnabled(true);
button5.setEnabled(true);
button6.setEnabled(true);
//
button7.setEnabled(true);
button8.setEnabled(true);
button9.setEnabled(true);
button1.setBackground(Color.lightGray);
button2.setBackground(Color.lightGray);
button3.setBackground(Color.lightGray);
//
button4.setBackground(Color.lightGray);
button5.setBackground(Color.lightGray);
button6.setBackground(Color.lightGray);
//
button7.setBackground(Color.lightGray);
button8.setBackground(Color.lightGray);
button9.setBackground(Color.lightGray);
}
//X
if (status == false) {
// Random buttons = new Random();
// int a;
//
// for (int i=0; i<1; i++) {
//
// a = 1 + buttons.nextInt(9);
//
// System.out.println("Random: " + a);
if (button1 == clicked) {
button1.setText("X");
button1.setEnabled(false);
status = true;
F1.setText("O is an der Reihe!!!");
// if (a == 1 && (button1.getText().endsWith("X")||button1.getText().endsWith("O"))) {
// a = 1 + buttons.nextInt(9);
// System.out.println("Random2: " + a);
// }
// if (a == 2 && button2.getText().endsWith("")) {
// button2.doClick();
// }else if (a == 3 && button3.getText().endsWith("")) {
// button3.doClick();
// } else if (a == 4 && button4.getText().endsWith("")) {
// button4.doClick();
// } else if (a == 5 && button5.getText().endsWith("")) {
// button5.doClick();
// } else if (a == 6 && button6.getText().endsWith("")) {
// button6.doClick();
// } else if (a == 7 && button7.getText().endsWith("")) {
// button7.doClick();
// } else if (a == 8 && button8.getText().endsWith("")) {
// button8.doClick();
// } else if (a == 9 && button9.getText().endsWith("")) {
// button9.doClick();
// }
} else if (button2 == clicked) {
button2.setText("X");
button2.setEnabled(false);
status = true;
F1.setText("O is an der Reihe!!!");
// if (a == 2 && (button2.getText().endsWith("X")||button2.getText().endsWith("O"))) {
// a = 1 + buttons.nextInt(9);
// System.out.println("Random2: " + a);
// }
//
// if (a == 1 && button1.getText().endsWith("")) {
// button1.doClick();
// } else if (a == 3 && button3.getText().endsWith("")) {
// button3.doClick();
// } else if (a == 4 && button4.getText().endsWith("")) {
// button4.doClick();
// } else if (a == 5 && button5.getText().endsWith("")) {
// button5.doClick();
// } else if (a == 6 && button6.getText().endsWith("")) {
// button6.doClick();
// } else if (a == 7 && button7.getText().endsWith("")) {
// button7.doClick();
// } else if (a == 8 && button8.getText().endsWith("")) {
// button8.doClick();
// } else if (a == 9 && button9.getText().endsWith("")) {
// button9.doClick();
// }
} else if (button3 == clicked) {
button3.setText("X");
button3.setEnabled(false);
status = true;
F1.setText("O is an der Reihe!!!");
// if (a == 3 && (button3.getText().endsWith("X")||button3.getText().endsWith("O"))) {
// a = 1 + buttons.nextInt(9);
// System.out.println("Random2: " + a);
// }
//
// if (a == 1 && button1.getText().endsWith("")) {
// button1.doClick();
// } else if (a == 2 && button2.getText().endsWith("")) {
// button2.doClick();
// } else if (a == 4 && button4.getText().endsWith("")) {
// button4.doClick();
// } else if (a == 5 && button5.getText().endsWith("")) {
// button5.doClick();
// } else if (a == 6 && button6.getText().endsWith("")) {
// button6.doClick();
// } else if (a == 7 && button7.getText().endsWith("")) {
// button7.doClick();
// } else if (a == 8 && button8.getText().endsWith("")) {
// button8.doClick();
// } else if (a == 9 && button9.getText().endsWith("")) {
// button9.doClick();
// }
} else if (button4 == clicked) {
button4.setText("X");
button4.setEnabled(false);
status = true;
F1.setText("O is an der Reihe!!!");
// if (a == 4 && (button4.getText().endsWith("X")||button4.getText().endsWith("O"))) {
// a = 1 + buttons.nextInt(9);
// System.out.println("Random2: " + a);
// }
//
// if (a == 1 && button1.getText().endsWith("")) {
// button1.doClick();
// } else if (a == 2 && button2.getText().endsWith("")) {
// button2.doClick();
// }else if (a == 3 && button3.getText().endsWith("")) {
// button3.doClick();
// } else if (a == 5 && button5.getText().endsWith("")) {
// button5.doClick();
// } else if (a == 6 && button6.getText().endsWith("")) {
// button6.doClick();
// } else if (a == 7 && button7.getText().endsWith("")) {
// button7.doClick();
// } else if (a == 8 && button8.getText().endsWith("")) {
// button8.doClick();
// } else if (a == 9 && button9.getText().endsWith("")) {
// button9.doClick();
// }
} else if (button5 == clicked) {
button5.setText("X");
button5.setEnabled(false);
status = true;
F1.setText("O is an der Reihe!!!");
//
// if (a == 5 && (button5.getText().endsWith("X")||button5.getText().endsWith("O"))) {
// a = 1 + buttons.nextInt(9);
// System.out.println("Random2: " + a);
// }
//
// if (a == 1 && button1.getText().endsWith("")) {
// button1.doClick();
// } else if (a == 2 && button2.getText().endsWith("")) {
// button2.doClick();
// }else if (a == 3 && button3.getText().endsWith("")) {
// button3.doClick();
// } else if (a == 4 && button4.getText().endsWith("")) {
// button4.doClick();
// } else if (a == 6 && button.getText().endsWith("")) {
// button6.doClick();
// } else if (a == 7 && button2.getText().endsWith("")) {
// button7.doClick();
// } else if (a == 8 && button2.getText().endsWith("")) {
// button8.doClick();
// } else if (a == 9 && button2.getText().endsWith("")) {
// button9.doClick();
// }
} else if (button6 == clicked) {
button6.setText("X");
button6.setEnabled(false);
status = true;
F1.setText("O is an der Reihe!!!");
// if (a == 6 && (button6.getText().endsWith("X")||button6.getText().endsWith("O"))) {
// a = 1 + buttons.nextInt(9);
// System.out.println("Random2: " + a);
// }
//
// if (a == 1 && button1.getText().endsWith("")) {
// button1.doClick();
// } else if (a == 2 && button2.getText().endsWith("")) {
// button2.doClick();
// }else if (a == 3 && button3.getText().endsWith("")) {
// button3.doClick();
// } else if (a == 4 && button4.getText().endsWith("")) {
// button4.doClick();
// } else if (a == 5 && button5.getText().endsWith("")) {
// button5.doClick();
// } else if (a == 7 && button7.getText().endsWith("")) {
// button7.doClick();
// } else if (a == 8 && button8.getText().endsWith("")) {
// button8.doClick();
// } else if (a == 9 && button9.getText().endsWith("")) {
// button9.doClick();
// }
} else if (button7 == clicked) {
button7.setText("X");
button7.setEnabled(false);
status = true;
F1.setText("O is an der Reihe!!!");
// if (a == 7 && (button7.getText().endsWith("X")||button7.getText().endsWith("O"))) {
// a = 1 + buttons.nextInt(9);
// System.out.println("Random2: " + a);
// }
//
// if (a == 1 && button1.getText().endsWith("")) {
// button1.doClick();
// } else if (a == 2 && button2.getText().endsWith("")) {
// button2.doClick();
// }else if (a == 3 && button3.getText().endsWith("")) {
// button3.doClick();
// } else if (a == 4 && button4.getText().endsWith("")) {
// button4.doClick();
// } else if (a == 5 && button5.getText().endsWith("")) {
// button5.doClick();
// } else if (a == 6 && button6.getText().endsWith("")) {
// button6.doClick();
// } else if (a == 8 && button8.getText().endsWith("")) {
// button8.doClick();
// } else if (a == 9 && button9.getText().endsWith("")) {
// button9.doClick();
// }
} else if (button8 == clicked) {
button8.setText("X");
button8.setEnabled(false);
status = true;
F1.setText("O is an der Reihe!!!");
// if (a == 8 && (button8.getText().endsWith("X")||button8.getText().endsWith("O"))) {
// a = 1 + buttons.nextInt(9);
// System.out.println("Random2: " + a);
// }
//
// if (a == 1 && button1.getText().endsWith("")) {
// button1.doClick();
// } else if (a == 2 && button2.getText().endsWith("")) {
// button2.doClick();
// }else if (a == 3 && button3.getText().endsWith("")) {
// button3.doClick();
// } else if (a == 4 && button4.getText().endsWith("")) {
// button4.doClick();
// } else if (a == 5 && button5.getText().endsWith("")) {
// button5.doClick();
// } else if (a == 6 && button6.getText().endsWith("")) {
// button6.doClick();
// } else if (a == 7 && button7.getText().endsWith("")) {
// button7.doClick();
// } else if (a == 9 && button9.getText().endsWith("") ) {
// button9.doClick();
// }
} else if (button9 == clicked) {
button9.setText("X");
button9.setEnabled(false);
status = true;
F1.setText("O is an der Reihe!!!");
// if (a == 9 && (button9.getText().endsWith("X")||button9.getText().endsWith("O"))) {
// a = 1 + buttons.nextInt(9);
// System.out.println("Random2: " + a);
// }
//
// if (a == 1 && button1.getText().endsWith("")) {
// button1.doClick();
// } else if (a == 2 && button2.getText().endsWith("")) {
// button2.doClick();
// }else if (a == 3 && button3.getText().endsWith("")) {
// button3.doClick();
// } else if (a == 4 && button4.getText().endsWith("")) {
// button4.doClick();
// } else if (a == 5 && button5.getText().endsWith("")) {
// button5.doClick();
// } else if (a == 6 && button6.getText().endsWith("")) {
// button6.doClick();
// } else if (a == 7 && button7.getText().endsWith("")) {
// button7.doClick();
// } else if (a == 8 && button8.getText().endsWith("")) {
// button8.doClick();
// }
}
System.out.println("Status 2: " + status);
// }
//O
}else if (status == true) {
if (button1 == clicked) {
button1.setText("O");
button1.setEnabled(false);
status = false;
F1.setText("X ist an der Reihe!!!");
} else if (button2 == clicked) {
button2.setText("O");
button2.setEnabled(false);
status = false;
F1.setText("X ist an der Reihe!!!");
} else if (button3 == clicked) {
button3.setText("O");
button3.setEnabled(false);
status = false;
F1.setText("X ist an der Reihe!!!");
} else if (button4 == clicked) {
button4.setText("O");
button4.setEnabled(false);
status = false;
F1.setText("X ist an der Reihe!!!");
} else if (button5 == clicked) {
button5.setText("O");
button5.setEnabled(false);
status = false;
F1.setText("X ist an der Reihe!!!");
} else if (button6 == clicked) {
button6.setText("O");
button6.setEnabled(false);
status = false;
F1.setText("X ist an der Reihe!!!");
} else if (button7 == clicked) {
button7.setText("O");
button7.setEnabled(false);
status = false;
F1.setText("X ist an der Reihe!!!");
} else if (button8 == clicked) {
button8.setText("O");
button8.setEnabled(false);
status = false;
F1.setText("X ist an der Reihe!!!");
} else if (button9 == clicked) {
button9.setText("O");
button9.setEnabled(false);
status = false;
F1.setText("X ist an der Reihe!!!");
}
System.out.println("Status 3: "+status);
}
button1.getText();
System.out.println("text: "+button1.getText());
if (button1.getText().endsWith("X") && button2.getText().endsWith("X") && button3.getText().endsWith("X")){
F1.setVisible(false);
F2.setText("X hat Gewonnen!!!");
F2.setVisible(true);
button1.setBackground(Color.green);
button2.setBackground(Color.green);
button3.setBackground(Color.green);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
//
button4.setEnabled(false);
button5.setEnabled(false);
button6.setEnabled(false);
//
button7.setEnabled(false);
button8.setEnabled(false);
button9.setEnabled(false);
}else if (button4.getText().endsWith("X") && button5.getText().endsWith("X") && button6.getText().endsWith("X")){
F1.setVisible(false);
F2.setText("X hat Gewonnen!!!");
F2.setVisible(true);
button4.setBackground(Color.green);
button5.setBackground(Color.green);
button6.setBackground(Color.green);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
//
button4.setEnabled(false);
button5.setEnabled(false);
button6.setEnabled(false);
//
button7.setEnabled(false);
button8.setEnabled(false);
button9.setEnabled(false);
}else if (button7.getText().endsWith("X") && button8.getText().endsWith("X") && button9.getText().endsWith("X")){
F1.setVisible(false);
F2.setText("X hat Gewonnen!!!");
F2.setVisible(true);
button7.setBackground(Color.green);
button8.setBackground(Color.green);
button9.setBackground(Color.green);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
//
button4.setEnabled(false);
button5.setEnabled(false);
button6.setEnabled(false);
//
button7.setEnabled(false);
button8.setEnabled(false);
button9.setEnabled(false);
}else if (button1.getText().endsWith("X") && button4.getText().endsWith("X") && button7.getText().endsWith("X")) {
F1.setVisible(false);
F2.setText("X hat Gewonnen!!!");
F2.setVisible(true);
button1.setBackground(Color.green);
button4.setBackground(Color.green);
button7.setBackground(Color.green);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
//
button4.setEnabled(false);
button5.setEnabled(false);
button6.setEnabled(false);
//
button7.setEnabled(false);
button8.setEnabled(false);
button9.setEnabled(false);
}else if (button2.getText().endsWith("X") && button5.getText().endsWith("X") && button8.getText().endsWith("X")){
F1.setVisible(false);
F2.setText("X hat Gewonnen!!!");
F2.setVisible(true);
button2.setBackground(Color.green);
button5.setBackground(Color.green);
button8.setBackground(Color.green);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
//
button4.setEnabled(false);
button5.setEnabled(false);
button6.setEnabled(false);
//
button7.setEnabled(false);
button8.setEnabled(false);
button9.setEnabled(false);
}else if (button3.getText().endsWith("X") && button6.getText().endsWith("X") && button9.getText().endsWith("X")){
F1.setVisible(false);
F2.setText("X hat Gewonnen!!!");
F2.setVisible(true);
button3.setBackground(Color.green);
button6.setBackground(Color.green);
button9.setBackground(Color.green);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
//
button4.setEnabled(false);
button5.setEnabled(false);
button6.setEnabled(false);
//
button7.setEnabled(false);
button8.setEnabled(false);
button9.setEnabled(false);
}else if (button1.getText().endsWith("X") && button5.getText().endsWith("X") && button9.getText().endsWith("X")){
F1.setVisible(false);
F2.setText("X hat Gewonnen!!!");
F2.setVisible(true);
button1.setBackground(Color.green);
button5.setBackground(Color.green);
button9.setBackground(Color.green);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
//
button4.setEnabled(false);
button5.setEnabled(false);
button6.setEnabled(false);
//
button7.setEnabled(false);
button8.setEnabled(false);
button9.setEnabled(false);
}else if (button3.getText().endsWith("X") && button5.getText().endsWith("X") && button7.getText().endsWith("X")){
F1.setVisible(false);
F2.setText("X hat Gewonnen!!!");
F2.setVisible(true);
button3.setBackground(Color.green);
button5.setBackground(Color.green);
button7.setBackground(Color.green);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
//
button4.setEnabled(false);
button5.setEnabled(false);
button6.setEnabled(false);
//
button7.setEnabled(false);
button8.setEnabled(false);
button9.setEnabled(false);
}else if (button1.getText().endsWith("O") && button2.getText().endsWith("O") && button3.getText().endsWith("O")){
F1.setVisible(false);
F2.setText("O hat Gewonnen!!!");
F2.setVisible(true);
button1.setBackground(Color.green);
button2.setBackground(Color.green);
button3.setBackground(Color.green);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
//
button4.setEnabled(false);
button5.setEnabled(false);
button6.setEnabled(false);
//
button7.setEnabled(false);
button8.setEnabled(false);
button9.setEnabled(false);
}else if (button4.getText().endsWith("O") && button5.getText().endsWith("O") && button6.getText().endsWith("O")){
F1.setVisible(false);
F2.setText("O hat Gewonnen!!!");
F2.setVisible(true);
button4.setBackground(Color.green);
button5.setBackground(Color.green);
button6.setBackground(Color.green);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
//
button4.setEnabled(false);
button5.setEnabled(false);
button6.setEnabled(false);
//
button7.setEnabled(false);
button8.setEnabled(false);
button9.setEnabled(false);
}else if (button7.getText().endsWith("O") && button8.getText().endsWith("O") && button9.getText().endsWith("O")){
F1.setVisible(false);
F2.setText("O hat Gewonnen!!!");
F2.setVisible(true);
button7.setBackground(Color.green);
button8.setBackground(Color.green);
button9.setBackground(Color.green);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
//
button4.setEnabled(false);
button5.setEnabled(false);
button6.setEnabled(false);
//
button7.setEnabled(false);
button8.setEnabled(false);
button9.setEnabled(false);
}else if (button1.getText().endsWith("O") && button4.getText().endsWith("O") && button7.getText().endsWith("O")){
F1.setVisible(false);
F2.setText("O hat Gewonnen!!!");
F2.setVisible(true);
button1.setBackground(Color.green);
button4.setBackground(Color.green);
button7.setBackground(Color.green);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
//
button4.setEnabled(false);
button5.setEnabled(false);
button6.setEnabled(false);
//
button7.setEnabled(false);
button8.setEnabled(false);
button9.setEnabled(false);
}else if (button2.getText().endsWith("O") && button5.getText().endsWith("O") && button8.getText().endsWith("O")){
F1.setVisible(false);
F2.setText("O hat Gewonnen!!!");
F2.setVisible(true);
button2.setBackground(Color.green);
button5.setBackground(Color.green);
button8.setBackground(Color.green);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
//
button4.setEnabled(false);
button5.setEnabled(false);
button6.setEnabled(false);
//
button7.setEnabled(false);
button8.setEnabled(false);
button9.setEnabled(false);
}else if (button3.getText().endsWith("O") && button6.getText().endsWith("O") && button9.getText().endsWith("O")){
F1.setVisible(false);
F2.setText("O hat Gewonnen!!!");
F2.setVisible(true);
button3.setBackground(Color.green);
button6.setBackground(Color.green);
button9.setBackground(Color.green);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
//
button4.setEnabled(false);
button5.setEnabled(false);
button6.setEnabled(false);
//
button7.setEnabled(false);
button8.setEnabled(false);
button9.setEnabled(false);
}else if (button1.getText().endsWith("O") && button5.getText().endsWith("O") && button9.getText().endsWith("O")){
F1.setVisible(false);
F2.setText("O hat Gewonnen!!!");
F2.setVisible(true);
button1.setBackground(Color.green);
button5.setBackground(Color.green);
button9.setBackground(Color.green);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
//
button4.setEnabled(false);
button5.setEnabled(false);
button6.setEnabled(false);
//
button7.setEnabled(false);
button8.setEnabled(false);
button9.setEnabled(false);
}else if (button3.getText().endsWith("O") && button5.getText().endsWith("O") && button7.getText().endsWith("O")) {
F1.setVisible(false);
F2.setText("O hat Gewonnen!!!");
F2.setVisible(true);
button3.setBackground(Color.green);
button5.setBackground(Color.green);
button7.setBackground(Color.green);
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
//
button4.setEnabled(false);
button5.setEnabled(false);
button6.setEnabled(false);
//
button7.setEnabled(false);
button8.setEnabled(false);
button9.setEnabled(false);
}
}
};
//Button Aktion
button1.addActionListener(actionListener);
button2.addActionListener(actionListener);
button3.addActionListener(actionListener);
button4.addActionListener(actionListener);
button5.addActionListener(actionListener);
button6.addActionListener(actionListener);
//
button7.addActionListener(actionListener);
button8.addActionListener(actionListener);
button9.addActionListener(actionListener);
buttonNeu.addActionListener(actionListener);
}
}