Java實(shí)現(xiàn)簡單的掃雷小程序
前兩天看了個(gè)掃雷的視頻,于是自己跟著做了下,感覺還不是很難。
初學(xué)Java的同學(xué)可以嘗試自己操作下Java小程序
這樣子才能提高自己的理解能力和編程水平
不用多說了,直接上代碼吧!
具體代碼操作如下:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.*;
public class saolei implements ActionListener {
JFrame frame=new JFrame("掃雷游戲");
JButton reset=new JButton("重來");
Container container=new Container();
//游戲數(shù)據(jù)結(jié)構(gòu)
final int row=20;
final int col=20;
final int leiCount=30;
JButton [][] buttons=new JButton[row][col];
int [][] counts=new int[row][col];
final int LEICODE=10;
// 構(gòu)造函數(shù)
public saolei(){
//1、設(shè)置窗口
frame.setSize(900, 800);
frame.setResizable(true);//是否可改變窗口大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
//2、添加重來按鈕
addResetButton();
//添加按鈕
addButtons();
//埋雷
addLei();
//添加雷的計(jì)算
calcNeiboLei();
frame.setVisible(true);
}
public void addResetButton(){
reset.setBackground(Color.green);
reset.setOpaque(true);
reset.addActionListener(this);
frame.add(reset,BorderLayout.NORTH);
}
public void addLei(){
Random rand=new Random();
int randRow,randCol;
for(int i=0;i<leiCount;i++){
randRow=rand.nextInt(row);
randCol=rand.nextInt(col);
if(counts[randRow][randCol]== LEICODE){
i--;
}else{
counts[randRow][randCol]=LEICODE;
//buttons[randRow][randCol].setText("*");
}
}
}
public void addButtons(){
frame.add(container,BorderLayout.CENTER);
container.setLayout(new GridLayout(row,col));
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
JButton button=new JButton();
button.setBackground(Color.yellow);
button.setOpaque(true);
button.addActionListener(this);
buttons[i][j]=button;
container.add(button);
}
}
}
public void calcNeiboLei(){
int count;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
count=0;
if(counts[i][j]==LEICODE) continue;
if(i>0 && j>0 && counts[i-1][j-1]==LEICODE) count++;
if(i>0&&counts[i-1][j]==LEICODE) count++;
if(i>0 && j<19 && counts[i-1][j+1]==LEICODE) count++;
if(j>0 && counts[i][j-1]==LEICODE) count++;
if(j<19 && counts[i][j+1]==LEICODE) count++;
if(i<19&&j>0&&counts[i+1][j-1]==LEICODE) count++;
if(i<19&&counts[i+1][j]==LEICODE) count++;
if(i<19&&j<19&&counts[i+1][j+1]==LEICODE) count++;
counts[i][j]=count;
//buttons[i][j].setText(counts[i][j]+"");
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton button=(JButton)e.getSource();
if(button.equals(reset)){
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
buttons[i][j].setText("");
buttons[i][j].setEnabled(true);
buttons[i][j].setBackground(Color.yellow);
counts[i][j]=0;
}
}
addLei();
calcNeiboLei();
}else{
int count=0;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(button.equals(buttons[i][j])){
count=counts[i][j];
if(count==LEICODE){
LoseGame();
}else{
openCell(i,j);
checkWin();
} return;
}
}
}
}
}
void checkWin(){
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(buttons[i][j].isEnabled()==true && counts[i][j]!=LEICODE) return;
}
}
JOptionPane.showMessageDialog(frame, "Yeah,你贏了!");
}
void openCell(int i,int j){
if(buttons[i][j].isEnabled()==false) return;
buttons[i][j].setEnabled(false);
if(counts[i][j]==0){
if(i>0 && j>0 && counts[i-1][j-1]!=LEICODE) openCell(i-1, j-1);
if(i>0&&counts[i-1][j]!=LEICODE) openCell(i-1, j);
if(i>0 && j<19 && counts[i-1][j+1]!=LEICODE) openCell(i-1, j+1);
if(j>0 && counts[i][j-1]!=LEICODE) openCell(i, j-1);
if(j<19 && counts[i][j+1]!=LEICODE) openCell(i, j+1);
if(i<19&&j>0&&counts[i+1][j-1]!=LEICODE) openCell(i+1, j-1);
if(i<19&&counts[i+1][j]!=LEICODE) openCell(i+1, j);
if(i<19&&j<19&&counts[i+1][j+1]!=LEICODE) openCell(i+1, j+1);
buttons[i][j].setText(counts[i][j]+"");
}else{
buttons[i][j].setText(counts[i][j]+"");
}
}
void LoseGame(){
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
int count=counts[i][j];
if(count==LEICODE){
buttons[i][j].setText("X");
buttons[i][j].setBackground(Color.red);
buttons[i][j].setEnabled(false);
}else{
buttons[i][j].setText(count+"");
buttons[i][j].setEnabled(false);
}
}
}
}
public static void main(String[] args) {
saolei lei=new saolei();
}
}
大致具體的代碼和結(jié)果如下


更多精彩游戲,請(qǐng)參考專題《java經(jīng)典小游戲》
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Jenkins來構(gòu)建GIT+Maven項(xiàng)目的方法步驟
這篇文章主要介紹了使用Jenkins來構(gòu)建GIT+Maven項(xiàng)目,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Java設(shè)計(jì)模式之訪問模式(Visitor者模式)介紹
這篇文章主要介紹了Java設(shè)計(jì)模式之訪問模式(Visitor者模式)介紹,本文講解了為何使用Visitor模式、如何使用Visitor模式、使用Visitor模式的前提等內(nèi)容,需要的朋友可以參考下2015-03-03
使用Spring掃描Mybatis的mapper接口的三種配置
這篇文章主要介紹了使用Spring掃描Mybatis的mapper接口的三種配置,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
如何解決Mybatis--java.lang.IllegalArgumentException: Result Maps
這兩天因?yàn)轫?xiàng)目需要整合spring、struts2、mybatis三大框架,但啟動(dòng)的時(shí)候總出現(xiàn)這個(gè)錯(cuò)誤,困擾我好久,折騰了好久終于找到問題根源,下面小編給大家分享下問題所在及解決辦法,一起看看吧2016-12-12
Java concurrency集合之ConcurrentLinkedQueue_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java concurrency集合之ConcurrentLinkedQueue,需要的朋友可以參考下2017-06-06
Spring?Boot整合Kafka+SSE實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)展示
本文主要介紹了Spring?Boot整合Kafka+SSE實(shí)現(xiàn)實(shí)時(shí)數(shù)據(jù)展示2024-06-06
Java中l(wèi)ist.foreach()和list.stream().foreach()用法詳解
在Java中List是一種常用的集合類,用于存儲(chǔ)一組元素,List提供了多種遍歷元素的方式,包括使用forEach()方法和使用Stream流的forEach()方法,這篇文章主要給大家介紹了關(guān)于Java中l(wèi)ist.foreach()和list.stream().foreach()用法的相關(guān)資料,需要的朋友可以參考下2024-07-07
Spring AOP實(shí)現(xiàn)復(fù)雜的日志記錄操作(自定義注解)
Spring AOP實(shí)現(xiàn)復(fù)雜的日志記錄操作(自定義注解),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Java 延遲隊(duì)列的常用的實(shí)現(xiàn)方式
這篇文章主要介紹了Java 延遲隊(duì)列的常用的實(shí)現(xiàn)方式,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下2021-04-04

