KONTAK SAYA

eMail Twitter Facebook

TELUSURI

Rabu, 04 Januari 2012

tiktaktuk

import java.awt.*;
import java.awt.event.*;

public class TikTakTuk extends Frame implements ActionListener {
Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
String giliran;

public TikTakTuk() {
super.setTitle("TikTakTuk");
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});

this.setLayout(new GridLayout(3,3));
this.btn1=new Button();
this.btn1.setFont(new Font("Dialog",2,40));
this.btn1.addActionListener(this);
this.add(btn1);
this.btn2=new Button();
this.btn2.setFont(new Font("Dialog",2,40));
this.btn2.addActionListener(this);
this.add(btn2);
this.btn3=new Button();
this.btn3.setFont(new Font("Dialog",2,40));
this.btn3.addActionListener(this);
this.add(btn3);
this.btn4=new Button();
this.btn4.setFont(new Font("Dialog",2,40));
this.btn4.addActionListener(this);
this.add(btn4);
this.btn5=new Button();
this.btn5.setFont(new Font("Dialog",2,40));
this.btn5.addActionListener(this);
this.add(btn5);
this.btn6=new Button();
this.btn6.setFont(new Font("Dialog",2,40));
this.btn6.addActionListener(this);
this.add(btn6);
this.btn7=new Button();
this.btn7.setFont(new Font("Dialog",2,40));
this.btn7.addActionListener(this);
this.add(btn7);
this.btn8=new Button();
this.btn8.setFont(new Font("Dialog",2,40));
this.btn8.addActionListener(this);
this.add(btn8);
this.btn9=new Button();
this.btn9.setFont(new Font("Dialog",2,40));
this.btn9.addActionListener(this);
this.add(btn9);

this.giliran = "X";

}

public void actionPerformed(ActionEvent e) {
Boolean ganti = false;
if (e.getSource()==btn1 && btn1.getLabel()=="") {btn1.setLabel(giliran);ganti=true;}
else if (e.getSource()==btn2 && btn2.getLabel()=="") {btn2.setLabel(giliran);ganti=true;}
else if (e.getSource()==btn3 && btn3.getLabel()=="") {btn3.setLabel(giliran);ganti=true;}
else if (e.getSource()==btn4 && btn4.getLabel()=="") {btn4.setLabel(giliran);ganti=true;}
else if (e.getSource()==btn5 && btn5.getLabel()=="") {btn5.setLabel(giliran);ganti=true;}
else if (e.getSource()==btn6 && btn6.getLabel()=="") {btn6.setLabel(giliran);ganti=true;}
else if (e.getSource()==btn7 && btn7.getLabel()=="") {btn7.setLabel(giliran);ganti=true;}
else if (e.getSource()==btn8 && btn8.getLabel()=="") {btn8.setLabel(giliran);ganti=true;}
else if (e.getSource()==btn9 && btn9.getLabel()=="") {btn9.setLabel(giliran);ganti=true;}

if ((btn1.getLabel()==giliran &&
btn2.getLabel()==giliran &&
btn3.getLabel()==giliran) ||
(btn4.getLabel()==giliran &&
btn5.getLabel()==giliran &&
btn6.getLabel()==giliran) ||
(btn7.getLabel()==giliran &&
btn8.getLabel()==giliran &&
btn9.getLabel()==giliran) ||
(btn1.getLabel()==giliran &&
btn4.getLabel()==giliran &&
btn7.getLabel()==giliran) ||
(btn2.getLabel()==giliran &&
btn5.getLabel()==giliran &&
btn8.getLabel()==giliran) ||
(btn3.getLabel()==giliran &&
btn5.getLabel()==giliran &&
btn9.getLabel()==giliran) ||
(btn1.getLabel()==giliran &&
btn5.getLabel()==giliran &&
btn9.getLabel()==giliran) ||
(btn3.getLabel()==giliran &&
btn5.getLabel()==giliran &&
btn7.getLabel()==giliran) ){
this.setTitle(giliran + " menang!");
btn1.setEnabled(false);btn2.setEnabled(false);btn3.setEnabled(false);
btn4.setEnabled(false);btn5.setEnabled(false);btn6.setEnabled(false);
btn7.setEnabled(false);btn8.setEnabled(false);btn9.setEnabled(false);
}


if (ganti==true){
if (giliran=="X" && ganti==true) giliran="O"; else giliran = "X";
}
}


public static void main(String[] args){

TikTakTuk frm = new TikTakTuk();

frm.setSize(300, 300);
frm.setVisible(true);

}
}