欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

用java實(shí)現(xiàn)掃雷游戲

 更新時(shí)間:2022年06月06日 10:37:41   作者:城城正開(kāi)心  
這篇文章主要為大家詳細(xì)介紹了用java實(shí)現(xiàn)掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

用java做出簡(jiǎn)單一個(gè)掃雷游戲,供大家參考,具體內(nèi)容如下

1.創(chuàng)造窗口

//創(chuàng)建掃雷窗口界面 ?
?? ?public Saolei() {
?? ??? ?
?? ??? ??? ?frame.setSize(600,700);
?? ??? ??? ?frame.setResizable(false);//設(shè)置窗口尺寸不能變化
?? ??? ??? ?frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
?? ??? ??? ?frame.setLayout(new BorderLayout());//分塊
?? ??? ??? ?setHeader();//設(shè)置界面初始化
?? ??? ??? ?addlei(); ? //添加雷
?? ??? ??? ?setButtons();//添加按鈕
?? ??? ??? ?timer.start(); ? //啟動(dòng)時(shí)鐘;
?? ??? ??? ?frame.setVisible(true);
?? ??? ?}

2.定義數(shù)據(jù)結(jié)構(gòu)以及初始化

//數(shù)據(jù)結(jié)構(gòu)start
?? ?int ROW = 20;
?? ?int COL = 20;
?? ?int [][] data = new int[ROW][COL];
?? ?JButton[][] btns = new JButton[ROW][COL];
?? ?int LEICOUNT = 30; ? //雷個(gè)數(shù)
?? ?int LEICODE = -1;
?? ?int unopened = ROW*COL;
?? ?int opened = 0;
?? ?int timeSecond =0;
?? ?//三個(gè) jlabel 狀態(tài)欄 已開(kāi)未開(kāi),用時(shí)
?? ?JLabel label1= new JLabel("待開(kāi):"+unopened);
?? ?JLabel label2= new JLabel("已開(kāi):"+opened);
?? ?JLabel label3= new JLabel("用時(shí):"+timeSecond+"s");
?? ?Timer timer = new Timer(1000, this); //定義時(shí)間為一秒 ?

3.窗體按鈕

private void setButtons() {
?? ? ?Container con = new Container();//container容器
?? ? ?con.setLayout(new GridLayout(ROW,COL));//創(chuàng)建方格
?? ? ?
?? ? ?for(int i=0;i<ROW;i++) {
?? ??? ? ?for(int j=0;j<COL;j++) {
?? ??? ??? ? ?JButton btn = new JButton();
?? ??? ??? ? ?btn.setOpaque(true);
?? ??? ??? ? ?btn.setBackground(new Color(200,183,113)); ?//設(shè)置掃雷背景顏色
?? ??? ??? ? ?btn.addActionListener(this); ?//Btn添加按鈕監(jiān)聽(tīng)事件
?? ??? ??? ? ?btn.setMargin(new Insets(0,0,0,0)); ?//button數(shù)字顯示不出來(lái),加上該語(yǔ)句即可顯示
?? ??? ??? ? ?con.add(btn);
?? ??? ??? ? ?btns[i][j] = btn;
?? ??? ? ?}
?? ? ?}
?? ? ?frame.add(con,BorderLayout.CENTER);//中間位置
?? ? ?
?? ?}

4.埋雷

private void addlei() {
?? ??? ?Random rand = new Random();
?? ??? ?for(int i=0;i<LEICOUNT;) {
?? ??? ??? ?int r = rand.nextInt(ROW);
?? ??? ??? ?int c= rand.nextInt(COL);
?? ??? ??? ?if(data[r][c]!=LEICODE) {
?? ??? ??? ??? ?data[r][c] = LEICODE;
?? ??? ??? ??? ?i++;
//?? ??? ??? ??? ?System.out.println(r+" ?"+c+" "+data[r][c]);
?? ??? ??? ?}
?? ??? ?}

5.計(jì)算周圍雷的數(shù)量

//計(jì)算周邊雷的數(shù)量
?? ??? ? ?for(int i=0;i<ROW;i++) {
?? ??? ??? ? ?for(int j=0;j<COL;j++) {
?? ??? ??? ??? ? ?if(data[i][j]!=LEICODE) {
?? ??? ??? ??? ??? ? ? int ?c=0;
?? ??? ??? ??? ??? ? ? if(i>0&&j>0&&data[i-1][j-1]==LEICODE) c++;
?? ??? ??? ??? ??? ? ? if(i>0&&data[i-1][j]==LEICODE) c++;
?? ??? ??? ??? ??? ? ? if(i>0&&j<19&&data[i-1][j+1]==LEICODE) c++;
?? ??? ??? ??? ??? ? ? if(j>0&&data[i][j-1]==LEICODE) c++;
?? ??? ??? ??? ??? ? ? if(j<19&&data[i][j+1]==LEICODE) c++;
?? ??? ??? ??? ??? ? ? if(i<19&&j>0&&data[i+1][j-1]==LEICODE) c++;
?? ??? ??? ??? ??? ? ? if(i<19&&data[i+1][j]==LEICODE) c++;
?? ??? ??? ??? ??? ? ? if(i<19&&j<19&&data[i+1][j+1]==LEICODE) c++;
?? ??? ??? ??? ??? ? ? data[i][j]=c;
?? ??? ??? ??? ? ?}?? ??
?? ??? ??? ? ?}
?? ??? ? ?}

6.Banner設(shè)置

//設(shè)置開(kāi)頭顯示
?? ?private void setHeader() {
?? ??? ?//設(shè)置畫(huà)布 Jpanel
?? ??? ?JPanel panel = new JPanel(new GridBagLayout());
?? ??? ?GridBagConstraints c1 = new GridBagConstraints(0,0,3,1,1.0,1.0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0);
?? ??? ?panel.add(bannerBtn,c1);
?? ??? ?
?? ??? ?bannerBtn.addActionListener(this);
?? ??? ?label1.setOpaque(true); ? ?//設(shè)置不透明,背景色,
?? ??? ?label1.setBackground(Color.white); ??
?? ??? ?label1.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
?? ??? ?
?? ??? ?label2.setOpaque(true);
?? ??? ?label2.setBackground(Color.white);
?? ??? ?label2.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
?? ??? ?
?? ??? ?label3.setOpaque(true);
?? ??? ?label3.setBackground(Color.white);
?? ??? ?label3.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
?? ??? ?
?? ??? ?bannerBtn.setOpaque(true);
?? ??? ?bannerBtn.setBackground(Color.white);
?? ??? ?bannerBtn.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
?? ??? ?
?? ??? ?GridBagConstraints c2 = new GridBagConstraints(0,1,1,1,1.0,1.0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0);
?? ??? ?panel.add(label1,c2);
?? ??? ?GridBagConstraints c3 = new GridBagConstraints(1,1,1,1,1.0,1.0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0);
?? ??? ?panel.add(label2,c3);
?? ??? ?GridBagConstraints c4 = new GridBagConstraints(2,1,1,1,1.0,1.0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0);
?? ??? ?panel.add(label3,c4);
?? ??? ?
?? ??? ?frame.add(panel,BorderLayout.NORTH);
?? ??? ?
?? ?}

7.游戲勝利還是失敗

//判斷勝利?。?!
private void checkWin() {
?? ??? ?
?? ??? ? int count=0;
?? ??? ??? ?for(int i=0;i<ROW;i++) {
?? ??? ??? ??? ?for(int j=0;j<COL;j++) {
?? ??? ??? ??? ??? ?if(btns[i][j].isEnabled()) {
?? ??? ??? ??? ??? ??? ?count++;
?? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?unopened = count;
?? ??? ??? ?opened = ?ROW*COL-count;
?? ??? ??? ?
?? ??? ??? ?label1.setText("待開(kāi):"+ unopened);
?? ??? ??? ?label2.setText("已開(kāi):"+ opened);
?? ??? ??? ?if(count==LEICOUNT) {
?? ??? ??? ??? ?timer.stop();?
?? ??? ??? ??? ?bannerBtn.setText("游戲勝利?。?!");
?? ??? ??? ??? ?for(int i=0;i<ROW;i++) {
?? ??? ??? ??? ??? ?for(int j=0;j<COL;j++) {
?? ??? ??? ??? ??? ??? ?if(btns[i][j].isEnabled()) {
?? ??? ??? ??? ??? ??? ??? ? ?btns[i][j].setBackground(new Color(100,183,0));
? ? ? ? ? ? ? ? ? ?
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ??? ?bannerBtn.setText("Banner:restart!");
?? ??? ??? ??? ?JOptionPane.showMessageDialog(frame, "恭喜你!游戲勝利啦!\n 可以點(diǎn)擊上面的Banner重新開(kāi)始!","游戲結(jié)束!",JOptionPane.PLAIN_MESSAGE);
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ?
?? ?}

?? ?//踩雷成功,游戲結(jié)束!
?? ?private void lose() {
?? ??? ?timer.stop();
?? ??? ? bannerBtn.setText("很遺憾,踩雷成功,游戲結(jié)束?。?!");
?? ??? ?for(int i=0;i<ROW;i++) {
?? ??? ??? ?for(int j=0;j<COL;j++) {
?? ??? ??? ??? ?if(btns[i][j].isEnabled()) {
?? ??? ??? ??? ??? ?JButton btn = btns[i][j];
?? ??? ??? ??? ??? ?if(data[i][j]==LEICODE) {
//?? ??? ??? ??? ??? ?btns[i][j].setEnabled(false);?? ?btns[i][j].setIcon(bombIcon); btns[i][j].setDisabledIcon(bombIcon);
?? ??? ??? ??? ? ??
?? ??? ??? ??? ??? ??? ?btn.setBackground(Color.red);?? ? //置為紅色
?? ??? ??? ??? ??? ??? ?btn.setText(data[i][j]+"");
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?else {
?? ??? ??? ??? ??? ??? ?btn.setIcon(null);
?? ??? ??? ??? ??? ??? ?btn.setEnabled(false); ? //btn已經(jīng)打開(kāi)不可點(diǎn)擊
?? ??? ??? ??? ??? ??? ?btn.setOpaque(true);
?? ??? ?
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?bannerBtn.setText("Banner:restart!");
?? ??? ?JOptionPane.showMessageDialog(frame, "很遺憾,暴雷成功,游戲結(jié)束?。?!\n 可以點(diǎn)擊上面的Banner重新開(kāi)始!","游戲結(jié)束!",JOptionPane.PLAIN_MESSAGE);
?? ??? ?
?? ?}

8.空白級(jí)聯(lián)打開(kāi)(實(shí)現(xiàn)點(diǎn)擊一個(gè)即可打開(kāi)多個(gè))

private void openCell(int i,int j,int Blankcount ){
?? ??? ?JButton btn=btns[i][j];
?? ??? ?if(!btn.isEnabled()) return ;
?? ??? ?if(Blankcount==10) ? //部分打開(kāi),設(shè)置數(shù)字限制
?? ??? ??? ?return;
?? ??? ?btn.setIcon(null);
?? ??? ?btn.setEnabled(false);
?? ??? ?btn.setOpaque(true);
?? ??? ?Blankcount++;
?? ??? ?btn.setBackground(Color.GREEN);
?? ??? ?btn.setText(data[i][j]+"");
?? ??? ?if(data[i][j]==0) {
?? ??? ??? ? if(i>0&&j>0&&data[i-1][j-1]==0) openCell(i-1,j-1,Blankcount);
?? ??? ??? ? ? if(i>0&&data[i-1][j]==0) openCell(i-1,j,Blankcount);
?? ??? ??? ? ? if(i>0&&j<19&&data[i-1][j+1]==0) openCell(i-1,j+1,Blankcount);
?? ??? ??? ? ? if(j>0&&data[i][j-1]==0) openCell(i,j-1,Blankcount);
?? ??? ??? ? ? if(j<19&&data[i][j+1]==0) openCell(i,j+1,Blankcount);
?? ??? ??? ? ? if(i<19&&j>0&&data[i+1][j-1]==0) openCell(i+1,j-1,Blankcount);
?? ??? ??? ? ? if(i<19&&data[i+1][j]==0) openCell(i+1,j,Blankcount);
?? ??? ??? ? ? if(i<19&&j<19&&data[i+1][j+1]==0) openCell(i+1,j+1,Blankcount);
?? ??? ?}
?? ??? ?
?? ?}

9.最終游戲界面

總結(jié)

游戲界面顏色設(shè)置的有點(diǎn)丑,代碼是跟著某站上老師做的,感謝老師!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot中使用spring-retry 解決失敗重試調(diào)用

    SpringBoot中使用spring-retry 解決失敗重試調(diào)用

    本文主要介紹了SpringBoot中使用spring-retry 解決失敗重試調(diào)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Java8 Stream對(duì)兩個(gè) List 遍歷匹配數(shù)據(jù)的優(yōu)化處理操作

    Java8 Stream對(duì)兩個(gè) List 遍歷匹配數(shù)據(jù)的優(yōu)化處理操作

    這篇文章主要介紹了Java8 Stream對(duì)兩個(gè) List 遍歷匹配數(shù)據(jù)的優(yōu)化處理操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-08-08
  • MapStruct實(shí)體間轉(zhuǎn)換的簡(jiǎn)單用法

    MapStruct實(shí)體間轉(zhuǎn)換的簡(jiǎn)單用法

    今天小編就為大家分享一篇關(guān)于MapStruct實(shí)體間轉(zhuǎn)換的簡(jiǎn)單用法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-03-03
  • 使用log4j2打印mybatis的sql執(zhí)行日志方式

    使用log4j2打印mybatis的sql執(zhí)行日志方式

    這篇文章主要介紹了使用log4j2打印mybatis的sql執(zhí)行日志方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Java方法重載和方法重寫(xiě)的區(qū)別到底在哪?

    Java方法重載和方法重寫(xiě)的區(qū)別到底在哪?

    今天給大家?guī)?lái)的是關(guān)于Java的相關(guān)知識(shí),文章圍繞著Java方法重載和方法重寫(xiě)的區(qū)別到底在哪展開(kāi),文中有非常詳細(xì)的解釋,需要的朋友可以參考下
    2021-06-06
  • 基于SpringBoot和Vue3的博客平臺(tái)文章詳情與評(píng)論功能實(shí)現(xiàn)

    基于SpringBoot和Vue3的博客平臺(tái)文章詳情與評(píng)論功能實(shí)現(xiàn)

    在前面的教程中,我們已經(jīng)實(shí)現(xiàn)了基于Spring Boot和Vue3的發(fā)布、編輯、刪除文章功能以及文章列表與分頁(yè)功能。本教程將引導(dǎo)您實(shí)現(xiàn)博客平臺(tái)的文章詳情與評(píng)論功能,需要的朋友可以參考一下
    2023-04-04
  • 如何通過(guò)zuul添加或修改請(qǐng)求參數(shù)

    如何通過(guò)zuul添加或修改請(qǐng)求參數(shù)

    這篇文章主要介紹了如何通過(guò)zuul添加或修改請(qǐng)求參數(shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 詳解Spring中Bean的加載的方法

    詳解Spring中Bean的加載的方法

    本篇文章主要介紹了Spring中Bean的加載的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-04-04
  • JSON 格式的弊端與解決方法(真實(shí)示例)

    JSON 格式的弊端與解決方法(真實(shí)示例)

    JSON 格式是目前最流行的數(shù)據(jù)交互格式,廣泛應(yīng)用于前后端分離的系統(tǒng)。但也有一些場(chǎng)合不適合使用 JSON 格式,這篇文章主要介紹了JSON 格式的弊端與解決方法,需要的朋友可以參考下
    2022-09-09
  • mybatis對(duì)于list更新sql語(yǔ)句的寫(xiě)法說(shuō)明

    mybatis對(duì)于list更新sql語(yǔ)句的寫(xiě)法說(shuō)明

    這篇文章主要介紹了mybatis對(duì)于list更新sql語(yǔ)句的寫(xiě)法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08

最新評(píng)論