最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年04月22日 16:25:20   作者:tf1997  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xià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("重新開始");
 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());
 
 //添加重來、選擇難度按鈕
 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("初級難度");
 combobox.addItem("中級難度");
 combobox.addItem("高級難度");
 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 == "初級難度") {
 constant.leiCount = 43;
 ResetGame();
 } else if(item == "中級難度"){
 constant.leiCount = 63;
 ResetGame();
 } else if(item == "高級難度"){
 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();//重新開始游戲
 } 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,你贏了!"); 
 }
 
 //使用遞歸方法打開格子
 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
 
 }
}

常量類

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
}

效果圖

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

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

相關(guān)文章

  • Java SpringBoot自動(dòng)裝配原理詳解

    Java SpringBoot自動(dòng)裝配原理詳解

    這篇文章主要介紹了詳解Spring Boot自動(dòng)裝配的原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-09-09
  • 手把手教你設(shè)置IntelliJ IDEA 的彩色代碼主題的圖文教程

    手把手教你設(shè)置IntelliJ IDEA 的彩色代碼主題的圖文教程

    本文給出一系列 IntelliJ IDEA 代碼的彩色主題,感興趣的朋友一起看看吧
    2018-01-01
  • Java SPI簡單應(yīng)用案例詳解

    Java SPI簡單應(yīng)用案例詳解

    這篇文章主要介紹了Java SPI簡單應(yīng)用案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Java實(shí)現(xiàn)貪吃蛇大作戰(zhàn)小游戲的示例代碼

    Java實(shí)現(xiàn)貪吃蛇大作戰(zhàn)小游戲的示例代碼

    本文主要介紹了Java實(shí)現(xiàn)貪吃蛇大作戰(zhàn)小游戲的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • java解析xml之sax解析xml示例分享

    java解析xml之sax解析xml示例分享

    SAX基于事件的解析,解析器在一次讀取XML文件中根據(jù)讀取的數(shù)據(jù)產(chǎn)生相應(yīng)的事件,由應(yīng)用程序?qū)崿F(xiàn)相應(yīng)的事件處理邏輯,即它是一種“推”的解析方式;這種解析方法速度快、占用內(nèi)存少,但是它需要應(yīng)用程序自己處理解析器的狀態(tài),實(shí)現(xiàn)起來會(huì)比較麻煩
    2014-01-01
  • Java Lambda表達(dá)式和函數(shù)式接口實(shí)例分析

    Java Lambda表達(dá)式和函數(shù)式接口實(shí)例分析

    這篇文章主要介紹了Java Lambda表達(dá)式和函數(shù)式接口,結(jié)合實(shí)例形式分析了Java8 Lambda表達(dá)式和函數(shù)式接口相關(guān)原理、用法及操作注意事項(xiàng),需要的朋友可以參考下
    2019-09-09
  • Java使用Ehcache緩存框架的技術(shù)指南

    Java使用Ehcache緩存框架的技術(shù)指南

    Ehcache 是 Java 平臺(tái)下一個(gè)開源、高性能的分布式緩存框架,常用于提高系統(tǒng)性能和可擴(kuò)展性,它能夠幫助開發(fā)者緩存頻繁訪問的數(shù)據(jù),從而減少對數(shù)據(jù)庫和其他持久化存儲(chǔ)的訪問壓力,本文給大家介紹了Java使用Ehcache緩存框架的技術(shù)指南,需要的朋友可以參考下
    2025-03-03
  • Java數(shù)據(jù)結(jié)構(gòu)之二叉排序樹的實(shí)現(xiàn)

    Java數(shù)據(jù)結(jié)構(gòu)之二叉排序樹的實(shí)現(xiàn)

    二叉排序樹(Binary Sort Tree),又稱二叉查找樹(Binary Search Tree),亦稱二叉搜索樹。本文詳細(xì)介紹了二叉排序樹的原理,并且提供了Java代碼的完全實(shí)現(xiàn)。需要的可以參考一下
    2022-01-01
  • Spring MVC學(xué)習(xí)之DispatcherServlet請求處理詳析

    Spring MVC學(xué)習(xí)之DispatcherServlet請求處理詳析

    這篇文章主要給大家介紹了關(guān)于Spring MVC學(xué)習(xí)教程之DispatcherServlet請求處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • springboot獲取URL請求參數(shù)的多種方式

    springboot獲取URL請求參數(shù)的多種方式

    這篇文章主要介紹了springboot獲取URL請求參數(shù)的多種方式,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01

最新評論

鄄城县| 离岛区| 济源市| 皋兰县| 河北省| 潢川县| 密山市| 额济纳旗| 凤阳县| 厦门市| 漠河县| 威远县| 深州市| 晴隆县| 西宁市| 安乡县| 黑水县| 久治县| 启东市| 天祝| 安顺市| 澄迈县| 广宁县| 桐庐县| 郴州市| 天镇县| 呼和浩特市| 竹山县| 中阳县| 综艺| 封开县| 克拉玛依市| 阿城市| 昌邑市| 栾城县| 浏阳市| 沈阳市| 五常市| 呼玛县| 正阳县| 惠东县|