Java小游戏之扫雷

Post by zerob13

前几天看到室长在做c#的作业,貌似是写一个扫雷。。。于是当时就想用java也写写看。。。
发现java的可视化真不是一般的难过。。。
不过,磕磕碰碰的还是写出来了,虽然样子比较挫,但是还是可以玩的
9*9 9个雷~
下面上代码~

/* * To change this template, choose Tools | Templates * and open the template in the editor. / /* * * @author Zerob13 / import java.awt.; import java.awt.event.; import javax.swing.; class Mineb extends JButton { private boolean ismine; int x, y; int num; public boolean isMine() { return ismine; } public void setMine(boolean a) { ismine = a; } Mineb(String a, int i, int j) { super.setText(a); x = i; y = j; ismine = false; } } public class Deom extends JFrame implements MouseListener { /** * @param args */ Mineb[][] b; JLabel state; int last; JMenuBar mb; int elas; class node { int x, y; } int[][] dir = { { 0, 1 }, { 1, 0 }, { 1, 1 }, { -1, 1 }, { -1, -1 }, { 1, -1 }, { -1, 0 }, { 0, -1 } }; private void gen() { int co = 0; elas = 81 - 9; last = 9; int[] hash = new int[81]; for (int i = 0; i = 9) x = 8; if (y >= 9) y = 8; b[x][y].setMine(true); } } for (int i = 0; i = 9 || z >= 9) continue; if (b[w][z].isMine()) { k++; } } return k; } void showall() { int i, j; for (i = 0; i top) { head = queue[top]; top++; for (i = 0; i = 9 || z >= 9) continue; if(b[w][z].getText()!="") continue; if (b[w][z].isMine()) continue; if (hash[w][z]) continue; hash[w][z] = true; if (b[w][z].num == 0) { queue[tail] = new node(); queue[tail].x = w; queue[tail].y = z; tail++; if (b[w][z].isEnabled()) { b[w][z].setEnabled(false); elas--; } b[w][z].setText("-"); } } } if (elas == 0) { showall(); JOptionPane.showMessageDialog(null, "YOU WIN!", "YOU WIN!", JOptionPane.DEFAULT_OPTION); state.setText("WIN"); } } @Override public void mouseClicked(MouseEvent e) { Mineb temp = (Mineb) e.getSource(); if (e.getButton() == MouseEvent.BUTTON1) { if (temp.getText() == "P" || temp.getText() == "?") { } else { if (temp.isMine()) { showall(); state.setText("LOSE"); JOptionPane.showMessageDialog(null, "YOU LOSE!", "YOU LOSE!", JOptionPane.DEFAULT_OPTION); } else { if (temp.num == 0) { showsame(temp.x, temp.y); temp.setText("-"); } else { temp.setText(String.valueOf(temp.num)); } } if (temp.isEnabled()) { temp.setEnabled(false); elas--; } if (elas == 0) { showall(); state.setText("WIN"); JOptionPane.showMessageDialog(null, "YOU WIN!", "YOU WIN!", JOptionPane.DEFAULT_OPTION); } } } else if (e.getButton() == MouseEvent.BUTTON3) { if (temp.getText() == "P" || this.last == 0) { if (temp.getText() == "P") this.last++; temp.setText("?"); } else if (temp.getText() == "?") { temp.setText(""); } else { temp.setText("P"); this.last--; } state.setText(String.valueOf(this.last)); } } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } }