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

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

 更新時(shí)間:2022年06月06日 10:37:41   作者:城城正開心  
這篇文章主要為大家詳細(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)欄 已開未開,用時(shí)
?? ?JLabel label1= new JLabel("待開:"+unopened);
?? ?JLabel label2= new JLabel("已開:"+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è)置開頭顯示
?? ?private void setHeader() {
?? ??? ?//設(shè)置畫布 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("待開:"+ unopened);
?? ??? ??? ?label2.setText("已開:"+ 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重新開始!","游戲結(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)打開不可點(diǎn)擊
?? ??? ??? ??? ??? ??? ?btn.setOpaque(true);
?? ??? ?
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?bannerBtn.setText("Banner:restart!");
?? ??? ?JOptionPane.showMessageDialog(frame, "很遺憾,暴雷成功,游戲結(jié)束?。?!\n 可以點(diǎn)擊上面的Banner重新開始!","游戲結(jié)束!",JOptionPane.PLAIN_MESSAGE);
?? ??? ?
?? ?}

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

private void openCell(int i,int j,int Blankcount ){
?? ??? ?JButton btn=btns[i][j];
?? ??? ?if(!btn.isEnabled()) return ;
?? ??? ?if(Blankcount==10) ? //部分打開,設(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)文章

  • 淺談Spring自定義注解從入門到精通

    淺談Spring自定義注解從入門到精通

    這篇文章主要介紹了淺談Spring自定義注解從入門到精通,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Java Durid進(jìn)行JDBC連接詳解

    Java Durid進(jìn)行JDBC連接詳解

    今天給大家?guī)?lái)的是關(guān)于Java的相關(guān)知識(shí),文章簡(jiǎn)單使用Durid進(jìn)行JDBC連接,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-09-09
  • 基于Java回顧之多線程同步的使用詳解

    基于Java回顧之多線程同步的使用詳解

    在這篇文章里,我們關(guān)注線程同步的話題。這是比多線程更復(fù)雜,稍不留意,我們就會(huì)“掉到坑里”,而且和單線程程序不同,多線程的錯(cuò)誤是否每次都出現(xiàn),也是不固定的,這給調(diào)試也帶來(lái)了很大的挑戰(zhàn)
    2013-05-05
  • Java各種內(nèi)存溢出的問(wèn)題剖析

    Java各種內(nèi)存溢出的問(wèn)題剖析

    本文主要介紹了Java各種內(nèi)存溢出的問(wèn)題剖析,了解其根源、排查方法以及有效的修改策略,具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-03-03
  • 以武俠形式理解Java LinkedList源碼

    以武俠形式理解Java LinkedList源碼

    鏈表(Linked list)是一種常見(jiàn)的基礎(chǔ)數(shù)據(jù)結(jié)構(gòu),是一種線性表,但是并不會(huì)按線性的順序存儲(chǔ)數(shù)據(jù),而是在每一個(gè)節(jié)點(diǎn)里存到下一個(gè)節(jié)點(diǎn)的地址
    2021-11-11
  • java Collection 之List學(xué)習(xí)介紹

    java Collection 之List學(xué)習(xí)介紹

    本篇文章小編為大家介紹,java Collection 之List學(xué)習(xí)介紹。需要的朋友參考下
    2013-04-04
  • 基于Springboot實(shí)現(xiàn)JWT認(rèn)證的示例代碼

    基于Springboot實(shí)現(xiàn)JWT認(rèn)證的示例代碼

    本文主要介紹了基于Springboot實(shí)現(xiàn)JWT認(rèn)證,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Java中使用Thread類和Runnable接口實(shí)現(xiàn)多線程的區(qū)別

    Java中使用Thread類和Runnable接口實(shí)現(xiàn)多線程的區(qū)別

    這篇文章主要介紹了使用Thread類和Runnable接口實(shí)現(xiàn)多線程的區(qū)別,本文給大家介紹了兩種實(shí)現(xiàn)方式的步驟,除了以上兩種多線程實(shí)現(xiàn)方式,還可以使用 Callable 接口實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07
  • SpringBoot如何實(shí)現(xiàn)文件下載

    SpringBoot如何實(shí)現(xiàn)文件下載

    這篇文章主要介紹了SpringBoot如何實(shí)現(xiàn)文件下載問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • java 中設(shè)計(jì)模式(裝飾設(shè)計(jì)模式)的實(shí)例詳解

    java 中設(shè)計(jì)模式(裝飾設(shè)計(jì)模式)的實(shí)例詳解

    這篇文章主要介紹了java 中設(shè)計(jì)模式(裝飾設(shè)計(jì)模式)的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下
    2017-09-09

最新評(píng)論

周至县| 湾仔区| 田阳县| 吴忠市| 金阳县| 抚远县| 蓬安县| 罗田县| 定安县| 通化市| 安乡县| 嘉祥县| 定日县| 阿尔山市| 定结县| 马尔康县| 大名县| 昆山市| 上饶市| 柘荣县| 宁津县| 福清市| 日照市| 科尔| 鄂温| 嘉定区| 苍梧县| 诏安县| 平昌县| 平凉市| 鱼台县| 宁阳县| 班戈县| 织金县| 四子王旗| 公安县| 旌德县| 浮梁县| 唐山市| 云安县| 福州市|