import java.awt.*;
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),容縣企業(yè)網(wǎng)站建設(shè),容縣品牌網(wǎng)站建設(shè),網(wǎng)站定制,容縣網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,容縣網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
import java.awt.event.*;
import javax.swing.*;
public class Frame
extends JFrame {
JTextField text;
JLabel nowBomb, setBomb;
int BombNum, BlockNum; // 當(dāng)前雷數(shù),當(dāng)前方塊數(shù)
int rightBomb, restBomb, restBlock; // 找到的地雷數(shù),剩余雷數(shù),剩余方塊數(shù)
JButton start = new JButton(" 開(kāi)始 ");
JPanel MenuPamel = new JPanel();
JPanel bombPanel = new JPanel();
Bomb[][] bombButton;
JPanel c;
BorderLayout borderLayout1 = new BorderLayout();
GridLayout gridLayout1 = new GridLayout();
public Frame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
c = (JPanel) getContentPane();
setTitle("掃雷");
c.setBackground(Color.WHITE);
MenuPamel.setBackground(Color.GRAY);
c.setLayout(borderLayout1);
setSize(new Dimension(600, 600));
setResizable(false);
BlockNum = 144;
BombNum = 10;
text = new JTextField("10 ", 3);
nowBomb = new JLabel("當(dāng)前雷數(shù)" + ":" + BombNum);
setBomb = new JLabel("設(shè)置地雷數(shù)");
start.addActionListener(new Frame1_start_actionAdapter(this));
MenuPamel.add(setBomb);
MenuPamel.add(text);
MenuPamel.add(start);
MenuPamel.add(nowBomb);
c.add(MenuPamel, java.awt.BorderLayout.SOUTH);
bombPanel.setLayout(gridLayout1);
gridLayout1.setColumns( (int) Math.sqrt(BlockNum));
gridLayout1.setRows( (int) Math.sqrt(BlockNum));
bombButton = new Bomb[ (int) Math.sqrt(BlockNum)][ (int) Math.sqrt(BlockNum)];
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
bombButton[i][j] = new Bomb(i, j);
//bombButton[i][j].setSize(10, 10);
bombButton[i][j].setFont(new Font("", Font.PLAIN, 14));//設(shè)置字體大小
bombButton[i][j].setForeground(Color.white);
bombButton[i][j].addMouseListener(new Bomb_mouseAdapter(this));
bombButton[i][j].addActionListener(new Bomb_actionAdapter(this));
bombPanel.add(bombButton[i][j]);
}
}
c.add(bombPanel, java.awt.BorderLayout.CENTER);
startBomb();
}
/* 開(kāi)始按鈕 */
public void start_actionPerformed(ActionEvent e) {
int num=Integer.parseInt(text.getText().trim());
if (num = 5 num 50) {
BombNum = num;
startBomb();
}
else if (num 5) {
JOptionPane.showMessageDialog(null, "您設(shè)置的地雷數(shù)太少了,請(qǐng)重設(shè)!", "錯(cuò)誤",
JOptionPane.ERROR_MESSAGE);
num=10;
BombNum = num;
}
else {
JOptionPane.showMessageDialog(null, "您設(shè)置的地雷數(shù)太多了,請(qǐng)重設(shè)!", "錯(cuò)誤",
JOptionPane.ERROR_MESSAGE);
num=10;
BombNum = num;
}
}
/* 開(kāi)始,布雷 */
public void startBomb() {
nowBomb.setText("當(dāng)前雷數(shù)" + ":" + BombNum);
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
bombButton[i][j].isBomb = false;
bombButton[i][j].isClicked = false;
bombButton[i][j].isRight = false;
bombButton[i][j].BombFlag = 0;
bombButton[i][j].BombRoundCount = 9;
bombButton[i][j].setEnabled(true);
bombButton[i][j].setText("");
bombButton[i][j].setFont(new Font("", Font.PLAIN, 14));//設(shè)置字體大小
bombButton[i][j].setForeground(Color.BLUE);
rightBomb = 0;
restBomb = BombNum;
restBlock = BlockNum - BombNum;
}
}
for (int i = 0; i BombNum; ) {
int x = (int) (Math.random() * (int) (Math.sqrt(BlockNum) - 1));
int y = (int) (Math.random() * (int) (Math.sqrt(BlockNum) - 1));
if (bombButton[x][y].isBomb != true) {
bombButton[x][y].isBomb = true;
i++;
}
}
CountRoundBomb();
}
/* 計(jì)算方塊周圍雷數(shù) */
public void CountRoundBomb() {
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
int count = 0;
// 當(dāng)需要檢測(cè)的單元格本身無(wú)地雷的情況下,統(tǒng)計(jì)周圍的地雷個(gè)數(shù)
if (bombButton[i][j].isBomb != true) {
for (int x = i - 1; x i + 2; x++) {
for (int y = j - 1; y j + 2; y++) {
if ( (x = 0) (y = 0)
(x ( (int) Math.sqrt(BlockNum)))
(y ( (int) Math.sqrt(BlockNum)))) {
if (bombButton[x][y].isBomb == true) {
count++;
}
}
}
}
bombButton[i][j].BombRoundCount = count;
}
}
}
}
/* 是否挖完了所有的雷 */
public void isWin() {
restBlock = BlockNum - BombNum;
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
if (bombButton[i][j].isClicked == true) {
restBlock--;
}
}
}
if (rightBomb == BombNum || restBlock == 0) {
JOptionPane.showMessageDialog(this, "您挖完了所有的雷,您勝利了!", "勝利",
JOptionPane.INFORMATION_MESSAGE);
startBomb();
}
}
/** 當(dāng)選中的位置為空,則翻開(kāi)周圍的地圖* */
public void isNull(Bomb ClickedButton) {
int i, j;
i = ClickedButton.num_x;
j = ClickedButton.num_y;
for (int x = i - 1; x i + 2; x++) {
for (int y = j - 1; y j + 2; y++) {
if ( ( (x != i) || (y != j)) (x = 0) (y = 0)
(x ( (int) Math.sqrt(BlockNum)))
(y ( (int) Math.sqrt(BlockNum)))) {
if (bombButton[x][y].isBomb == false
bombButton[x][y].isClicked == false
bombButton[x][y].isRight == false) {
turn(bombButton[x][y]);
}
}
}
}
}
/* 翻開(kāi) */
public void turn(Bomb ClickedButton) {
ClickedButton.setEnabled(false);
ClickedButton.isClicked = true;
if (ClickedButton.BombRoundCount 0) {
ClickedButton.setText(ClickedButton.BombRoundCount + "");
}
else {
isNull(ClickedButton);
}
}
/* 左鍵點(diǎn)擊 */
public void actionPerformed(ActionEvent e) {
if ( ( (Bomb) e.getSource()).isClicked == false
( (Bomb) e.getSource()).isRight == false) {
if ( ( (Bomb) e.getSource()).isBomb == false) {
turn( ( (Bomb) e.getSource()));
isWin();
}
else {
for (int i = 0; i (int) Math.sqrt(BlockNum); i++) {
for (int j = 0; j (int) Math.sqrt(BlockNum); j++) {
if (bombButton[i][j].isBomb == true) {
bombButton[i][j].setText("b");
}
}
}
( (Bomb) e.getSource()).setForeground(Color.RED);
( (Bomb) e.getSource()).setFont(new Font("", Font.BOLD, 20));
( (Bomb) e.getSource()).setText("X");
JOptionPane.showMessageDialog(this, "你踩到地雷了,按確定重來(lái)", "踩到地雷", 2);
startBomb();
}
}
}
/* 右鍵點(diǎn)擊 */
public void mouseClicked(MouseEvent e) {
Bomb bombSource = (Bomb) e.getSource();
boolean right = SwingUtilities.isRightMouseButton(e);
if ( (right == true) (bombSource.isClicked == false)) {
bombSource.BombFlag = (bombSource.BombFlag + 1) % 3;
if (bombSource.BombFlag == 1) {
if (restBomb 0) {
bombSource.setForeground(Color.RED);
bombSource.setText("F");
bombSource.isRight = true;
restBomb--;
}
else {
bombSource.BombFlag = 0;
}
}
else if (bombSource.BombFlag == 2) {
restBomb++;
bombSource.setText("Q");
bombSource.isRight = false;
}
else {
bombSource.setText("");
}
if (bombSource.isBomb == true) {
if (bombSource.BombFlag == 1) {
rightBomb++;
}
else if (bombSource.BombFlag == 2) {
rightBomb--;
}
}
nowBomb.setText("當(dāng)前雷數(shù)" + ":" + restBomb);
isWin();
}
}
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
}
}
class Frame1_start_actionAdapter
implements ActionListener {
private Frame adaptee;
Frame1_start_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.start_actionPerformed(e);
}
}
////////////////////////////
class Bomb
extends JButton {
int num_x, num_y; // 第幾號(hào)方塊
int BombRoundCount; // 周圍雷數(shù)
boolean isBomb; // 是否為雷
boolean isClicked; // 是否被點(diǎn)擊
int BombFlag; // 探雷標(biāo)記
boolean isRight; // 是否點(diǎn)擊右鍵
public Bomb(int x, int y) {
num_x = x;
num_y = y;
BombFlag = 0;
BombRoundCount = 9;
isBomb = false;
isClicked = false;
isRight = false;
}
}
class Bomb_actionAdapter
implements ActionListener {
private Frame adaptee;
Bomb_actionAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.actionPerformed(e);
}
}
class Bomb_mouseAdapter
extends MouseAdapter {
private Frame adaptee;
Bomb_mouseAdapter(Frame adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.mouseClicked(e);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main
{
public static void main(String[] argus)
{
Landmine Zhang = new Landmine();
}
}
//
// Landmine類 主界面
class Landmine extends JFrame
{
static Resources resources = new Resources();
Playing listener = new Playing(this); //主要監(jiān)聽(tīng)者,監(jiān)聽(tīng)地雷面板的動(dòng)作
Help helpListener = new Help(this); //輔助監(jiān)聽(tīng)者,監(jiān)聽(tīng)“幫助”、“關(guān)于”
JPanel landminePanel = new JPanel(); //創(chuàng)建地雷面板
JPanel topPanel = new JPanel(); //創(chuàng)建頂部面板
JPanel lowerPanel = new JPanel(); //創(chuàng)建底部面板
public static MyButton [][] lei; //主區(qū)按鈕組
public static int numberOfUnflaged ; //剩余的雷數(shù),顯示在topPanel上,用于提示用戶
public static int numberOfClicked; //已經(jīng)翻開(kāi)的格子數(shù),當(dāng)數(shù)字?jǐn)?shù)字到"總格子數(shù)—雷數(shù)"時(shí),即勝利
public static int usedTime; //已用時(shí)間
public static JLabel numberOfUnflagedLabel = new JLabel(); //創(chuàng)建剩雷數(shù)標(biāo)簽
public static JLabel timeLabel = new JLabel();//創(chuàng)建時(shí)間標(biāo)簽
public static Timer timer; //創(chuàng)建計(jì)時(shí)
Keylistener keyListener = new Keylistener(this);
public Landmine()
{
super("掃雷__1.2版__小老頭"); //標(biāo)題
setBounds(300,90,800,800); //設(shè)置窗口位置和大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//最大化、最小化、關(guān)閉按鈕
BorderLayout ff = new BorderLayout(); //創(chuàng)建布局管理器
setLayout(ff); //關(guān)聯(lián)布局管理器
setResizable(false); //禁止改變窗口大小
/*初始化一些數(shù)據(jù)*/
numberOfClicked = 0;
numberOfUnflaged = 40;
usedTime = 0;
/*設(shè)置頂部面板*/
numberOfUnflagedLabel.setText("剩余雷數(shù):"+numberOfUnflaged);//顯示剩余雷數(shù)
numberOfUnflagedLabel.setFont(resources.fontOne);//設(shè)置剩雷數(shù)標(biāo)簽字體
numberOfUnflagedLabel.setIcon(resources.bombIconForLabel);//剩雷數(shù)標(biāo)簽圖標(biāo)(地雷形)
topPanel.add(numberOfUnflagedLabel); //剩雷數(shù)標(biāo)簽加入topPanel
timeLabel.setText("用時(shí):" + usedTime); //顯示剩余時(shí)間
timeLabel.setFont(resources.fontOne); //設(shè)置時(shí)間標(biāo)簽字體
timeLabel.setIcon(resources.clockIcon); //設(shè)置時(shí)間標(biāo)簽圖標(biāo)
topPanel.add(timeLabel); //時(shí)間標(biāo)簽加入topPanel
add(topPanel,BorderLayout.NORTH); //加入主面板上部
timer = new Timer(1000,new TimerListener());//計(jì)算器注冊(cè)監(jiān)聽(tīng)者
/*設(shè)置底部面板*/
JButton aboutJB = new JButton("關(guān)于"); //創(chuàng)建“關(guān)于”按鈕
JButton helpJB = new JButton("求救"); //創(chuàng)建“求救”按鈕
helpJB.addActionListener(helpListener); //"求救"按鈕加入監(jiān)聽(tīng)者
aboutJB.addActionListener(helpListener);//"關(guān)于"按鈕加入監(jiān)聽(tīng)者
helpJB.addKeyListener(keyListener);
aboutJB.addKeyListener(keyListener); //注冊(cè)按鍵監(jiān)聽(tīng)
lowerPanel.add(aboutJB); //“關(guān)于”按鈕加入lowerPanel
lowerPanel.add(helpJB); //“幫助”按鈕加入lowerPanel
add(lowerPanel,BorderLayout.SOUTH);
/*設(shè)置地雷面板*/
GridLayout dd = new GridLayout(16,16);
landminePanel.setLayout(dd); //布局管理
lei = new MyButton[18][18];
for(int i=0; i18; ++i)
{//創(chuàng)建下標(biāo)0—17的按鈕,18*18矩陣
for(int j=0; j18; ++j)
{
lei[i][j] = new MyButton(i,j);
}
}
for(int i=1; i17; ++i)
{//將下標(biāo)1-16的按鈕,加入面板、設(shè)置圖標(biāo)、翻開(kāi)標(biāo)記為假、加入監(jiān)聽(tīng)者
for(int j=1; j17; ++j)
{
landminePanel.add(lei[i][j]); //按鈕加入地雷面板
lei[i][j].setIcon(resources.smallIcon); //設(shè)置按鈕圖標(biāo)
lei[i][j].isClicked = false; //翻開(kāi)標(biāo)記設(shè)置為 假lei[i][j].setIcon(dead);
lei[i][j].addActionListener(listener); //加入監(jiān)聽(tīng)者
lei[i][j].addMouseListener(listener); //加入鼠標(biāo)事件監(jiān)聽(tīng)者
lei[i][j].addKeyListener(keyListener); //按鈕注冊(cè)按鍵監(jiān)聽(tīng),當(dāng)焦點(diǎn)在按鈕上是能監(jiān)聽(tīng)按鍵
}
}
add(landminePanel,BorderLayout.CENTER); //landminePanel加入主框架中央
addLandmine(); //布雷
timer.start(); //啟動(dòng)計(jì)時(shí)器
setVisible(true);//顯示之
}
/*布雷*/
public static void addLandmine()
{//隨機(jī)將40的按鈕的是否為雷的標(biāo)記isBomb設(shè)為真
for(int count = 0; count40; /*blank*/)
{
int i = (int)(Math.random()*100 % 16 +1 ) ;
int j = (int)(Math.random()*100 % 16 +1 ) ;
if(lei[i][j].isBomb == false)
{
lei[i][j].isBomb = true;
count++;
}
}
}
class TimerListener implements ActionListener
{//內(nèi)部類,時(shí)間監(jiān)聽(tīng)
public void actionPerformed(ActionEvent e)
{
usedTime++;
timeLabel.setText("用時(shí):" + usedTime);
}
}
}
//
// Playing類 執(zhí)行主要游戲操作
class Playing implements ActionListener,MouseListener
{
static Resources resources = new Resources();
public static Landmine gui;
public Playing(Landmine in )
{
gui = in;
}
public void actionPerformed(ActionEvent event)
{
MyButton receive = (MyButton)event.getSource();
if(receive.isBomb)
{//如果翻到了雷。。
for(int i=1; i17; ++i)
{//將所有的雷圖標(biāo)設(shè)為 “地雷”
for(int j=1; j17; ++j)
{
if(gui.lei[i][j].isBomb)
gui.lei[i][j].setIcon(resources.bombIcon);
}
}
receive.setIcon(resources.deadIcon);//將踩到的地雷圖標(biāo)設(shè)為 “衰”
gui.timer.stop(); //停止計(jì)時(shí)器
JOptionPane.showMessageDialog(null,"小朋友,你掛了…","失敗!",
JOptionPane.INFORMATION_MESSAGE,
resources.deadIcon);//提示失敗
int yourChose = JOptionPane.showConfirmDialog(null,"你可能是一不小心點(diǎn)錯(cuò)了,再來(lái)一局?" );
if(yourChose == JOptionPane.OK_OPTION)
{//點(diǎn)擊“是”時(shí)
replay();
}
else
{//點(diǎn)擊 “否” 或 “取消” 時(shí)退出程序
System.exit(0);
}
}
else if(receive.isClicked ==false)
{//未翻到雷
showBombNumber(receive);
}
}
public static void showBombNumber(MyButton in)
{//翻開(kāi)點(diǎn)擊的按鈕
int numberOfLandmine = 0;//記錄雷的個(gè)數(shù)
in.isClicked = true; //翻開(kāi)標(biāo)記設(shè)為真
/*檢測(cè)周圍8個(gè)方塊是否為雷*/
if(gui.lei[in.num_x-1][in.num_y-1].isBomb == true) numberOfLandmine++;//左上
if(gui.lei[in.num_x][in.num_y-1].isBomb == true) numberOfLandmine++; //上
if(gui.lei[in.num_x+1][in.num_y-1].isBomb == true) numberOfLandmine++;//右上
if(gui.lei[in.num_x+1][in.num_y].isBomb == true) numberOfLandmine++; //右
if(gui.lei[in.num_x+1][in.num_y+1].isBomb == true) numberOfLandmine++;//右下
if(gui.lei[in.num_x][in.num_y+1].isBomb == true) numberOfLandmine++; //下
if(gui.lei[in.num_x-1][in.num_y+1].isBomb == true) numberOfLandmine++;//左下
if(gui.lei[in.num_x-1][in.num_y].isBomb == true) numberOfLandmine++; //左
in.setIcon(new ImageIcon("images/"+numberOfLandmine+".png"));//根據(jù)周圍的雷數(shù)顯示數(shù)字圖標(biāo)
gui.numberOfClicked++;//翻開(kāi)格子數(shù)+1
if(gui.numberOfClicked==216)
{//翻開(kāi)216個(gè)格子時(shí)游戲成功,用戶選擇是否再來(lái)一局
int yourChoice = JOptionPane.showConfirmDialog(null,"恭喜你成功了!再來(lái)一盤嗎?");
if(yourChoice == JOptionPane.OK_OPTION)
replay();
else
System.exit(0);
}
if(numberOfLandmine==0)
{//如果周圍無(wú)雷,則將周圍未翻開(kāi)格子的全部翻開(kāi)
if(gui.lei[in.num_x-1][in.num_y-1].isClicked == false)
showBombNumber(gui.lei[in.num_x-1][in.num_y-1]);
if(gui.lei[in.num_x][in.num_y-1].isClicked == false)
showBombNumber(gui.lei[in.num_x][in.num_y-1]);
if(gui.lei[in.num_x+1][in.num_y-1].isClicked == false)
showBombNumber(gui.lei[in.num_x+1][in.num_y-1]);
if(gui.lei[in.num_x+1][in.num_y].isClicked == false)
showBombNumber(gui.lei[in.num_x+1][in.num_y]);
if(gui.lei[in.num_x+1][in.num_y+1].isClicked == false)
showBombNumber(gui.lei[in.num_x+1][in.num_y+1]);
if(gui.lei[in.num_x][in.num_y+1].isClicked == false)
showBombNumber(gui.lei[in.num_x][in.num_y+1]);
if(gui.lei[in.num_x-1][in.num_y+1].isClicked == false)
showBombNumber(gui.lei[in.num_x-1][in.num_y+1]);
if(gui.lei[in.num_x-1][in.num_y].isClicked == false)
showBombNumber(gui.lei[in.num_x-1][in.num_y]);
}
}
public static void replay()
{//重新開(kāi)始
gui.dispose(); //釋放框架資源
gui.timer.stop(); //終止計(jì)時(shí)器
Landmine ff = new Landmine();//重新創(chuàng)建一個(gè)主類的實(shí)例
//這幾條語(yǔ)句實(shí)現(xiàn)了重新開(kāi)始————關(guān)閉上一個(gè)窗口,重新開(kāi)啟一個(gè)
//但是這種方法會(huì)造成內(nèi)存的浪費(fèi),一個(gè)改進(jìn)的方法是不關(guān)閉當(dāng)年窗口,而是將當(dāng)前窗口重新初始化
}
public void mousePressed(MouseEvent e)
{//當(dāng)鼠標(biāo)右鍵點(diǎn)擊時(shí)自動(dòng)調(diào)用此函數(shù)
int mods = e.getModifiers();
MyButton receive = (MyButton)e.getSource();
if((mods InputEvent.BUTTON3_MASK) != 0)
{//鼠標(biāo)右鍵
if(receive.isClicked == false)
{
receive.isRight = receive.isRight ? false : true;//改變r(jià)eceive.isRight的值
if(receive.isRight)
{//如果添加標(biāo)記,則剩余雷數(shù)-1,設(shè)置標(biāo)簽為“旗幟”
gui.numberOfUnflaged--;
receive.setIcon(resources.flagIcon);
}
else
{//如果清除標(biāo)記,則剩余雷數(shù)+1,設(shè)置標(biāo)簽為“未翻開(kāi)”
gui.numberOfUnflaged++;
receive.setIcon(resources.smallIcon);
}
gui.numberOfUnflagedLabel.setText("剩余雷數(shù):"+gui.numberOfUnflaged);
//更新剩余雷數(shù)標(biāo)簽
}
}
}
public void mouseReleased(MouseEvent e){}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
}
//
// Help類,響應(yīng)“關(guān)于”、“求救”
class Help implements ActionListener
{
static Resources resources = new Resources();
public static Landmine gui;
public Help(Landmine in)
{
gui = in ;
}
public void actionPerformed(ActionEvent event)
{
if(event.getActionCommand()=="關(guān)于")
JOptionPane.showMessageDialog(null,"掃雷1.2版。。小老頭出品");
if(event.getActionCommand()=="求救")
help();
}
public static void help()
{//求救
int stopNumber = (int)(Math.random() * gui.numberOfUnflaged + 1 );
int count = 0;
for(int i=1; i17;++i )
{
for(int j=1; j17; ++j)
{
if( gui.lei[i][j].isBomb !gui.lei[i][j].isClicked !gui.lei[i][j].isRight )
{
count++;
}
if(count == stopNumber)
{
gui.lei[i][j].setIcon(resources.badIcon);
return;
}
}
}
}
}
//
// Keylistener類,響應(yīng)鍵盤事件
class Keylistener implements KeyListener
{
static Resources resources = new Resources();
Landmine gui;
public Keylistener(Landmine in)
{
gui = in;
}
public void keyPressed(KeyEvent e)
{//有鍵按下時(shí)自動(dòng)執(zhí)行該方法
if(e.getKeyCode() == KeyEvent.VK_UP)
{//按鍵為 向上 時(shí),將所有未標(biāo)記的地雷顯示出
for(int i=1; i17; ++i)
{
for(int j=1; j17; ++j)
{
if(gui.lei[i][j].isBomb !gui.lei[i][j].isRight)
gui.lei[i][j].setIcon(resources.badIcon);
}
}
}
if(e.getKeyCode() == KeyEvent.VK_DOWN)
{//按鍵為 向下 時(shí),將所有未標(biāo)記的地雷恢復(fù)為未點(diǎn)擊的圖標(biāo)
for(int i=1; i17; ++i)
{
for(int j=1; j17; ++j)
{
if(gui.lei[i][j].isBomb !gui.lei[i][j].isRight)
gui.lei[i][j].setIcon(resources.smallIcon);
}
}
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
}
//
// 按鈕類 MyBtton
class MyButton extends JButton
{
public int num_x,num_y; //第幾號(hào)方塊
public boolean isBomb; //是否為雷
public boolean isClicked; //是否被點(diǎn)擊
public int BombFlag; //探雷標(biāo)記
public boolean isRight; //是否點(diǎn)擊右鍵
public MyButton(int x, int y)
{
BombFlag = 0;
num_x = x;
num_y = y;
isBomb = false;
isClicked = true;
isRight = false;
}
}
//
// 資源類 其他類中用到的圖標(biāo),字體等
class Resources
{
public static ImageIcon deadIcon;
public static ImageIcon smallIcon;
public static ImageIcon clockIcon;
public static ImageIcon bombIcon;
public static ImageIcon flagIcon;
public static ImageIcon badIcon;
public static ImageIcon bombIconForLabel;
public static Font fontOne;
public Resources()
{
deadIcon = new ImageIcon("images/dead.gif");
smallIcon = new ImageIcon("images/smallIcon.png");
clockIcon = new ImageIcon("images/clock2.png");
bombIcon = new ImageIcon("images/bomb.png");
flagIcon = new ImageIcon("images/flag_2.png");
badIcon = new ImageIcon("images/bad.gif");
bombIconForLabel = new ImageIcon("images/bombForLabel.gif");
fontOne = new Font("null",Font.BOLD,20);
}
}
import java.awt.*;
import javax.swing.*;
import java.util.Random;
import java.awt.event.*;
class Min extends JPanel //雷的類
{
//備注:鼠標(biāo)的左鍵 = 1;右鍵 = 3;中鍵 = 2
private int flag = 0,statu = 0; //定義雷的屬性 0:沒(méi)有打開(kāi) 1:打開(kāi) 2:標(biāo)示為雷 3:不確定
//flag = 0 不是雷 ; flag = 1是雷
private int but,count = 0; //but:哪一個(gè)鼠標(biāo)鍵被按下去了 count:這個(gè)區(qū)域周圍有多少個(gè)雷
private int mx = 0,my = 0,mw = 10; //定義雷的坐標(biāo)和寬度
public Min() //構(gòu)造函數(shù)
{
statu = 0;
}
public Min(int f,int x,int y,int w)
//構(gòu)造函數(shù)
{
flag = f;
mx = x;
my = y;
mw = w;
}
public int getFlag(){return flag;}
public int getStatu(){return statu;}
public int getMx(){return mx;}
public int getMy(){return my;}
public int getMw(){return mw;}
public int getCount(){return count;}
public void setFlag(int f){flag = f;}
public void setCount(int c){count = c;}
public void setData(int f,int x,int y,int w,int s)
//傳遞值
{
flag = f;
mx = (x-1)*w;
my = (y-1)*w;
mw = w-1;
statu = s;
}
//根據(jù)你點(diǎn)擊鼠標(biāo)的不同來(lái)改變雷的屬性
public int sendKey(int key)
{
//返回值,如果游戲結(jié)束則返回-1
int rtn = 1;
if(key == 3)
{
switch(statu)
{
case 1:
break;
case 2:
statu = 3;
break;
case 3:
statu = 0;
break;
case 0:
statu = 2;
break;
}
rtn = 1;
}
if(key == 1 statu == 0)
{
switch(flag)
{
case 0:
statu = 1;
rtn = 2;
break;
case 1:
statu = 1;
rtn = -1;
break;
}
}
return rtn;
}
}
class DrawPanel extends JPanel
{
private int i,j;
private int f = 0; //if f = 1 then game over ,if f =2 then win
private int chx = 0,chy = 0; //專門記錄坐標(biāo)x,y的值
private int msum = 6,ksum = 0; //msum:雷的個(gè)數(shù),ksum:標(biāo)示雷的個(gè)數(shù)
private int bx = 10,by = 10,bw = 40; //bx,by:棋盤的大小,bw:棋子的大小
public Min board[][] = {
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
};
// 畫(huà)坐標(biāo)為ax,ay區(qū)域的雷的狀態(tài)
public void draw(Graphics g,int ax,int ay)
{
int x,y,w; // 坐標(biāo)x,y;和寬度:w
int s,c,flag; //狀態(tài);雷的個(gè)數(shù);
int cx = bw/2 - 4;
int cy = bw/2 + 4;
x = board[ax][ay].getMx();
y = board[ax][ay].getMy();
w = board[ax][ay].getMw();
s = board[ax][ay].getStatu();
c = board[ax][ay].getCount();
flag= board[ax][ay].getFlag();
switch(s)
{
case 0: //沒(méi)有打開(kāi)狀態(tài)
{
g.setColor(Color.black);
g.fillRect(x,y,w,w);
break;
}
case 1: //打開(kāi)狀態(tài)
{
g.setColor(Color.blue);
g.fillRect(x,y,w,w);
if(c != 0 flag == 0) //此處沒(méi)有雷
{
g.setColor(Color.red);
g.drawString(String.valueOf(c),x + cx,y + cy);
}
if(flag == 1) //此處有雷
{
g.setColor(Color.red);
g.fillRect(x,y,w,w);
g.setColor(Color.blue);
g.drawString(" 雷",x + cx,y + cy);
}
break;
}
case 2: //標(biāo)雷狀態(tài)
{
g.setColor(Color.green);
g.fillRect(x,y,w,w);
g.setColor(Color.blue);
g.drawString(" 旗",x + cx,y + cy);
break;
}
case 3: //不確定狀態(tài)
{
g.setColor(Color.black);
g.fillRect(x,y,w,w);
g.setColor(Color.red);
g.drawString("?",x + cx,y + cy);
break;
}
default:
break;
}
}
// 沒(méi)有圖形器的繪圖函數(shù):畫(huà)出坐標(biāo)ax,ay的雷的狀態(tài)和圖形
public void draw(int ax,int ay)
{
Graphics g;
g = this.getGraphics();
draw(g,ax,ay);
}
//打開(kāi)周圍沒(méi)有雷的地方,并且繪畫(huà)所在區(qū)域點(diǎn)擊左鍵觸發(fā)
public int openNoMin(int ax,int ay)
{
int i,j;
if(ax1||ay1||axbx||ayby) return 0; //鼠標(biāo)點(diǎn)擊的區(qū)域出界了
if(board[ax][ay].getStatu() != 0) return 0; //如果此區(qū)域打開(kāi)了,返回
board[ax][ay].sendKey(1); //如果返回值等于-1,就說(shuō)明游戲結(jié)束
draw(ax,ay);
if(board[ax][ay].getFlag() == 1)
//如果游戲結(jié)束,把所有的雷都顯示出來(lái)
{
for(i = 1;i=bx;i++)
{
for(j = 1;j = by;j++)
{
if(board[i][j].getFlag() == 1)
{
board[i][j].sendKey(1);
draw(i,j);
}
}
}
return -1;
}
//如果游戲沒(méi)有結(jié)束
if(board[ax][ay].getCount() 0)
{
ksum ++;
return 1; //周圍有雷,就不用打開(kāi)周圍地區(qū)
}
if(board[ax][ay].getCount() == 0 board[ax][ay].getFlag() == 0)
//周圍沒(méi)有雷,打開(kāi)周圍地區(qū),直到有雷的地區(qū)
{
openNoMin(ax-1,ay-1);openNoMin(ax,ay-1);openNoMin(ax+1,ay-1);
openNoMin(ax-1,ay ); openNoMin(ax+1,ay );
openNoMin(ax-1,ay+1);openNoMin(ax,ay+1);openNoMin(ax+1,ay+1);
}
ksum ++;
return 1;
}
//計(jì)算坐標(biāo)x,y的周圍雷的個(gè)數(shù)
public int getCount(int ai,int aj)
{
int sum = 0;
if(board[ai][aj].getFlag() == 1)
{
return sum;
}
if(ai1aj1aibxajby)
{
sum = board[ai-1][aj-1].getFlag()+ board[ai][aj-1].getFlag()+ board[ai+1][aj-1].getFlag()+
board[ai-1][aj ].getFlag()+ board[ai+1][aj ].getFlag()+
board[ai-1][aj+1].getFlag()+ board[ai][aj+1].getFlag()+ board[ai+1][aj+1].getFlag();
}
if(ai==1aj==1)
{
sum = board[ai+1][aj ].getFlag()+
board[ai][aj+1].getFlag()+ board[ai+1][aj+1].getFlag();
}
if(ai==1aj==by)
{
sum = board[ai][aj-1].getFlag()+ board[ai+1][aj-1].getFlag()+
board[ai+1][aj ].getFlag();
}
if(ai==bxaj==1)
{
sum = board[ai-1][aj ].getFlag()+
board[ai-1][aj+1].getFlag()+ board[ai][aj+1].getFlag();
}
if(ai==bxaj==by)
{
sum = board[ai-1][aj-1].getFlag()+ board[ai][aj-1].getFlag()+
board[ai-1][aj ].getFlag();
}
if(ai==1aj1ajby)
{
sum = board[ai][aj-1].getFlag()+ board[ai+1][aj-1].getFlag()+
board[ai+1][aj ].getFlag()+
board[ai][aj+1].getFlag()+ board[ai+1][aj+1].getFlag();
}
if(ai==bxaj1ajby)
{
sum = board[ai-1][aj-1].getFlag()+ board[ai][aj-1].getFlag()+
board[ai-1][aj ].getFlag()+
board[ai-1][aj+1].getFlag()+ board[ai][aj+1].getFlag();
}
if(ai1aibxaj==1)
{
sum = board[ai-1][aj ].getFlag()+ board[ai+1][aj ].getFlag()+
board[ai-1][aj+1].getFlag()+ board[ai][aj+1].getFlag()+ board[ai+1][aj+1].getFlag();
}
if(ai1aibxaj==by)
{
sum = board[ai-1][aj-1].getFlag()+ board[ai][aj-1].getFlag()+ board[ai+1][aj-1].getFlag()+
board[ai-1][aj ].getFlag()+ board[ai+1][aj ].getFlag();
}
return sum;
}
// 傳入?yún)?shù):幾列,幾行,寬度,雷數(shù)
public void initMin(int ax,int ay,int aw,int as)
{
int k = 1; //表明產(chǎn)生的第幾個(gè)雷
Random r; //隨機(jī)數(shù)
f = 0; //f=0表示游戲還沒(méi)有結(jié)束
ksum = 0;
bx = ax;
by = ay;
bw = aw;
msum = as;
r = new Random();
//初始化底盤的值
for(i = 1;i = bx;i++)
{
for(j=1;j=by;j++)
{
board[i][j].setData(0,i,j,bw,0);
}
}
// 隨機(jī)產(chǎn)生雷
while(k = msum)
{
i = r.nextInt(bx)+1;
j = r.nextInt(by)+1;
if(board[i][j].getFlag() != 1)
{
board[i][j].setFlag(1);
k++;
}
}
// 非雷區(qū)的周圍有幾個(gè)雷,初始化其值
for(i = 1;i = bx;i++)
{
for(j=1;j=by;j++)
{
board[i][j].setCount(getCount(i,j));
}
}
setBackground(Color.white);
repaint();
}
// 構(gòu)造函數(shù)
public DrawPanel(int ax,int ay,int aw,int as)
{
initMin(ax,ay,aw,as);
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
int r;
if(f != 0) return; //如果游戲結(jié)束,返回
chx = me.getX();
chy = me.getY();
if(me.getButton() != 1)
{
board[chx/bw+1][chy/bw+1].sendKey(me.getButton());
draw(chx/bw+1,chy/bw+1);
}
else if(me.getButton() == 1)
{
if(openNoMin(chx/bw+1,chy/bw+1) == -1)
{
f = 1;
repaint();
}
else if ( ksum + msum == bx*by )
{
f = 2;
repaint();
}
}
}
}
);
}
// 重畫(huà)所有的圖形,包括一些修飾的圖形
public void paint(Graphics g)
{
int x,y,w;
int s;
int cx = bw/2 - 4;
int cy = bw/2 + 4;
g.clearRect(0,0,600,600);
for(i=1;i=bx;i++)
{
for(j=1;j=by;j++)
{
draw(g,i,j);
}
}
if(f == 1)
{
Font f = new Font("11",1,70);
Font fo = g.getFont();
g.setColor(Color.white);
g.setFont(f);
//g.setSize();
g.drawString("Game Over",0,200);
g.setFont(fo);
}
if( f == 2 )
{
Font f = new Font("11",1,70);
Font fo = g.getFont();
g.setColor(Color.white);
g.setFont(f);
//g.setSize();
g.drawString("You win!",0,200);
g.setFont(fo);
}
}
};
// 主類和程序的入口
public class Mine extends JFrame implements ActionListener
{
Container cp = getContentPane();
JButton bt = new JButton("開(kāi)局");
Label l1 = new Label("列:");
Label l2 = new Label("行:");
Label l3 = new Label("寬度:");
Label l4 = new Label("雷的個(gè)數(shù):");
TextField tf1 = new TextField("10",2); //列
TextField tf2 = new TextField("10",2); //行
TextField tf3 = new TextField("40",2); //寬度
TextField tf4 = new TextField("15",2); //雷的個(gè)數(shù)
int x=10,y=10,w=40,sum=15;
DrawPanel dp = new DrawPanel(x,y,w,sum);
public Mine()
{
setBackground(Color.white);
cp.setLayout(null);
cp.add(dp);
cp.add(bt);
cp.add(tf1);
cp.add(tf2);
cp.add(tf3);
cp.add(tf4);
cp.add(l1);
cp.add(l2);
cp.add(l3);
cp.add(l4);
l1.setBounds(20 ,10,20,20);
tf1.setBounds(40,10,20,20);
l2.setBounds(70,10,20,20);
tf2.setBounds(90,10,20,20);
l3.setBounds(120,10,40,20);
tf3.setBounds(160,10,20,20);
l4.setBounds(190,10,60,20);
tf4.setBounds(250,10,20,20);
bt.setBounds(300,10,80,20);
dp.setBounds(20,40,x*w,y*w);
setResizable(false);
setSize(x*w+40,y*w+80);
setTitle(" 掃雷");
show();
bt.addActionListener(this);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{System.exit(0);}
}
);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == bt)
{
//x = Integer.parseInt(tf1.getText());
//y = Integer.parseInt(tf2.getText());
//w = Integer.parseInt(tf3.getText());
sum = Integer.parseInt(tf4.getText());
setSize(x*w+40,y*w+80);
dp.setBounds(20,40,x*w,y*w);
show();
dp.initMin(x,y,w,sum);
}
}
public static void main(String args[])
{
new Mine();
}
};
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ScanLei1 extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private Container contentPane;
private JButton btn;
private JButton[] btns;
private JLabel b1;
private JLabel b2;
private JLabel b3;
private Timer timer;
private int row=9;
private int col=9;
private int bon=10;
private int[][] a;
private int b;
private int[] a1;
private JPanel p,p1,p2,p3;
public ScanLei1(String title){
super(title);
contentPane=getContentPane();
setSize(297,377);
this.setBounds(400, 100, 400, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
timer =new Timer(1000,(ActionListener) this);
a = new int[row+2][col+2];
initGUI();
}
public void initGUI(){
p3=new JPanel();
b=bon;
JMenuBar menuBar=new JMenuBar();
JMenu menu1=new JMenu("游戲");
JMenu menu2=new JMenu("幫助");
JMenuItem mi1=new JMenuItem("初級(jí)");
JMenuItem mi2 = new JMenuItem("中級(jí)");
JMenuItem mi3 =new JMenuItem("高級(jí)");
mi1.addActionListener(this);
menu1.add(mi1);
mi2.addActionListener(this);
menu1.add(mi2);
mi3.addActionListener(this);
menu1.add(mi3);
menuBar.add(menu1);
menuBar.add(menu2);
p3.add(menuBar);
b1=new JLabel(bon+"");
a1=new int[bon];
btn =new JButton("開(kāi)始");
btn.addActionListener(this);
b2=new JLabel("0");
b3=new JLabel("");
btns=new JButton[row*col];
p=new JPanel();
p.setLayout(new BorderLayout());
contentPane.add(p);
p.add(p3,BorderLayout.NORTH);
//combo=new JComboBox(new Object[]{"初級(jí)","中級(jí)","高級(jí)"} );
//加監(jiān)聽(tīng)
/*combo.addItemListener(new ItemListener(){
}});*/
p1=new JPanel();
//在那個(gè)位置
//(( FlowLayout)p1.getLayout()).setAlignment( FlowLayout.RIGHT);
p1.add(b1);
p1.add(btn);
p1.add(b2);
p1.add(b3);
p.add(p3,BorderLayout.NORTH);
p.add(p1,BorderLayout.CENTER);
p2=new JPanel();
p2.setLayout(new GridLayout(row,col,0,0));
for(int i=0;irow*col;i++){
btns[i]=new JButton("");
btns[i].setMargin(new Insets(0,0,0,0));
btns[i].setFont(new Font(null,Font.BOLD,25));
btns[i].addActionListener(this);
btns[i].addMouseListener(new NormoreMouseEvent());
p2.add(btns[i]);
}
contentPane.add(p,BorderLayout.NORTH);
contentPane.add(p2,BorderLayout.CENTER);
}
public void go(){
setVisible(true);
}
public static void main(String[] args){
new ScanLei1("掃雷").go();
}
public void out(int[][] a,JButton[] btns,ActionEvent e,int i,int x,int y){
int p=1;
if(a[x][y]==0){
a[x][y]=10;
btns[i].setEnabled(false); //33
for(int l=y-1;l=y+1;l++){
int m=x-1-1;
int n=l-1;
p=1;
System.out.println(a[1][2]);
if(n-1ncolm-1mrow)
{
for(int q=0;qrowp==1;q++){//col--row;
if(((n+col*q)=(m*col))((n+col*q)(m+1)*col)){
if(a[x-1][l]!=0a[x-1][l]!=10){
btns[n+col*q].setText(a[x-1][l]+"");
a[x-1][l]=10;
btns[n+col*q].setEnabled(false);
}
else if(a[x-1][l]==0){
//a[x-1][l]=10;
btns[n+col*q].setEnabled(false);
out(a,btns,e,n+col*q,x-1,l); ////55////
a[x-1][l]=10;
btns[n+col*q].setEnabled(false);
}
p=0;
}
}
}
p=1;
m=x;
if(n-1ncolm-1mcol)
{
for(int q=0;qrowp==1;q++){
if(((n+col*q)=(m*col))((n+col*q)(m+1)*col)){
if(a[x+1][l]!=0a[x+1][l]!=10){
btns[n+col*q].setText(a[x+1][l]+"");
a[x+1][l]=10;
btns[n+col*q].setEnabled(false);
}
else if(a[x+1][l]==0){
out(a,btns,e,n+col*q,x+1,l);///55////
a[x+1][l]=10;
btns[n+col*q].setEnabled(false);
}
p=0;
}
}
}
}
int m=x-1;
int n=y-1-1;
p=1;
if(n-1ncolm-1mcol)
{
for(int q=0;qrowp==1;q++){
if(((n+col*q)=(m*col))((n+col*q)(m+1)*col)){
if(a[x][y-1]!=0a[x][y-1]!=10){
btns[n+col*q].setText(a[x][y-1]+"");
a[x][y-1]=10;
btns[n+col*q].setEnabled(false);
}
else if(a[x][y-1]==0){
out(a,btns,e,n+col*q,x,y-1);
a[x][y-1]=10;
btns[n+col*q].setEnabled(false);
}
p=0;
}
}
}
p=1;
m=x-1;
n=y+1-1;
if(n-1ncolm-1mcol)
{
for(int q=0;qrowp==1;q++){
if(((n+col*q)=(m*col))((n+col*q)(m+1)*col)){
if(a[x][y+1]!=0a[x][y+1]!=10){
btns[n+col*q].setText(a[x][y+1]+"");
a[x][y+1]=10;
btns[n+col*q].setEnabled(false);
}
else if(a[x][y+1]==0){
out(a,btns,e,n+col*q,x,y+1);
a[x][y+1]=10;
btns[n+col*q].setEnabled(false);
}
p=0;
}
}
}
}
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="初級(jí)"){
row=9;
col=9;
bon=10;
a1=new int[bon];
b=bon;
//setSize(297,377);
a = new int[row+2][col+2];
this.remove(p2);
timer.stop();
b1.setText("10");
b2.setText("0");
b3.setText("");
btns=new JButton[row*col];
p2=new JPanel();
p2.setLayout(new GridLayout(row,col,0,0));
for(int i=0;irow*col;i++){
btns[i]=new JButton(" ");
btns[i].setMargin(new Insets(0,0,0,0));
btns[i].setFont(new Font(null,Font.BOLD,25));
btns[i].addActionListener(this);
btns[i].addMouseListener(new NormoreMouseEvent());
p2.add(btns[i]);
}
contentPane.add(p2,BorderLayout.CENTER);
//setSize(297,377);
this.pack();
for(int i=0;irow*col;i++){
btns[i].setText(" ");
btns[i].setEnabled(true);
}
for(int i=0;irow+2;i++){
for(int j=0;jcol+2;j++){
a[i][j]=0;
}
}
}else if(e.getActionCommand()=="中級(jí)"){
row=16;
col=16;
bon=40;
//setSize(33*col,33*row+80);
a1=new int[bon];
a = new int[row+2][col+2];
b=bon;
this.remove(p2);
timer.stop();
b1.setText("40");
b2.setText("0");
b3.setText("");
btns=new JButton[row*col];
p2=new JPanel();
p2.setLayout(new GridLayout(row,col,0,0));
for(int i=0;irow*col;i++){
btns[i]=new JButton(" ");
btns[i].setMargin(new Insets(0,0,0,0));
btns[i].setFont(new Font(null,Font.BOLD,25));
btns[i].addActionListener(this);
btns[i].addMouseListener(new NormoreMouseEvent());
p2.add(btns[i]);
}
contentPane.add(p2,BorderLayout.CENTER);
this.pack();
//setSize(33*col,33*row+80);
for(int i=0;irow*col;i++){
btns[i].setText("");
btns[i].setEnabled(true);
}
for(int i=0;irow+2;i++){
for(int j=0;jcol+2;j++){
a[i][j]=0;
}
}
}else if(e.getActionCommand()=="高級(jí)"){
row=16;
col=32;
bon=99;
setSize(33*col,33*row+80);
a1=new int[bon];
a = new int[row+2][col+2];
b=bon;
this.remove(p2);
timer.stop();
b1.setText("99");
b2.setText("0");
b3.setText("");
btns=new JButton[row*col];
p2=new JPanel();
p2.setLayout(new GridLayout(row,col,0,0));
for(int i=0;irow*col;i++){
btns[i]=new JButton(" ");
btns[i].setMargin(new Insets(0,0,0,0));
btns[i].setFont(new Font(null,Font.BOLD,25));
btns[i].addActionListener(this);
btns[i].addMouseListener(new NormoreMouseEvent());
p2.add(btns[i]);
}
contentPane.add(p2,BorderLayout.CENTER);
//setSize(33*col,33*row+80);
this.pack();
for(int i=0;irow*col;i++){
btns[i].setText("");
btns[i].setEnabled(true);
}
for(int i=0;irow+2;i++){
for(int j=0;jcol+2;j++){
a[i][j]=0;
}
}
}
if(e.getSource()==btn){
timer.start();
b=bon;
b3.setText("");
//System.out.println(bon);
//清空
for(int i=0;irow*col;i++){
btns[i].setText("");
btns[i].setEnabled(true);
}
for(int i=0;irow+2;i++){
for(int j=0;jcol+2;j++){
a[i][j]=0;
}
}
//產(chǎn)生隨機(jī)數(shù)
for(int i=0;ibon;i++)
{ int p=1;
int m=(int)(Math.random()*row*col);
while(p==1){
int l=1;
int j;
for( j=0;jil==1;j++){
if(a1[j]==m){
m=(int)(Math.random()*row*col);
l=0;
}
}
if(j==i){
a1[i]=m;
p=0;
}
}
}
b1.setText(bon+"");
b2.setText("0");
//布雷
for(int i=0;ibon;i++){
int x=(a1[i]/col+1);
int y=(a1[i]%col+1);
a[x][y]=100;
}
for(int i=0;irow+2;i++){
for(int j=0;jcol+2;j++){
if(i==0||j==0||i==row+1||j==col+1){
a[i][j]=0;
}
}
}
for(int i=1;i=row;i++){
for(int j=1;j=col;j++){
if(a[i][j]!=100){
for(int l=j-1;l=j+1;l++){
if(a[i-1][l]==100){
a[i][j]++;
}
if(a[i+1][l]==100){
a[i][j]++;
}
}
if(a[i][j-1]==100){
a[i][j]++;
}
if(a[i][j+1]==100){
a[i][j]++;
}
}
}
}
}
if(e.getSource()==timer)
{
String time=b2.getText().trim();
int t=Integer.parseInt(time);
//System.out.println(t);
if(t=600){
timer.stop();
}else{
t++;
b2.setText(t+"");
}
}
for(int i=0;icol*row;i++){
if(btns[i].getText()!="★")
{
int x=i/col+1;
int y=i%col+1;
if(e.getSource()==btns[i]a[x][y]==100){
btns[i].setText("★");
btns[i].setEnabled(false);
a[x][y]=10;
for(int k=0;kcol*row;k++){
int m1=k/col+1;
int n1=k%col+1;
if(a[m1][n1]!=10btns[k].getText()=="★"){
btns[k].setText("*o*");
}
}
for(int j=0;jcol*row;j++){
int m=j/col+1;
int n=j%col+1;
if(a[m][n]==100){
btns[j].setText("★");
btns[j].setEnabled(false);
b3.setText("你輸了 ?。?);
}
btns[j].setEnabled(false);
a[m][n]=10;
}
timer.stop();
}
else if(e.getSource()==btns[i]){
if(a[x][y]==0){
out(a,btns,e,i,x,y);
a[x][y]=10;
btns[i].setEnabled(false);
}
if(a[x][y]!=0a[x][y]!=10){
btns[i].setText(a[x][y]+"");
btns[i].setEnabled(false);
a[x][y]=10;
}
}
}else if(btns[i].getText()=="★"){
}
}
}
class NormoreMouseEvent extends MouseAdapter{
public void mouseClicked(MouseEvent e) {
System.out.println(b);
for(int i=0;icol*row;i++){
int x1=i/col+1;
int y1=i%col+1;
if(e.getSource()==btns[i]btns[i].getText()!="★"a[x1][y1]!=10)
{
if(e.getButton()==MouseEvent.BUTTON3){
btns[i].setText("★");
b--;
if(b==0){
int flag=0;
for(int j=0;jcol*row;j++){
int x=j/col+1;
int y=j%col+1;
if(a[x][y]==100btns[j].getText()=="★"){
flag++;
}
}
if(flag==bon){
timer.stop();
b3.setText("你贏了!");
}
}
b1.setText(b+"");
}
}else if(e.getSource()==btns[i]btns[i].getText()=="★"a[x1][y1]!=-1){
if(e.getButton()==MouseEvent.BUTTON3){
btns[i].setText("");
b++;
if(bbon){
b1.setText(bon+"");
}
else{
b1.setText(b+"");
}
btns[i].setEnabled(true);
}
}
}
}
}
}
新聞標(biāo)題:java掃雷源代碼可復(fù)制 java掃雷代碼簡(jiǎn)單實(shí)現(xiàn)
本文網(wǎng)址:http://jinyejixie.com/article6/dodhoig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、面包屑導(dǎo)航、電子商務(wù)、手機(jī)網(wǎng)站建設(shè)、軟件開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容