/*
* To change this
template, choose Tools | Templates
* and open the
template in the editor.
*/
/**
*
* @author
Administrator
*/
public class game_AI{
private
final String[] pilihan =
{"Kertas","Gunting","Batu"};
private
int anda_menang = 0, anda_kalah = 0, CPU_in_action = 0;
public
String bermain (int cpu_action){
CPU_in_action
= RandomAction();
String
status_nilai;
if
(cpu_action == CPU_in_action){
status_nilai
= "Draw !!, ulangi lagi";
}
else
if (cpu_action == 0){
if
(CPU_in_action == 1){
status_nilai
= "Anda Kalah !!";
}
else{
status_nilai
= "Anda Menang !!";
}
}
else
if (cpu_action == 1){
if
(CPU_in_action == 0){
status_nilai
= "Anda Menang !!";
}
else{
status_nilai
= "Anda Kalah !!";
}
}
else{
if
(CPU_in_action == 0){
status_nilai
= "Anda Kalah !!";
}
else{
status_nilai
= "Anda Menang !!";
}
}
nilai_hasil(cpu_action);
return
status_nilai;
}
private
int RandomAction (){
return
(1 + (int) (Math.random() * 3)) - 1;
}
private
void nilai_hasil (int jagoan){
boolean draw;
int
bonus=10;
int
penalti=5;
int[]
peluang_kertas = {0,1,-1};
int[]
peluang_batu = {-1,0,1};
int[]
peluang_gunting = {1,-1,0};
int[][]
peluang = {peluang_kertas, peluang_batu, peluang_gunting};
int
tampil_hasil = peluang[jagoan][CPU_in_action];
if
(tampil_hasil == 0) {
draw=true;
}
else
if (tampil_hasil == -1) {
anda_menang=anda_menang+bonus;
anda_kalah=anda_kalah-penalti;
}
else{
anda_kalah=anda_kalah+bonus;
anda_menang=anda_menang-penalti;
}
if(anda_menang<0)
anda_menang=0;
if(anda_kalah<0) anda_kalah=0;
}
public
void new_game (){
anda_menang
= anda_kalah = CPU_in_action = 0;
}
public
String[] pilih_item (){
return
pilihan;
}
public
String cek_item (int index){
return
pilihan[index];
}
public
String cekCPU_action (){
return
pilihan[CPU_in_action];
}
public
int cekCPU_in_action (){
return
CPU_in_action;
}
public
int cek_menang (){
return
anda_menang;
}
public
int cek_kalah (){
return
anda_kalah;
}
}
/*
* To change this
template, choose Tools | Templates
* and open the
template in the editor.
*/
/**
*
* @author
Administrator
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Latihan3 extends JPanel implements ActionListener,
ItemListener{
private JPanel
panel1, panel2, panel3, panel4, panel5, panel6, panel7, panel8, panel9,
panel10, panel11;
private
JComboBox combo;
private
JTextField txt1, txt2, txt3;
private
JLabel label1, label2, label3, label4, label5, label6, label7;
private
JButton tombol1, tombol2;
private final
Color warna1 = new Color(255,164,155);
private
final Color warna2 = new Color(144,85,42);
private
ImageIcon[] icon1;
private
game_AI sistem = new game_AI();
private
objek_gambar tampil_gambar = new objek_gambar();
private
boolean eror = false;
public
Latihan3 (){
combo
= new JComboBox(sistem.pilih_item());
combo.addItemListener(this);
txt1
= new JTextField(sistem.cekCPU_action(), 5);
txt2
= new JTextField("0", 5);
txt3
= new JTextField("0", 5);
txt1.setEditable(false);
txt2.setEditable(false);
txt3.setEditable(false);
label1
= new JLabel("Player", JLabel.CENTER);
label2
= new JLabel("CPU", JLabel.CENTER);
label3
= new JLabel("Skor Anda :", JLabel.RIGHT);
label4
= new JLabel("Skor CPU :", JLabel.RIGHT);
label5
= new JLabel("", JLabel.CENTER);
label6
= new JLabel("", JLabel.CENTER);
label7
= new JLabel("", JLabel.CENTER);
label6.setPreferredSize(new
Dimension(150,150));
label7.setPreferredSize(new
Dimension(150,150));
tombol1
= new JButton("Mulai !");
tombol2
= new JButton("New Game");
tombol1.addActionListener(this);
tombol2.addActionListener(this);
try{
icon1
= new ImageIcon[3];
for
(int i = 0; i < 3; i++){
icon1[i]
= tampil_gambar.cek_gambar(this, "images/" + sistem.cek_item(i) +
".gif", "Icon for " + sistem.cek_item(i), 13000);
}
label6.setIcon(icon1[0]);
label7.setIcon(icon1[0]);
}
catch
(Exception xxx){
eror
= true;
}
setLayout(new
BorderLayout());
panel1
= new JPanel(new BorderLayout());
panel2
= new JPanel(new BorderLayout());
panel3
= new JPanel(new BorderLayout());
panel4
= new JPanel(new BorderLayout());
panel5
= new JPanel(new BorderLayout());
panel6
= new JPanel(new BorderLayout());
panel7
= new JPanel();
panel8
= new JPanel(new GridLayout(3,1,2,2));
panel9
= new JPanel();
panel10
= new JPanel();
panel11
= new JPanel();
add(panel1, BorderLayout.CENTER);
panel1.add(panel2,
BorderLayout.WEST);
panel2.add(panel3, BorderLayout.NORTH);
panel3.add(label1, BorderLayout.NORTH);
panel3.add(combo, BorderLayout.SOUTH);
panel2.add(label6, BorderLayout.SOUTH);
panel1.add(panel4, BorderLayout.EAST);
panel4.add(panel5, BorderLayout.NORTH);
panel5.add(label2, BorderLayout.NORTH);
panel5.add(txt1, BorderLayout.SOUTH);
panel4.add(label7, BorderLayout.SOUTH);
panel1.add(panel6, BorderLayout.SOUTH);
panel6.add(panel7, BorderLayout.NORTH);
panel7.add(tombol1);
panel7.add(tombol2);
panel7.add(label5);
panel6.add(panel8, BorderLayout.SOUTH);
panel8.add(panel9);
panel9.add(label3);
panel9.add(txt2);
panel8.add(panel10);
panel10.add(label4);
panel10.add(txt3);
panel8.add(panel11);
panel1.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
setBackground(warna1);
panel1.setBackground(warna1);
panel2.setBackground(warna1);
panel3.setBackground(warna1);
panel4.setBackground(warna1);
panel5.setBackground(warna1);
panel6.setBackground(warna1);
panel7.setBackground(warna1);
panel8.setBackground(warna1);
panel9.setBackground(warna1);
panel10.setBackground(warna1);
panel11.setBackground(warna1);
label1.setForeground(warna2);
label2.setForeground(warna2);
label3.setForeground(warna2);
label4.setForeground(warna2);
txt2.setForeground(warna2);
txt3.setForeground(warna2);
txt1.setForeground(warna2);
}
public
void new_game (){
combo.setSelectedIndex(0);
label5.setText("");
sistem.new_game();
}
public
void actionPerformed (ActionEvent e){
if
(e.getSource() == tombol2) {
new_game();
}
else{
label5.setText(sistem.bermain(combo.getSelectedIndex()));
}
txt1.setText(sistem.cekCPU_action());
txt2.setText(Integer.toString(sistem.cek_menang()));
txt3.setText(Integer.toString(sistem.cek_kalah()));
if
(!eror){
label7.setIcon(icon1[sistem.cekCPU_in_action()]);
}
}
public
void itemStateChanged (ItemEvent xxx){
if
(!eror){
label6.setIcon(icon1[combo.getSelectedIndex()]);
label7.setIcon(null);
}
}
public
static void main(String[] args){
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame tampil
= new JFrame("Latihan BAB III");
tampil.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tampil.setResizable(false);
JComponent
game = new Latihan3();
game.setOpaque(true);
game.setPreferredSize(new
Dimension(420,350));
tampil.setContentPane(game);
tampil.pack();
tampil.setVisible(true);
}
}
============================
objek_gambar.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Administrator
*/
import java.awt.*;
import java.io.*;
import javax.swing.ImageIcon;
public class objek_gambar{
public void objek_gambar (){}
public ImageIcon cek_gambar (Object x, String y, String z, int ukuran) throws Exception{
int xy = 0;
BufferedInputStream tipe = new BufferedInputStream(x.getClass().getResourceAsStream(y));
byte bufer[] = new byte[ukuran];
if (tipe == null){
throw new Exception("gambar tidak ada");
}
try{
xy = tipe.read(bufer);
tipe.close();
}
catch (IOException xxx){
throw new Exception("error");
}
return new ImageIcon(Toolkit.getDefaultToolkit().createImage(bufer), z);
}
}
Tidak ada komentar:
Posting Komentar