「布雷游戏java」小兔布雷游戏
今天给各位分享布雷游戏java的知识,其中也会对小兔布雷游戏进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
怎么用Java做一个扫雷程序,要原创。。。 做好了给加100
第一个JAVA文件
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* 显示所有按钮的面板
* @author Administrator
*
*/
public class AllButtonPanel extends JPanel implements ActionListener{
private int row;//行数
private int col;//列数
private int mineCount;//地雷数
private MineButton[][] allButtons;//所有按钮
public AllButtonPanel(int row,int col,int mineCount){
this.row=row;
this.col=col;
this.mineCount=mineCount;
allButtons=new MineButton[row][col];
createButtons();
createMine();
init();
}
private void init(){
this.setLayout(new GridLayout(row,col));
for(int i=0;iallButtons.length;i++){
for(int j=0;jallButtons[i].length;j++){
this.add(allButtons[i][j]);
}
}
}
/**
* 随机布雷的方法
*
*/
private void createMine(){
int n=0;
while(nmineCount){//随机生成mineCount个地雷
int i=(int)(Math.random()*row);
int j=(int)(Math.random()*col);
if(allButtons[i][j].getCountOfSurroundMines()!=-1){
allButtons[i][j].setCountOfSurroundMines(-1);
n++;
}
}
for(int i=0;iallButtons.length;i++){//计算每个位置的周围地雷数
for(int j=0;jallButtons[i].length;j++){
if(allButtons[i][j].getCountOfSurroundMines()!=-1){
allButtons[i][j].setCountOfSurroundMines(getSurroundMineCount(i,j));
}
}
}
}
/**
* 统计(i,j)坐标周围8个位置的地雷数
* @param data
* @param i
* @param j
* @return
*/
private int getSurroundMineCount(int i,int j){
int num=0;//统计周围的雷数
if(i-1=0j-1=0){
num+=(allButtons[i-1][j-1].getCountOfSurroundMines()==-1?1:0);
}
if(i-1=0){
num+=(allButtons[i-1][j].getCountOfSurroundMines()==-1?1:0);
}
if(i-1=0j+1allButtons[0].length){
num+=(allButtons[i-1][j+1].getCountOfSurroundMines()==-1?1:0);
}
if(j-1=0){
num+=(allButtons[i][j-1].getCountOfSurroundMines()==-1?1:0);
}
if(j+1allButtons[0].length){
num+=(allButtons[i][j+1].getCountOfSurroundMines()==-1?1:0);
}
if(i+1allButtons.lengthj-1=0){
num+=(allButtons[i+1][j-1].getCountOfSurroundMines()==-1?1:0);
}
if(i+1allButtons.length){
num+=(allButtons[i+1][j].getCountOfSurroundMines()==-1?1:0);
}
if(i+1allButtons.lengthj+1allButtons[0].length){
num+=(allButtons[i+1][j+1].getCountOfSurroundMines()==-1?1:0);
}
return num;
}
/**
* 生成按钮
*
*/
private void createButtons(){
for(int i=0;iallButtons.length;i++){
for(int j=0;jallButtons[i].length;j++){
allButtons[i][j]=new MineButton(i,j);
allButtons[i][j].setSize(6,6);
allButtons[i][j].addActionListener(this);//添加点击事件监听
allButtons[i][j].addMouseListener(new MouseAdapter(){//添加鼠标右键事件监听
public void mouseClicked(MouseEvent e) {
if(e.getButton()==MouseEvent.BUTTON3){
int remain=Integer.parseInt(CleanMine.remainMine.getText());
JButton b=(JButton)e.getSource();
if(b.getText().equals("")){
remain--;
CleanMine.remainMine.setText(remain+"");
b.setText("");
}else if(b.getText().equals("")){
remain++;
CleanMine.remainMine.setText(remain+"");
b.setText("");
}
}
}
});
}
}
}
public void actionPerformed(ActionEvent e) {//点击事件监听的方法
MineButton b=(MineButton)e.getSource();
int r=b.getRow();
int c=b.getCol();
if(allButtons[r][c].getCountOfSurroundMines()==-1){//如果是地雷
for(int i=0;iallButtons.length;i++){//把所有按钮都显示出来
for(int j=0;jallButtons[i].length;j++){
if(allButtons[i][j].getCountOfSurroundMines()==-1){//如果该位置是地雷
allButtons[i][j].setText("$");
}else if(allButtons[i][j].getCountOfSurroundMines()==0){//如果该位置为空(该位置不是地雷,周围8个位置也没有地雷)
allButtons[i][j].setText("");
allButtons[i][j].setBackground(Color.CYAN);
}else{//如果该位置不是地雷,但周围8个位置中有地雷
allButtons[i][j].setText(allButtons[i][j].getCountOfSurroundMines()+"");
allButtons[i][j].setBackground(Color.CYAN);
}
}
}
}else{//如果不是地雷
showEmpty(r,c);//执行排空操作
}
}
/**
* 排空方法,若(i,j)位置为空,则显示空白。然后依次递归找它周围的8个位置。
* @param data
* @param i
* @param j
*/
private void showEmpty(int i,int j){
MineButton b=allButtons[i][j];
if(b.isCleared()){
return;
}
if(allButtons[i][j].getCountOfSurroundMines()==0){
b.setBackground(Color.CYAN);
b.setCleared(true);
if(i-1=0j-1=0){
showEmpty(i-1,j-1);
}
if(i-1=0){
showEmpty(i-1,j);
}
if(i-1=0j+1allButtons[0].length){
showEmpty(i-1,j+1);
}
if(j-1=0){
showEmpty(i,j-1);
}
if(j+1allButtons[0].length){
showEmpty(i,j+1);
}
if(i+1allButtons.lengthj-1=0){
showEmpty(i+1,j-1);
}
if(i+1allButtons.length){
showEmpty(i+1,j);
}
if(i+1allButtons.lengthj+1allButtons[0].length){
showEmpty(i+1,j+1);
}
}else if(allButtons[i][j].getCountOfSurroundMines()0){
b.setText(allButtons[i][j].getCountOfSurroundMines()+"");
b.setBackground(Color.CYAN);
b.setCleared(true);
}
}
}
第二个JAVA文件
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* 扫雷游戏主界面
* @author tony.tang
*
*/
public class CleanMine extends JFrame implements ActionListener{
private JLabel text1,text2;
public static JLabel remainMine;//剩余地雷数
private JLabel time;//消耗时间
private JButton reset;//重新开始
private JPanel center;
private int row,col,mine;
public CleanMine(){
text1=new JLabel("剩余地雷:");
text2=new JLabel("消耗时间:");
remainMine=new JLabel("10");
time=new JLabel("0");
reset=new JButton("重新开始");
reset.addActionListener(this);
JMenuBar bar=new JMenuBar();
JMenu game=new JMenu("游戏");
JMenu help=new JMenu("帮助");
JMenuItem item;
game.add(item=new JMenuItem("开局"));item.addActionListener(this);
game.addSeparator();
ButtonGroup bg=new ButtonGroup();
game.add(item=new JCheckBoxMenuItem("初级",true));bg.add(item);item.addActionListener(this);
game.add(item=new JCheckBoxMenuItem("中级"));bg.add(item);item.addActionListener(this);
game.add(item=new JCheckBoxMenuItem("高级"));bg.add(item);item.addActionListener(this);
game.add(item=new JCheckBoxMenuItem("自定义..."));bg.add(item);item.addActionListener(this);
game.addSeparator();
game.add(item=new JMenuItem("退出"));item.addActionListener(this);
help.add(item=new JMenuItem("查看帮助"));item.addActionListener(this);
help.add(item=new JMenuItem("关于扫雷..."));item.addActionListener(this);
bar.add(game);
bar.add(help);
this.setJMenuBar(bar);
init();
}
private void init(){
JPanel north=new JPanel();
north.add(text1);
north.add(remainMine);
north.add(reset);
north.add(text2);
north.add(time);
this.add(north,BorderLayout.NORTH);
this.row=9;
this.col=9;
this.mine=10;
restart();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
new Thread(){
public void run(){
while(Integer.parseInt(remainMine.getText())0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
time.setText((Integer.parseInt(time.getText())+1)+"");
}
}
}.start();
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("初级")){
this.row=9;
this.col=9;
this.mine=10;
restart();
return;
}
if(e.getActionCommand().equals("中级")){
this.row=16;
this.col=16;
this.mine=40;
restart();
return;
}
if(e.getActionCommand().equals("高级")){
this.row=16;
this.col=30;
this.mine=99;
restart();
return;
}
if(e.getActionCommand().equals("重新开始")){
restart();
return;
}
}
private void restart(){
if(center!=null){
this.remove(center);
}
center=new AllButtonPanel(row,col,mine);
this.add(center,BorderLayout.CENTER);
this.remainMine.setText(mine+"");
this.time.setText("0");
this.setSize(col*30,row*30+10);
this.setResizable(false);
this.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
new CleanMine();
}
}
第三个JAVA文件.
import javax.swing.JButton;
import java.awt.*;
public class MineButton extends JButton {
private int row;
private int col;
private boolean cleared=false;
private int countOfSurroundMines;//周围地雷数,如果本按钮是雷,则为-1;
public MineButton(int row,int col){
this.row=row;
this.col=col;
this.setMargin(new Insets(0,0,0,0));
}
public int getCol() {
return col;
}
public int getRow() {
return row;
}
public boolean isCleared() {
return cleared;
}
public void setCleared(boolean cleared) {
this.cleared = cleared;
}
public int getCountOfSurroundMines() {
return countOfSurroundMines;
}
public void setCountOfSurroundMines(int countOfSurroundMines) {
this.countOfSurroundMines = countOfSurroundMines;
}
}
全部编译以后就可以执行了
怎样用JAVA实现扫雷游戏
要详细代码?还是只要启动?
java编写实现,代码如下:import Java.awt.*;
import java.awt.event.*;
import javax.Swing.*;
/*按扭类*/
class Bomb extends JButton
{
public int num_x,num_y; //第几号方块
public int BombRoundCount; //周围雷数
public boolean isBomb; //是否为雷
public boolean isClicked; //是否被点击
public int BombFlag; //探雷标记
public boolean isRight; //是否点击右键
public Bomb(int x,int y)
{
BombFlag = 0;
num_x = x;
num_y = y;
BombRoundCount = 0;
isBomb = false;
isClicked = false;
isRight = false;
}
}
/*窗口及算法实现类*/
class MainBomb extends JFrame implements ActionListener,MouseListener
{
public JTextField text;
public Label nowBomb,setBomb;
public int BlockNum,BombNum; //当前方块数当前雷数
public Icon icon_bomb = new ImageIcon("Bomb.gif"); //踩雷
public Icon icon_bomb_big = new ImageIcon("bomb_big.gif"); //踩雷标记
public Icon icon_flag = new ImageIcon("flag.gif"); //雷标记
public Icon icon_question = new ImageIcon("question.gif"); //疑惑是否有雷
public JButton start = new JButton(" 开始 ");
public Panel MenuPamel = new Panel();
public Panel mainPanel = new Panel();
public Bomb[][] bombButton;
/*界面设计*/
public MainBomb()
{
super("扫雷 Aaron2004制作 2004.8 ");
BlockNum = 64;
BombNum = 10;
Container c=getContentPane();
c.setBackground(Color.gray);
c.setLayout(new BorderLayout());
text=new JTextField("10 ",3);
nowBomb = new Label("当前雷数"+" "+BombNum+"");
setBomb= new Label("设置地雷数");
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
BombNum = Integer.parseInt(text.getText().trim());
if(BombNum = 10 BombNum 50 )
replay();
else
{
JOptionPane msg = new JOptionPane();
JOptionPane.showMessageDialog(null,"您设置的地雷数太多了,请重设!","错误",2);
}
}
} );
MenuPamel.add(setBomb);
MenuPamel.add(text);
MenuPamel.add(start);
MenuPamel.add(nowBomb);
c.add(MenuPamel,"North");
mainPanel.setLayout(new GridLayout( (int)Math.sqrt(BlockNum) , (int)Math.sqrt(BlockNum)) );
bombButton=new Bomb[ (int)Math.sqrt(BlockNum) ][];
for(int i = 0 ; i (int)Math.sqrt(BlockNum) ; i++)
{
bombButton[ i ]=new Bomb[ (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 ].setForeground( Color.gray);
bombButton[ i ][ j ].addActionListener(this);
bombButton[ i ][ j ].addMouseListener(this);
}
for(int i = 0 ; i (int)Math.sqrt(BlockNum) ; i++ )
for(int j = 0 ; j (int)Math.sqrt(BlockNum) ; j++ )
mainPanel.add(bombButton[ i ][ j ]);
c.add(mainPanel,"Center");
startBomb();
setSize(400,400);
setLocation(350,200);
setResizable(false);
}
/*布雷*/
public void startBomb()
{
for(int i=0;iBombNum;i++)
{
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)
i--;
else
bombButton[ x ][ y ].isBomb=true ;
}
}
/*重新开始*/
public void replay()
{
nowBomb.setText("当前雷数"+" "+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 ].setEnabled(true);
bombButton[ i ][ j ].setText("");
bombButton[ i ][ j ].setIcon(null);
}
startBomb();
}
/*是否挖完了所有的雷*/
public void isWin()
{
int findBomb=0; //找到的地雷数
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 ].isRight == true)
findBomb++;
}
if( findBomb == Integer.parseInt(text.getText().trim()) )
{
JOptionPane msg = new JOptionPane();
JOptionPane.showMessageDialog(this,"您挖完了所有的雷,您胜利了!","您胜利了",2);
}
}
/*计算方块周围雷数 */
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;
//当需要检测的单元格本身无地雷的情况下,统计周围的地雷个数
if (bombButton[ i ][ j ].isBomb != true) {
if ( (i - 1 = 0) (j - 1 = 0)) {
if (bombButton[i - 1][j - 1].isBomb == true) {
count += 1; //检测左上方空格是否是地雷
}
}
if ( (i - 1 = 0)) {
if (bombButton[i - 1][ j ].isBomb == true) {
count += 1; //检测上方空格是否为地雷
}
}
if ( (i - 1 = 0) (j + 1 = (int)Math.sqrt(BlockNum)-1)) {
if (bombButton[i - 1][j + 1] .isBomb == true) {
count += 1; //检测右上方是否为地雷
}
}
if ( (j - 1 = 0)) {
if (bombButton[ i ][j - 1] .isBomb == true) {
count += 1; //检测左边是否为地雷
}
}
if ( (i = 0) (j + 1 = (int)Math.sqrt(BlockNum)-1)) {
if (bombButton[ i ][j + 1].isBomb == true) {
count += 1; //右边
}
}
if ( (j - 1 = 0) (i + 1 = (int)Math.sqrt(BlockNum)-1)) {
if (bombButton[i + 1][j - 1].isBomb == true) {
count += 1; //左下
}
}
if ( (i + 1 = (int)Math.sqrt(BlockNum)-1)) {
if (bombButton[i + 1][ j ].isBomb == true) {
count += 1; //下
}
}
if ( (j + 1 = (int)Math.sqrt(BlockNum)-1) (i + 1 = Math.sqrt(BlockNum)-1)) {
if (bombButton[i + 1][j + 1].isBomb == true) {
count += 1; //右下
}
}
bombButton[ i ][ j ].BombRoundCount = count;
}
}
}
}
/**当选中的位置为空,则翻开周围的地图**/
public void isNull(Bomb[][] bombButton,Bomb ClickecButton)
{
int i,j;
i=ClickecButton.num_x;
j=ClickecButton.num_y;
if (ClickecButton.isBomb==true) {
}
else {
if ( (i - 1 = 0) (j - 1 = 0)) { //检测左上方空格是否是空
if (bombButton[i - 1][j - 1].isBomb == false bombButton[i - 1][j - 1].isClicked == false bombButton[i - 1][j - 1].isRight == false) {
bombButton[i - 1][j - 1].setText((bombButton[i - 1][j - 1].BombRoundCount)+"");
bombButton[i - 1][j - 1].setEnabled(false);
bombButton[i - 1][j - 1].isClicked=true;
}
}
if ( (i - 1 = 0)) { //检测上方空格是否为空
if (bombButton[i - 1][ j ] .isBomb == false bombButton[i - 1][ j ].isClicked == false bombButton[i - 1][ j ].isRight == false) {
bombButton[i - 1][ j ].setText((bombButton[i - 1][ j ].BombRoundCount)+"");
bombButton[i - 1][ j ].setEnabled(false);
bombButton[i - 1][ j ].isClicked=true;
}
}
if ( (i - 1 = 0) (j + 1 = ((int)Math.sqrt(BlockNum)-1)) ) { //检测右上方是否为空
if (bombButton[i - 1][j + 1] .isBomb == false bombButton[i - 1][j + 1].isClicked == false bombButton[i - 1][j + 1].isRight == false) {
bombButton[i - 1][j + 1].setText((bombButton[i - 1][j + 1].BombRoundCount)+"");
bombButton[i - 1][j + 1].setEnabled(false);
bombButton[i - 1][j + 1].isClicked=true;
}
}
if ( (j - 1 = 0)) { //检测左边是否为空
if (bombButton[ i ][j - 1].isBomb == false bombButton[ i ][j - 1].isClicked == false bombButton[ i ][j - 1].isRight == false) {
bombButton[ i ][j - 1].setText((bombButton[ i ][j - 1].BombRoundCount)+"");
bombButton[ i ][j - 1].setEnabled(false);
bombButton[ i ][j - 1].isClicked=true;
}
}
if ( (i = 0) (j + 1 = ((int)Math.sqrt(BlockNum)-1)) ) { //检测右边空格是否是空
if (bombButton[ i ][j + 1].isBomb == false bombButton[ i ][j + 1].isClicked == false bombButton[ i ][j + 1].isRight == false) {
bombButton[ i ][j + 1].setText((bombButton[ i ][j + 1].BombRoundCount)+"");
bombButton[ i ][j + 1].setEnabled(false);
bombButton[ i ][j + 1].isClicked=true;
}
}
if ( (j - 1 = 0) (i + 1 = ((int)Math.sqrt(BlockNum)-1)) ) { //检测左下空格是否是空
if (bombButton[i + 1][j - 1].isBomb == false bombButton[i + 1][j - 1].isClicked == false bombButton[i + 1][j - 1].isRight == false) {
bombButton[i + 1][j - 1].setText((bombButton[i + 1][j - 1].BombRoundCount)+"");
bombButton[i + 1][j - 1].setEnabled(false);
bombButton[i + 1][j - 1].isClicked=true;
}
}
if ( (i + 1 = ((int)Math.sqrt(BlockNum)-1)) ) { //检测下边空格是否是空
if (bombButton[i + 1][ j ].isBomb == false bombButton[i + 1][ j ].isClicked == false bombButton[i + 1][ j ].isRight == false) {
bombButton[i + 1][ j ].setText((bombButton[i + 1][ j ].BombRoundCount)+"");
bombButton[i + 1][ j ].setEnabled(false);
bombButton[i + 1][ j ].isClicked=true;
}
}
if ( (j + 1 = ((int)Math.sqrt(BlockNum)-1) ) (i + 1 = ((int)Math.sqrt(BlockNum)-1)) ) { //检测右下边空格是否是空
if (bombButton[i + 1][j + 1].isBomb == false bombButton[i + 1][j + 1].isClicked == false bombButton[i + 1][j + 1].isRight == false) {
bombButton[i + 1][j + 1].setText((bombButton[i + 1][j + 1].BombRoundCount)+"");
bombButton[i + 1][j + 1].setEnabled(false);
bombButton[i + 1][j + 1].isClicked=true;
}
}
if ( (i - 1 = 0) (j - 1 = 0))//检测左上
isNull(bombButton,bombButton[i - 1][j - 1]);
if ( (i - 1 = 0))
isNull( bombButton,bombButton[i - 1][ j ]);//检测上方
if ( (i - 1 = 0) (j + 1 = (int)Math.sqrt(BlockNum)-1))
isNull( bombButton,bombButton[i - 1][j + 1]);//检测右上
if ( (j - 1 = 0))
isNull(bombButton,bombButton[i][j - 1]);//检测左边
if ( (i = 0) (j + 1 = ((int)Math.sqrt(BlockNum)-1)) )
isNull(bombButton,bombButton[i][j + 1]);//检测右边
if ( (j - 1 = 0) (i + 1 = ((int)Math.sqrt(BlockNum)-1)) )
isNull(bombButton,bombButton[i + 1][j - 1]); //检测左下
if ( (i + 1 = ((int)Math.sqrt(BlockNum)-1)) ) //检测下
isNull(bombButton,bombButton[i + 1][ j ]);
if ( (j + 1 = ((int)Math.sqrt(BlockNum)-1)) (i + 1 = ((int)Math.sqrt(BlockNum)-1)) ) //检测右下
isNull(bombButton,bombButton[i + 1][j + 1]);
}
}
public void actionPerformed(ActionEvent e)
{
CountRoundBomb();
if(((Bomb)e.getSource()).isBomb==false ((Bomb)e.getSource()).isClicked == false)
{
((Bomb)e.getSource()).setText(( ((Bomb)e.getSource()).BombRoundCount )+"");
((Bomb)e.getSource()).isClicked=true;
((Bomb)e.getSource()).setIcon(null);
((Bomb)e.getSource()).setEnabled(false);
if((((Bomb)e.getSource()).BombRoundCount) == 0)
isNull(bombButton,(Bomb)e.getSource());
isWin();
}
else if(((Bomb)e.getSource()).isBomb == true)
{
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 ].setIcon(icon_bomb);
}
((Bomb)e.getSource()).setIcon(icon_bomb_big);
JOptionPane msg = new JOptionPane();
JOptionPane.showMessageDialog(this,"你踩到地雷了,按确定重来","你踩到地雷了",2);
replay();
}
}
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(BombNum 0 bombSource.isRight == false ){
bombSource.setIcon(icon_flag);
bombSource.isRight = true;
BombNum--;
}
isWin();
nowBomb.setText("当前雷数"+" "+BombNum+"");
}
else if(bombSource.BombFlag == 2)
{
if( (BombNum !=0 ) ||(BombNum ==0 (bombSource.getIcon()==icon_flag)) )
BombNum++;
bombSource.setIcon(icon_question);
nowBomb.setText("当前雷数"+" "+BombNum+"");
}
else if(bombSource.BombFlag == 0)
{
bombSource.setIcon(null);
bombSource.isRight = false;
}
}
}
public void mouseEntered(MouseEvent e)
{}
public void mouseReleased(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void mousePressed(MouseEvent e)
{}
}
/*主类*/
public class Main
{
public static void main(String args[])
{
(new MainBomb()).show();
}
}
java程序设计,扫雷,帮助看看
这里是随机生成二维坐标,页面方面可以使用JButton
import java.util.Random;
public class SweepMine {
int row=20;
int column=20;
int array[][]=new int[row][column];
int mineNum=40;
boolean bool=true;
public void init(){
Random r =new Random();
for(int i=0;irow;i++)
for(int j=0;jcolumn;j++)
array[i][j]=0;
for(int k=0;kmineNum;k++){
bool=true;//由于随机生成的坐标可能已是地雷,所以这里设置标记,使其循环获取
while(bool){
int x=r.nextInt(row-1);
int y=r.nextInt(column-1);
if(x=row)
x=x%row;
if(y=column)
y=y%column;
if(array[x][y]==0){
array[x][y]=1;
bool=false;
}
}
}
}
public void print(){
int n=0;
for(int i=0;irow;i++){
for(int j=0;jcolumn;j++){
System.out.print(array[i][j]+" ");
if(array[i][j]==1)
n++;
}
System.out.println();
}
System.out.println("地雷数量:"+n);
}
public static void main(String[]args){
SweepMine sw=new SweepMine();
sw.init();
sw.print();
}
}
关于布雷游戏java和小兔布雷游戏的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-25,除非注明,否则均为
原创文章,转载请注明出处。