java消消樂


package game消消樂;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.*;

public class Myjframe extends JFrame {
    private JPanel panel1 = new JPanel();
    private JButton buttona = new JButton("開始");
    private JLabel label1 = new JLabel("分數");
    private JTextField textarea1 = new JTextField(10);
    private JLabel buttonc = new JLabel("時間");
    private JProgressBar jindu = new JProgressBar();
    private Timer timer;   //定時器
    private JButton buttonb = new JButton("退出");
    private JPanel panel2 = new JPanel();
    private JButton button[][] = new JButton[8][8];
    private int animal[][] = new int[8][8];
    private ImageIcon Iocn[] = new ImageIcon[7];
    // 標記是否有三個以上的連接
    private final int EMPTY = 0;// 無為0,有為1
    // 隨機數
    private Random rand = new Random();// new 一個監聽器
    // 標記是否有三個以上的連接
    private boolean isThreeLinked;
    // 標記單擊次數
    private boolean isDoubleClicked;    
    private int x1;// 記錄第一次被點擊按鈕的X坐標    
    private int y1;// 記錄第一次被點擊按鈕的Y坐標
    private int grade = 0; //得分

    Myjframe() {
        // 加載圖片
        Iocn[0] = new ImageIcon("image//cat.png");
        Iocn[1] = new ImageIcon("image//cattle.png");
        Iocn[2] = new ImageIcon("image//chicken.png");
        Iocn[3] = new ImageIcon("image//fox.png");
        Iocn[4] = new ImageIcon("image//monkey.png");
        Iocn[5] = new ImageIcon("image//panda.png");
        Iocn[6] = new ImageIcon("image//frog.png");
        panel1.setLayout(new FlowLayout());
        panel1.add(buttona);
        panel1.add(label1);
        panel1.add(textarea1);
        textarea1.setEditable(false);
        textarea1.setText(Integer.toString(grade));//
        panel1.add(buttonc);
        jindu.setMaximum(100);
        panel1.add(jindu);
        panel1.add(buttonb);
        this.setLayout(new BorderLayout());
        this.add(panel1, BorderLayout.NORTH);        
        panel2.setLayout(new GridLayout(8, 8, 1, 1));        
        MyListener mylisten = new MyListener();
        int m;
        // 初始化動物數組
        for (int i = 0; i < 8; i++)
            for (int j = 0; j < 8; j++) {
                m = (int) (Math.random() * 7);
                button[i][j] = new JButton(Iocn[m]);
                animal[i][j] = m;
                button[i][j].setSize(50, 50);
                //button[i][j].setFont(new Font(null, Font.BOLD, 25));
                button[i][j].addActionListener(mylisten);                
                button[i][j].setEnabled(false); //圖形按鈕無效
                panel2.add(button[i][j]);
            }
        this.add(panel2, BorderLayout.CENTER);        
        buttona.addActionListener(mylisten);
        buttonb.addActionListener(mylisten);
    }

    // 初始化動物數組
    private void initAnimalMatrix() {
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                // 隨機選取動物
                animal[i][j] = rand.nextInt(7);
            }
        }
    }
    //消除三個以上連接的點
    private void removeLinked(int x, int y) {
        if(animal[x][y] == EMPTY)return;
        int n=0;
        int tmp;
        int linked = 1;
        if (x + 1 < 8) {
            tmp = x + 1;
            while (tmp < 8 && animal[x][y] == animal[tmp][y]) {
                linked++;
                tmp++;
            }
        }
        if (x - 1 >= 0) {
            tmp = x - 1;
            while (tmp >= 0 && animal[x][y] == animal[tmp][y]) {
                linked++;
                tmp--;
            }
        }        
        if (linked >= 3) {
            n=n+linked; 
            tmp = x + 1;
            while (tmp < 8 && animal[tmp][y] == animal[x][y]) {                
                animal[tmp][y] = EMPTY;
                tmp++;
            }
            tmp = x - 1;
            while (tmp >= 0 && animal[tmp][y] == animal[x][y]) {
                animal[tmp][y] = EMPTY;
                tmp--;
            }
            // 當前交換過來的點
            animal[x][y] = EMPTY;
        }
        tmp = 0;
        linked = 1;
        if (y + 1 < 8) {
            tmp = y + 1;
            while (tmp < 8 && animal[x][y] == animal[x][tmp]) {
                linked++;
                tmp++;
            }
        }
        if (y - 1 >= 0) {
            tmp = y - 1;
            while (tmp >= 0 && animal[x][y] == animal[x][tmp]) {
                linked++;
                tmp--;
            }
        }
        if (linked >= 3) {
            n=n+linked; 
            tmp = y + 1;
            while (tmp < 8 && animal[x][y] == animal[x][tmp]) {
                animal[x][tmp] = EMPTY;
                tmp++;
            }
            tmp = y - 1;
            while (tmp >= 0 && animal[x][y] == animal[x][tmp]) {
                animal[x][tmp] = EMPTY;
                tmp--;
            }
            // 當前交換過來的點
            animal[x][y] = EMPTY;
        }        
        grade+=n*10;
        textarea1.setText(Integer.toString(grade));
        System.out.println(grade + " ======"+x+" "+y);
    }
    private boolean globalSearch(int flag) {
        if (1 == flag) {
            for (int i = 0; i < 8; i++) {
                for (int j = 0; j < 8; j++) {
                    if (isThreeLinked(i, j)) {                        
                        return true;
                    }
                }
            }
        } else if (2 == flag) {
            
            for (int i = 0; i < 8; i++) {
                for (int j = 0; j < 8; j++) {
                    // 刪除三個相連的點
                    removeLinked(i, j);

                }
            }
        }
        return false;
    }

    // 是否有三個以上連接的點
    private boolean isThreeLinked(int x, int y) {
        int tmp;
        int linked = 1;
        if (x + 1 < 8) {
            tmp = x + 1;
            while (tmp < 8 && animal[x][y] == animal[tmp][y]) {
                linked++;
                tmp++;
            }
        }
        if (x - 1 >= 0) {
            tmp = x - 1;
            while (tmp >= 0 && animal[x][y] == animal[tmp][y]) {
                linked++;
                tmp--;
            }
        }
        if (linked >= 3) {

            return true;
        }
        linked = 1;
        if (y + 1 < 8) {
            tmp = y + 1;
            while (tmp < 8 && animal[x][y] == animal[x][tmp]) {
                linked++;
                tmp++;
            }
        }
        if (y - 1 >= 0) {
            tmp = y - 1;
            while (tmp >= 0 && animal[x][y] == animal[x][tmp]) {
                linked++;
                tmp--;
            }
        }
        if (linked >= 3) {

            return true;
        }
        return false;
    }

    // 更新動物矩陣
    private void updateAnimal() {
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (animal[i][j] == EMPTY) {
                    // 將矩陣中為空值的點再進行隨機賦值
                    animal[i][j] = rand.nextInt(7);
                }
            }
        }
    }

    // 動物下降
    private void downAnimal() {
        int tmp;
        for (int j = 8 - 1; j >= 0; j--) {
            for (int i = 0; i < 8; i++) {
                if (animal[j][i] == EMPTY) {
                    for (int k = j - 1; k >= 0; k--) {
                        if (animal[k][i] != EMPTY) {
                            tmp = animal[k][i];
                            animal[k][i] = animal[j][i];
                            animal[j][i] = tmp;
                            break;
                        }
                    }
                }
            }
        }
    }

    private void print() { //重新顯示按鈕圖形
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                button[i][j].setIcon(Iocn[animal[i][j]]);
            }
        }
    }

    private void swapAnimal(int x, int y) {        
        if ((x >= 0 && x <= 8) && (y >= 0 && y <= 8)) {
            // 被點擊的動物的坐標
            int x2;
            int y2;
            if (!isDoubleClicked) { //第一次單擊
                isDoubleClicked = true;
                x1 = x;
                y1 = y;
                System.out.println("被單擊的點的X坐標 = " + x1);
                System.out.println("被單擊的點的Y坐標 = " + y1);
            } else{               //第2次單擊
                x2 = x;
                y2 = y;
                isDoubleClicked = false;
                //兩點的坐標絕對值等於1時視為相鄰的兩點
                if (1 == Math.abs(x2 - x1) && y2 == y1
                        || 1 == Math.abs(y2 - y1)&& x2 == x1) {
                    //-------------交換矩陣中相鄰的兩點的值--------------
                    int tmp;
                    tmp = animal[y2][x2];
                    animal[y2][x2] = animal[y1][x1];
                    animal[y1][x1] = tmp;
                    //-----------------------------------------------------

                    if (isThreeLinked(y2, x2)||isThreeLinked(y1, x1)) {
                        System.out.println("消除點");
                        if(isThreeLinked(y2, x2))
                            removeLinked(y2, x2);
                        if(isThreeLinked(y1, x1))
                            removeLinked(y1, x1);
                        downAnimal();//被削去處上方的動物下降
                        //該列上方重新隨機產生新的動物,更新動物矩陣
                        updateAnimal();
                        print();
                        
                        //全局掃描判斷是否有新的三個以上連接點,有則刪除
                        while (globalSearch(1)) {
                            //全局掃描消除三個以上相連的點
                            globalSearch(2);
                            //動物再次下落
                            downAnimal();
                            //再次更新動物矩陣
                            updateAnimal();
                            print();
                            
                        }  
                    }
                    else {//沒有三個以上相同的點,交換回來  
                        System.out.println("交換回來");
                        tmp = animal[y1][x1];
                        animal[y1][x1] = animal[y2][x2];
                        animal[y2][x2] = tmp;
                        print();                         
                    }
                }
            }
        }
    }
    public void init() {
        do {
            System.out.println("重新初始化");
            initAnimalMatrix();
        } while (globalSearch(1));
        print();
        pack();
        setResizable(false);
        setVisible(true);
    }


    public static void main(String[] args) {
        Myjframe frame = new Myjframe();
        frame.setTitle("對對碰游戲2012-6-28");
        frame.setSize(500, 500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.init();
    }
    // 監聽器
    class MyListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == buttona) {
                buttona.setEnabled(false);
                jindu.setStringPainted(true);
                jindu.setMaximum(100);
                jindu.setMinimum(0);
                timer = new Timer(800, new TimeListener());
                timer.start();
                grade = 0;
                textarea1.setText(Integer.toString(grade));
                for (int i = 0; i < 8; i++)
                    for (int j = 0; j < 8; j++) {
                        button[i][j].setEnabled(true); //圖形按鈕有效
                    }
            }
            if (e.getSource() == buttonb) {
                System.out.println("end");
                System.exit(1);
            }
            for (int i = 0; i < 8; i++) {
                for (int j = 0; j < 8; j++) {
                    if (e.getSource() == button[i][j]) {
                        //System.out.println("第" + i + " " + j + "鍵");
                        swapAnimal(j, i);
                    }
                }
            }
        }
    }

    class TimeListener implements ActionListener {
        int times = 0;
        public void actionPerformed(ActionEvent e) {
            jindu.setValue(times++);
            if (times > 100) {
                timer.stop();
                //定時器結束
                for (int i = 0; i < 8; i++)
                    for (int j = 0; j < 8; j++) {
                        button[i][j].setEnabled(false); //圖形按鈕無效
                    }
                buttona.setEnabled(true);
            }
        }

    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM