成都网站建设设计

将想法与焦点和您一起共享

java扫雷取消插旗代码 Java实现扫雷

JAVA 扫雷的一段代码求解释

int BombNum, BlockNum; // 当前雷数,当前方块数

创新互联长期为成百上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为延寿企业提供专业的成都网站设计、成都网站建设,延寿网站改版等技术服务。拥有十多年丰富建站经验和众多成功案例,为您定制开发。

int rightBomb, restBomb, restBlock; // 找到的地雷数,剩余雷数,剩余方块数

JButton start = new JButton(" 开始 ");

JPanel MenuPamel = new JPanel(); //新建一个区域,看名字是放菜单.但是打错字了.

JPanel bombPanel = new JPanel();//新建一个区域,雷区,由于雷是按钮,这里面应该都是按钮(JButton).

Bomb[][] bombButton; 2维组数,放地雷.

class Bomb extends JButton {

int num_x, num_y; // 第几号方块

int BombRoundCount; // 周围雷数

boolean isBomb; // 是否为雷

boolean isClicked; // 是否被点击

int BombFlag; // 探雷标记

boolean isRight; // 是否点击右键

public Bomb(int x, int y) {

num_x = x; //雷的位置 x,不解释

num_y = y; //雷的位置 y,不解释.获得是参数的值,所new Bomb的时候传入雷的位置,套嵌2个for循环.

BombFlag = 0; //是不是被插旗了

BombRoundCount = 9; //环绕数

isBomb = false; //是雷

isClicked = false; //被点

isRight = false; //是真的.( 以上都很好理解,直译^_^)

}

}

/* 计算方块周围雷数 */

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) { //如果不是雷

for (int x = i - 1; x i + 2; x++) { //从左边1个,到右边1个,一共3个

for (int y = j - 1; y j + 2; y++) { //我不知道,java y坐标是上还是下,总之

//邻近的上中下.(这里会多找一个自己)

if ( (x = 0) (y = 0)

(x ( (int) Math.sqrt(BlockNum)))

(y ( (int) Math.sqrt(BlockNum)))) { //因边前面 x=i-1,所以排除超出边界

//的情况

if (bombButton[x][y].isBomb == true) { //如果是雷;

count++; //加一个

}

}

}

}

bombButton[i][j].BombRoundCount = count; //设置该Bomb环绕数的值

}

}

}

}

总之就是,建个一个Bomb类. 别外有一个方法统计那些不是雷的地方,的周围有几颗雷,到时候点开,显示出来.

高分求一个运行在Eclipse环境下的java 扫雷游戏的初级代码 越小越好 越短越好 运行就好,就是初级就好了,

import java.awt.Button;

import java.util.Set;

// 每一个小方块类

public class Diamond extends Button {

private Diamond[] diamonds;

// 该小方块周围的八个方向上的小方块

private Diamond east;

private Diamond north;

private Diamond northEast;

private Diamond northWest;

private Diamond south;

private Diamond southEast;

private Diamond southWest;

private Diamond west;

private boolean isBomb;// 是否是雷

private boolean isChange;// 又没有被翻过

private int no;// 产生的方块的编号

// 持有所有小方块的引用,方便进行操作

public Diamond(Diamond[] diamonds) {

this.diamonds = diamonds;

}

// 按键时方块发生改变

public boolean change() {

this.isChange = true;// 说明已经翻过了

if(isBomb) {// 触雷

//this.setBackground(Color.red);

return true;

} else {// 不是雷,就显示周围雷的数目

//this.setLabel(this.getNearBombNo() + "");

this.setLabel(this.getNearBombNo() + "");

//if(this.getNearBombNo() == 0) {

// this.moveon();

//}

return false;

}

}

// 获得该小方块周围雷的数量

public int getNearBombNo() {

int no = 0;

if(this.northWest != null this.northWest.isBomb) no++;

if(this.north != null this.north.isBomb) no++;

if(this.northEast != null this.northEast.isBomb) no++;

if(this.east != null this.east.isBomb) no++;

if(this.southEast != null this.southEast.isBomb) no++;

if(this.south != null this.south.isBomb) no++;

if(this.southWest != null this.southWest.isBomb) no++;

if(this.west != null this.west.isBomb) no++;

return no;

}

// 获得该小方块周围的小方块

public Diamond getNearDimaond(int i) {

int index = -1;

switch (i) {

case 1:// 1表示西北,2,表示北,以此类推

index = no - 10;

if(index 1 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {

return null;

} else {

return diamonds[index];

}

case 2:

index = no - 9;

if(index 1) {

return null;

} else {

return diamonds[index];

}

case 3:

index = no - 8;

if(index 1 || no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72) {

return null;

} else {

return diamonds[index];

}

case 4:

index = no + 1;

if(no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72 || no == 81) {

return null;

} else {

return diamonds[index];

}

case 5:

index = no + 10;

if(index = 81 ||no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72 || no == 81) {

return null;

} else {

return diamonds[index];

}

case 6:

index = no + 9;

if(index 81) {

return null;

} else {

return diamonds[index];

}

case 7:

index = no + 8;

if(index = 81 || no==1 || no == 10 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {

return null;

} else {

return diamonds[index];

}

case 8:

index = no - 1;

if(no==1 || no==10 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {

return null;

} else {

return diamonds[index];

}

}

return null;

}

// 递归,set是用来装已经翻过的小方块的,不然会死循环,为什么用set,因为set是不重复的

public void moveon(SetDiamond set) {

set.add(this);// 先把自己加上

if(this.getNorthWest() != null this.getNorthWest().isBomb == false) {

this.getNorthWest().change();

if(this.getNorthWest().getNearBombNo() == 0) {

if(set.contains(this.getNorthWest()) == false)

this.getNorthWest().moveon(set);

}

set.add(this.getNorthWest());

}

if(this.getNorth() != null this.getNorth().isBomb == false) {

this.getNorth().change();

if(this.getNorth().getNearBombNo() == 0) {

if(set.contains(this.getNorth()) == false)

this.getNorth().moveon(set);

}

set.add(this.getNorth());

}

if(this.getNorthEast() != null this.getNorthEast().isBomb == false) {

this.getNorthEast().change();

if(this.getNorthEast().getNearBombNo() == 0) {

if(set.contains(this.getNorthEast()) == false)

this.getNorthEast().moveon(set);

}

set.add(this.getNorthEast());

}

if(this.getEast() != null this.getEast().isBomb == false) {

this.getEast().change();

if(this.getEast().getNearBombNo() == 0) {

if(set.contains(this.getEast()) == false)

this.getEast().moveon(set);

}

set.add(this.getEast());

}

if(this.getSouthEast() != null this.getSouthEast().isBomb == false) {

this.getSouthEast().change();

if(this.getSouthEast().getNearBombNo() == 0) {

if(set.contains(this.getSouthEast()) == false)

this.getSouthEast().moveon(set);

}

set.add(this.getSouthEast());

}

if(this.getSouth() != null this.getSouth().isBomb == false) {

this.getSouth().change();

if(this.getSouth().getNearBombNo() == 0) {

if(set.contains(this.getSouth()) == false)

this.getSouth().moveon(set);

}

set.add(this.getSouth());

}

if(this.getSouthWest() != null this.getSouthWest().isBomb == false) {

this.getSouthWest().change();

if(this.getSouthWest().getNearBombNo() == 0) {

if(set.contains(this.getSouthWest()) == false)

this.getSouthWest().moveon(set);

}

set.add(this.getSouthWest());

}

if(this.getWest() != null this.getWest().isBomb == false) {

this.getWest().change();

if(this.getWest().getNearBombNo() == 0) {

if(set.contains(this.getWest()) == false)

this.getWest().moveon(set);

}

set.add(this.getWest());

}

}

/*public Diamond[] getDiamonds() {

return diamonds;

}*/

public Diamond getEast() {

return east;

}

public int getNo() {

return no;

}

public Diamond getNorth() {

return north;

}

public Diamond getNorthEast() {

return northEast;

}

public Diamond getNorthWest() {

return northWest;

}

public Diamond getSouth() {

return south;

}

public Diamond getSouthEast() {

return southEast;

}

public Diamond getSouthWest() {

return southWest;

}

public Diamond getWest() {

return west;

}

public boolean isBomb() {

return isBomb;

}

public boolean isChange() {

return isChange;

}

public void setBomb(boolean isBomb) {

this.isBomb = isBomb;

}

public void setChange(boolean isChange) {

this.isChange = isChange;

}

public void setDiamonds(Diamond[] diamonds) {

this.diamonds = diamonds;

}

public void setEast(Diamond east) {

this.east = east;

}

public void setNo(int no) {

this.no = no;

}

public void setNorth(Diamond north) {

this.north = north;

}

public void setNorthEast(Diamond northEast) {

this.northEast = northEast;

}

public void setNorthWest(Diamond northWest) {

this.northWest = northWest;

}

public void setSouth(Diamond south) {

this.south = south;

}

public void setSouthEast(Diamond southEast) {

this.southEast = southEast;

}

public void setSouthWest(Diamond southWest) {

this.southWest = southWest;

}

public void setWest(Diamond west) {

this.west = west;

}

}

求高手帮忙一个“扫雷”游戏的JAVA的程序代码

import javax.swing.ImageIcon; //程序入口

public class Block {

String name; //名字,比如"雷"或数字

int aroundMineNumber; //周围雷的数目

ImageIcon mineIcon; //雷的图标

boolean isMine=false; //是否是雷

boolean isMark=false; //是否被标记

boolean isOpen=false; //是否被挖开

public void setName(String name) {

this.name=name;

}

//设置周围的雷数

public void setAroundMineNumber(int n) {

aroundMineNumber=n;

}

//获得周围的雷数

public int getAroundMineNumber() {

return aroundMineNumber;

}

public String getName() {

return name;

}

//判断是否是雷

public boolean isMine() {

return isMine;

}

//设置是否为雷

public void setIsMine(boolean b) {

isMine=b;

}

//设置雷的图标

public void setMineIcon(ImageIcon icon){

mineIcon=icon;

}

//获得雷的图标

public ImageIcon getMineicon(){

return mineIcon;

}

//确定雷是否被挖开

public boolean getIsOpen() {

return isOpen;

}

//设置为已经被挖开

public void setIsOpen(boolean p) {

isOpen=p;

}

//返回此处是否已经被标记

public boolean getIsMark() {

return isMark;

}

//设置此处是否已经被标记

public void setIsMark(boolean m) {

isMark=m;

}

import javax.swing.*;

import java.awt.*;

public class BlockView extends JPanel{

JLabel blockNameOrIcon; //用来显示Block对象的name、number和mineIcon属性

JButton blockCover; //用来遮挡blockNameOrIcon.

CardLayout card; //卡片式布局

BlockView(){

card=new CardLayout();

setLayout(card);

blockNameOrIcon=new JLabel("",JLabel.CENTER);

blockNameOrIcon.setHorizontalTextPosition(AbstractButton.CENTER);

blockNameOrIcon.setVerticalTextPosition(AbstractButton.CENTER);

blockCover=new JButton();

add("cover",blockCover);

add("view",blockNameOrIcon);

}

//给出视觉效果变化

public void giveView(Block block){

// 如果是雷,将对应的图标和文字更改

if(block.isMine){

blockNameOrIcon.setText(block.getName());

blockNameOrIcon.setIcon(block.getMineicon());

}

else {

int n=block.getAroundMineNumber();

if(n=1)

blockNameOrIcon.setText(""+n);

else

blockNameOrIcon.setText(" ");

}

}

public void seeBlockNameOrIcon(){

card.show(this,"view");

validate();

}

public void seeBlockCover(){

card.show(this,"cover");

validate();

}

public JButton getBlockCover(){

return blockCover;

}

}


当前文章:java扫雷取消插旗代码 Java实现扫雷
URL地址:http://chengdu.cdxwcx.cn/article/hpsgch.html