hey,
ich bin dabei fuer die Schule ein TicTacToe Spiel zu schreiben. ich bin nur noch nicht zufrieden mit dem Umfang meines Codes. Gibt es eine möglichkeit diesen zu kürzen? (Im besonderen die ganzen Abfragen). Wäre nett wenn sich jemadn die mühe macht und den code mal durchliest
gruß stephan
ich bin dabei fuer die Schule ein TicTacToe Spiel zu schreiben. ich bin nur noch nicht zufrieden mit dem Umfang meines Codes. Gibt es eine möglichkeit diesen zu kürzen? (Im besonderen die ganzen Abfragen). Wäre nett wenn sich jemadn die mühe macht und den code mal durchliest
Code:
package Projekt;
import java.awt.TextArea;
import java.awt.Panel;
import java.awt.Font;
import java.awt.Color;
import java.awt.Label;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TicTacToe extends Frame {
Button[] b = new Button[9];
Button b10 = new Button();
Button b11 = new Button();
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();
Panel p4 = new Panel();
Label l1 = new Label();
Label l2 = new Label();
Label l3 = new Label();
Label l4 = new Label();
Font f1 = new Font("Arial",Font.BOLD,60);
Font f2 = new Font("Arial",Font.PLAIN,15);
int i = 1;
int gX = 0;
int gO = 0;
int u = 0;
public TicTacToe() {
setTitle("TicTacToe");
setSize(500, 500);
setLayout(new GridLayout(2,1));
add(p1);
p1.setLayout(new GridLayout(3,3,2,2));
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
ActionListener l = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(b10))
{
Neu();
}
else if(e.getSource().equals(b11))
{
ZaehlerZuruecksetzen();
}
else
{
Button b = (Button) e.getSource();
if (i % 2 == 0) {
b.setLabel("O");
l1.setText("Spieler X am Zug");
} else {
b.setLabel("X");
l1.setText("Spieler O am Zug");
}
b.disable();
i = i + 1;
if(ErgebnisPruefen())
{
FelderAusschalten();
}
else{
if(SpielfeldVoll())
{
FelderAusschalten();
}
}
}
}
};
for (int i = 0; i < 9; i++) {
b[i] = new Button();
b[i].addActionListener(l);
p1.add(b[i]);
b[i].setBackground(Color.lightGray);
b[i].setFont(f1);
}
add(p2);
p2.setLayout(new GridLayout(2,2));
p2.add(p3);
p3.setLayout(new GridLayout(1,1));
p3.add(l1);
l1.setBackground(Color.lightGray);
l1.setFont(f2);
p2.add(p4);
p4.setLayout(new GridLayout(3,1));
p4.add(l2);
l2.setBackground(Color.lightGray);
l2.setFont(f2);
p4.add(l3);
l3.setBackground(Color.lightGray);
l3.setFont(f2);
p4.add(l4);
l4.setBackground(Color.lightGray);
l4.setFont(f2);
p2.add(b10);
b10.setBackground(Color.lightGray);
b10.setLabel("Neues Spiel");
b10.addActionListener(l);
p2.add(b11);
b11.setBackground(Color.lightGray);
b11.setLabel("Zähler zurücksetzen");
b11.addActionListener(l);
l2.setText("Siege Spieler X: "+gX);
l3.setText("Siege Spieler O: "+gO);
l4.setText("Unentschieden: "+u);
}
private boolean ErgebnisPruefen() {
boolean a = false;
if (b[0].getLabel() == "X" && b[0].getLabel() != "" && b[0].getLabel()==b[1].getLabel()&& b[1].getLabel()==b[2].getLabel()) {
l1.setText("Spieler X hat gewonnen"); a = true; gX++;b[0].setBackground(Color.green);b[1].setBackground(Color.green);b[2].setBackground(Color.green);}
else if (b[3].getLabel() == "X" && b[3].getLabel() != "" && b[3].getLabel()==b[4].getLabel()&& b[4].getLabel()==b[5].getLabel()) {
l1.setText("Spieler X hat gewonnen"); a = true; gX++;b[3].setBackground(Color.green);b[4].setBackground(Color.green);b[5].setBackground(Color.green);}
else if (b[6].getLabel() == "X" && b[6].getLabel() != "" && b[6].getLabel()==b[7].getLabel()&& b[7].getLabel()==b[8].getLabel()) {
l1.setText("Spieler X hat gewonnen"); a = true; gX++;b[6].setBackground(Color.green);b[7].setBackground(Color.green);b[8].setBackground(Color.green);}
else if (b[0].getLabel() == "X" && b[0].getLabel() != "" && b[0].getLabel()==b[3].getLabel()&& b[3].getLabel()==b[6].getLabel()) {
l1.setText("Spieler X hat gewonnen"); a = true; gX++;b[0].setBackground(Color.green);b[3].setBackground(Color.green);b[6].setBackground(Color.green);}
else if (b[1].getLabel() == "X" && b[1].getLabel() != "" && b[1].getLabel()==b[4].getLabel()&& b[4].getLabel()==b[7].getLabel()) {
l1.setText("Spieler X hat gewonnen"); a = true; gX++;b[1].setBackground(Color.green);b[4].setBackground(Color.green);b[7].setBackground(Color.green);}
else if (b[2].getLabel() == "X" && b[2].getLabel() != "" && b[2].getLabel()==b[5].getLabel()&& b[5].getLabel()==b[8].getLabel()) {
l1.setText("Spieler X hat gewonnen"); a = true; gX++;b[2].setBackground(Color.green);b[5].setBackground(Color.green);b[8].setBackground(Color.green);}
else if (b[0].getLabel() == "X" && b[0].getLabel() != "" && b[0].getLabel()==b[4].getLabel()&& b[4].getLabel()==b[8].getLabel()) {
l1.setText("Spieler X hat gewonnen"); a = true; gX++;b[0].setBackground(Color.green);b[4].setBackground(Color.green);b[8].setBackground(Color.green);}
else if (b[2].getLabel() == "X" && b[2].getLabel() != "" && b[2].getLabel()==b[4].getLabel()&& b[4].getLabel()==b[6].getLabel()) {
l1.setText("Spieler X hat gewonnen"); a = true; gX++;b[2].setBackground(Color.green);b[4].setBackground(Color.green);b[6].setBackground(Color.green);}
if (b[0].getLabel() == "O" && b[0].getLabel() != "" && b[0].getLabel()==b[1].getLabel()&& b[1].getLabel()==b[2].getLabel()) {
l1.setText("Spieler O hat gewonnen"); a = true; gO++;b[0].setBackground(Color.green);b[1].setBackground(Color.green);b[2].setBackground(Color.green);}
else if (b[3].getLabel() == "O" && b[3].getLabel() != "" && b[3].getLabel()==b[4].getLabel()&& b[4].getLabel()==b[5].getLabel()) {
l1.setText("Spieler O hat gewonnen"); a = true; gO++;b[3].setBackground(Color.green);b[4].setBackground(Color.green);b[5].setBackground(Color.green);}
else if (b[6].getLabel() == "O" && b[6].getLabel() != "" && b[6].getLabel()==b[7].getLabel()&& b[7].getLabel()==b[8].getLabel()) {
l1.setText("Spieler O hat gewonnen"); a = true; gO++;b[6].setBackground(Color.green);b[7].setBackground(Color.green);b[8].setBackground(Color.green);}
else if (b[0].getLabel() == "O" && b[0].getLabel() != "" && b[0].getLabel()==b[3].getLabel()&& b[3].getLabel()==b[6].getLabel()) {
l1.setText("Spieler O hat gewonnen"); a = true; gO++;b[0].setBackground(Color.green);b[3].setBackground(Color.green);b[6].setBackground(Color.green);}
else if (b[1].getLabel() == "O" && b[1].getLabel() != "" && b[1].getLabel()==b[4].getLabel()&& b[4].getLabel()==b[7].getLabel()) {
l1.setText("Spieler O hat gewonnen"); a = true; gO++;b[1].setBackground(Color.green);b[4].setBackground(Color.green);b[7].setBackground(Color.green);}
else if (b[2].getLabel() == "O" && b[2].getLabel() != "" && b[2].getLabel()==b[5].getLabel()&& b[5].getLabel()==b[8].getLabel()) {
l1.setText("Spieler O hat gewonnen"); a = true; gO++;b[2].setBackground(Color.green);b[5].setBackground(Color.green);b[8].setBackground(Color.green);}
else if (b[0].getLabel() == "O" && b[0].getLabel() != "" && b[0].getLabel()==b[4].getLabel()&& b[4].getLabel()==b[8].getLabel()) {
l1.setText("Spieler O hat gewonnen"); a = true; gO++;b[0].setBackground(Color.green);b[4].setBackground(Color.green);b[8].setBackground(Color.green);}
else if (b[2].getLabel() == "O" && b[2].getLabel() != "" && b[2].getLabel()==b[4].getLabel()&& b[4].getLabel()==b[6].getLabel()) {
l1.setText("Spieler O hat gewonnen"); a = true; gO++;b[2].setBackground(Color.green);b[4].setBackground(Color.green);b[6].setBackground(Color.green);}
l2.setText("Siege Spieler X: "+gX);
l3.setText("Siege Spieler O: "+gO);
return a;
}
private boolean SpielfeldVoll() {
boolean c = false;
if (b[0].getLabel() != "" && b[1].getLabel() != "" && b[2].getLabel() != "" && b[3].getLabel() != "" && b[4].getLabel() != "" && b[5].getLabel() != "" && b[6].getLabel() != "" && b[7].getLabel() != "" && b[8].getLabel() != ""){
l1.setText("Unentschieden");
c = true;u++;}
l4.setText("Unentschieden: "+u);
return c;
}
private void Neu(){
for (int i = 0; i < 9; i++) {
b[i].enable();
b[i].setLabel("");
b[i].setBackground(Color.lightGray);
l1.setText("");
}
}
private void FelderAusschalten(){
for (int i = 0; i < 9; i++) {
b[i].disable();
}
}
private void ZaehlerZuruecksetzen(){
gX=0; gO=0; u=0;
l2.setText("Siege Spieler X: "+gX);
l3.setText("Siege Spieler O: "+gO);
l4.setText("Unentschieden: "+u);
}
}
gruß stephan