最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java基于swing實(shí)現(xiàn)的連連看代碼

 更新時(shí)間:2014年11月11日 15:30:30   投稿:shichen2014  
這篇文章主要介紹了java基于swing實(shí)現(xiàn)的連連看代碼,包含了游戲中涉及的事件處理與邏輯功能,需要的朋友可以參考下

本文實(shí)例講述了java基于swing實(shí)現(xiàn)連連看代碼。分享給大家供大家參考。

主要功能代碼如下:

復(fù)制代碼 代碼如下:

package llkan;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
 * 連連看游戲
 * @author Administrator
 *2014年10月17日
 */
public class MainGame implements ActionListener {
        JFrame mainFrame; // 主面板
        Container thisContainer;
        JPanel centerPanel, southPanel, northPanel; // 子面板
        JButton diamondsButton[][] = new JButton[6][5];// 游戲按鈕數(shù)組
        JButton exitButton, resetButton, newlyButton; // 退出,重列,重新開始按鈕
        JLabel fractionLable = new JLabel("0"); // 分?jǐn)?shù)標(biāo)簽
        JButton firstButton, secondButton; // 分別記錄兩次被選中的按鈕
        int grid[][] = new int[8][7];// 儲存游戲按鈕位置
        static boolean pressInformation = false; // 判斷是否有按鈕被選中
        int x0 = 0, y0 = 0, x = 0, y = 0, fristMsg = 0, secondMsg = 0, validateLV; // 游戲按鈕的位置坐標(biāo)
        int i, j, k, n;// 消除方法控制

        public void init() {
                mainFrame = new JFrame("連連看游戲");
                thisContainer = mainFrame.getContentPane();
                thisContainer.setLayout(new BorderLayout());
                centerPanel = new JPanel();
                southPanel = new JPanel();
                northPanel = new JPanel();
                thisContainer.add(centerPanel, "Center");
                thisContainer.add(southPanel, "South");
                thisContainer.add(northPanel, "North");
                centerPanel.setLayout(new GridLayout(6, 5));
                for (int cols = 0; cols < 6; cols++) {
                        for (int rows = 0; rows < 5; rows++) {
                                diamondsButton[cols][rows] = new JButton(
                                                String.valueOf(grid[cols + 1][rows + 1]));
                                diamondsButton[cols][rows].addActionListener(this);
                                centerPanel.add(diamondsButton[cols][rows]);
                        }
                }
                exitButton = new JButton("退出");
                exitButton.addActionListener(this);
                resetButton = new JButton("重列");
                resetButton.addActionListener(this);
                newlyButton = new JButton("再來一局");
                newlyButton.addActionListener(this);
                southPanel.add(exitButton);
                southPanel.add(resetButton);
                southPanel.add(newlyButton);
                fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable
                                .getText())));
                northPanel.add(fractionLable);
                mainFrame.setBounds(280, 100, 500, 450);
                mainFrame.setVisible(true);
        }

        public void randomBuild() {
                int randoms, cols, rows;
                for (int twins = 1; twins <= 15; twins++) {
                        randoms = (int) (Math.random() * 25 + 1);
                        for (int alike = 1; alike <= 2; alike++) {
                                cols = (int) (Math.random() * 6 + 1);
                                rows = (int) (Math.random() * 5 + 1);
                                while (grid[cols][rows] != 0) {
                                        cols = (int) (Math.random() * 6 + 1);
                                        rows = (int) (Math.random() * 5 + 1);
                                }
                                this.grid[cols][rows] = randoms;
                        }
                }
        }

        public void fraction() {
                fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable
                                .getText()) + 100));
        }

        public void reload() {
                int save[] = new int[30];
                int n = 0, cols, rows;
                int grid[][] = new int[8][7];
                for (int i = 0; i <= 6; i++) {
                        for (int j = 0; j <= 5; j++) {
                                if (this.grid[i][j] != 0) {
                                        save[n] = this.grid[i][j];
                                        n++;
                                }
                        }
                }
                n = n - 1;
                this.grid = grid;
                while (n >= 0) {
                        cols = (int) (Math.random() * 6 + 1);
                        rows = (int) (Math.random() * 5 + 1);
                        while (grid[cols][rows] != 0) {
                                cols = (int) (Math.random() * 6 + 1);
                                rows = (int) (Math.random() * 5 + 1);
                        }
                        this.grid[cols][rows] = save[n];
                        n--;
                }
                mainFrame.setVisible(false);
                pressInformation = false; // 這里一定要將按鈕點(diǎn)擊信息歸為初始
                init();
                for (int i = 0; i < 6; i++) {
                        for (int j = 0; j < 5; j++) {
                                if (grid[i + 1][j + 1] == 0)
                                        diamondsButton[i][j].setVisible(false);
                        }
                }
        }

        public void estimateEven(int placeX, int placeY, JButton bz) {
                if (pressInformation == false) {
                        x = placeX;
                        y = placeY;
                        secondMsg = grid[x][y];
                        secondButton = bz;
                        pressInformation = true;
                } else {
                        x0 = x;
                        y0 = y;
                        fristMsg = secondMsg;
                        firstButton = secondButton;
                        x = placeX;
                        y = placeY;
                        secondMsg = grid[x][y];
                        secondButton = bz;
                        if (fristMsg == secondMsg && secondButton != firstButton) {
                                xiao();
                        }
                }
        }

        public void xiao() { // 相同的情況下能不能消去。仔細(xì)分析,不一條條注釋
                if ((x0 == x && (y0 == y + 1 || y0 == y - 1))
                                || ((x0 == x + 1 || x0 == x - 1) && (y0 == y))) { // 判斷是否相鄰
                        remove();
                } else {
                        for (j = 0; j < 7; j++) {
                                if (grid[x0][j] == 0) { // 判斷第一個(gè)按鈕同行哪個(gè)按鈕為空
                                        if (y > j) { // 如果第二個(gè)按鈕的Y坐標(biāo)大于空按鈕的Y坐標(biāo)說明第一按鈕在第二按鈕左邊
                                                for (i = y - 1; i >= j; i--) { // 判斷第二按鈕左側(cè)直到第一按鈕中間有沒有按鈕
                                                        if (grid[x][i] != 0) {
                                                                k = 0;
                                                                break;
                                                        } else {
                                                                k = 1;
                                                        } // K=1說明通過了第一次驗(yàn)證
                                                }
                                                if (k == 1) {
                                                        linePassOne();
                                                }
                                        }
                                        if (y < j) { // 如果第二個(gè)按鈕的Y坐標(biāo)小于空按鈕的Y坐標(biāo)說明第一按鈕在第二按鈕右邊
                                                for (i = y + 1; i <= j; i++) { // 判斷第二按鈕左側(cè)直到第一按鈕中間有沒有按鈕
                                                        if (grid[x][i] != 0) {
                                                                k = 0;
                                                                break;
                                                        } else {
                                                                k = 1;
                                                        }
                                                }
                                                if (k == 1) {
                                                        linePassOne();
                                                }
                                        }
                                        if (y == j) {
                                                linePassOne();
                                        }
                                }
                                if (k == 2) {
                                        if (x0 == x) {
                                                remove();
                                        }
                                        if (x0 < x) {
                                                for (n = x0; n <= x - 1; n++) {
                                                        if (grid[n][j] != 0) {
                                                                k = 0;
                                                                break;
                                                        }
                                                        if (grid[n][j] == 0 && n == x - 1) {
                                                                remove();
                                                        }
                                                }
                                        }
                                        if (x0 > x) {
                                                for (n = x0; n >= x + 1; n--) {
                                                        if (grid[n][j] != 0) {
                                                                k = 0;
                                                                break;
                                                        }
                                                        if (grid[n][j] == 0 && n == x + 1) {
                                                                remove();
                                                        }
                                                }
                                        }
                                }
                        }
                        for (i = 0; i < 8; i++) { // 列
                                if (grid[i][y0] == 0) {
                                        if (x > i) {
                                                for (j = x - 1; j >= i; j--) {
                                                        if (grid[j][y] != 0) {
                                                                k = 0;
                                                                break;
                                                        } else {
                                                                k = 1;
                                                        }
                                                }
                                                if (k == 1) {
                                                        rowPassOne();
                                                }
                                        }
                                        if (x < i) {
                                                for (j = x + 1; j <= i; j++) {
                                                        if (grid[j][y] != 0) {
                                                                k = 0;
                                                                break;
                                                        } else {
                                                                k = 1;
                                                        }
                                                }
                                                if (k == 1) {
                                                        rowPassOne();
                                                }
                                        }
                                        if (x == i) {
                                                rowPassOne();
                                        }
                                }
                                if (k == 2) {
                                        if (y0 == y) {
                                                remove();
                                        }
                                        if (y0 < y) {
                                                for (n = y0; n <= y - 1; n++) {
                                                        if (grid[i][n] != 0) {
                                                                k = 0;
                                                                break;
                                                        }
                                                        if (grid[i][n] == 0 && n == y - 1) {
                                                                remove();
                                                        }
                                                }
                                        }
                                        if (y0 > y) {
                                                for (n = y0; n >= y + 1; n--) {
                                                        if (grid[i][n] != 0) {
                                                                k = 0;
                                                                break;
                                                        }
                                                        if (grid[i][n] == 0 && n == y + 1) {
                                                                remove();
                                                        }
                                                }
                                        }
                                }
                        }
                }
        }

        public void linePassOne() {
                if (y0 > j) { // 第一按鈕同行空按鈕在左邊
                        for (i = y0 - 1; i >= j; i--) { // 判斷第一按鈕同左側(cè)空按鈕之間有沒按鈕
                                if (grid[x0][i] != 0) {
                                        k = 0;
                                        break;
                                } else {
                                        k = 2;
                                } // K=2說明通過了第二次驗(yàn)證
                        }
                }
                if (y0 < j) { // 第一按鈕同行空按鈕在與第二按鈕之間
                        for (i = y0 + 1; i <= j; i++) {
                                if (grid[x0][i] != 0) {
                                        k = 0;
                                        break;
                                } else {
                                        k = 2;
                                }
                        }
                }
        }

        public void rowPassOne() {
                if (x0 > i) {
                        for (j = x0 - 1; j >= i; j--) {
                                if (grid[j][y0] != 0) {
                                        k = 0;
                                        break;
                                } else {
                                        k = 2;
                                }
                        }
                }
                if (x0 < i) {
                        for (j = x0 + 1; j <= i; j++) {
                                if (grid[j][y0] != 0) {
                                        k = 0;
                                        break;
                                } else {
                                        k = 2;
                                }
                        }
                }
        }

        public void remove() {
                firstButton.setVisible(false);
                secondButton.setVisible(false);
                fraction();
                pressInformation = false;
                k = 0;
                grid[x0][y0] = 0;
                grid[x][y] = 0;
        }

        public void actionPerformed(ActionEvent e) {
                if (e.getSource() == newlyButton) {
                        int grid[][] = new int[8][7];
                        this.grid = grid;
                        randomBuild();
                        mainFrame.setVisible(false);
                        pressInformation = false;
                        init();
                }
                if (e.getSource() == exitButton)
                        System.exit(0);
                if (e.getSource() == resetButton)
                        reload();
                for (int cols = 0; cols < 6; cols++) {
                        for (int rows = 0; rows < 5; rows++) {
                                if (e.getSource() == diamondsButton[cols][rows])
                                        estimateEven(cols + 1, rows + 1, diamondsButton[cols][rows]);
                        }
                }
        }

        public static void main(String[] args) {
                MainGame mg = new MainGame();
                mg.randomBuild();
                mg.init();
        }
}

希望本文所述對大家的Java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Jenkins遠(yuǎn)程部署war包過程圖解

    Jenkins遠(yuǎn)程部署war包過程圖解

    這篇文章主要介紹了Jenkins遠(yuǎn)程部署war包過程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • JAVA及相關(guān)字符集編碼問題研究分享

    JAVA及相關(guān)字符集編碼問題研究分享

    對于JAVA學(xué)習(xí),或多或少都會遇到這樣的問題:編碼基本知識,java,系統(tǒng)軟件,url,工具軟件等
    2014-10-10
  • 使用maven整合Spring+SpringMVC+Mybatis框架詳細(xì)步驟(圖文)

    使用maven整合Spring+SpringMVC+Mybatis框架詳細(xì)步驟(圖文)

    這篇文章主要介紹了使用maven整合Spring+SpringMVC+Mybatis框架詳細(xì)步驟(圖文),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-05-05
  • Java使用poi導(dǎo)出ppt文件的實(shí)現(xiàn)代碼

    Java使用poi導(dǎo)出ppt文件的實(shí)現(xiàn)代碼

    Apache POI 是用Java編寫的免費(fèi)開源的跨平臺的 Java API,Apache POI提供API給Java對Microsoft Office格式檔案讀和寫的功能。本文給大家介紹Java使用poi導(dǎo)出ppt文件的實(shí)現(xiàn)代碼,需要的朋友參考下吧
    2021-06-06
  • springboot定時(shí)任務(wù)@Scheduled執(zhí)行多次的問題

    springboot定時(shí)任務(wù)@Scheduled執(zhí)行多次的問題

    這篇文章主要介紹了springboot定時(shí)任務(wù)@Scheduled執(zhí)行多次問題的解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • java Struts2框架下實(shí)現(xiàn)文件上傳功能

    java Struts2框架下實(shí)現(xiàn)文件上傳功能

    這篇文章主要為大家詳細(xì)介紹了java Struts2框架下實(shí)現(xiàn)文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • java使用nio2拷貝文件的示例

    java使用nio2拷貝文件的示例

    這篇文章主要介紹了java使用nio2拷貝文件的示例,需要的朋友可以參考下
    2014-04-04
  • Java中的MapStruct實(shí)現(xiàn)詳解

    Java中的MapStruct實(shí)現(xiàn)詳解

    這篇文章主要介紹了Java中的MapStruct實(shí)現(xiàn)詳解,MapStruct 是一個(gè)代碼生成器,它基于約定優(yōu)先于配置的方法大大簡化了 JavaBean 類型之間映射的實(shí)現(xiàn),生成的映射代碼使用普通方法調(diào)用,需要的朋友可以參考下
    2023-11-11
  • Java 如何同時(shí)返回多個(gè)不同類型

    Java 如何同時(shí)返回多個(gè)不同類型

    這篇文章主要介紹了Java 同時(shí)返回多個(gè)不同類型的方法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java常用類之比較器的使用詳解

    Java常用類之比較器的使用詳解

    這篇文章主要為大家詳細(xì)介紹了Java中比較器的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2022-11-11

最新評論

梓潼县| 司法| 内丘县| 资阳市| 开阳县| 边坝县| 普洱| 曲水县| 观塘区| 中西区| 青铜峡市| 四会市| 厦门市| 徐州市| 兴隆县| 彰化县| 正宁县| 沂源县| 遂平县| 财经| 河源市| 四平市| 红原县| 调兵山市| 镇沅| 雷州市| 兴国县| 山阴县| 得荣县| 娱乐| 潮安县| 元氏县| 康定县| 彩票| 泰宁县| 八宿县| 栾城县| 溧阳市| 平江县| 综艺| 兰西县|