易码技术论坛

 找回密码
 加入易码
搜索
查看: 509992|回复: 14

[归档] [源代码]扫雷之JAVA版~

[复制链接]
发表于 2006-7-25 07:55:00 | 显示全部楼层
呱叽呱叽,你在哪儿上班呢?
 楼主| 发表于 2006-7-25 08:58:00 | 显示全部楼层
呵呵,北京海淀区,一家小公司...

应该说体验工作比较恰当,再过一个多月又要回去读书鸟~
发表于 2006-7-28 10:52:36 | 显示全部楼层
左右键同时点击的事件到底是哪个???
……
 楼主| 发表于 2006-7-28 11:11:16 | 显示全部楼层
晕,上面帖子中的代码里面还没实现左右键的事件啊~
不过我电脑上的已经弄好了,你想要过会用QQ发给你
ps:你的c#版的扫雷弄好没?
发表于 2006-7-28 13:39:12 | 显示全部楼层
冒的时间弄得……我们又不是做游戏的……
发表于 2006-7-28 13:40:53 | 显示全部楼层
主要还是没有什么好的实现想法,初期设计本来就不好,繁琐活不好做。实力不行啊
发表于 2006-7-28 19:43:09 | 显示全部楼层
呵呵,真的很佩服老E,贴了那么多的C程序的愿代码!!
谢谢了!!!
发表于 2006-8-17 21:22:52 | 显示全部楼层
扫雷之jar2exe版

赫赫,没事干,逛时发现的,就转了一下(LZ莫怪)!!

winmine.rar

41 KB, 下载次数: 1376

扫雷之jar2exe版

j2e_free.rar

487 KB, 下载次数: 1373

jar2exe实现工具

 楼主| 发表于 2006-8-17 21:39:12 | 显示全部楼层
呵呵,多谢ls帮忙转换啦~

ps:不过那个jar2exe工具并非真正把jar文件转换为exe文件,只是包装了一下而已.
电脑上还是得装好JRE才能运行.
发表于 2006-8-17 23:00:00 | 显示全部楼层
没办法,我也发现了这个问题(在新装的WinXPSP2(白机) 上不能运行),不过,那工具的官方网站上那么说的(可能是我看错了),只好把那转换工具也附上!
 楼主| 发表于 2006-8-18 08:21:33 | 显示全部楼层
呵呵,那应该是你看错了,因为我也是看了官方网站得说明才知道它不是一个真正意义的JAR2exe [s:2]
发表于 2006-8-18 14:08:27 | 显示全部楼层
赫赫!!
我只看了标题!!
发表于 2006-8-18 17:55:46 | 显示全部楼层
做个K-JAVA版我比较喜欢...
 楼主| 发表于 2006-8-18 18:00:28 | 显示全部楼层
呵呵,正在看J2ME方面的东东,不过每天只能抽出一个多小时来看...
估计也会拿这个游戏来练练手...其实还有另一个构思....
哈哈,还是不放空气了,等有空做出来再说.
 楼主| 发表于 2006-7-24 13:57:48 | 显示全部楼层 |阅读模式
这是我写的第一个JAVA GUI程序,前两天乘着周末不用上班看了下JAVA的Swing编程,顺便写了个扫雷游戏练练手~.

代码很简单,因为是第一次弄Swing编程,估计有些地方处理的不好.

发贴只是为了纪念一下第一个GUI程序^_^



  1. /**
  2. * Created by IntelliJ IDEA.
  3. * User: Eastsun
  4. * Date: 2006-7-23
  5. * Time: 17:30:16
  6. * To change this template use File | Settings | File Templates.
  7. */
  8. package mine;
  9. import java.util.*;
  10. import javax.swing.*;
  11. import java.awt.*;
  12. import java.awt.event.*;
  13. import javax.swing.border.*;
  14. import java.net.*;

  15. class Timer implements Runnable{
  16.     private Thread thread;
  17.     private JLabel label;
  18.     long startTime;

  19.     public void startTimer(){//开始计时
  20.         startTime = System.currentTimeMillis();
  21.         thread = new Thread(this);
  22.         thread.start();
  23.     }

  24.     private static String formatStr(long n){
  25.         if(n < 10) return "00" + n;
  26.         else if(n < 100) return "0" + n;
  27.         else if(n < 1000) return String.valueOf(n);
  28.         else return "...";
  29.     }

  30.     public void stopTimer(){//停止计时
  31.         thread = null;
  32.     }

  33.     public void run(){
  34.         while(thread != null){
  35.             long curTime = System.currentTimeMillis();
  36.             label.setText(formatStr((curTime - startTime) / 1000));
  37.             try{
  38.                 thread.sleep(500);
  39.             } catch(Exception e){

  40.             }
  41.         }
  42.     }

  43.     public Timer(JLabel label){
  44.         this.label = label;
  45.     }
  46. }


  47. public class Mine extends JFrame{
  48.     private static final int STATUS_M = 1, STATUS_O = 2, STATUS_F = 4;
  49.     private static final int BUTTON_HGAP = 2, BUTTON_VGAP = 2, BUTTON_WIDTH = 30, BUTTON_HEIGHT = 30;
  50.     private static final int EASY_LEVEL = 0, NORMAL_LEVEL = 1, HARD_LEVEL = 2, DEFINE_LEVEL = 3;

  51.     private Icon numIcon[];
  52.     private Icon mineIcon;
  53.     private Icon smileIcon, laughIcon, cryIcon;
  54.     private Block[][] blocks = null;
  55.     private int height, width, mine, level, mark, remainder;    //mark用来计数:打开的和标记的总数,当等于height*width时游戏获胜  ,remainder;剩余的雷
  56.     private JPanel panel = null, bar = null;
  57.     private JLabel left, mid, right;
  58.     private Timer timer;
  59.     private boolean flags = false;    //是否已经开始游戏

  60.     public Mine(){
  61.         super("扫雷");
  62.         initSource();
  63.         setJMenuBar(getMenu());
  64.         setLevel(NORMAL_LEVEL);
  65.         refresh();
  66.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  67.         setResizable(false);
  68.         setVisible(true);
  69.     }
  70.     public Icon getIcon(String filename){
  71.         URLClassLoader urlLoader = (URLClassLoader)this.getClass().getClassLoader();
  72.         URL url = null;
  73.         Image image = null;
  74.         url = urlLoader.findResource(filename);
  75.         image = Toolkit.getDefaultToolkit().getImage(url);
  76.         MediaTracker mediatracker = new MediaTracker(this);
  77.         try {
  78.            mediatracker.addImage(image, 0);
  79.            mediatracker.waitForID(0);
  80.         }
  81.         catch (InterruptedException _ex) {
  82.            image = null;
  83.         }
  84.         if (mediatracker.isErrorID(0)) {
  85.            image = null;
  86.         }

  87.         return new ImageIcon(image);
  88.     }
  89.     private void initSource(){
  90.         numIcon = new Icon[9];
  91.         for(int n = 0; n <= 8; n++) numIcon[n] = getIcon("image/"+n + "x.jpg");
  92.         mineIcon = getIcon("image/mine.jpg");
  93.         smileIcon = getIcon("image/smile.gif");
  94.         laughIcon = getIcon("image/laugh.gif");
  95.         cryIcon = getIcon("image/cry.gif");
  96.         
  97.         left = new JLabel();
  98.         mid = new JLabel();
  99.         right = new JLabel();
  100.         timer = new Timer(right);
  101.     }

  102.     private JMenuBar getMenu(){
  103.         //设置菜单,只需在构造函数中调用一次
  104.         JMenuBar menuBar = new JMenuBar();

  105.         JMenu gameMenu = new JMenu("游 戏");
  106.         menuBar.add(gameMenu);

  107.         JMenuItem newGame = new JMenuItem("开 局");
  108.         gameMenu.add(newGame);

  109.         newGame.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, ActionEvent.ALT_MASK));
  110.         newGame.addActionListener(new ActionListener(){
  111.             public void actionPerformed(ActionEvent e){
  112.                 refresh();
  113.             }
  114.         });
  115.         gameMenu.addSeparator();

  116.         ButtonGroup levelGroup = new ButtonGroup();

  117.         JMenuItem easyLevel = new JRadioButtonMenuItem("初 级");
  118.         levelGroup.add(easyLevel);
  119.         gameMenu.add(easyLevel);
  120.         easyLevel.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
  121.         easyLevel.addActionListener(new ActionListener(){
  122.             public void actionPerformed(ActionEvent e){
  123.                 setLevel(EASY_LEVEL);
  124.                 refresh();
  125.             }
  126.         });
  127.         JMenuItem normalLevel = new JRadioButtonMenuItem("中 级");
  128.         levelGroup.add(normalLevel);
  129.         gameMenu.add(normalLevel);
  130.         normalLevel.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));
  131.         normalLevel.addActionListener(new ActionListener(){
  132.             public void actionPerformed(ActionEvent e){
  133.                 setLevel(NORMAL_LEVEL);
  134.                 refresh();
  135.             }
  136.         });
  137.         JMenuItem hardLevel = new JRadioButtonMenuItem("高 级");
  138.         levelGroup.add(hardLevel);
  139.         gameMenu.add(hardLevel);
  140.         hardLevel.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_3, ActionEvent.ALT_MASK));
  141.         hardLevel.addActionListener(new ActionListener(){
  142.             public void actionPerformed(ActionEvent e){
  143.                 setLevel(HARD_LEVEL);
  144.                 refresh();
  145.             }
  146.         });

  147.         normalLevel.setSelected(true);

  148.         gameMenu.addSeparator();
  149.         JMenuItem exitItem = new JMenuItem("退 出");
  150.         gameMenu.add(exitItem);
  151.         exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.ALT_MASK));
  152.         exitItem.addActionListener(new ActionListener(){
  153.             public void actionPerformed(ActionEvent e){
  154.                 System.exit(0);
  155.             }
  156.         });

  157.         JMenu aboutMenu = new JMenu("关 于");
  158.         menuBar.add(aboutMenu);
  159.         JMenuItem aboutItem = new JMenuItem("About");
  160.         aboutMenu.add(aboutItem);
  161.         aboutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.ALT_MASK));
  162.         aboutItem.addActionListener(new ActionListener(){
  163.             public void actionPerformed(ActionEvent e){
  164.                 JOptionPane.showMessageDialog(null, "             By Eastsun", "About", JOptionPane.INFORMATION_MESSAGE);
  165.             }
  166.         });

  167.         return menuBar;
  168.     }

  169.     private void fail(){
  170.         timer.stopTimer();
  171.         mid.setIcon(cryIcon);
  172.         for(int h = 0; h < height; h++)
  173.             for(int w = 0; w < width; w++){
  174.                 blocks[h][w].setOpen();
  175.                 if(blocks[h][w].isMine()) blocks[h][w].setIcon(mineIcon);
  176.             }
  177.         JOptionPane.showMessageDialog(null, "             你挂了!", "lost", JOptionPane.INFORMATION_MESSAGE);
  178.     }

  179.     private void win(){
  180.         timer.stopTimer();
  181.         mid.setIcon(laughIcon);
  182.         JOptionPane.showMessageDialog(null, "             你赢了!", "win", JOptionPane.INFORMATION_MESSAGE);
  183.     }

  184.     private void generateGame(int x, int y){
  185.         //生成一地图,并保证blocks[y][x]处无雷
  186.         Random rand = new Random();
  187.         int[]  num = new int[width * height];
  188.         for(int n = 0; n < height * width; n++) num[n] = n;
  189.         for(int n = 0; n < mine; n++){
  190.             int r = Math.abs(rand.nextInt());
  191.             int p = r % (height * width - n) + n;
  192.             blocks[num[p] / width][num[p] % width].setMine();
  193.             num[p] = num[n];
  194.         }
  195.         while(blocks[y][x].isMine()){
  196.             int r = Math.abs(rand.nextInt()) % (width * height);
  197.             int h = r / width, w = r % width;
  198.             if(!blocks[h][w].isMine()){
  199.                 blocks[h][w].setMine();
  200.                 blocks[y][x].clear();
  201.                 break;
  202.             }
  203.         }
  204.         return;
  205.     }

  206.     private void setLevel(int l){  //设置
  207.         switch(l){
  208.             case EASY_LEVEL:
  209.                 height = 9;
  210.                 width = 9;
  211.                 mine = 10;
  212.                 level = EASY_LEVEL;
  213.                 break;
  214.             case NORMAL_LEVEL:
  215.                 height = 12;
  216.                 width = 15;
  217.                 mine = 40;
  218.                 level = NORMAL_LEVEL;
  219.                 break;
  220.             case HARD_LEVEL:
  221.                 height = 19;
  222.                 width = 29;
  223.                 mine = 100;
  224.                 level = HARD_LEVEL;
  225.                 break;
  226.         }
  227.         return;
  228.     }

  229.     private void refresh(){//刷新游戏,在setLevel或newGame之后执行
  230.         if(blocks == null||blocks.length != height||blocks[0].length != width){
  231.             //第一次游戏或游戏设置已经改变
  232.             if(panel != null){
  233.                 this.getContentPane().remove(panel);
  234.                 this.getContentPane().remove(bar);
  235.             }
  236.             panel = new JPanel(new GridLayout(height, width, BUTTON_HGAP, BUTTON_VGAP));
  237.             panel.setPreferredSize(new Dimension(width * (BUTTON_WIDTH + BUTTON_HGAP) - BUTTON_HGAP, height * (BUTTON_HEIGHT + BUTTON_VGAP) - BUTTON_VGAP));
  238.             blocks = new Block[height][width];
  239.             for(int h = 0; h < height; h++)
  240.                 for(int w = 0; w < width; w++){
  241.                     blocks[h][w] = new Block(w, h);
  242.                     panel.add(blocks[h][w]);
  243.                 }
  244.             panel.setBorder(BorderFactory.createEmptyBorder(0, 6, 6, 6));

  245.             bar = new JPanel(new GridLayout(1, 3));
  246.             bar.setPreferredSize(new Dimension(width * (BUTTON_WIDTH + BUTTON_HGAP) - BUTTON_HGAP, 30));
  247.             bar.setBorder(BorderFactory.createEmptyBorder(6, 6, 0, 6));
  248.             bar.add(left, BorderLayout.WEST);
  249.             bar.add(mid, BorderLayout.CENTER);
  250.             bar.add(right, BorderLayout.EAST);
  251.             this.getContentPane().setPreferredSize(new Dimension(width * (BUTTON_WIDTH + BUTTON_HGAP) - BUTTON_HGAP + 12, height * (BUTTON_HEIGHT + BUTTON_VGAP) - BUTTON_VGAP + 42));
  252.             this.getContentPane().add(bar, BorderLayout.NORTH);
  253.             this.getContentPane().add(panel, BorderLayout.SOUTH);
  254.             this.pack();
  255.         } else{
  256.             for(int h = 0; h < height; h++)
  257.                 for(int w = 0; w < width; w++)
  258.                     blocks[h][w].clear();
  259.         }

  260.         left.setText(String.valueOf(mine));
  261.         left.setHorizontalAlignment(JLabel.CENTER);
  262.         mid.setIcon(smileIcon);
  263.         mid.setHorizontalAlignment(JLabel.CENTER);
  264.         right.setText("000");
  265.         right.setHorizontalAlignment(JLabel.CENTER);

  266.         timer.stopTimer();

  267.         flags = false;
  268.         mark = 0;
  269.         remainder = mine;
  270.     }

  271.     class Block extends JButton implements MouseListener{
  272.         private int x, y, status;

  273.         public void mouseClicked(MouseEvent e){
  274.             if(isOpen()) return;
  275.             if(!flags){
  276.                 generateGame(x, y);
  277.                 timer.startTimer();
  278.                 flags = true;
  279.             }
  280.             if(SwingUtilities.isLeftMouseButton(e)){
  281.                 if((status&STATUS_F) != 0) return;
  282.                 if(isMine()){
  283.                     fail();
  284.                     //refresh();
  285.                 } else open();
  286.             } else{//right
  287.                 if(isFlag()){
  288.                     this.setBackground(Color.ORANGE);
  289.                     status = (status^STATUS_F);
  290.                     mark --;
  291.                     remainder ++;
  292.                 } else{
  293.                     this.setBackground(Color.RED);
  294.                     status = (status|STATUS_F);
  295.                     mark ++;
  296.                     remainder --;
  297.                 }
  298.                 left.setText(String.valueOf(remainder));
  299.             }
  300.             if(mark == height * width) win();
  301.         }

  302.         public void mousePressed(MouseEvent e){
  303.         }

  304.         public void mouseEntered(MouseEvent e){
  305.         }

  306.         public void mouseExited(MouseEvent e){
  307.         }

  308.         public void mouseReleased(MouseEvent e){
  309.         }

  310.         public Block(int x, int y){
  311.             this.x = x;
  312.             this.y = y;
  313.             this.status = 0;
  314.             this.addMouseListener(this);
  315.             this.setBackground(Color.ORANGE);
  316.         }

  317.         public boolean isMine(){
  318.             return ((status&STATUS_M) != 0);
  319.         }

  320.         public int getNum(){
  321.             int count = 0;
  322.             if(x > 0){
  323.                 if(y > 0&&blocks[y - 1][x - 1].isMine()) count++;
  324.                 if(blocks[y][x - 1].isMine()) count++;
  325.                 if(y + 1 < height&&blocks[y + 1][x - 1].isMine()) count++;
  326.             }
  327.             if(y > 0&&blocks[y - 1][x].isMine()) count++;
  328.             if(y + 1 < height&&blocks[y + 1][x].isMine()) count++;
  329.             if(x + 1 < width){
  330.                 if(y > 0&&blocks[y - 1][x + 1].isMine()) count++;
  331.                 if(blocks[y][x + 1].isMine()) count++;
  332.                 if(y + 1 < height&&blocks[y + 1][x + 1].isMine()) count++;
  333.             }
  334.             return count;
  335.         }

  336.         public void clear(){
  337.             status = 0;
  338.             this.setIcon(null);
  339.             this.setBackground(Color.ORANGE);
  340.         }

  341.         public void setMine(){
  342.             status = STATUS_M;
  343.         }

  344.         public boolean isFlag(){
  345.             return ((status&STATUS_F) != 0);
  346.         }

  347.         public void setOpen(){   //设置为打开状态
  348.             status = (status|STATUS_O);
  349.             setIcon(numIcon[getNum()]);
  350.         }

  351.         public boolean isOpen(){
  352.             return ((status&STATUS_O) != 0);
  353.         }

  354.         private void open(){
  355.             if(isOpen()||isFlag()) return;
  356.             setOpen();
  357.             mark ++;
  358.             if(getNum() == 0){
  359.                 if(x > 0){
  360.                     blocks[y][x - 1].open();
  361.                     if(y > 0) blocks[y - 1][x - 1].open();
  362.                     if(y + 1 < height) blocks[y + 1][x - 1].open();
  363.                 }
  364.                 if(y > 0) blocks[y - 1][x].open();
  365.                 if(y + 1 < height) blocks[y + 1][x].open();
  366.                 if(x + 1 < width){
  367.                     blocks[y][x + 1].open();
  368.                     if(y > 0) blocks[y - 1][x + 1].open();
  369.                     if(y + 1 < height) blocks[y + 1][x + 1].open();
  370.                 }
  371.             }
  372.         }
  373.     }

  374.     public static void main(String[] args){
  375.         new Mine();
  376.     }
  377. }
复制代码


附1:程序的源代码及资源


附2:可以直接点击运行的jar程序(需要装JAVA运行时环境)
您需要登录后才可以回帖 登录 | 加入易码

本版积分规则

Archiver|手机版|小黑屋|EMAX Studio

GMT+8, 2024-4-19 03:22 , Processed in 0.014386 second(s), 21 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表