Senin, 24 Oktober 2011

Program Dasar Game Tic Tac Toe dengan Java AWT


Gloddaggghhhh..!!!
Jadi juga bikin program game tic tac toe yang dinamai TikTakTuk menggunakan bahasa Java, pakai AWT aja ah (yang Swing ntar aja [tapi ga janji] :p).

Sesuai judul, ini hanya program dasar... bukan program final... tujuannya untuk mempelajari algoritmanya dan even driven pada Java GUI doang... nothing less nothing more. Dan program dimainkan oleh dua orang.

Yang tidak ada di program ini :
  • new game
  • skor
  • pop up message (message box gitu loh)
  • milih X atau O
  • artificial intelligence / kecerdasan buatan (manusia vs komputer)
  • status bar
  • menu bar
  • bayaran utk penulis :p~~~
Ini dia source code-nya :
/*
* TikTakTuk.java
*
* Created on October 24, 2011, 12:07 PM
*
*/

/**
*
* @author ilmubiner@gmail.com
*/
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);

}
}


Selamat belajar

2 komentar:

dyre mengatakan...

bisa ngasi keterangan untuk skripnya gk?

seventhsky mengatakan...

gk