0


Java编程实现三种等级的扫雷游戏(完整版)

大家好,我是陈橘又青,今天用Java编程实现图形化界面的扫雷游戏(三种难度),以下是完整的开发思路以及代码,供各位讨论交流。

在这里插入图片描述

文章目录


一、效果展示

初级难度

在这里插入图片描述

中级难度

在这里插入图片描述

高级难度

在这里插入图片描述

测试界面

在这里插入图片描述


二、项目介绍

项目背景

扫雷是一款大众类的益智小游戏。根据点击格子出现的数字找出所有非雷格子,同时避免踩雷,踩到一个雷即全盘皆输。这款游戏有着很长的历史,从扫雷被开发出来到现在进行了无数次的优化,这款游戏通过简单的玩法,加上一个好看的游戏界面,每一处的细节都体现了扫雷的魅力。

功能分析

完成难度选择雷随机生成数字生成左右键翻开等功能实现
游戏四种状态:难度选择游戏状态游戏胜利游戏失败
游戏难度:初级中级高级(不同难度对应不同的雷区大小和雷数量)
游戏核心:二维数组 的相关操作
其他: 窗口绘制、界面规划、操作计数、重新开始。


三、代码展示

图形界面设计(gui包)

主类:AppWindows类

AppWindow类负责创建游戏的主窗口,该类含有main方法,程序从该类开始执行。

package ch8.gui;import java.awt.*;import javax.swing.*;import javax.swing.event.*;import java.awt.event.*;import ch8.view.MineArea;import ch8.view.ShowRecord;
public class AppWindow extends JFrame implements MenuListener,ActionListener{
     JMenuBar bar;
     JMenu fileMenu;
     JMenu gradeOne,gradeTwo,gradeThree;//扫雷级别
     JMenuItem gradeOneList,gradeTwoList,gradeThreeList;//初,中,高级英雄榜
     MineArea mineArea=null;//扫雷区域
     ShowRecord showHeroRecord=null;//查看英雄榜
     public AppWindow(){
         bar=newJMenuBar();
         fileMenu=newJMenu("扫雷游戏");
         gradeOne=newJMenu("初级");
         gradeTwo=newJMenu("中级");
         gradeThree=newJMenu("高级");
         gradeOneList=newJMenuItem("初级英雄榜");
         gradeTwoList=newJMenuItem("中级英雄榜");
         gradeThreeList=newJMenuItem("高级英雄榜");
         gradeOne.add(gradeOneList);
         gradeTwo.add(gradeTwoList);
         gradeThree.add(gradeThreeList);
         fileMenu.add(gradeOne);
         fileMenu.add(gradeTwo);
         fileMenu.add(gradeThree);
         bar.add(fileMenu);setJMenuBar(bar);
         gradeOne.addMenuListener(this);
         gradeTwo.addMenuListener(this);
         gradeThree.addMenuListener(this);
         gradeOneList.addActionListener(this);
         gradeTwoList.addActionListener(this);
         gradeThreeList.addActionListener(this);
         mineArea=newMineArea(9,9,10,gradeOne.getText());//创建初级扫雷区add(mineArea,BorderLayout.CENTER);
         showHeroRecord=newShowRecord();setBounds(300,100,500,450);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);validate();}
     public void menuSelected(MenuEvent e){if(e.getSource()==gradeOne){
              mineArea.initMineArea(9,9,10,gradeOne.getText());validate();}elseif(e.getSource()==gradeTwo){
              mineArea.initMineArea(16,16,40,gradeTwo.getText());validate();}elseif(e.getSource()==gradeThree){
              mineArea.initMineArea(22,30,99,gradeThree.getText());validate();}}
     public void menuCanceled(MenuEvent e){}
     public void menuDeselected(MenuEvent e){}
     public void actionPerformed(ActionEvent e){if(e.getSource()==gradeOneList){
              showHeroRecord.setGrade(gradeOne.getText());
              showHeroRecord.showRecord();}elseif(e.getSource()==gradeTwoList){
              showHeroRecord.setGrade(gradeTwo.getText());
              showHeroRecord.showRecord();}elseif(e.getSource()==gradeThreeList){
              showHeroRecord.setGrade(gradeThree.getText());
              showHeroRecord.showRecord();}}
    public static void main(String args[]){newAppWindow();}}

用户操作设计(data包)

Block类

packagech8.data;importjavax.swing.ImageIcon;publicclassBlock{String name;//名字,比如"雷"或数字int aroundMineNumber;//如果不是类,此数据是周围雷的数目ImageIcon mineIcon;//雷的图标publicboolean isMine=false;//是否是雷boolean isMark=false;//是否被标记boolean isOpen=false;//是否被挖开ViewForBlock  blockView;//方块的视图publicvoidsetName(String name){this.name=name;}publicvoidsetAroundMineNumber(int n){ 
         aroundMineNumber=n;}publicintgetAroundMineNumber(){return aroundMineNumber;}publicStringgetName(){return name;}publicbooleanisMine(){return isMine;}publicvoidsetIsMine(boolean b){
         isMine=b;}publicvoidsetMineIcon(ImageIcon icon){
         mineIcon=icon;}publicImageIcongetMineicon(){return mineIcon;}publicbooleangetIsOpen(){return isOpen;}publicvoidsetIsOpen(boolean p){
         isOpen=p;}publicbooleangetIsMark(){return isMark;}publicvoidsetIsMark(boolean m){
         isMark=m;}publicvoidsetBlockView(ViewForBlock view){
        blockView = view;
        blockView.acceptBlock(this);}publicViewForBlockgetBlockView(){return  blockView ;}}

LayMines类

packagech8.data;importjava.util.LinkedList;importjavax.swing.ImageIcon;publicclassLayMines{ImageIcon mineIcon;publicLayMines(){
          mineIcon=newImageIcon("扫雷图片/mine.gif");}publicvoidinitBlock(Block[][] block){for(int i=0;i<block.length;i++){for(int j=0;j<block[i].length;j++)
                block[i][j].setIsMine(false);}}publicvoidlayMinesForBlock(Block[][] block,int mineCount){//在雷区布置mineCount个雷initBlock(block);//先都设置是无雷int row=block.length;int column=block[0].length;LinkedList<Block> list=newLinkedList<Block>();for(int i=0;i<row;i++){for(int j=0;j<column;j++)
                list.add(block[i][j]);}while(mineCount>0){//开始布雷int size=list.size();// list返回节点的个数int randomIndex=(int)(Math.random()*size);Block b=list.get(randomIndex);
            b.setIsMine(true);
            b.setName("雷");
            b.setMineIcon(mineIcon);
            list.remove(randomIndex);//list删除索引值为randomIndex的节点
            mineCount--;}for(int i=0;i<row;i++){//检查布雷情况,标记每个方块周围的雷的数目for(int j=0;j<column;j++){if(block[i][j].isMine()){
                 block[i][j].setIsOpen(false);
                 block[i][j].setIsMark(false);}else{int mineNumber=0;for(int k=Math.max(i-1,0);k<=Math.min(i+1,row-1);k++){for(int t=Math.max(j-1,0);t<=Math.min(j+1,column-1);t++){if(block[k][t].isMine())
                              mineNumber++;}}
                 block[i][j].setIsOpen(false); 
                 block[i][j].setIsMark(false);       
                 block[i][j].setName(""+mineNumber);
                 block[i][j].setAroundMineNumber(mineNumber);//设置该方块周围的雷数目}}}}}

PeopleScoutMine类

packagech8.data;importjava.util.Stack;publicclassPeopleScoutMine{publicBlock[][] block;//雷区的全部方块Stack<Block>  notMineBlock;//存放一个方块周围区域内不是雷的方块int m,n ;//方块的索引下标int row,colum;//雷区的行和列int mineCount;//雷的数目publicPeopleScoutMine(){
       notMineBlock =newStack<Block>();}publicvoidsetBlock(Block[][] block,int mineCount){this.block = block;this.mineCount =  mineCount;
       row = block.length;
       colum = block[0].length;}publicStack<Block>getNoMineAroundBlock(Block bk){//得到方块bk附近区域不是雷的方块
       notMineBlock.clear();for(int i=0;i<row;i++){//寻找bk在雷区block中的位置索引for(int j=0;j<colum;j++){if(bk == block[i][j]){
                  m=i;
                  n=j;break;}}}if(!bk.isMine()){//方块不是雷show(m,n);//见后面的递归方法}return notMineBlock;}publicvoidshow(int m,int n){if(block[m][n].getAroundMineNumber()>0&&block[m][n].getIsOpen()==false){
          block[m][n].setIsOpen(true);
          notMineBlock.push(block[m][n]);//将不是雷的方块压栈return;}elseif(block[m][n].getAroundMineNumber()==0&&block[m][n].getIsOpen()==false){
          block[m][n].setIsOpen(true);
          notMineBlock.push(block[m][n]);//将不是雷的方块压栈for(int k=Math.max(m-1,0);k<=Math.min(m+1,row-1);k++){for(int t=Math.max(n-1,0);t<=Math.min(n+1,colum-1);t++)show(k,t);}}}publicbooleanverifyWin(){boolean isOK =false;int number=0;for(int i=0;i<row;i++){for(int j=0;j<colum;j++){if(block[i][j].getIsOpen()==false)
                 number++;}}if(number==mineCount){
           isOK =true;}return isOK;}}

RecordOrShowRecord类

packagech8.data;importjava.sql.*;publicclassRecordOrShowRecord{Connection con;String tableName ;int heroNumber   =3;//英雄榜显示的最多英雄数目publicRecordOrShowRecord(){try{Class.forName("org.apache.derby.jdbc.EmbeddedDriver");}catch(Exception e){}}publicvoidsetTable(String str){
      tableName ="t_"+str;connectDatabase();//连接数据库try{Statement sta = con.createStatement();String SQL="create table "+tableName+"(p_name varchar(50) ,p_time int)";
          sta.executeUpdate(SQL);//创建表
          con.close();}catch(SQLException e){//如果表已经存在,将触发SQL异常,即不再创建该表}}publicbooleanaddRecord(String name,int time){boolean ok  =true;if(tableName ==null)
         ok =false;//检查time是否达到标准(进入前heroNumber名),见后面的verifyScore方法:int amount =verifyScore(time);if(amount >= heroNumber){
          ok =false;}else{connectDatabase();//连接数据库try{String SQL ="insert into "+tableName+" values(?,?)";PreparedStatement sta  = con.prepareStatement(SQL);
             sta.setString(1,name);
             sta.setInt(2,time);
             sta.executeUpdate();
             con.close();
             ok =true;}catch(SQLException e){
             ok =false;}}return ok;}publicString[][]queryRecord(){if(tableName ==null)returnnull;String[][]record=null;Statement sql;ResultSet rs;try{ 
        sql=
        con.createStatement
       (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);String str ="select * from "+tableName+" order by p_time ";
        rs=sql.executeQuery(str);boolean boo =rs.last();if(boo ==false)returnnull;int recordAmount =rs.getRow();//结果集中的全部记录record=newString[recordAmount][2];
        rs.beforeFirst();int i=0;while(rs.next()){record[i][0]= rs.getString(1);record[i][1]= rs.getString(2);
          i++;}
        con.close();}catch(SQLException e){}returnrecord;}privatevoidconnectDatabase(){try{String uri ="jdbc:derby:record;create=true";
         con=DriverManager.getConnection(uri);//连接数据库,如果不存在就创建}catch(Exception e){}}privateintverifyScore(int time){if(tableName ==null)returnInteger.MAX_VALUE ;connectDatabase();//连接数据库Statement sql;ResultSet rs;int amount =0;String str ="select * from "+tableName+" where p_time < "+time;try{ 
        sql=con.createStatement();
        rs=sql.executeQuery(str);while(rs.next()){ 
           amount++;}                        
        con.close();}catch(SQLException e){}return amount;}}

ViewForBlock接口

packagech8.data;publicinterfaceViewForBlock{publicvoidacceptBlock(Block block);//确定是哪个方块的视图publicvoidsetDataOnView();//设置视图上需要显示的数据publicvoidseeBlockNameOrIcon();//显示图标方块上的名字或publicvoidseeBlockCover();//显示视图上负责遮挡的组件publicObjectgetBlockCover();//得到视图上的遮挡组件}

游戏视图设计(view包)

BlockView类

packagech8.view;importjavax.swing.*;importjava.awt.*;importch8.data.*;publicclassBlockViewextendsJPanelimplementsViewForBlock{JLabel blockNameOrIcon;//用来显示Block对象的name、number和mineIcon属性JButton blockCover;//用来遮挡blockNameOrIcon.CardLayout card;//卡片式布局Block block ;//被提供视图的方块BlockView(){
        card=newCardLayout();setLayout(card);
        blockNameOrIcon=newJLabel("",JLabel.CENTER);
        blockNameOrIcon.setHorizontalTextPosition(AbstractButton.CENTER);
        blockNameOrIcon.setVerticalTextPosition(AbstractButton.CENTER); 
        blockCover=newJButton();add("cover",blockCover);add("view",blockNameOrIcon);}publicvoidacceptBlock(Block block){this.block = block;}publicvoidsetDataOnView(){if(block.isMine()){
           blockNameOrIcon.setText(block.getName());
           blockNameOrIcon.setIcon(block.getMineicon());}else{int n=block.getAroundMineNumber();if(n>=1)
             blockNameOrIcon.setText(""+n);else
             blockNameOrIcon.setText(" ");}}publicvoidseeBlockNameOrIcon(){
        card.show(this,"view");validate();}publicvoidseeBlockCover(){
        card.show(this,"cover");validate();}publicJButtongetBlockCover(){return blockCover;}}

MineArea类

packagech8.view;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importch8.data.*;importjava.util.Stack;publicclassMineAreaextendsJPanelimplementsActionListener,MouseListener{JButton reStart;Block[][] block;//雷区的方块BlockView[][] blockView;//方块的视图LayMines lay;//负责布雷PeopleScoutMine peopleScoutMine;//负责扫雷int row,colum,mineCount,markMount;//雷区的行数、列数以及地雷个数和用户给出的标记数ImageIcon mark;//探雷作的标记String grade;//游戏级别 JPanel pCenter,pNorth;//布局用的面板JTextField showTime,showMarkedMineCount;//显示用时和探雷作的标记数目(不一定是雷哦)Timer time;//计时器int spendTime=0;//扫雷的用时Recordrecord;//负责记录到英雄榜PlayMusic playMusic;//负责播放雷爆炸的声音publicMineArea(int row,int colum,int mineCount,String grade){record=newRecord();//负责保存成绩到英雄榜
         reStart=newJButton("重新开始");
         mark=newImageIcon("扫雷图片/mark.png");//探雷标记
         time=newTimer(1000,this);//计时器,每个一秒触发ActionEvent事件一次
         showTime=newJTextField(5);
         showMarkedMineCount=newJTextField(5);
         showTime.setHorizontalAlignment(JTextField.CENTER);
         showMarkedMineCount.setHorizontalAlignment(JTextField.CENTER);
         showMarkedMineCount.setFont(newFont("Arial",Font.BOLD,16));
         showTime.setFont(newFont("Arial",Font.BOLD,16));         
         pCenter=newJPanel();
         pNorth=newJPanel();
         lay=newLayMines();//创建布雷者
         peopleScoutMine =newPeopleScoutMine();//创建扫雷者initMineArea(row,colum,mineCount,grade);//初始化雷区,见下面的initMineArea方法
         reStart.addActionListener(this);
         pNorth.add(newJLabel("剩余雷数(千万别弄错啊):"));
         pNorth.add(showMarkedMineCount);
         pNorth.add(reStart);
         pNorth.add(newJLabel("用时:"));
         pNorth.add(showTime);setLayout(newBorderLayout());add(pNorth,BorderLayout.NORTH);add(pCenter,BorderLayout.CENTER);
         playMusic =newPlayMusic();//负责播放触雷爆炸的声音
         playMusic.setClipFile("扫雷图片/mine.wav");}publicvoidinitMineArea(int row,int colum,int mineCount,String grade){
       pCenter.removeAll();
       spendTime=0;
       markMount=mineCount;this.row=row;this.colum=colum;this.mineCount=mineCount;this.grade=grade; 
       block=newBlock[row][colum];for(int i=0;i<row;i++){for(int j=0;j<colum;j++)
              block[i][j]=newBlock();}
       lay.layMinesForBlock(block,mineCount);//布雷
       peopleScoutMine.setBlock(block,mineCount);//准备扫雷   
       blockView=newBlockView[row][colum];//创建方块的视图
       pCenter.setLayout(newGridLayout(row,colum));for(int i=0;i<row;i++){for(int j=0;j<colum;j++){
               blockView[i][j]=newBlockView(); 
               block[i][j].setBlockView(blockView[i][j]);//方块设置自己的视图
               blockView[i][j].setDataOnView();//将block[i][j]的数据放入视图
               pCenter.add(blockView[i][j]);
               blockView[i][j].getBlockCover().addActionListener(this);//注册监视器
               blockView[i][j].getBlockCover().addMouseListener(this);
               blockView[i][j].seeBlockCover();//初始时盖住block[i][j]的数据信息
               blockView[i][j].getBlockCover().setEnabled(true);
               blockView[i][j].getBlockCover().setIcon(null);}}
      showMarkedMineCount.setText(""+markMount);repaint();}publicvoidsetRow(int row){this.row=row;}publicvoidsetColum(int colum){this.colum=colum;}publicvoidsetMineCount(int mineCount){this.mineCount=mineCount;}publicvoidsetGrade(String grade){this.grade=grade;}publicvoidactionPerformed(ActionEvent e){if(e.getSource()!=reStart&&e.getSource()!=time){
          time.start();int m=-1,n=-1;for(int i=0;i<row;i++){//找到单击的方块以及它的位置索引for(int j=0;j<colum;j++){if(e.getSource()==blockView[i][j].getBlockCover()){
                  m=i;
                  n=j;break;}}}if(block[m][n].isMine()){//用户输掉游戏for(int i=0;i<row;i++){for(int j=0;j<colum;j++){
                   blockView[i][j].getBlockCover().setEnabled(false);//用户单击无效了if(block[i][j].isMine())
                      blockView[i][j].seeBlockNameOrIcon();//视图显示方块上的数据信息}}
             time.stop();
             spendTime=0;//恢复初始值
             markMount=mineCount;//恢复初始值
             playMusic.playMusic();//播放类爆炸的声音}else{//扫雷者得到block[m][n]周围区域不是雷的方块Stack<Block> notMineBlock =peopleScoutMine.getNoMineAroundBlock(block[m][n]);while(!notMineBlock.empty()){Block bk = notMineBlock.pop();ViewForBlock viewforBlock = bk.getBlockView();
                viewforBlock.seeBlockNameOrIcon();//视图显示方块上的数据信息System.out.println("ookk");}}}if(e.getSource()==reStart){initMineArea(row,colum,mineCount,grade);repaint();validate();}if(e.getSource()==time){
         spendTime++;
         showTime.setText(""+spendTime);}if(peopleScoutMine.verifyWin()){//判断用户是否扫雷成功
         time.stop();record.setGrade(grade);record.setTime(spendTime);record.setVisible(true);//弹出录入到英雄榜对话框}}publicvoidmousePressed(MouseEvent e){//探雷:给方块上插一个小旗图标(再次单击取消)JButton source=(JButton)e.getSource();for(int i=0;i<row;i++){for(int j=0;j<colum;j++){if(e.getModifiers()==InputEvent.BUTTON3_MASK&&
                 source==blockView[i][j].getBlockCover()){if(block[i][j].getIsMark()){
                        source.setIcon(null);
                        block[i][j].setIsMark(false);
                        markMount=markMount+1;
                        showMarkedMineCount.setText(""+markMount);}else{
                        source.setIcon(mark);
                        block[i][j].setIsMark(true);
                        markMount=markMount-1;
                        showMarkedMineCount.setText(""+markMount);}}}}}publicvoidmouseReleased(MouseEvent e){}publicvoidmouseEntered(MouseEvent e){}publicvoidmouseExited(MouseEvent e){}publicvoidmouseClicked(MouseEvent e){}}

PlayMusic类

packagech8.view;importjava.net.URI;importjava.net.URL;importjava.io.File;importjava.applet.Applet;importjava.applet.AudioClip;publicclassPlayMusicimplementsRunnable{String musicName;Thread threadPlay;AudioClip clip =null;publicPlayMusic(){
         threadPlay =newThread(this);}publicvoidrun(){
          clip.play();}publicvoidplayMusic(){
          threadPlay =newThread(this);try{
             threadPlay.start();}catch(Exception exp){}}publicvoidsetClipFile(String name){
           musicName = name;if(musicName ==null)
             musicName ="扫雷图像/mine.wav";File file=newFile(musicName);try{URI uri=file.toURI();URL url=uri.toURL();
                clip=Applet.newAudioClip(url);}catch(Exception ee){}}}

Record类

packagech8.view;importjavax.swing.*;importjava.awt.event.*;importch8.data.RecordOrShowRecord;publicclassRecordextendsJDialogimplementsActionListener{int time=0;String grade=null;String key=null;String message=null;JTextField textName;JLabel label=null;JButton confirm,cancel;publicRecord(){setTitle("记录你的成绩");this.time=time;this.grade=grade;setBounds(100,100,240,160);setResizable(false);setModal(true); 
      confirm=newJButton("确定");
      cancel=newJButton("取消");
      textName=newJTextField(8);
      textName.setText("匿名");
      confirm.addActionListener(this);
      cancel.addActionListener(this);setLayout(newjava.awt.GridLayout(2,1));
      label=newJLabel("输入您的大名看是否可上榜");add(label);JPanel p=newJPanel();
      p.add(textName);
      p.add(confirm);
      p.add(cancel);add(p);setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);}publicvoidsetGrade(String grade){this.grade=grade;}publicvoidsetTime(int time){this.time=time;}publicvoidactionPerformed(ActionEvent e){if(e.getSource()==confirm){String name = textName.getText();writeRecord(name,time);setVisible(false);}if(e.getSource()==cancel){setVisible(false);}}publicvoidwriteRecord(String name,int time){RecordOrShowRecord rd =newRecordOrShowRecord();
     rd.setTable(grade);boolean boo= rd.addRecord(name,time);if(boo){JOptionPane.showMessageDialog
          (null,"恭喜您,上榜了","消息框",JOptionPane.WARNING_MESSAGE);}else{JOptionPane.showMessageDialog
          (null,"成绩不能上榜","消息框",JOptionPane.WARNING_MESSAGE);}}}

ShowRecord类

packagech8.view;importjava.awt.*;importjavax.swing.*;importch8.data.RecordOrShowRecord;publicclassShowRecordextendsJDialog{String[][]record;JTextArea showMess;RecordOrShowRecord rd;//负责查询数据库的对象publicShowRecord(){
      rd =newRecordOrShowRecord();
      showMess =newJTextArea();
      showMess.setFont(newFont("楷体",Font.BOLD,15));add(newJScrollPane(showMess));setTitle("显示英雄榜");setBounds(400,200,400,300);setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);}publicvoidsetGrade(String grade){
      rd.setTable(grade);}publicvoidsetRecord(String[][]record){this.record=record;}publicvoidshowRecord(){
       showMess.setText(null);record= rd.queryRecord();if(record==null){JOptionPane.showMessageDialog
            (null,"没人上榜呢","消息框",JOptionPane.WARNING_MESSAGE);}else{for(int i =0;i<record.length;i++){int m = i+1;
              showMess.append("\n英雄"+m+":"+record[i][0]+"         "+"成绩:"+record[i][1]);
              showMess.append("\n--------------------------------");}setVisible(true);}}}

四、代码测试

这里我们创建test包,实现AppTest类来进行代码的测试,代码如下:

packagech8.test;importch8.data.*;importjava.util.Stack;publicclassAppTest{publicstaticvoidmain(String[] args){Block block[][]=newBlock[5][10];//雷区for(int i=0;i<block.length;i++){for(int j =0;j<block[i].length;j++){
             block[i][j]=newBlock();}}LayMines layMines =newLayMines();//布雷者PeopleScoutMine peopleScoutMine  =newPeopleScoutMine();//扫雷者
       layMines.layMinesForBlock(block,10);//在雷区布雷System.out.println("雷区情况:");intputShow(block);
       peopleScoutMine.setBlock(block,10);//准备扫雷 Stack<Block> stack = peopleScoutMine.getNoMineAroundBlock(block[0][0]);//扫雷if(block[0][0].isMine()){System.out.println("我的天啊,踩着地雷了啊");return;}System.out.println("扫雷情况:");intputProcess(block,stack);System.out.println("成功了吗:"+peopleScoutMine.verifyWin());if(block[3][3].isMine()){System.out.println("我的天啊,踩着地雷了啊");return;}
       stack = peopleScoutMine.getNoMineAroundBlock(block[3][3]);//扫雷System.out.println("扫雷情况:");intputProcess(block,stack);System.out.println("成功了吗:"+peopleScoutMine.verifyWin());}staticvoidintputProcess(Block[][] block,Stack<Block> stack){int k =0;for(int i=0;i<block.length;i++){for(int j =0;j<block[i].length;j++){if(!stack.contains(block[i][j])&&block[i][j].getIsOpen()==false){System.out.printf("%2s","■ ");//输出■表示未挖开方块}else{int m = block[i][j].getAroundMineNumber();//显示周围雷的数目System.out.printf("%2s","□"+m);}}System.out.println();}}staticvoidintputShow(Block[][] block){int k =0;for(int i=0;i<block.length;i++){for(int j =0;j<block[i].length;j++){if(block[i][j].isMine()){System.out.printf("%2s","#");//输出#表示是地雷}else{int m = block[i][j].getAroundMineNumber();//显示周围雷的数目System.out.printf("%2s",m);}}System.out.println();}}}

五、项目结构

在这里插入图片描述


六、设计总结

本次项目设计是通过 Java语言编制一个扫雷游戏,Java语言是当今较为流行的网络编程语言,它具有面向对象、跨平台、分布应用等特点。这次设计,还有利于加深对 Java课程的进一步了解,也可以巩固所学 Java语言基本知识,增进 Java语言编辑基本功,拓宽常用类库的应用。采用面向对象思想的程序,锻炼了面向对象的设计能力,使编程者通过该教学环节与手段,把所学课程及相关知识加以融会贯通,全面掌握 Java语言的编程思想及面向对象程序设计的方法。


在这里插入图片描述

标签: java 游戏 jvm

本文转载自: https://blog.csdn.net/m0_63947499/article/details/127482467
版权归原作者 陈橘又青 所有, 如有侵权,请联系我们删除。

“Java编程实现三种等级的扫雷游戏(完整版)”的评论:

还没有评论