java做扫雷需要学什么(怎么用Java做一个扫雷程序,要原创。。。 做好了给加100)
本篇文章给大家谈谈java做扫雷需要学什么,以及怎么用Java做一个扫雷程序,要原创。。。 做好了给加100对应的知识点,文章可能有点长,但是希望大家可以阅读完,增长自己的知识,最重要的是希望对各位有所帮助,可以解决了您的问题,不要忘了收藏本站喔。
java的扫雷程序的一些问题
楼主你好
(问题1)
lz说改变字体的大小有问题这个应该不会这样的改变字体(Font)是可以的
给lz一个例子:
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame{
private JLabel[] num= new JLabel[100];
public Test(){
super("改变字体大小");
setLayout(new GridLayout(10,10));
for(int i= 0; i< num.length; i++){
num[i]= new JLabel(""+(i+1));
num[i].setFont(new Font("宋体", Font.PLAIN, 10));
//这里如果不设置字体大小那么数字100就不能显示
getContentPane().add(num[i]);
}
setLocation(200,200);
setSize(200,200);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(3);
}
public static void main(String[] args){
new Test();
}
}
(问题2)
你使用的应该是GridLayout这个布局管理器吧
那么在使用了java自带的布局管理器后大多数都是不能修改这个布局管理器下的组件的大小和位置
如果你非要自己定义组件的大小那么就只能不使用布局管理器了(setLayout(null))
在不使用布局管理器的情况下可以自由设置组件的大小的位置但是这样很麻烦
希望能帮助你哈
java编写扫雷程序的流程图,哪位大侠帮下,小弟急用~~~
扫雷是一款相当经典的小游戏。他提供了非常友好的界面。
???下面就来讲解我的扫雷程序思想。首先我们在雷区上随机地放上雷,这可以用random类来实现。当没有雷的地方被点击后就会显示一个数字表示它周围有几个雷,这是怎么实现的呢?我们可以把整个雷区看成一个二维数组a[?i ][ j ],如雷区:
????????????? 11?12?13?14?15?16?17?18
?????????????21?22?23?24?25?26?27?28
??????????????31?32?33?34?35?36?37?38
????????????? 41?42?43?44?45?46?47?48
??????????????51?52?53?54?55?56?57?58
????我们可以发现a[ I ][ j ]周围存在着如下关系:
?????????? a[i– 1 ][ j– 1 ]????????? a[?i– 1 ] [ j ]???????????? a[ I– 1 ][ j+ 1 ]
a[????????????? a[ i ][ j– 1 ]??????????????a[?i ][ j ]????????????????????a[ i ][ j+ 1 ]
a[????????????? a[?i+ 1 ][ j- 1]??????????a[?i+ 1 ][ j ]????????????? a[ i+ 1][ j+ 1 ]
????????????于是,可以从a[ i ][ j ]的左上角顺时针开始检测。当然,如果超出边界,要用约束条件再加以判断!
????????????扫雷程序还会自动展开已确定没有雷的雷区。如果a[3][4]周围雷数为1,a[2][3]已被标示为地雷,那么a[2][4],a[2][5],a[3][3],a[3][5],a[4][3],a[4][4],a[4][5]将被展开,一直波及到不可确定的雷区。这也是实现的关键。我们可以把数组的元素设定为一个类对象,它们所属的类设定这样的一个事件:在被展开时,检查周围的雷数是否与周围标示出来的雷数相等,如果相等则展开周围未标示的雷区。这样新的雷区展开又触发这个事件,就这样递归下去,一直蔓延到不可展开的雷区。相信在了解以上两个要点后,把雷区这个类编写完全(如添加是否有雷标记,是否展开标记,周围雷数等,双击,左右单击的鼠标事件等),实现扫雷程序应是十分简单的一件事。
--------------------------------------
自定义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);
}
}
);
}
}
怎么用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;i<allButtons.length;i++){
for(int j=0;j<allButtons[i].length;j++){
this.add(allButtons[i][j]);
}
}
}
/**
*随机布雷的方法
*
*/
private void createMine(){
int n=0;
while(n<mineCount){//随机生成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;i<allButtons.length;i++){//计算每个位置的周围地雷数
for(int j=0;j<allButtons[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>=0&&j-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>=0&&j+1<allButtons[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+1<allButtons[0].length){
num+=(allButtons[i][j+1].getCountOfSurroundMines()==-1?1:0);
}
if(i+1<allButtons.length&&j-1>=0){
num+=(allButtons[i+1][j-1].getCountOfSurroundMines()==-1?1:0);
}
if(i+1<allButtons.length){
num+=(allButtons[i+1][j].getCountOfSurroundMines()==-1?1:0);
}
if(i+1<allButtons.length&&j+1<allButtons[0].length){
num+=(allButtons[i+1][j+1].getCountOfSurroundMines()==-1?1:0);
}
return num;
}
/**
*生成按钮
*
*/
private void createButtons(){
for(int i=0;i<allButtons.length;i++){
for(int j=0;j<allButtons[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;i<allButtons.length;i++){//把所有按钮都显示出来
for(int j=0;j<allButtons[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>=0&&j-1>=0){
showEmpty(i-1,j-1);
}
if(i-1>=0){
showEmpty(i-1,j);
}
if(i-1>=0&&j+1<allButtons[0].length){
showEmpty(i-1,j+1);
}
if(j-1>=0){
showEmpty(i,j-1);
}
if(j+1<allButtons[0].length){
showEmpty(i,j+1);
}
if(i+1<allButtons.length&&j-1>=0){
showEmpty(i+1,j-1);
}
if(i+1<allButtons.length){
showEmpty(i+1,j);
}
if(i+1<allButtons.length&&j+1<allButtons[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做一个扫雷程序,要原创。。。 做好了给加100的答案你都知道了吗?欢迎再次光临本站哦!