0


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

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

在这里插入图片描述

文章目录


一、效果展示

初级难度

在这里插入图片描述

中级难度

在这里插入图片描述

高级难度

在这里插入图片描述

测试界面

在这里插入图片描述


二、项目介绍

项目背景

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

功能分析

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


三、代码展示

图形界面设计(gui包)

主类:AppWindows类

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

  1. 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;
  2. public class AppWindow extends JFrame implements MenuListener,ActionListener{
  3. JMenuBar bar;
  4. JMenu fileMenu;
  5. JMenu gradeOne,gradeTwo,gradeThree;//扫雷级别
  6. JMenuItem gradeOneList,gradeTwoList,gradeThreeList;//初,中,高级英雄榜
  7. MineArea mineArea=null;//扫雷区域
  8. ShowRecord showHeroRecord=null;//查看英雄榜
  9. public AppWindow(){
  10. bar=newJMenuBar();
  11. fileMenu=newJMenu("扫雷游戏");
  12. gradeOne=newJMenu("初级");
  13. gradeTwo=newJMenu("中级");
  14. gradeThree=newJMenu("高级");
  15. gradeOneList=newJMenuItem("初级英雄榜");
  16. gradeTwoList=newJMenuItem("中级英雄榜");
  17. gradeThreeList=newJMenuItem("高级英雄榜");
  18. gradeOne.add(gradeOneList);
  19. gradeTwo.add(gradeTwoList);
  20. gradeThree.add(gradeThreeList);
  21. fileMenu.add(gradeOne);
  22. fileMenu.add(gradeTwo);
  23. fileMenu.add(gradeThree);
  24. bar.add(fileMenu);setJMenuBar(bar);
  25. gradeOne.addMenuListener(this);
  26. gradeTwo.addMenuListener(this);
  27. gradeThree.addMenuListener(this);
  28. gradeOneList.addActionListener(this);
  29. gradeTwoList.addActionListener(this);
  30. gradeThreeList.addActionListener(this);
  31. mineArea=newMineArea(9,9,10,gradeOne.getText());//创建初级扫雷区add(mineArea,BorderLayout.CENTER);
  32. showHeroRecord=newShowRecord();setBounds(300,100,500,450);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);validate();}
  33. public void menuSelected(MenuEvent e){if(e.getSource()==gradeOne){
  34. mineArea.initMineArea(9,9,10,gradeOne.getText());validate();}elseif(e.getSource()==gradeTwo){
  35. mineArea.initMineArea(16,16,40,gradeTwo.getText());validate();}elseif(e.getSource()==gradeThree){
  36. mineArea.initMineArea(22,30,99,gradeThree.getText());validate();}}
  37. public void menuCanceled(MenuEvent e){}
  38. public void menuDeselected(MenuEvent e){}
  39. public void actionPerformed(ActionEvent e){if(e.getSource()==gradeOneList){
  40. showHeroRecord.setGrade(gradeOne.getText());
  41. showHeroRecord.showRecord();}elseif(e.getSource()==gradeTwoList){
  42. showHeroRecord.setGrade(gradeTwo.getText());
  43. showHeroRecord.showRecord();}elseif(e.getSource()==gradeThreeList){
  44. showHeroRecord.setGrade(gradeThree.getText());
  45. showHeroRecord.showRecord();}}
  46. public static void main(String args[]){newAppWindow();}}

用户操作设计(data包)

Block类

  1. 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){
  2. aroundMineNumber=n;}publicintgetAroundMineNumber(){return aroundMineNumber;}publicStringgetName(){return name;}publicbooleanisMine(){return isMine;}publicvoidsetIsMine(boolean b){
  3. isMine=b;}publicvoidsetMineIcon(ImageIcon icon){
  4. mineIcon=icon;}publicImageIcongetMineicon(){return mineIcon;}publicbooleangetIsOpen(){return isOpen;}publicvoidsetIsOpen(boolean p){
  5. isOpen=p;}publicbooleangetIsMark(){return isMark;}publicvoidsetIsMark(boolean m){
  6. isMark=m;}publicvoidsetBlockView(ViewForBlock view){
  7. blockView = view;
  8. blockView.acceptBlock(this);}publicViewForBlockgetBlockView(){return blockView ;}}

LayMines类

  1. packagech8.data;importjava.util.LinkedList;importjavax.swing.ImageIcon;publicclassLayMines{ImageIcon mineIcon;publicLayMines(){
  2. mineIcon=newImageIcon("扫雷图片/mine.gif");}publicvoidinitBlock(Block[][] block){for(int i=0;i<block.length;i++){for(int j=0;j<block[i].length;j++)
  3. 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++)
  4. list.add(block[i][j]);}while(mineCount>0){//开始布雷int size=list.size();// list返回节点的个数int randomIndex=(int)(Math.random()*size);Block b=list.get(randomIndex);
  5. b.setIsMine(true);
  6. b.setName("雷");
  7. b.setMineIcon(mineIcon);
  8. list.remove(randomIndex);//list删除索引值为randomIndex的节点
  9. mineCount--;}for(int i=0;i<row;i++){//检查布雷情况,标记每个方块周围的雷的数目for(int j=0;j<column;j++){if(block[i][j].isMine()){
  10. block[i][j].setIsOpen(false);
  11. 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())
  12. mineNumber++;}}
  13. block[i][j].setIsOpen(false);
  14. block[i][j].setIsMark(false);
  15. block[i][j].setName(""+mineNumber);
  16. block[i][j].setAroundMineNumber(mineNumber);//设置该方块周围的雷数目}}}}}

PeopleScoutMine类

  1. packagech8.data;importjava.util.Stack;publicclassPeopleScoutMine{publicBlock[][] block;//雷区的全部方块Stack<Block> notMineBlock;//存放一个方块周围区域内不是雷的方块int m,n ;//方块的索引下标int row,colum;//雷区的行和列int mineCount;//雷的数目publicPeopleScoutMine(){
  2. notMineBlock =newStack<Block>();}publicvoidsetBlock(Block[][] block,int mineCount){this.block = block;this.mineCount = mineCount;
  3. row = block.length;
  4. colum = block[0].length;}publicStack<Block>getNoMineAroundBlock(Block bk){//得到方块bk附近区域不是雷的方块
  5. notMineBlock.clear();for(int i=0;i<row;i++){//寻找bk在雷区block中的位置索引for(int j=0;j<colum;j++){if(bk == block[i][j]){
  6. m=i;
  7. 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){
  8. block[m][n].setIsOpen(true);
  9. notMineBlock.push(block[m][n]);//将不是雷的方块压栈return;}elseif(block[m][n].getAroundMineNumber()==0&&block[m][n].getIsOpen()==false){
  10. block[m][n].setIsOpen(true);
  11. 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)
  12. number++;}}if(number==mineCount){
  13. isOK =true;}return isOK;}}

RecordOrShowRecord类

  1. 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){
  2. tableName ="t_"+str;connectDatabase();//连接数据库try{Statement sta = con.createStatement();String SQL="create table "+tableName+"(p_name varchar(50) ,p_time int)";
  3. sta.executeUpdate(SQL);//创建表
  4. con.close();}catch(SQLException e){//如果表已经存在,将触发SQL异常,即不再创建该表}}publicbooleanaddRecord(String name,int time){boolean ok =true;if(tableName ==null)
  5. ok =false;//检查time是否达到标准(进入前heroNumber名),见后面的verifyScore方法:int amount =verifyScore(time);if(amount >= heroNumber){
  6. ok =false;}else{connectDatabase();//连接数据库try{String SQL ="insert into "+tableName+" values(?,?)";PreparedStatement sta = con.prepareStatement(SQL);
  7. sta.setString(1,name);
  8. sta.setInt(2,time);
  9. sta.executeUpdate();
  10. con.close();
  11. ok =true;}catch(SQLException e){
  12. ok =false;}}return ok;}publicString[][]queryRecord(){if(tableName ==null)returnnull;String[][]record=null;Statement sql;ResultSet rs;try{
  13. sql=
  14. con.createStatement
  15. (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);String str ="select * from "+tableName+" order by p_time ";
  16. rs=sql.executeQuery(str);boolean boo =rs.last();if(boo ==false)returnnull;int recordAmount =rs.getRow();//结果集中的全部记录record=newString[recordAmount][2];
  17. rs.beforeFirst();int i=0;while(rs.next()){record[i][0]= rs.getString(1);record[i][1]= rs.getString(2);
  18. i++;}
  19. con.close();}catch(SQLException e){}returnrecord;}privatevoidconnectDatabase(){try{String uri ="jdbc:derby:record;create=true";
  20. 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{
  21. sql=con.createStatement();
  22. rs=sql.executeQuery(str);while(rs.next()){
  23. amount++;}
  24. con.close();}catch(SQLException e){}return amount;}}

ViewForBlock接口

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

游戏视图设计(view包)

BlockView类

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

MineArea类

  1. 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();//负责保存成绩到英雄榜
  2. reStart=newJButton("重新开始");
  3. mark=newImageIcon("扫雷图片/mark.png");//探雷标记
  4. time=newTimer(1000,this);//计时器,每个一秒触发ActionEvent事件一次
  5. showTime=newJTextField(5);
  6. showMarkedMineCount=newJTextField(5);
  7. showTime.setHorizontalAlignment(JTextField.CENTER);
  8. showMarkedMineCount.setHorizontalAlignment(JTextField.CENTER);
  9. showMarkedMineCount.setFont(newFont("Arial",Font.BOLD,16));
  10. showTime.setFont(newFont("Arial",Font.BOLD,16));
  11. pCenter=newJPanel();
  12. pNorth=newJPanel();
  13. lay=newLayMines();//创建布雷者
  14. peopleScoutMine =newPeopleScoutMine();//创建扫雷者initMineArea(row,colum,mineCount,grade);//初始化雷区,见下面的initMineArea方法
  15. reStart.addActionListener(this);
  16. pNorth.add(newJLabel("剩余雷数(千万别弄错啊):"));
  17. pNorth.add(showMarkedMineCount);
  18. pNorth.add(reStart);
  19. pNorth.add(newJLabel("用时:"));
  20. pNorth.add(showTime);setLayout(newBorderLayout());add(pNorth,BorderLayout.NORTH);add(pCenter,BorderLayout.CENTER);
  21. playMusic =newPlayMusic();//负责播放触雷爆炸的声音
  22. playMusic.setClipFile("扫雷图片/mine.wav");}publicvoidinitMineArea(int row,int colum,int mineCount,String grade){
  23. pCenter.removeAll();
  24. spendTime=0;
  25. markMount=mineCount;this.row=row;this.colum=colum;this.mineCount=mineCount;this.grade=grade;
  26. block=newBlock[row][colum];for(int i=0;i<row;i++){for(int j=0;j<colum;j++)
  27. block[i][j]=newBlock();}
  28. lay.layMinesForBlock(block,mineCount);//布雷
  29. peopleScoutMine.setBlock(block,mineCount);//准备扫雷
  30. blockView=newBlockView[row][colum];//创建方块的视图
  31. pCenter.setLayout(newGridLayout(row,colum));for(int i=0;i<row;i++){for(int j=0;j<colum;j++){
  32. blockView[i][j]=newBlockView();
  33. block[i][j].setBlockView(blockView[i][j]);//方块设置自己的视图
  34. blockView[i][j].setDataOnView();//将block[i][j]的数据放入视图
  35. pCenter.add(blockView[i][j]);
  36. blockView[i][j].getBlockCover().addActionListener(this);//注册监视器
  37. blockView[i][j].getBlockCover().addMouseListener(this);
  38. blockView[i][j].seeBlockCover();//初始时盖住block[i][j]的数据信息
  39. blockView[i][j].getBlockCover().setEnabled(true);
  40. blockView[i][j].getBlockCover().setIcon(null);}}
  41. 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){
  42. 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()){
  43. m=i;
  44. n=j;break;}}}if(block[m][n].isMine()){//用户输掉游戏for(int i=0;i<row;i++){for(int j=0;j<colum;j++){
  45. blockView[i][j].getBlockCover().setEnabled(false);//用户单击无效了if(block[i][j].isMine())
  46. blockView[i][j].seeBlockNameOrIcon();//视图显示方块上的数据信息}}
  47. time.stop();
  48. spendTime=0;//恢复初始值
  49. markMount=mineCount;//恢复初始值
  50. 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();
  51. viewforBlock.seeBlockNameOrIcon();//视图显示方块上的数据信息System.out.println("ookk");}}}if(e.getSource()==reStart){initMineArea(row,colum,mineCount,grade);repaint();validate();}if(e.getSource()==time){
  52. spendTime++;
  53. showTime.setText(""+spendTime);}if(peopleScoutMine.verifyWin()){//判断用户是否扫雷成功
  54. 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&&
  55. source==blockView[i][j].getBlockCover()){if(block[i][j].getIsMark()){
  56. source.setIcon(null);
  57. block[i][j].setIsMark(false);
  58. markMount=markMount+1;
  59. showMarkedMineCount.setText(""+markMount);}else{
  60. source.setIcon(mark);
  61. block[i][j].setIsMark(true);
  62. markMount=markMount-1;
  63. showMarkedMineCount.setText(""+markMount);}}}}}publicvoidmouseReleased(MouseEvent e){}publicvoidmouseEntered(MouseEvent e){}publicvoidmouseExited(MouseEvent e){}publicvoidmouseClicked(MouseEvent e){}}

PlayMusic类

  1. 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(){
  2. threadPlay =newThread(this);}publicvoidrun(){
  3. clip.play();}publicvoidplayMusic(){
  4. threadPlay =newThread(this);try{
  5. threadPlay.start();}catch(Exception exp){}}publicvoidsetClipFile(String name){
  6. musicName = name;if(musicName ==null)
  7. musicName ="扫雷图像/mine.wav";File file=newFile(musicName);try{URI uri=file.toURI();URL url=uri.toURL();
  8. clip=Applet.newAudioClip(url);}catch(Exception ee){}}}

Record类

  1. 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);
  2. confirm=newJButton("确定");
  3. cancel=newJButton("取消");
  4. textName=newJTextField(8);
  5. textName.setText("匿名");
  6. confirm.addActionListener(this);
  7. cancel.addActionListener(this);setLayout(newjava.awt.GridLayout(2,1));
  8. label=newJLabel("输入您的大名看是否可上榜");add(label);JPanel p=newJPanel();
  9. p.add(textName);
  10. p.add(confirm);
  11. 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();
  12. rd.setTable(grade);boolean boo= rd.addRecord(name,time);if(boo){JOptionPane.showMessageDialog
  13. (null,"恭喜您,上榜了","消息框",JOptionPane.WARNING_MESSAGE);}else{JOptionPane.showMessageDialog
  14. (null,"成绩不能上榜","消息框",JOptionPane.WARNING_MESSAGE);}}}

ShowRecord类

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

四、代码测试

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

  1. 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++){
  2. block[i][j]=newBlock();}}LayMines layMines =newLayMines();//布雷者PeopleScoutMine peopleScoutMine =newPeopleScoutMine();//扫雷者
  3. layMines.layMinesForBlock(block,10);//在雷区布雷System.out.println("雷区情况:");intputShow(block);
  4. 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;}
  5. 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编程实现三种等级的扫雷游戏(完整版)”的评论:

还没有评论