Selasa, 03 Januari 2012

Untuk Tugas V-Class

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TicTacToe implements ActionListener
{

    private static final int LEBAR_TAMPILAN = 300;
    private static final int TINGGI_TAMPILAN = 200;
    private static final int LEBAR_TEKS = 20;

    private static final GridLayout LAYOUT_STYLE = new GridLayout(3,3);

    private JFrame window = new JFrame("TIC-TAC-TOE");
    private JButton box1 = new JButton("");
    private JButton box2 = new JButton("");
    private JButton box3 = new JButton("");
    private JButton box4 = new JButton("");
    private JButton box5 = new JButton("");
    private JButton box6 = new JButton("");
    private JButton box7 = new JButton("");
    private JButton box8 = new JButton("");
    private JButton box9 = new JButton("");
    String tanda = "X";
    boolean menang = false;
    Color black = new Color(0,0,0);
    Color notblack = new Color(255,255,255);

    public TicTacToe()
    {
        window.setSize(LEBAR_TAMPILAN,TINGGI_TAMPILAN);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        window.getContentPane().setLayout(LAYOUT_STYLE);
        window.getContentPane().add(box1);
        window.getContentPane().add(box2);
        window.getContentPane().add(box3);
        window.getContentPane().add(box4);
        window.getContentPane().add(box5);
        window.getContentPane().add(box6);
        window.getContentPane().add(box7);
        window.getContentPane().add(box8);
        window.getContentPane().add(box9);
        box1.addActionListener(this);
        box2.addActionListener(this);
        box3.addActionListener(this);
        box4.addActionListener(this);
        box5.addActionListener(this);
        box6.addActionListener(this);
        box7.addActionListener(this);
        box8.addActionListener(this);
        box9.addActionListener(this);

        window.setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        int count=1;
    do
    {

    if (e.getSource()==box1)
    {
        box1.setText(tanda);
        box1.setEnabled(false);
    }
    else if (e.getSource()==box2)
    {
        box2.setText(tanda);
        box2.setEnabled(false);
    }
    else if (e.getSource()==box3)
    {
        box3.setText(tanda);
        box3.setEnabled(false);
    }
    else if (e.getSource()==box4)
    {
        box4.setText(tanda);
        box4.setEnabled(false);
    }
    else if (e.getSource()==box5)
    {
        box5.setText(tanda);
        box5.setEnabled(false);
    }
    else if (e.getSource()==box6)
    {
        box6.setText(tanda);
        box6.setEnabled(false);
    }
    else if (e.getSource()==box7)
    {
        box7.setText(tanda);
        box7.setEnabled(false);
    }
    else if (e.getSource()==box8)
    {
        box8.setText(tanda);
        box8.setEnabled(false);
    }
    else if (e.getSource()==box9)
    {
        box9.setText(tanda);
        box9.setEnabled(false);
    }


    if (box1.getText().equals(box2.getText()) && box2.getText().equals(box3.getText()) && box1.getText().equals("")==false)
    {
        box1.setBackground(notblack);
        box2.setBackground(notblack);
        box3.setBackground(notblack);
        menang=true;
    }
    else if (box4.getText().equals(box5.getText()) && box5.getText().equals(box6.getText())&& box4.getText().equals("")==false)
    {
        box4.setBackground(notblack);
        box5.setBackground(notblack);
        box6.setBackground(notblack);
        menang=true;
    }
    else if (box7.getText().equals(box8.getText()) && box8.getText().equals(box9.getText())&& box7.getText().equals("")==false)
    {
        box7.setBackground(notblack);
        box8.setBackground(notblack);
        box9.setBackground(notblack);
        menang=true;
    }
    else if (box1.getText().equals(box4.getText()) && box4.getText().equals(box7.getText())&& box1.getText().equals("")==false)
    {
        box1.setBackground(notblack);
        box4.setBackground(notblack);
        box7.setBackground(notblack);
        menang=true;
    }
    else if (box2.getText().equals(box5.getText()) && box5.getText().equals(box8.getText())&& box2.getText().equals("")==false)
    {
        box2.setBackground(notblack);
        box5.setBackground(notblack);
        box8.setBackground(notblack);
        menang=true;
    }
    else if (box3.getText().equals(box6.getText()) && box6.getText().equals(box9.getText())&& box3.getText().equals("")==false)
    {
        box3.setBackground(notblack);
        box6.setBackground(notblack);
        box9.setBackground(notblack);
        menang=true;
    }
    else if (box1.getText().equals(box5.getText()) && box5.getText().equals(box9.getText())&& box1.getText().equals("")==false)
    {
        box1.setBackground(notblack);
        box5.setBackground(notblack);
        box9.setBackground(notblack);
        menang=true;
    }
    else if (box3.getText().equals(box5.getText()) && box5.getText().equals(box7.getText())&& box3.getText().equals("")==false)
    {
        box3.setBackground(notblack);
        box5.setBackground(notblack);
        box7.setBackground(notblack);
        menang=true;
    }
    else if (count==9 && menang==false)
    {
        JOptionPane.showMessageDialog(null, "Hasilnya Seri");
        menang=true;
    }

    System.out.println(count);
    if (count!=9 && menang==true)
    {
        JOptionPane.showMessageDialog(null, tanda + " MENANG !");
        System.exit(1);
    }
        if (tanda.equals("X"))
    {
        tanda="O";
    }
    else
    {
        tanda="X";
    }

    }

        while(menang=false);

    }
public static void main(String[] args)
    {
        TicTacToe gui = new TicTacToe();
    }
}