Netbeans 6.5 çıktı JSF Richfaces Hibernate Spring Örnek Uygulaması
Nov 24

Aşağıda basit bir hafıza oyunu bulacaksınız. Oyunun amacı gizlenmiş birbirinin aynı olan ikili simgeleri bulmak.

Oyunu çalıştırabilmek için Java 6 kullanmanız gerekecek.

SwingWorker sınıfını kullandım ve bu sınıf Java 6 ile gelen bir özellik. Eğer Java 6 sürümü kurulu değil yine de kodu çalıştırmak istiyorsanız, resetButton methodunu aşağıdaki gibi değiştirin.

    private void resetButton(final JButton fButton, final JButton sButton) {
        fButton.setForeground(FAILURE);
        sButton.setForeground(FAILURE);
 
        try {
            // 2 sn bekle
            Thread.sleep(2000);
        } catch(Exception exc) {}
 
        fButton.setText(DEFAULT_STR);
        fButton.setForeground(DEFAULT);
        sButton.setText(DEFAULT_STR);
        sButton.setForeground(DEFAULT);
    }

Aşağıdaki kodu indirin.

Download: Game  Game (4.3 KiB, 261 hits)

javac Game.java

komutu ile derleyin

java Game

komutu ile konsoldan çalıştırın.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
 
 
public class GUI extends JFrame implements ActionListener {
    private int n = 0;
    private JButton buttons[][];
    private String[][] hiddens;
    private JButton firstButton, secondButton;
    private String SPLITTER = ":";
    private String DEFAULT_STR = "X";
    private Color DEFAULT = Color.BLACK;
    private Color SUCCESS = Color.BLUE;
    private Color FAILURE = Color.ORANGE;
 
    public GUI() {
        try {
            do {
                String nStr = JOptionPane.showInputDialog("Çift sayı giriniz:");
                n = Integer.parseInt(nStr);
            } while(n%2!=0);
        } catch(NumberFormatException exc) {
            JOptionPane.showMessageDialog(null, "Lütfen geçerli sayı giriniz!");
            System.exit(1);
        }
 
        fillHiddens(n);
        init();
 
        setSize(600, 600);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
 
    private void init() {
        Container c = this.getContentPane();
        c.setLayout(new GridLayout(n, n));
 
        buttons = new JButton[n][n];
        for(int i=0; i<n; i++) {
            for(int j=0; j<n; j++) {
                buttons[i][j] = new JButton(DEFAULT_STR);
                buttons[i][j].setForeground(DEFAULT);
                buttons[i][j].setFont(new Font("", Font.BOLD, 18));
                buttons[i][j].addActionListener(this);
                buttons[i][j].setActionCommand(i + SPLITTER + j);
                c.add(buttons[i][j]);
            }
        }
    }
 
    private void fillHiddens(int h) {
        java.util.List<String> list = new ArrayList<String>();
 
        for(int i=0; i<h; i++) {
            for(int j=0; j<h/2; j++) {
                String randomStr = generateRandomString();
                while(list.contains(randomStr) || randomStr.equals(DEFAULT_STR)) {
                    randomStr = generateRandomString();
                }
                list.add(randomStr);                
            }
        }
 
        hiddens = new String[h][h];
        for(String str : list) {
            int i, j;
            do {
                i = (int)(Math.random()*h);
                j = (int)(Math.random()*h);
            } while(hiddens[i][j] != null);
            hiddens[i][j] = str;
 
            do {
                i = (int)(Math.random()*h);
                j = (int)(Math.random()*h);
            } while(hiddens[i][j] != null);
            hiddens[i][j] = str;
        }
    }
 
    private String generateRandomString() {
        return ((char)(30 + (int)(Math.random()*60))) + "";
    }
 
    public void actionPerformed(ActionEvent e) {
        if(!((JButton)e.getSource()).getText().equals(DEFAULT_STR)) {
            return;
        }
        String command = e.getActionCommand();
        String[] indexes = command.split(SPLITTER);
 
        int i = Integer.parseInt(indexes[0]);
        int j = Integer.parseInt(indexes[1]);
 
        if(firstButton==null) {
            firstButton = buttons[i][j];
            firstButton.setText(hiddens[i][j]);
            firstButton.setForeground(SUCCESS);
        }
        else {
            secondButton = buttons[i][j];
            secondButton.setText(hiddens[i][j]);
            secondButton.setForeground(SUCCESS);
 
            if(secondButton.getText().equals(firstButton.getText())) {
                // Puan hesaplaması burada yapılacak    
            }
            else {
                resetButton(firstButton, secondButton);
            }
 
            firstButton = null;
            secondButton = null;
        }
    }
 
    private void resetButton(final JButton fButton, final JButton sButton) {
        SwingWorker worker = new SwingWorker() {
            protected Object doInBackground() throws Exception {
                fButton.setForeground(FAILURE);
                sButton.setForeground(FAILURE);
 
                // 2 sn bekle
                Thread.sleep(2000);
 
                fButton.setText(DEFAULT_STR);
                fButton.setForeground(DEFAULT);
                sButton.setText(DEFAULT_STR);
                sButton.setForeground(DEFAULT);
                return null;
            }
        };
        worker.execute();
    }
 
    public static void main(String[] args) {
        new GUI();
    }
}

yazan Erol KOCAMAN \\ tags:

Cevapla