package 扫雷小游戏; import javax.swing.JOptionPane; public class miner { public static void main(String aString[]) { int run=0; do{ int option = JOptionPane.showConfirmDialog(null, "要使用全屏模式吗? "); if (option==0) { fullscreenframe gameFullscreenframe = new fullscreenframe(23,43); gameFullscreenframe.setVisible(true); run++; } else if (option==1) { windownedframe gameWindownedframe = new windownedframe(12,18); gameWindownedframe.setVisible(true); run++; } else { if (0==JOptionPane.showConfirmDialog(null, "确认要退出吗?",null, JOptionPane.YES_OPTION)) { System.exit(0); } } }while(run==0); } } package 扫雷小游戏; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import java.awt.MediaTracker; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import javax.swing.BorderFactory; import javax.swing.Icon; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class gameFrame extends JFrame implements ActionListener, MouseListener, MouseMotionListener { private static final long serialVersionUID = 1L; static int hole_rows, hole_cols; // 雷坑的行数和列数 static int image_width = 30; // 小图片的宽 static int image_heigh = 30; // 小图片的高 static int font_size = 24; // 设置雷数提示字体大小 static int menuHeigh = 20;// JMenu默认高度 static int TitleHeight = 25;// +标题栏的高度 static int extraHeigh; int max_hole_cols; int max_hole_rows; int skew_x;// 地图整体x轴偏移量 int skew_y;// 地图y轴偏移量 static int holes; // 面板上总共的坑数,即雷坑的行数乘以列数 protected static Font tipsFont = new Font("Berlin Sans FB Demi", Font.PLAIN, font_size); static int mines; // 用一个getMines()方法获取一组随即数来表示雷的位置 static int map[][]; // 存储地图的二维数组// 存储一张地图标志着每个坑的属性,即0,1,2,3,4,5,6,7,8 static int MineRows[];// 地图中雷位置的行数 static int MineCols[];// 地图中雷位置的列数 static int mask[][];// 整个地图的"面具"用来标识地图中每个坑的状态 static int Position[];// 一个以为数组用来存储总雷数个随机数来确定每个雷的位置 static Toolkit tkToolkit = Toolkit.getDefaultToolkit(); protected Image mineImage = tkToolkit.getImage("e:/image/mine.jpg"); protected Image flagImage = tkToolkit.getImage("e:/image/flag.jpg"); protected Image maskImage = tkToolkit.getImage("e:/image/mask.jpg"); protected Image boundImage = tkToolkit.getImage("e:/image/bound.png"); protected Image windowIcon = tkToolkit.getImage("E:/image/ico.png"); protected Image loserIcon = tkToolkit.getImage("E:/image/loser.png"); protected Image winrIcon = tkToolkit.getImage("E:/image/winer1.png"); public static final Dimension screenSize = tkToolkit.getScreenSize(); static Color fontColor[] = { Color.green, Color.yellow, Color.red, Color.blue, Color.cyan, Color.black, Color.gray }; public gameFrame(String title, int InitHoleRows, int initHoleCols) { hole_cols = initHoleCols; hole_rows = InitHoleRows; holes = hole_cols * hole_rows; mines = getMines(); mask = new int[hole_rows + 2][hole_cols + 2];// 同map一样边界全部赋给1用来区分以免数组访问越界 map = makeMineList(hole_rows, hole_cols); setDefaultCloseOperation(EXIT_ON_CLOSE); setIconImage(windowIcon); MenuBar myBar = new MenuBar(); Menu settingMenu = new Menu("设置"); MenuItem viewItem = new MenuItem("设置难度"); viewItem.addActionListener(this); MenuItem exitiItem = new MenuItem("退出"); exitiItem.addActionListener(this); settingMenu.add(exitiItem); settingMenu.add(viewItem); myBar.add(settingMenu); setMenuBar(myBar); MediaTracker mytMediaTracker = new MediaTracker(this); mytMediaTracker.addImage(mineImage, 1); mytMediaTracker.addImage(flagImage, 2); mytMediaTracker.addImage(maskImage, 3); mytMediaTracker.addImage(boundImage, 4); mytMediaTracker.addImage(loserIcon, 5); mytMediaTracker.addImage(winrIcon, 6); addMouseListener(this); try { mytMediaTracker.waitForAll(); } catch (InterruptedException e) { e.printStackTrace(); } JPanel buttonPanel = new JPanel() { private static final long serialVersionUID = 1L; protected void paintComponent(Graphics g) { int i, j; g.setFont(tipsFont); for (j = 0; j < hole_rows + 2; j++) for (i = 0; i < hole_cols + 2; i++) { { if (mask[j][i] == 1) { if (map[j][i] > 0 && map[j][i] < 9) { g.setColor(fontColor[map[j][i] % 7]); g.drawString(Integer.toString(map[j][i]), i * image_width + image_width / 4 + skew_x, (j + 1) * image_heigh - 4 + skew_y); } else if (map[j][i] == -1) { g.drawImage(mineImage, i * 30 + skew_x, j * 30 + skew_y, null); } else if (map[j][i] > 10) { g.drawImage(boundImage, i * 30 + skew_x, j * 30 + skew_y, null); } else { g.setColor(Color.gray); g.fillRect(i * 30 + skew_x, j * 30 + skew_y, 30, 30); } } else if (mask[j][i] == 2) { g.drawImage(flagImage, i * 30 + skew_x, j * 30 + skew_y, null); } else if (mask[j][i] == 0) { g.drawImage(maskImage, i * 30 + skew_x, j * 30 + skew_y, null); } } } } }; buttonPanel.setBorder(BorderFactory.createRaisedBevelBorder()); getContentPane().add(buttonPanel); } protected static int getMines() { int nums = 10; try { nums = Integer.parseInt(JOptionPane.showInputDialog("请输入总雷数:")); } catch (NumberFormatException e) { nums = 10; } if (nums <= holes && nums > 0) return nums; else { JOptionPane.showMessageDialog(null, "超过限制,将默认设置为1"); return 1; } } protected int RandInt(int max) { return (int) Math.ceil((Math.random() * max)); } private int RandInt(int min, int max) { return min < max ? RandInt(max - min) + min : RandInt(min - max) + max; } private int Max(int a, int b) { return a > b ? a : b; } private void MinePosition() { MineRows = new int[mines]; MineCols = new int[mines]; Position = new int[mines]; System.out.println("mines=" + mines + "/nholes=" + holes); int tmprand = RandInt(Max(hole_cols, hole_rows), holes + 1); int findmines = 0; Position[findmines] = tmprand; MineRows[findmines] = tmprand / hole_cols; MineCols[findmines] = tmprand / hole_rows; System.out.println(findmines + " " + tmprand + "/tR=" + (MineRows[findmines])); findmines++; while (findmines < mines) { tmprand = RandInt(Max(hole_cols, hole_rows), holes + 1); MineRows[findmines] = tmprand / hole_cols; System.out.println(findmines + " " + tmprand + "/tR=" + (MineRows[findmines - 1])); findmines++; } findmines = 1; System.out.println("0 " + tmprand + "/tR=" + (MineRows[findmines - 1]) + "/tC=" + (MineCols[findmines - 1])); while (findmines < mines) { tmprand = RandInt(Max(hole_cols, hole_rows), holes + 1); if (!isOf(tmprand, MineRows, MineCols, findmines)) { Position[findmines] = tmprand; MineCols[findmines] = tmprand / hole_rows; System.out.println(findmines + " " + tmprand + "/tR=" + (MineRows[findmines]) + "/tC=" + (MineCols[findmines])); findmines++; } } } private boolean isOf(int x, int Rarray[], int Carray[], int Count) { for (int i = 0; i < Count; i++) { if (x / hole_cols == Rarray[i] && x / hole_rows == Carray[i]) return true; } return false; } protected int[][] makeMineList(int rows, int cols) { int MineMap[][] = new int[rows + 2][cols + 2]; System.out.println("Begin!"); MinePosition(); // 第一次遍历,生成雷的位置,将地图数组map[][]中的雷的位置赋-1 for (int j = 1; j <= rows; j++) // 初始化数组使其除边界外所有元素为0 for (int j2 = 1; j2 <= cols; j2++) { MineMap[j][j2] = 0; mask[j][j2] = 0; } // 边界全部赋给88以区分 for (int i = 0; i < rows + 2; i++) { MineMap[i][0] = 88; MineMap[i][cols + 1] = 88; mask[i][0] = 1; mask[i][cols + 1] = 1; } for (int i = 0; i < cols + 2; i++) { MineMap[0][i] = 88; MineMap[rows + 1][i] = 88; mask[0][i] = 1; mask[rows + 1][i] = 1; } for (int i = 0; i < mines; i++) { MineMap[MineRows[i]][MineCols[i]] = -1; } // 第二次遍历,按照雷的位置付给四周坑相应的值0,1,2,3,4,5,6,7,8 for (int i = 1; i < rows + 1; i++) for (int j = 1; j < cols + 1; j++) { if (MineMap[i][j] != -1) for (int k = -1; k < 2; k++) for (int m = -1; m < 2; m++) if (MineMap[i + k][j + m] == -1) MineMap[i][j]++; } return MineMap; } protected void updateMask(int row, int col) { if (mask[row][col] == 0) { if (map[row][col] == -1) mineBomb(); else if (map[row][col] >= 1 && map[row][col] <= 8) { mask[row][col] = 1; repaint(); return; } else if (map[row][col] == 0) { mask[row][col] = 1; for (int i = -1; i < 2; i++) for (int j = -1; j < 2; j++) updateMask(row + i, col + j); } else { return; } } else { return; } } private void mineBomb() { for (int i = 0; i < mines; i++) { if (mask[MineRows[i]][MineCols[i]] == 0) { mask[MineRows[i]][MineCols[i]] = 1; } repaint(); } Icon aIcon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { g.drawImage(loserIcon, 0, 0, null); } @Override public int getIconWidth() { return 64; } @Override public int getIconHeight() { return 64; } }; JOptionPane.showMessageDialog(null, "比赛结束了???", "我早就知道你会输了!", JOptionPane.PLAIN_MESSAGE, aIcon); if (JOptionPane.showConfirmDialog(null, "快回家吃饭吧!你太差了!", "黄毛小子", JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE, aIcon) == 1) { map = makeMineList(hole_rows, hole_cols); repaint(); } else { System.exit(0); } } @Override public void mouseClicked(MouseEvent e) { int mouseRow = (e.getY() - extraHeigh - skew_y) / image_heigh; int mouseCol = (e.getX() - skew_x) / image_width; System.out.print("Size=" + getSize().height + "w=" + getSize().width); if (e.getButton() == 1) { System.out.println("x=" + e.getX() + "y=" + e.getY()); if (mouseRow <= hole_rows && mouseCol <= hole_cols) updateMask(mouseRow, mouseCol); else ; } else { System.out.println(mask[mouseRow][mouseRow]); if (true) { mask[mouseRow][mouseCol] = 2; repaint(); int findCont = 0; for (int i = 0; i < mines; i++) { if (mask[MineRows[i]][MineCols[i]] == 2) findCont++; } if (findCont == mines) { Icon aIcon = new Icon() { @Override public void paintIcon(Component c, Graphics g, int x, int y) { g.drawImage(winrIcon, 0, 0, null); } @Override public int getIconWidth() { return 64; } @Override public int getIconHeight() { return 64; } }; JOptionPane.showMessageDialog(null, "这是热身赛!!!", "我刚才是失误!", JOptionPane.PLAIN_MESSAGE, aIcon); if (JOptionPane.showConfirmDialog(null, "你刚才肯定作弊了!敢再来一次吗?", "小样儿", JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE, aIcon) == 0) { map = makeMineList(hole_rows, hole_cols); repaint(); } else { System.exit(0); } } } } repaint(); } @Override public void mousePressed(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } @Override public void actionPerformed(ActionEvent e) { String comString = e.getActionCommand(); if (comString.equals("设置难度")) { mines = getMines(); map = makeMineList(hole_rows, hole_cols); repaint(); } else { System.exit(0); } } @Override public void mouseDragged(MouseEvent e) { } @Override public void mouseMoved(MouseEvent e) { } } 'package 扫雷小游戏; import java.awt.Color; import java.awt.event.ActionEvent; public class fullscreenframe extends gameFrame { private static final long serialVersionUID = 1L; public void actionPerformed(ActionEvent e) { String comString = e.getActionCommand(); if (comString.equals("设置难度")) { mines = getMines(); map = makeMineList(hole_rows, hole_cols); repaint(); } else { System.exit(0); } } public fullscreenframe(int rows, int cols) { super("全屏模式", rows, cols); extraHeigh = menuHeigh; max_hole_cols = 43; max_hole_rows = 23; skew_x = 8 + (max_hole_cols - hole_cols) / 2 * image_width; skew_y = (max_hole_rows - hole_rows) / 2 * image_heigh; // skew_x = 8; // skew_y = 0; setBackground(Color.gray); setLocation(0, 0); setUndecorated(true); setSize(screenSize); } } package 扫雷小游戏; import java.awt.Color; import java.awt.event.ActionEvent; public class windownedframe extends gameFrame { private static final long serialVersionUID = 1L; public windownedframe(int rows, int cols) { super("超级扫雷", rows, cols); // mines = getMines(); map = makeMineList(hole_rows, hole_cols); max_hole_cols = 18; max_hole_rows = 12; skew_x = (max_hole_cols - hole_cols) / 2 * image_width; skew_y = (max_hole_rows - hole_rows) / 2 * image_heigh; int WindowHeith = 467; int WindowWidth = 606; extraHeigh = menuHeigh + TitleHeight; setResizable(false); setBackground(Color.gray); setSize(WindowWidth, WindowHeith); setLocation(screenSize.width / 2 - WindowWidth / 2, screenSize.height / 2 - WindowHeith / 2); } public void actionPerformed(ActionEvent e) { String comString = e.getActionCommand(); if (comString.equals("设置难度")) { mines = getMines(); map = makeMineList(hole_rows, hole_cols); repaint(); } else { System.exit(0); } } }