S
Skonrox
Gast
Hallo,
ich habe hier einmal versucht das berühmte Spiel Tick-Tack-Toe zu programmieren, jedoch bin ich schnell bei dem Versuch hingefallen.
Ich habe 3x3 Felder gemacht (normal für das Spiel), und wenn man ein feld anklickt (ToggleButton), soll immer abwechselnd O und X kommen, jedoch funktioniert dies nur bei den oberen feldern, ich verstehe nicht wieso und kriege es auch nicht unten hin.
Code:
Bin für jede Hilfe dankbar!
Mit freundlichen Grüßen,
Skonrox.
ich habe hier einmal versucht das berühmte Spiel Tick-Tack-Toe zu programmieren, jedoch bin ich schnell bei dem Versuch hingefallen.
Ich habe 3x3 Felder gemacht (normal für das Spiel), und wenn man ein feld anklickt (ToggleButton), soll immer abwechselnd O und X kommen, jedoch funktioniert dies nur bei den oberen feldern, ich verstehe nicht wieso und kriege es auch nicht unten hin.
Code:
Java:
package Tick_Tack_Toe;
import javax.swing.SwingUtilities;
import javax.swing.JPanel;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JToggleButton;
import java.awt.event.ActionEvent;
public class TickTackToe extends JFrame
{
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JToggleButton jToggleButton = null;
private JToggleButton jToggleButton1 = null;
private JToggleButton jToggleButton2 = null;
private JToggleButton jToggleButton3 = null;
private JToggleButton jToggleButton4 = null;
private JToggleButton jToggleButton5 = null;
private JToggleButton jToggleButton6 = null;
private JToggleButton jToggleButton7 = null;
private JToggleButton jToggleButton8 = null;
private JLabel O1 = null; //Die Kreise
private JLabel O2 = null;
private JLabel O3 = null;
private JLabel O4 = null;
private JLabel O5 = null;
private JLabel O6 = null;
private JLabel O7 = null;
private JLabel O8 = null;
private JLabel O9 = null;
private JLabel X1 = null; //Die Xe
private JLabel X2 = null;
private JLabel X3 = null;
private JLabel X4 = null;
private JLabel X5 = null;
private JLabel X6 = null;
private JLabel X7 = null;
private JLabel X8 = null;
private JLabel X9 = null;
public int tick = 0; //Int tick fürs abwechseln von Kreis und X
public TickTackToe() throws HeadlessException
{
super();
initialize();
}
public TickTackToe(GraphicsConfiguration arg0)
{
super(arg0);
initialize();
}
public TickTackToe(String arg0) throws HeadlessException
{
super(arg0);
initialize();
}
public TickTackToe(String arg0, GraphicsConfiguration arg1)
{
super(arg0, arg1);
initialize();
}
private JToggleButton getJToggleButton()
{
if (jToggleButton == null)
{
jToggleButton = new JToggleButton();
jToggleButton.setBounds(new Rectangle(31, 60, 76, 61));
jToggleButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
if(tick == 0 | tick == 2 | tick == 4 | tick == 6 | tick == 8 | tick == 10) //O wird angezeigt
{
jToggleButton.setEnabled(false);
jToggleButton.setVisible(false);
O1.setVisible(true);
}
if(tick == 1 | tick == 3 | tick == 5 | tick == 7 | tick == 9) //X Wird angezeigt
{
jToggleButton.setEnabled(false);
jToggleButton.setVisible(false);
X1.setVisible(true);
}
tick++; //tick wird hochgezählt
}
});
}
return jToggleButton;
}
private JToggleButton getJToggleButton1()
{
if (jToggleButton1 == null)
{
jToggleButton1 = new JToggleButton();
jToggleButton1.setBounds(new Rectangle(106, 60, 76, 61));
jToggleButton1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
if(tick == 0 | tick == 2 | tick == 4 | tick == 6 | tick == 8 | tick == 10)
{
jToggleButton1.setEnabled(false);
jToggleButton1.setVisible(false);
O2.setVisible(true);
}
if(tick == 1 | tick == 3 | tick == 5 | tick == 7 | tick == 9)
{
jToggleButton1.setEnabled(false);
jToggleButton1.setVisible(false);
X2.setVisible(true);
}
tick++;
}
});
}
return jToggleButton1;
}
private JToggleButton getJToggleButton2()
{
if (jToggleButton2 == null)
{
jToggleButton2 = new JToggleButton();
jToggleButton2.setBounds(new Rectangle(181, 60, 76, 61));
jToggleButton2.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
if(tick == 0 | tick == 2 | tick == 4 | tick == 6 | tick == 8 | tick == 10)
{
jToggleButton2.setEnabled(false);
jToggleButton2.setVisible(false);
O3.setVisible(true);
}
if(tick == 1 | tick == 3 | tick == 5 | tick == 7 | tick == 9)
{
jToggleButton2.setEnabled(false);
jToggleButton2.setVisible(false);
X3.setVisible(true);
}
tick++;
}
});
}
return jToggleButton2;
}
private JToggleButton getJToggleButton3()
{
if (jToggleButton3 == null)
{
jToggleButton3 = new JToggleButton();
jToggleButton3.setBounds(new Rectangle(31, 120, 76, 61));
jToggleButton3.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
if(tick == 0 | tick == 2 | tick == 4 | tick == 6 | tick == 8 | tick == 10)
{
jToggleButton3.setEnabled(false);
jToggleButton3.setVisible(false);
O4.setVisible(true);
}
if(tick == 1 | tick == 3 | tick == 5 | tick == 7 | tick == 9)
{
jToggleButton3.setEnabled(false);
jToggleButton3.setVisible(false);
X4.setVisible(true);
}
tick++;
}
});
}
return jToggleButton3;
}
private JToggleButton getJToggleButton4()
{
if (jToggleButton4 == null)
{
jToggleButton4 = new JToggleButton();
jToggleButton4.setBounds(new Rectangle(106, 120, 76, 61));
jToggleButton4.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
if(tick == 0 | tick == 2 | tick == 4 | tick == 6 | tick == 8 | tick == 10)
{
jToggleButton4.setEnabled(false);
jToggleButton4.setVisible(false);
O5.setVisible(true);
}
if(tick == 1 | tick == 3 | tick == 5 | tick == 7 | tick == 9)
{
jToggleButton4.setEnabled(false);
jToggleButton4.setVisible(false);
X5.setVisible(true);
}
tick++;
}
});
}
return jToggleButton4;
}
private JToggleButton getJToggleButton5()
{
if (jToggleButton5 == null)
{
jToggleButton5 = new JToggleButton();
jToggleButton5.setBounds(new Rectangle(106, 180, 76, 61));
jToggleButton5.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
if(tick == 0 | tick == 2 | tick == 4 | tick == 6 | tick == 8 | tick == 10)
{
jToggleButton5.setEnabled(false);
jToggleButton5.setVisible(false);
O6.setVisible(true);
}
if(tick == 1 | tick == 3 | tick == 5 | tick == 7 | tick == 9)
{
jToggleButton5.setEnabled(false);
jToggleButton5.setVisible(false);
X6.setVisible(true);
}
tick++;
}
});
}
return jToggleButton5;
}
private JToggleButton getJToggleButton6()
{
if (jToggleButton6 == null)
{
jToggleButton6 = new JToggleButton();
jToggleButton6.setBounds(new Rectangle(181, 120, 76, 61));
jToggleButton6.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
if(tick == 0 | tick == 2 | tick == 4 | tick == 6 | tick == 8 | tick == 10)
{
jToggleButton6.setEnabled(false);
jToggleButton6.setVisible(false);
O7.setVisible(true);
}
if(tick == 1 | tick == 3 | tick == 5 | tick == 7 | tick == 9)
{
jToggleButton6.setEnabled(false);
jToggleButton6.setVisible(false);
X7.setVisible(true);
}
tick++;
}
});
}
return jToggleButton6;
}
private JToggleButton getJToggleButton7()
{
if (jToggleButton7 == null)
{
jToggleButton7 = new JToggleButton();
jToggleButton7.setBounds(new Rectangle(31, 180, 76, 61));
jToggleButton7.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
if(tick == 0 | tick == 2 | tick == 4 | tick == 6 | tick == 8 | tick == 10)
{
jToggleButton7.setEnabled(false);
jToggleButton7.setVisible(false);
O8.setVisible(true);
}
if(tick == 1 | tick == 3 | tick == 5 | tick == 7 | tick == 9)
{
jToggleButton7.setEnabled(false);
jToggleButton7.setVisible(false);
X8.setVisible(true);
}
tick++;
}
});
}
return jToggleButton7;
}
private JToggleButton getJToggleButton8()
{
if (jToggleButton8 == null)
{
jToggleButton8 = new JToggleButton();
jToggleButton8.setBounds(new Rectangle(181, 180, 76, 61));
jToggleButton8.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
if(tick == 0 | tick == 2 | tick == 4 | tick == 6 | tick == 8 | tick == 10)
{
jToggleButton8.setEnabled(false);
jToggleButton8.setVisible(false);
O9.setVisible(true);
}
if(tick == 1 | tick == 3 | tick == 5 | tick == 7 | tick == 9)
{
jToggleButton8.setEnabled(false);
jToggleButton8.setVisible(false);
X9.setVisible(true);
}
tick++;
}
});
}
return jToggleButton8;
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
TickTackToe thisClass = new TickTackToe();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
private void initialize()
{
this.setSize(301, 289);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
X1.setVisible(false); //Alle X und O erst einmal verstecken
X2.setVisible(false);
X3.setVisible(false);
X4.setVisible(false);
X5.setVisible(false);
X6.setVisible(false);
X7.setVisible(false);
X8.setVisible(false);
X9.setVisible(false);
O1.setVisible(false);
O2.setVisible(false);
O3.setVisible(false);
O4.setVisible(false);
O5.setVisible(false);
O6.setVisible(false);
O7.setVisible(false);
O8.setVisible(false);
O9.setVisible(false);
}
private JPanel getJContentPane()
{
if (jContentPane == null)
{
X9 = new JLabel();
X9.setBounds(new Rectangle(182, 181, 75, 60));
X9.setHorizontalTextPosition(SwingConstants.CENTER);
X9.setFont(new Font("Dialog", Font.BOLD, 24));
X9.setText("X");
X9.setHorizontalAlignment(SwingConstants.CENTER);
X8 = new JLabel();
X8.setBounds(new Rectangle(107, 181, 75, 60));
X8.setHorizontalTextPosition(SwingConstants.CENTER);
X8.setFont(new Font("Dialog", Font.BOLD, 24));
X8.setText("X");
X8.setHorizontalAlignment(SwingConstants.CENTER);
X7 = new JLabel();
X7.setBounds(new Rectangle(33, 181, 75, 60));
X7.setHorizontalTextPosition(SwingConstants.CENTER);
X7.setFont(new Font("Dialog", Font.BOLD, 24));
X7.setText("X");
X7.setHorizontalAlignment(SwingConstants.CENTER);
X6 = new JLabel();
X6.setBounds(new Rectangle(181, 121, 75, 60));
X6.setHorizontalTextPosition(SwingConstants.CENTER);
X6.setFont(new Font("Dialog", Font.BOLD, 24));
X6.setText("X");
X6.setHorizontalAlignment(SwingConstants.CENTER);
X5 = new JLabel();
X5.setBounds(new Rectangle(107, 121, 75, 60));
X5.setHorizontalTextPosition(SwingConstants.CENTER);
X6.setFont(new Font("Dialog", Font.BOLD, 24));
X5.setText("X");
X5.setHorizontalAlignment(SwingConstants.CENTER);
X4 = new JLabel();
X4.setBounds(new Rectangle(31, 121, 75, 60));
X4.setHorizontalTextPosition(SwingConstants.CENTER);
X4.setFont(new Font("Dialog", Font.BOLD, 24));
X4.setText("X");
X4.setHorizontalAlignment(SwingConstants.CENTER);
X3 = new JLabel();
X3.setBounds(new Rectangle(182, 60, 75, 62));
X3.setHorizontalTextPosition(SwingConstants.CENTER);
X3.setFont(new Font("Dialog", Font.BOLD, 24));
X3.setText("X");
X3.setHorizontalAlignment(SwingConstants.CENTER);
X2 = new JLabel();
X2.setBounds(new Rectangle(107, 60, 75, 62));
X2.setHorizontalTextPosition(SwingConstants.CENTER);
X2.setFont(new Font("Dialog", Font.BOLD, 24));
X2.setText("X");
X2.setHorizontalAlignment(SwingConstants.CENTER);
X1 = new JLabel();
X1.setBounds(new Rectangle(30, 60, 78, 63));
X1.setHorizontalAlignment(SwingConstants.CENTER);
X1.setHorizontalTextPosition(SwingConstants.CENTER);
X1.setFont(new Font("Dialog", Font.BOLD, 24));
X1.setText("X");
O9 = new JLabel();
O9.setBounds(new Rectangle(182, 181, 74, 60));
O9.setHorizontalTextPosition(SwingConstants.CENTER);
O9.setFont(new Font("Dialog", Font.BOLD, 24));
O9.setText("O");
O9.setHorizontalAlignment(SwingConstants.CENTER);
O8 = new JLabel();
O8.setBounds(new Rectangle(106, 181, 76, 60));
O8.setHorizontalTextPosition(SwingConstants.CENTER);
O8.setFont(new Font("Dialog", Font.BOLD, 24));
O8.setText("O");
O8.setHorizontalAlignment(SwingConstants.CENTER);
O7 = new JLabel();
O7.setBounds(new Rectangle(31, 180, 76, 61));
O7.setHorizontalTextPosition(SwingConstants.CENTER);
O7.setFont(new Font("Dialog", Font.BOLD, 24));
O7.setText("O");
O7.setHorizontalAlignment(SwingConstants.CENTER);
O6 = new JLabel();
O6.setBounds(new Rectangle(181, 120, 75, 61));
O6.setHorizontalTextPosition(SwingConstants.CENTER);
O6.setFont(new Font("Dialog", Font.BOLD, 24));
O6.setText("O");
O6.setHorizontalAlignment(SwingConstants.CENTER);
O5 = new JLabel();
O5.setBounds(new Rectangle(106, 121, 76, 61));
O5.setHorizontalTextPosition(SwingConstants.CENTER);
O5.setFont(new Font("Dialog", Font.BOLD, 24));
O5.setText("O");
O5.setHorizontalAlignment(SwingConstants.CENTER);
O4 = new JLabel();
O4.setBounds(new Rectangle(31, 121, 75, 60));
O4.setHorizontalTextPosition(SwingConstants.CENTER);
O4.setFont(new Font("Dialog", Font.BOLD, 24));
O4.setText("O");
O4.setHorizontalAlignment(SwingConstants.CENTER);
O3 = new JLabel();
O3.setBounds(new Rectangle(182, 60, 75, 61));
O3.setHorizontalAlignment(SwingConstants.CENTER);
O3.setHorizontalTextPosition(SwingConstants.CENTER);
O3.setFont(new Font("Dialog", Font.BOLD, 24));
O3.setText("O");
O2 = new JLabel();
O2.setBounds(new Rectangle(106, 60, 76, 61));
O2.setHorizontalAlignment(SwingConstants.CENTER);
O2.setHorizontalTextPosition(SwingConstants.CENTER);
O2.setFont(new Font("Dialog", Font.BOLD, 24));
O2.setText("O");
O1 = new JLabel();
O1.setBounds(new Rectangle(31, 60, 76, 61));
O1.setHorizontalAlignment(SwingConstants.CENTER);
O1.setHorizontalTextPosition(SwingConstants.CENTER);
O1.setFont(new Font("Dialog", Font.BOLD, 24));
O1.setText("O");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(47, 11, 187, 36));
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
jLabel.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel.setFont(new Font("Dialog", Font.BOLD, 24));
jLabel.setText("Tick-Tack-Toe");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(getJToggleButton(), null);
jContentPane.add(getJToggleButton1(), null);
jContentPane.add(getJToggleButton2(), null);
jContentPane.add(getJToggleButton3(), null);
jContentPane.add(getJToggleButton4(), null);
jContentPane.add(getJToggleButton5(), null);
jContentPane.add(getJToggleButton6(), null);
jContentPane.add(getJToggleButton7(), null);
jContentPane.add(getJToggleButton8(), null);
jContentPane.add(O1, null);
jContentPane.add(O2, null);
jContentPane.add(O3, null);
jContentPane.add(O4, null);
jContentPane.add(O5, null);
jContentPane.add(O6, null);
jContentPane.add(O7, null);
jContentPane.add(O8, null);
jContentPane.add(O9, null);
jContentPane.add(X1, null);
jContentPane.add(X2, null);
jContentPane.add(X3, null);
jContentPane.add(X4, null);
jContentPane.add(X5, null);
jContentPane.add(X6, null);
jContentPane.add(X7, null);
jContentPane.add(X8, null);
jContentPane.add(X9, null);
}
return jContentPane;
}
} // @jve:decl-index=0:visual-constraint="10,10"
Mit freundlichen Grüßen,
Skonrox.