Skip to content

@BranislavLazic /tic-toe.java
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Worst program I ever made.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ticToe {
int set=1;
String check1,check2,check3,check4,check5,check6,check7,check8,check9;
JFrame frame=new JFrame("Tic-Toe");
JPanel panel=new JPanel();
JButton btn1=new JButton(" ");
JButton btn2=new JButton(" ");
JButton btn3=new JButton(" ");
JButton btn4=new JButton(" ");
JButton btn5=new JButton(" ");
JButton btn6=new JButton(" ");
JButton btn7=new JButton(" ");
JButton btn8=new JButton(" ");
JButton btn9=new JButton(" ");
JMenuBar mBar= new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem mItem = new JMenuItem("New");
public ticToe(){
frame.setVisible(true);
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(184,200);
frame.add(panel);
mBar.add(menu);
menu.add(mItem);
frame.setJMenuBar(mBar);
GridLayout layout = new GridLayout();
layout.setColumns(3);
layout.setRows(3);
layout.setHgap(7);
layout.setVgap(7);
panel.setLayout(layout);
panel.add(btn1);
panel.add(btn2);
panel.add(btn3);
panel.add(btn4);
panel.add(btn5);
panel.add(btn6);
panel.add(btn7);
panel.add(btn8);
panel.add(btn9);
btn1.setFont(new Font("sanserif", Font.BOLD, 16));
btn2.setFont(new Font("sanserif", Font.BOLD, 16));
btn3.setFont(new Font("sanserif", Font.BOLD, 16));
btn4.setFont(new Font("sanserif", Font.BOLD, 16));
btn5.setFont(new Font("sanserif", Font.BOLD, 16));
btn6.setFont(new Font("sanserif", Font.BOLD, 16));
btn7.setFont(new Font("sanserif", Font.BOLD, 16));
btn8.setFont(new Font("sanserif", Font.BOLD, 16));
btn9.setFont(new Font("sanserif", Font.BOLD, 16));
mItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
btn1.setText(" ");
btn2.setText(" ");
btn3.setText(" ");
btn4.setText(" ");
btn5.setText(" ");
btn6.setText(" ");
btn7.setText(" ");
btn8.setText(" ");
btn9.setText(" ");
check1="";
check2="";
check3="";
check4="";
check5="";
check6="";
check7="";
check8="";
check9="";
}});
// And awesome game logic.
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(set==1){
btn1.setText("X");
set=2;
check1=btn1.getText();
}
else if (set==2){
btn1.setText("O");
set=1;
check1=btn1.getText();
}
if (check1 == "X"){
if (check2=="X"){
if (check3=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check1=="X"){
if (check4=="X"){
if (check7=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check1=="X"){
if(check5=="X"){
if (check9=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check1=="O"){
if (check2=="O"){
if (check3=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
if (check1=="O"){
if (check4=="O"){
if (check7=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
if (check1=="O"){
if(check5=="O"){
if (check9=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
}
});
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(set==1){
btn2.setText("X");
set=2;
check2=btn2.getText();
}
else if (set==2){
btn2.setText("O");
set=1;
check2=btn2.getText();
}
if (check2=="X"){
if (check1=="X"){
if (check3=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check2=="X"){
if (check5=="X"){
if (check8=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check2=="O"){
if (check1=="O"){
if (check3=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
if (check2=="O"){
if (check5=="O"){
if (check8=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
}
});
btn3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(set==1){
btn3.setText("X");
set=2;
check3=btn3.getText();
}
else if (set==2){
btn3.setText("O");
set=1;
check3=btn3.getText();
}
if (check3=="X"){
if (check2=="X"){
if (check1=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check3=="X"){
if (check6=="X"){
if (check9=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check3=="X"){
if (check5=="X"){
if (check7=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check3=="O"){
if (check2=="O"){
if (check1=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
if (check3=="O"){
if (check6=="O"){
if (check9=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
if (check3=="O"){
if (check5=="O"){
if (check7=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
}
});
btn4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(set==1){
btn4.setText("X");
set=2;
check4=btn4.getText();
}
else if (set==2){
btn4.setText("O");
set=1;
check4=btn4.getText();
}
if (check4=="X"){
if (check5=="X"){
if (check6=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check4=="X"){
if (check1=="X"){
if(check7=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check4=="O"){
if (check5=="O"){
if (check6=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
if (check4=="O"){
if (check1=="O"){
if(check7=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
}
});
btn5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(set==1){
btn5.setText("X");
set=2;
check5=btn5.getText();
}
else if (set==2){
btn5.setText("O");
set=1;
check5=btn5.getText();
}
if (check5=="X"){
if(check4=="X"){
if(check6=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check5=="X"){
if (check2=="X"){
if (check8=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check5=="X"){
if (check1=="X"){
if (check9=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check5=="X"){
if (check3=="X"){
if (check7=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check5=="O"){
if(check4=="O"){
if(check6=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
if (check5=="O"){
if (check2=="O"){
if (check8=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
if (check5=="O"){
if (check1=="O"){
if (check9=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
if (check5=="O"){
if (check3=="O"){
if (check7=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
}
});
btn6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(set==1){
btn6.setText("X");
set=2;
check6=btn6.getText();
}
else if (set==2){
btn6.setText("O");
set=1;
check6=btn6.getText();
}
if (check6=="X"){
if (check3=="X"){
if (check9=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check6=="X"){
if (check5=="X"){
if (check4=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check6=="O"){
if (check3=="O"){
if (check9=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
if (check6=="O"){
if (check5=="O"){
if (check4=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
}
});
btn7.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(set==1){
btn7.setText("X");
set=2;
check7=btn7.getText();
}
else if (set==2){
btn7.setText("O");
set=1;
check7=btn7.getText();
}
if (check7=="X"){
if (check8=="X"){
if (check9=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check7=="X"){
if (check4=="X"){
if (check1=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check7=="X"){
if (check5=="X"){
if (check3=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check7=="O"){
if (check8=="O"){
if (check9=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
if (check7=="O"){
if (check4=="O"){
if (check1=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
if (check7=="O"){
if (check5=="O"){
if (check3=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
}
});
btn8.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(set==1){
btn8.setText("X");
set=2;
check8=btn8.getText();
}
else if (set==2){
btn8.setText("O");
set=1;
check8=btn8.getText();
}
if (check8=="X"){
if (check7=="X"){
if (check9=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check8=="X"){
if (check5=="X"){
if (check2=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check8=="O"){
if (check7=="O"){
if (check9=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
if (check8=="O"){
if (check5=="O"){
if (check2=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
}
});
btn9.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(set==1){
btn9.setText("X");
set=2;
check9=btn9.getText();
}
else if (set==2){
btn9.setText("O");
set=1;
check9=btn9.getText();
}
if (check9=="X"){
if (check7=="X"){
if (check8=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check9=="X"){
if (check6=="X"){
if (check3=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check9=="X"){
if (check5=="X"){
if (check1=="X"){
JOptionPane.showMessageDialog(null,"X won!");
}
}
}
if (check9=="O"){
if (check7=="O"){
if (check8=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
if (check9=="O"){
if (check6=="O"){
if (check3=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
if (check9=="O"){
if (check5=="O"){
if (check1=="O"){
JOptionPane.showMessageDialog(null,"O won!");
}
}
}
}
});
}
public static void main(String[] args) {
new ticToe();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.