欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)簡(jiǎn)單掃雷小游戲

 更新時(shí)間:2020年04月22日 16:25:20   作者:tf1997  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java實(shí)現(xiàn)掃雷游戲的具體代碼,供大家參考,具體內(nèi)容如下

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
 
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
 
public class SaoLei implements MouseListener,ActionListener{
 JPanel p=new JPanel();
 JFrame frame = new JFrame("掃雷");
 @SuppressWarnings("rawtypes")
 JComboBox combobox = new JComboBox();
 JButton reset = new JButton("重新開(kāi)始");
 Container container = new Container();
 
 //游戲數(shù)據(jù)結(jié)構(gòu)
 SaoLeiConstant constant = new SaoLeiConstant();
 JButton[][] buttons = new JButton[constant.row][constant.col];//定義按鈕
 int[][] counts = new int [constant.row][constant.col];//定義整型數(shù)組保存按鈕下方雷數(shù)
 
 //創(chuàng)建構(gòu)造方法
 public SaoLei() {
 //顯示窗口
 frame.setSize(600,700);//600*700
 frame.setResizable(false);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setLayout(new BorderLayout());
 
 //添加重來(lái)、選擇難度按鈕
 addtopButton();
 
 //添加雷區(qū)按鈕
 addButtons();
 
 //埋雷
 addLei();
 
 //添加雷的計(jì)數(shù)
 calcNeiboLei();
 
 frame.setVisible(true);
 }
 
 void addtopButton() {
 p.removeAll();
 p.add(reset);
 reset.setBackground(Color.green);
 reset.setOpaque(true);
 reset.addActionListener(this);
 //combobox.addItem("選擇難度");
 combobox.addItem("新手難度");
 combobox.addItem("初級(jí)難度");
 combobox.addItem("中級(jí)難度");
 combobox.addItem("高級(jí)難度");
 combobox.addItem("大師難度");
 combobox.setBackground(Color.GREEN);
 combobox.setOpaque(true);
 combobox.addItemListener(new ItemListener(){
 
 @Override
 public void itemStateChanged(ItemEvent e) {
 String item = e.getItem().toString(); 
 if(item == "新手難度") {
 constant.leiCount = 20;
 ResetGame();
 } else if(item == "初級(jí)難度") {
 constant.leiCount = 43;
 ResetGame();
 } else if(item == "中級(jí)難度"){
 constant.leiCount = 63;
 ResetGame();
 } else if(item == "高級(jí)難度"){
 constant.leiCount = 99;
 ResetGame();
 } else if(item == "大師難度") {
 constant.leiCount = 119;
 ResetGame();
 }
 
 }
 
 });
 p.add(combobox);
 frame.add(p,BorderLayout.NORTH);
 //p.add(new Label("總雷數(shù):"+constant.leiCount,Label.CENTER));
 //p.add(new Label("總雷數(shù):"+constant.leiCount,Label.RIGHT));
 
 }
 
 
 /*
 void addnanduButton() {
 nandu.setBackground(Color.green);
 nandu.setOpaque(true);
 nandu.addActionListener(this);
 frame.add(nandu,BorderLayout.WEST);
 }
 
 void addResetButton() {
 reset.setBackground(Color.green);
 reset.setOpaque(true);
 reset.addActionListener(this);
 //reset.addMouseListener(this);
 frame.add(reset,BorderLayout.NORTH); 
 }
 */
 
 void addLei() {
 Random rand = new Random();
 int randRow,randCol;
 for(int i=0; i<constant.leiCount; i++) {
 randRow = rand.nextInt(constant.row);
 randCol = rand.nextInt(constant.col);
 if(counts[randRow][randCol] == constant.LEICODE) {
 i--;
 } else {
 counts[randRow][randCol] = constant.LEICODE;
 //buttons[randRow][randCol].setText("X");
 }
 }
 }
 
 void addButtons() {
 frame.add(container,BorderLayout.CENTER);
 container.setLayout(new GridLayout(constant.row,constant.col));
 for(int i=0;i<constant.row;i++) {
 for(int j=0;j<constant.col;j++) {
 JButton button = new JButton();
 button.setBackground(Color.white);
 button.setOpaque(true);
 button.addActionListener(this);
 button.addMouseListener((MouseListener) this);
 buttons[i][j] = button;
 container.add(button);
 }
 }
 }
 
 void calcNeiboLei() {
 int count;
 for(int i=0;i<constant.row;i++) {
 for(int j=0;j<constant.col;j++) {
 count =0;
 if(counts[i][j] == constant.LEICODE) continue;
 if(i>0 && j>0 && counts[i-1][j-1] == constant.LEICODE) count++;
 if(i>0 && counts[i-1][j] == constant.LEICODE) count++;
 if(i>0 && j<19 &&counts[i-1][j+1] == constant.LEICODE) count++; 
 if(j>0 && counts[i][j-1] == constant.LEICODE) count++;
 if(j<19 && counts[i][j+1] == constant.LEICODE) count++;
 if(i<19 && j>0 && counts[i+1][j-1] == constant.LEICODE) count++;
 if(i<19 && counts[i+1][j] == constant.LEICODE) count++;
 if(i<19 && j<19 && counts[i+1][j+1] == constant.LEICODE) count++;
 counts[i][j] = count;
 buttons[i][j].setMargin(new Insets(0,0,0,0));//讓按鈕隨按鈕上的圖案變化
 //buttons[i][j].setText(counts[i][j] + "");
 }
 }
 }
 
 @Override
 public void actionPerformed(ActionEvent e) {
 
 JButton button = (JButton)e.getSource();
 if(button.equals(reset)) {
 ResetGame();//重新開(kāi)始游戲
 } else {
 int count = 0;
 for(int i=0;i<constant.row;i++) {
 for(int j=0;j<constant.col;j++) {
 if(button.equals(buttons[i][j])) {
 count = counts[i][j];
 if(count == constant.LEICODE) {
 loseGame();
 } else {
 openCell(i,j); 
 checkWin();
 }
 return;
 } 
 }
 }
 }
 }
 
 public void mouseClicked(MouseEvent e) {
 JButton button = (JButton)e.getSource();
 if (e.getButton() == MouseEvent.BUTTON3) {//判斷鼠標(biāo)右擊動(dòng)作
 for(int i=0;i<constant.row;i++) {
 for(int j=0;j<constant.col;j++) {
 if(button.equals(buttons[i][j])) {
 if((buttons[i][j].isEnabled() == true)) {
 //buttons[i][j].setEnabled(false);
 buttons[i][j].setMargin(new Insets(0,0,0,0));//讓按鈕隨按鈕上的圖案變化
 buttons[i][j].setText("?");
 return;
 } 
 }
 }
 } 
 }
 }
 
 void ResetGame() {
 for(int i=0;i<constant.row;i++) {
 for(int j=0;j<constant.col;j++) {
 buttons[i][j].setText("");
 buttons[i][j].setEnabled(true);
 buttons[i][j].setBackground(Color.white);
 counts[i][j] = 0; 
 } 
 }
 addLei();
 calcNeiboLei(); 
 }
 
 void checkWin() {
 for(int i=0;i<constant.row;i++) {
 for(int j=0;j<constant.col;j++) {
 if(buttons[i][j].isEnabled() == true && counts[i][j] != constant.LEICODE ) return; 
 }
 }
 JOptionPane.showMessageDialog(frame,"Yeah,你贏了!"); 
 }
 
 //使用遞歸方法打開(kāi)格子
 void openCell(int i, int j) {
 if(buttons[i][j].isEnabled() == false) return;
 buttons[i][j].setBackground(Color.yellow);
 buttons[i][j].setOpaque(true);
 buttons[i][j].setEnabled(false);
 if(counts[i][j] == 0) {
 if(i>0 && j>0 && counts[i-1][j-1] != constant.LEICODE) openCell(i-1,j-1);
 if(i>0 && j<19 && counts[i-1][j] != constant.LEICODE) openCell(i-1,j);
 if(i>0 && j<19 &&counts[i-1][j+1] != constant.LEICODE) openCell(i-1,j+1); 
 if(j>0 && counts[i][j-1] != constant.LEICODE) openCell(i,j-1);
 if(j<19 && counts[i][j+1] != constant.LEICODE) openCell(i,j+1);
 if(i<19 && j>0 && counts[i+1][j-1] != constant.LEICODE) openCell(i+1,j-1);
 if(i<19 && counts[i+1][j] != constant.LEICODE) openCell(i+1,j);
 if(i<19 && j<19 && counts[i+1][j+1] != constant.LEICODE) openCell(i+1,j+1);
 } else {
 buttons[i][j].setMargin(new Insets(0,0,0,0));
 buttons[i][j].setText(counts[i][j] + "");
 }
 }
 
 void loseGame() {
 for(int i=0;i<constant.row;i++) {
 for(int j=0;j<constant.col;j++) {
 int count = counts[i][j];
 if(count == constant.LEICODE) {
 buttons[i][j].setMargin(new Insets(0,0,0,0));
 buttons[i][j].setText("雷");
 buttons[i][j].setBackground(Color.red);
 buttons[i][j].setEnabled(false);
 } else {
 buttons[i][j].setMargin(new Insets(0,0,0,0));
 buttons[i][j].setText(count + "");
 buttons[i][j].setEnabled(false);
 
 }
 }
 }
 JOptionPane.showMessageDialog(frame,"error,你輸了!");
 }
 
 public static void main(String[] args) {
 new SaoLei();
 }
 
 @Override
 public void mousePressed(MouseEvent e) {
 // TODO Auto-generated method stub
 
 }
 
 @Override
 public void mouseReleased(MouseEvent e) {
 // TODO Auto-generated method stub
 
 }
 
 @Override
 public void mouseEntered(MouseEvent e) {
 // TODO Auto-generated method stub
 
 }
 
 @Override
 public void mouseExited(MouseEvent e) {
 // TODO Auto-generated method stub
 
 }
}

常量類(lèi)

public class SaoLeiConstant {
 final int row = 20;//行數(shù)30
 final int col = 20;//列數(shù)30
 final int LEICODE = 10;//定義雷下方的數(shù)字
 protected int temp = 20;
 protected int leiCount = temp;//雷數(shù)30
}

效果圖

更多精彩游戲,請(qǐng)參考專(zhuān)題《java經(jīng)典小游戲》

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java設(shè)計(jì)模式之模板方法模式Template Method Pattern詳解

    Java設(shè)計(jì)模式之模板方法模式Template Method Pattern詳解

    在我們實(shí)際開(kāi)發(fā)中,如果一個(gè)方法極其復(fù)雜時(shí),如果我們將所有的邏輯寫(xiě)在一個(gè)方法中,那維護(hù)起來(lái)就很困難,要替換某些步驟時(shí)都要重新寫(xiě),這樣代碼的擴(kuò)展性就很差,當(dāng)遇到這種情況就要考慮今天的主角——模板方法模式
    2022-11-11
  • kafka與storm集群環(huán)境的安裝步驟詳解

    kafka與storm集群環(huán)境的安裝步驟詳解

    這篇文章主要給大家介紹了關(guān)于kafka與storm集群環(huán)境安裝步驟的相關(guān)資料,兩者并不是一定聯(lián)系的,寫(xiě)在一起主要是因?yàn)閮蓚€(gè)都是有zookeeper管理的,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • Java?IO及BufferedReader.readline()出現(xiàn)的Bug

    Java?IO及BufferedReader.readline()出現(xiàn)的Bug

    這篇文章主要介紹了Java?IO及BufferedReader.readline()出現(xiàn)的Bug,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • SpringBoot 整合 Shiro 密碼登錄與郵件驗(yàn)證碼登錄功能(多 Realm 認(rèn)證)

    SpringBoot 整合 Shiro 密碼登錄與郵件驗(yàn)證碼登錄功能(多 Realm 認(rèn)證)

    這篇文章主要介紹了SpringBoot 整合 Shiro 密碼登錄與郵件驗(yàn)證碼登錄(多 Realm 認(rèn)證),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • 劍指Offer之Java算法習(xí)題精講二叉搜索樹(shù)與數(shù)組查找

    劍指Offer之Java算法習(xí)題精講二叉搜索樹(shù)與數(shù)組查找

    跟著思路走,之后從簡(jiǎn)單題入手,反復(fù)去看,做過(guò)之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化
    2022-03-03
  • java中重寫(xiě)equals和重寫(xiě)hashCode()

    java中重寫(xiě)equals和重寫(xiě)hashCode()

    這篇文章主要介紹了java中重寫(xiě)equals和重寫(xiě)hashCode()的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • 關(guān)于idea的gitignore文件編寫(xiě)及解決ignore文件不生效問(wèn)題

    關(guān)于idea的gitignore文件編寫(xiě)及解決ignore文件不生效問(wèn)題

    這篇文章主要介紹了idea的gitignore文件編寫(xiě)及解決ignore文件不生效問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • SpringBoot參數(shù)校驗(yàn)Validator框架詳解

    SpringBoot參數(shù)校驗(yàn)Validator框架詳解

    Validator框架就是為了解決開(kāi)發(fā)人員在開(kāi)發(fā)的時(shí)候少寫(xiě)代碼,提升開(kāi)發(fā)效率,Validator專(zhuān)門(mén)用來(lái)進(jìn)行接口參數(shù)校驗(yàn),今天通過(guò)本文給大家介紹SpringBoot參數(shù)校驗(yàn)Validator框架,感興趣的朋友一起看看吧
    2022-06-06
  • 解析mybatis-plus中的resultMap簡(jiǎn)單使用

    解析mybatis-plus中的resultMap簡(jiǎn)單使用

    mybatis-plus也只是聽(tīng)過(guò),可是終究沒(méi)有使用過(guò)。于是自己花幾天晚上的時(shí)間研究mybatis-plus的使用。這篇文章主要介紹了mybatis-plus的resultMap簡(jiǎn)單使用,需要的朋友可以參考下
    2021-11-11
  • SpringBoot通過(guò)@Value實(shí)現(xiàn)給靜態(tài)變量注入值詳解

    SpringBoot通過(guò)@Value實(shí)現(xiàn)給靜態(tài)變量注入值詳解

    這篇文章主要介紹了springboot如何通過(guò)@Value給靜態(tài)變量注入值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07

最新評(píng)論