·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » Java扫雷程序,初试Java

Java扫雷程序,初试Java

类别: JAVA教程  评论数:0 总得分:0
自定义JButton子类:

//ExtendButton.java

package ly.java;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ExtendButton extends JButton
{
private int Button_Pos;
private boolean Button_Status;
private boolean Button_Visited;

public int SetPostion(int pos)
{
this.Button_Pos = (pos >= 0 && pos <= 81) ? pos : 0;
return this.Button_Pos ;
}
public int GetPostion()
{
return this.Button_Pos;
}
public boolean SetStatus(boolean sta)
{
this.Button_Status = sta;
return this.Button_Status;
}
public boolean GetStatus()
{
return this.Button_Status;
}
public boolean Visited()
{
return this.Button_Visited;
}
public boolean SetVisited(boolean vis)
{
this.Button_Visited = vis;
return this.Button_Visited;
}
}



游戏类

//Game.java


package ly.java.game;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import ly.java.ExtendButton;

public class Game extends JFrame implements ActionListener{
private Container myContainer;
private GridLayout myLayout;
ExtendButton[] myButton = new ExtendButton[81];

private Game()
{
this.setTitle("Game");
this.setSize( 500,500 );
this.InitButton();
}

private void InitButton()
{
myContainer = getContentPane();
myLayout = new GridLayout( 9, 9, 1, 1 );
myContainer.setLayout( myLayout );
for(int i=0; i < 81; i++)
{
myButton[i] = new ExtendButton();
myButton[i].SetPostion(i);
myContainer.add(myButton[i]);
myButton[i].addActionListener( this );
}
System.gc();
this.SetBomb(13);
show();
}

private void SetBomb(int count)
{
int counter = 0;
int tempint;
while(counter != count)
{
tempint = ( int )(Math.random() * 81);
if(!myButton[tempint].GetStatus())
{
myButton[tempint].SetStatus(true);
counter++;
}
}
}

private void ShowBomb()
{
for(int i = 0; i < 81; i++)
{
if(myButton[i].GetStatus())
{
myButton[i].setBackground( new Color(0,0,0) );
}
}
}

private void CheckButton(ExtendButton TempButton)
{

if(TempButton.GetStatus())
{
JOptionPane.showMessageDialog( null, "You Failed!", "Game",JOptionPane.INFORMATION_MESSAGE );
this.ShowBomb();
return;
}
int[] CircleNum = new int[8];
int temp = 0;
if(!TempButton.Visited())
{

CircleNum[0] = TempButton.GetPostion() - 9;
CircleNum[0] = (CircleNum[0] < 0) ? -1 : CircleNum[0];
CircleNum[1] = TempButton.GetPostion() - 8;
CircleNum[1] = (CircleNum[1] < 0 || CircleNum[1] % 9 == 0) ? -1 : CircleNum[1];
CircleNum[2] = TempButton.GetPostion() + 1;
CircleNum[2] = (CircleNum[2] % 9 == 0) ? -1 : CircleNum[2];
CircleNum[3] = TempButton.GetPostion() + 10;
CircleNum[3] = (CircleNum[3] > 80 || CircleNum[3] % 9 == 0) ? -1 : CircleNum[3];
CircleNum[4] = TempButton.GetPostion() + 9;
CircleNum[4] = (CircleNum[4] > 80) ? -1 : CircleNum[4];
CircleNum[5] = TempButton.GetPostion() + 8;
CircleNum[5] = (CircleNum[5] > 80 || CircleNum[5] % 8 == 0) ? -1 : CircleNum[5];
CircleNum[6] = TempButton.GetPostion() - 1;
CircleNum[6] = (CircleNum[6] % 8 == 0) ? -1 : CircleNum[6];
CircleNum[7] = TempButton.GetPostion() - 10;
CircleNum[7] = (CircleNum[7] < 0 || CircleNum[7] % 8 == 0) ? -1 : CircleNum[7];
for(int i = 0; i < 8; i++)
{
if(CircleNum[i] != -1)
{
if(myButton[CircleNum[i]].GetStatus() && !myButton[CircleNum[i]].Visited()) temp++;
}
}
if(temp > 0)
{
TempButton.SetVisited( true );
TempButton.setText( String.valueOf(temp) );
temp = 0;
}
else if(temp == 0)
{
TempButton.SetVisited( true );
TempButton.setBackground(new Color( 125,152,0 ));
for(int i = 0; i < 8; i++)
{
if(CircleNum[i] != -1 && !myButton[CircleNum[i]].Visited())
{
CheckButton(myButton[CircleNum[i]]);
}
}
}
}
}

public void actionPerformed(ActionEvent e)
{
CheckButton((ExtendButton)e.getSource());
}

public static void main(String[] args)
{
Game newGame = new Game();
newGame.addWindowListener(
new WindowAdapter(){
public void windowClosing( WindowEvent e )
{
System.exit(0);
}
}
);
}
}






-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1