Java实现扫雷(2)

Java实现扫雷(2),第1张

*/

//清楚原来的方形

public void clearoldretangle(Graphics D gg int c int r)

{

if(hasclickbomb)

diamonds[r][c] clickshow(gg r c)

else

{

if(!diamonds[r][c] isOpened())

diamonds[r][c] draw(gg r c)

else

diamonds[r][c] clickshow(gg r c)

}

}

/*

//清除原来的圆形

public void clearOval(Graphics D gg int c int r)

{

if(c<col &&r<row)

{

for(int y=r y<r+ &&y<rowy++)

{

if(y<)continue

for(int x=c x<c+ &&x<colx++)

{

if(x>= )

{

if(hasclickbomb)

diamonds[y][x] clickshow(gg y x)

else

{

if(!diamonds[y][x] isOpened())

diamonds[y][x] draw(gg y x)

else

diamonds[y][x] clickshow(gg y x)

}

}

}

}

}

}

*/

//设置炸d

public void buildBombInMap()

{

int x= y=

for(int i= i<bombcount)

{

x=(int)(Math random()*col)

y=(int)(Math random()*row)

if(!diamonds[y][x] i *** omb())

{

diamonds[y][x] setbomb()

i++

}

}

countRoundBomb()//计算炸d

}

//计算附近的炸d

public void countRoundBomb()

{

for(int i= i<rowi++)

{

for(int j= j<colj++)

{

if(diamonds[i][j] i *** omb())

continue

else

{

//开始计算每个附近的炸d

int count=

for(int k=i k<i+ &&k<rowk++)

{

 猜银正纯 if(k<)continue

for(int l=j l<穗清宴j+ &&l<coll++)

{

if(l>= )

{

if(diamonds[k][l] i *** omb())

count++

}

}

}

if(count!= )

diamonds[i][j] setShowByte(String valueOf(count))

}

}

}

}

//踩中地雷后全部显示

public void showAllDiamonds(int ro int co)

{

Graphics D gg=(Graphics D)this getGraphics()

gg setRenderingHint(RenderingHints KEY_ANTIALIASING RenderingHints VALUE_ANTIALIAS_ON)

for(int r= r<rowr++)

{

for(int c= c<colc++)

{

if(!diamonds[r][c] i *** omb())

diamonds[r][c] setFillColor(Color white)

else if(diamonds[r][c] i *** omb())

diamonds[r][c] setFillColor(Color red)

diamonds[r][c] clickshow(gg r c)

}

}

}

//重载paint

public void paint(Graphics g)

{

Graphics D gg=(Graphics D)g

gg setRenderingHint(RenderingHints KEY_ANTIALIASING RenderingHints VALUE_ANTIALIAS_ON)

for(int y= y<rowy++)

{

for(int x= x<colx++)

{

if(hasclickbomb)

diamonds[y][x] clickshow(gg y x)

else

{

if(!diamonds[y][x] isOpened())

diamonds[y][x] draw(gg y x)

else

diamonds[y][x] clickshow(gg y x)

}

}

}

gg dispose()

}

}

Diamonds java文件

import java awt BasicStroke

import java awt Color

import java awt Graphics

import java awt Graphics D

import java awt RenderingHints

/**

* 单元方格

* @author dragon

*

*/

public class Diamonds

{

private Color fillcolor=Color darkGray

private Color rimcolor=Color blue

private boolean bomb=false

private String showbyte=

private int diamondWidth=

private boolean opened=false

private boolean signbomb=false

public Diamonds(Color c int width)

{

this rimcolor=c

this diamondWidth=width

}

public Diamonds(int width)

{

this diamondWidth=width

}

/**

* 设置是否被标记为炸d

* dragon

*Sep

* @param b

*/

public void setSignBomb(boolean b)

{

this signbomb=b

}

public boolean isSignBomb()

{

return this signbomb

}

/**

* 设置方格的边长

* dragon

*Sep

* @param width

*/

public void setDiamondWidth(int width)

{

this diamondWidth=width

}

/**

* 返回当前方格的字符串

* dragon

*Sep

* @return

*/

public String getShowByte()

{

return this showbyte

}

/**

* 设置方格的字符串

* dragon

*Sep

* @param b

*/

public void setShowByte(String b)

{

showbyte=b

}

/**

* 复位

*/

public void reset()

{

fillcolor=Color DARK_GRAY

rimcolor=Color blue

this signbomb=false

bomb=false

showbyte=

opened=false

}

/**

* 判断方格是否已经被打开

*/

public boolean isOpened()

{

return this opened

}

/**

* 打开该方格

*/

public void Opened()

{

this opened=true

}

/**

* 判断是否是炸d

*/

public boolean i *** omb()

{

return bomb

}

/**

* 设置为炸d

*/

public void setbomb()

{

this bomb=true

}

/**

* 点击时的显示函数

*/

public void clickshow(Graphics D gg int r int c)

{

gg setStroke(new BasicStroke( f))

if(bomb)

{

if(opened)

this fillcolor=Color red

gg setColor(this fillcolor)

gg fillRect(c*diamondWidth r*diamondWidth diamondWidth diamondWidth)

gg setColor(this rimcolor)

gg drawRect(c*diamondWidth r*diamondWidth diamondWidth diamondWidth)

gg setColor(Color black)

gg drawOval(c*diamondWidth+diamondWidth/ r*diamondWidth+diamondWidth/ diamondWidth/ diamondWidth/ )

}

else

{

gg setColor(this fillcolor)

gg fillRect(c*diamondWidth r*diamondWidth diamondWidth diamondWidth)

gg setColor(this rimcolor)

gg drawRect(c*diamondWidth r*diamondWidth diamondWidth diamondWidth)

gg setColor(Color black)

gg drawString(showbyte c*diamondWidth+diamondWidth/ r*diamondWidth+diamondWidth* / )

}

}

/**

* 绘画单元格

*/

public void draw(Graphics D gg int r int c)

{

gg setStroke(new BasicStroke( f))

gg setColor(this fillcolor)

gg fillRect(c*diamondWidth r*diamondWidth diamondWidth diamondWidth)

gg setColor(this rimcolor)

gg drawRect(c*diamondWidth r*diamondWidth diamondWidth diamondWidth)

}

/**

* 设置边框颜色

*/

public void setRimColor(Color c)

{

rimcolor=c

}

/**

* 设置填充颜色

*/

public void setFillColor(Color c)

{

this fillcolor=c

}

lishixinzhi/Article/program/Java/hx/201311/26439

没做过,就是说点啥想法。

是不是银闷晌可以用二维数组来定义每个格子,这样判断周围8个格子的时候就可以[i][j]

i和j+-1来查看有没有雷,雷也可以存在一个二维数组里,有雷存1,没雷存0,你再把它也保存锋锋起来,存档的话可以添加一个初始化判断,罩侍有存档就调出二维数组,去布局显示。^^

要详细代码?还是只要启动?

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=0i<BombNumi++)

{

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 = 0i <(int)Math.sqrt(BlockNum) i++)

for(int j = 0j <(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 = 0i <(int)Math.sqrt(BlockNum)i++) {

for (int j = 0j <(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=0i<(int)Math.sqrt(BlockNum)i++)

for(int j=0j<(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()

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12370917.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存