这些东西早忘光了说下我的理解希望可以帮助你把。
你先想好用什么来做,java swing里面做这个的,我记得这个东西可以直接用button来做的。
这些button形成一个矩形,用一个数组来记录每个位置,比如point (x,y)这种。
用一个map来存放每个位置上button的状态,比如用0表示是雷,1不是。2是已经显示空白的区域,最后可能就是map((x,y),1);这种。
然后基本就是一些逻辑问题了,比如随机地雷位置(设置3里面随机数设置多少个是01)怎么右键点击显示周围雷个数,这些都是不少工作。
我能想到的就这些,毕竟过了很久了,你现在要是上学的话就抓紧写这个东西,我感觉你有了思路查资料的话一个礼拜差不多也就能看得出能不能做出来,不能做出来在找源码学吧,这些东西自己先做一遍和看源码在学习效果差挺多。
很简单,用文件读写!
------------------
模块:
1) 文件读写模块(字段:用户|时间|成绩)
2) 分数大小判断模块
-------------------
思路:
1)一个游戏结束后,读成绩文件,判断分数够高(前10名或其他),恭喜用户要求输用户名,保存写入文件。
2)游戏中途查看英雄榜。直接读成绩文件,遍历输出即可。 追问有没有源文件神马的???? 回答木有木有
问题已经细化啦,亲,剩下的场面应该要Hold住了
读写文件(fread, fwrite)
数值排序 (冒泡法、选择法)
--------------
百科都有:
首先你要在你的电脑上安装jdk。你可以在后面链接地址下载适合你自己的版本(>
在你的电脑上配置java环境变量,主要是配置path和classpath。你可以百度java环境变量配置,可以找到很多java环境变量配置方法。配置完毕,可以在cmd窗口下用java -version来查看是否配置成功。如果显示出java版本相关的信息表示配置成功,可以进行下一步了。
编译你的源代码,cmd窗口下把路径改变(cd)到你源代码文件所在的路径,然后用javac 源文件名编译,例如javac Hellojava(需要注意的是源文件名需要是你文件public类的类名,如果你的文件有public类的话)。当然你也可以不改变(cd)到源文件所在的路径,你的文件就需要加上绝对路径就可以了。例如:javac e:\src\Hellojava
运行你编译好的文件,java Hello(需要注意运行的时候没有后缀java或者class),同样你可以不改变路径用绝对路径运行,例如:java e:\src\Hello如果你的代码中有窗口这样的类似的图形化界面,你就需要用javaw来运行。
另外,你可以使用eclipse,NetBeans这样的集成开发环境(IDE)来写代码,这样方便很多。
我有源代码:绝对可以通过,不过比较简单而已,对学生而言应该可以了吧,这是以前写的:
一下两个文件放在一个包里就行了
/
This class defines a class that contains some useful
attributions and some methods to set or get these attributions
/
import javaxswingJButton;
public class ExButton extends JButton
{
//if the button is a mine,the isMine will be true
private boolean isMine;
//to check if a button has been visited is useful
//when using the recursion in the Game class
private boolean isVisited;
//the row number of the button
int btnRowNumber;
//the column number of the button
int btnColumnNumber;
//the mines around a button
int minesAround=0;
public void setIndex(int btnRowNumber,int btnColumnNumber)
{
thisbtnRowNumber=btnRowNumber;
thisbtnColumnNumber=btnColumnNumber;
}
public int getRowNumber()
{
return thisbtnRowNumber;
}
public int getColumnNumber()
{
return thisbtnColumnNumber;
}
public void setVisited(boolean isVisited)
{
thisisVisited=isVisited;
}
public boolean getVisited()
{
return thisisVisited;
}
public void setMine(boolean isMine)
{
thisisMine=isMine;
}
public boolean getMine()
{
return thisisMine;
}
//the attribute of minesAround add one each
//time a mine is put down around the button
public void addMinesAround()
{
thisminesAround++;
}
public int getMinesAround()
{
return thisminesAround;
}
}
-------------------------------------------------
/
File Name: Gamejava
Author: Tian Wei Student Number: Email: xiangchensuiyue@163com
Assignment number: #4
Description: In this program ,a frame will be created which contains
ten "mines"When you click a button ,it will present the
number of mines around or a message of losing the game
(if the button is a mine)You can make a right click to
sign a dengerous button as wellWhen all the mines have
been signed ,a message box of winning the game will jump
to the screenAnd the the message of the time you used in
the gameMore over,you can click the button on the bottom
to restart the game
/
import javaxswing;
import javaawt;
import javaawtevent;
import javautil;
import javautilRandom;
import javautilTimer;
public class Game extends JFrame{
//define some menber variables
private long minute=0,second=0;//take down time used int the game
private ExButton[][] btn;//two-dimension array present the buttons
private JLabel label;
private JButton restart;//restart button
private int minesRemained;//remained mines that you have not signed
private boolean thisTry=true;
private JLabel timeUsed=new JLabel ();
private Random rand=new Random();
private final int ROWS,COLUMNS;
private final int MINES;
// the constuctor
public Game(int rows,int columns,int mines)
{
super("Find mines");
thisROWS=rows;
thisCOLUMNS=columns;
thisMINES=mines;
minesRemained=MINES;
Timer timer=new Timer();//Timer's object to timer the game
timerschedule(new MyTimer(), 0, 1000);//do the function every second
Container container=getContentPane();
containersetLayout(new BorderLayout());
//Jpanel in the Container
JPanel jpanel=new JPanel();
jpanelsetLayout(new GridLayout(ROWS,COLUMNS));
restart=new JButton("click me to restart the game");
JPanel jpanel2=new JPanel();
//Another JPanel in the Container
jpanel2setLayout(new FlowLayout());
jpanel2add(timeUsed);
jpanel2add(restart);
ButtonListener restartHandler=new ButtonListener();
restartaddActionListener(restartHandler);
containeradd(jpanel2,BorderLayoutSOUTH);
btn=new ExButton[ROWS+2][COLUMNS+2];
//initialize the buttons
for(int i=0;i<=ROWS+1;i++)
{
for(int j=0;j<=COLUMNS+1;j++)
{
btn[i][j]=new ExButton();
btn[i][j]addMouseListener(new MouseClickHandler());
btn[i][j]setIndex(i,j);
btn[i][j]setVisited(false);
}
}
for(int i=1;i<=ROWS;i++)
for(int j=1;j<=COLUMNS;j++)
jpaneladd(btn[i][j]);
containeradd(jpanel,BorderLayoutCENTER);
JPanel jpanel3=new JPanel ();
label=new JLabel();
labelsetText("Mines remaining "+MINES);
jpanel3add(label);
containeradd(jpanel3,BorderLayoutNORTH );
thisaddMines();
thisaddMinesAround();
}
//randomly put ten mines
private void addMines()
{
for(int i=1;i<=MINES;i++)
{
int raInt1=randnextInt(ROWS);
int raInt2=randnextInt(COLUMNS);
if((raInt1==0)||(raInt2==0)||btn[raInt1][raInt2]getMine())
i--;
else
btn[raInt1][raInt2]setMine(true);
}
}
//take down the mines around a button
private void addMinesAround()
{
for(int i=1;i<=ROWS;i++)
{
for(int j=1;j<=COLUMNS;j++)
{
if(btn[i][j]getMine())
{
btn[i][j-1]addMinesAround();
btn[i][j+1]addMinesAround();
btn[i-1][j-1]addMinesAround();
btn[i-1][j]addMinesAround();
btn[i-1][j+1]addMinesAround();
btn[i+1][j-1]addMinesAround();
btn[i+1][j]addMinesAround();
btn[i+1][j+1]addMinesAround();
}
}
}
}
//if a button clicked is a empty one,then use a recursion
//to find all the empty buttons around
private void checkEmpty(ExButton button)
{
buttonsetVisited(true);
int x=buttongetRowNumber();
int y=buttongetColumnNumber();
buttonsetBackground(Colorwhite);
if((buttongetMinesAround()==0)&&(x>=1)&&(x<=ROWS)
&&(y>=1)&&(y<=COLUMNS))
{
if(!btn[x][y-1]getVisited())
checkEmpty(btn[x][y-1]);
if(!btn[x][y+1]getVisited())
checkEmpty(btn[x][y+1]);
if(!btn[x-1][y]getVisited())
checkEmpty(btn[x-1][y]);
if(!btn[x+1][y]getVisited())
checkEmpty(btn[x+1][y]);
if(!btn[x-1][y-1]getVisited())
checkEmpty(btn[x-1][y-1]);
if(!btn[x-1][y+1]getVisited())
checkEmpty(btn[x-1][y+1]);
if(!btn[x+1][y-1]getVisited())
checkEmpty(btn[x+1][y-1]);
if(!btn[x+1][y+1]getVisited())
checkEmpty(btn[x+1][y+1]);
}
else if(buttongetMinesAround()>0)
buttonsetText(""+buttongetMinesAround());
}
//the main function
public static void main(String args[])
{
String rows,columns,mines;
int rowNumber,columnNumber,mineNumber;
rows=JOptionPaneshowInputDialog("Enter the rows of the game");
columns=JOptionPaneshowInputDialog("Enter the columns of the game");
mines=JOptionPaneshowInputDialog("Enter the mines of the game");
rowNumber=IntegerparseInt(rows);
columnNumber=IntegerparseInt(columns);
mineNumber=IntegerparseInt(mines);
Game frame=new Game(rowNumber,columnNumber,mineNumber);
framesetTitle("Find Mines");
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
framesetLocation(220, 80);
framesetSize(600, 600 );
framesetVisible(true);
}
//there are three inner class below
//The first inner class is used to do the mouse listener's
//functionWhen you click a button ,it will present the
//number of mines around or a message of losing the game
//(if the button is a mine)You can make a right click to
//sign a dengerous button as wellWhen ten mines have been
//signed,it will check whether all the signed ones are mines
private class MouseClickHandler extends MouseAdapter
{
public void mouseClicked(MouseEvent event)
{
//get the button that been clicked
ExButton eventButton=new ExButton();
eventButton=(ExButton)eventgetSource();
eventButtonsetVisited(true);
//when it is a right click
if(eventisMetaDown())
{
if(eventButtongetText()=="#")
{
minesRemained++;
eventButtonsetText("");
}
else
{
if((eventButtongetBackground()==Colorwhite)||
(eventButtongetText()!=""))
{
//do nothing
}
else
{
minesRemained--;
eventButtonsetText("#");
}
}
labelsetText("Mines remaining "+minesRemained);
//check if all the signed buttons are mines
if(minesRemained==0)
{
for(int i=1;i<=ROWS;i++)
for(int j=1;j<=COLUMNS;j++)
{
if(btn[i][j]getMine()&&btn[i][j]getText()!="#")
thisTry=false;
if(!btn[i][j]getMine()&&btn[i][j]getText()=="#")
thisTry=false;
}
if(thisTry)
{
//win the game
JOptionPaneshowMessageDialog(null, "You succeed" +
" in this experience!");
JOptionPaneshowMessageDialog(null, "Time used:"+
timeUsedgetText());
}
else//you have wrongly signed one or more mines
JOptionPaneshowMessageDialog(null, "You have wrongly " +
"signed one or more mines,please check it and go on!");
}
}
else if(eventisAltDown())
{
//do nothing
}
else
{//normally click(left click)
if(eventButtongetText()=="#")
{
//do nothing
}
else if(eventButtongetMine())
{
//lose the game
JOptionPaneshowMessageDialog(null, "What a pity!" +
"You failed!" );
//show all the mines to the loser
for(int i=1;i<=ROWS;i++)
for(int j=1;j<=COLUMNS;j++)
{
if(btn[i][j]getMine())
btn[i][j]setBackground(ColorBLACK);
}
JOptionPaneshowMessageDialog(null, "Time used: 0"+
minute+":"+second);
}
else
{
if(eventButtongetMinesAround()==0)
{
//call the function to find all the empty buttons around
checkEmpty(eventButton);
}
else
eventButtonsetText(""+eventButtongetMinesAround());
}
}
}
}
//The second class is to listen to the button which used to
//restart the gameIn this class,it will dispose the old frame
//and create a new one(Of course,the mines's position have
//been changed)
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
//what to dispose is the object of Game class
Gamethisdispose();
//the same code as in the main function
Game frame=new Game(ROWS,COLUMNS,MINES);
framesetTitle("Find Mines");
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
framesetSize(600, 600 );
//make sure the frame is at the center of the screen
framesetLocation(220, 80);
framesetVisible(true);
}
}
//The last class is the class that will be used in the
//Timer's object timerIt should inherit the class TimerTask
//It is the task that the Timer will do every second
private class MyTimer extends TimerTask
{
public void run()
{
second+=1;
minute+=second/60;
second=second%60;
//change the text of the time used in the game
timeUsedsetText("Time used 0"+minute+":"+second);
}
}
}//end of the class Game
VC60默认使用Multi-Byte Character Set,新的visual studio创建的工程默认使用unicode character set,所以所有的函数会使用unicode版本。
你可以手动做成使用ansi版本,比如
GetEnvironmentVariable,函数就直接写成GetEnvironmentVariableA,
或者就把工程的设置改成使用MBCS
import javaawt;
import javaxswing;
import javautilRandom;
import javaawtevent;
class Min extends JPanel //雷的类
{
//备注:鼠标的左键 = 1;右键 = 3;中键 = 2
private int flag = 0,statu = 0; //定义雷的属性 0:没有打开 1:打开 2:标示为雷 3:不确定
//flag = 0 不是雷 ; flag = 1是雷
private int but,count = 0; //but:哪一个鼠标键被按下去了 count:这个区域周围有多少个雷
private int mx = 0,my = 0,mw = 10; //定义雷的坐标和宽度
public Min() //构造函数
{
statu = 0;
}
public Min(int f,int x,int y,int w)
//构造函数
{
flag = f;
mx = x;
my = y;
mw = w;
}
public int getFlag(){return flag;}
public int getStatu(){return statu;}
public int getMx(){return mx;}
public int getMy(){return my;}
public int getMw(){return mw;}
public int getCount(){return count;}
public void setFlag(int f){flag = f;}
public void setCount(int c){count = c;}
public void setData(int f,int x,int y,int w,int s)
//传递值
{
flag = f;
mx = (x-1)w;
my = (y-1)w;
mw = w-1;
statu = s;
}
//根据你点击鼠标的不同来改变雷的属性
public int sendKey(int key)
{
//返回值,如果游戏结束则返回-1
int rtn = 1;
if(key == 3)
{
switch(statu)
{
case 1:
break;
case 2:
statu = 3;
break;
case 3:
statu = 0;
break;
case 0:
statu = 2;
break;
}
rtn = 1;
}
if(key == 1 && statu == 0)
{
switch(flag)
{
case 0:
statu = 1;
rtn = 2;
break;
case 1:
statu = 1;
rtn = -1;
break;
}
}
return rtn;
}
}
class DrawPanel extends JPanel
{
private int i,j;
private int f = 0; //if f = 1 then game over ,if f =2 then win
private int chx = 0,chy = 0; //专门记录坐标x,y的值
private int msum = 6,ksum = 0; //msum:雷的个数,ksum:标示雷的个数
private int bx = 10,by = 10,bw = 40; //bx,by:棋盘的大小,bw:棋子的大小
public Min board[][] = {
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
{new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min(),new Min()},
};
// 画坐标为ax,ay区域的雷的状态
public void draw(Graphics g,int ax,int ay)
{
int x,y,w; // 坐标x,y;和宽度:w
int s,c,flag; //状态;雷的个数;
int cx = bw/2 - 4;
int cy = bw/2 + 4;
x = board[ax][ay]getMx();
y = board[ax][ay]getMy();
w = board[ax][ay]getMw();
s = board[ax][ay]getStatu();
c = board[ax][ay]getCount();
flag= board[ax][ay]getFlag();
switch(s)
{
case 0: //没有打开状态
{
gsetColor(Colorblack);
gfillRect(x,y,w,w);
break;
}
case 1: //打开状态
{
gsetColor(Colorblue);
gfillRect(x,y,w,w);
if(c != 0 && flag == 0) //此处没有雷
{
gsetColor(Colorred);
gdrawString(StringvalueOf(c),x + cx,y + cy);
}
if(flag == 1) //此处有雷
{
gsetColor(Colorred);
gfillRect(x,y,w,w);
gsetColor(Colorblue);
gdrawString(" 雷",x + cx,y + cy);
}
break;
}
case 2: //标雷状态
{
gsetColor(Colorgreen);
gfillRect(x,y,w,w);
gsetColor(Colorblue);
gdrawString(" 旗",x + cx,y + cy);
break;
}
case 3: //不确定状态
{
gsetColor(Colorblack);
gfillRect(x,y,w,w);
gsetColor(Colorred);
gdrawString("",x + cx,y + cy);
break;
}
default:
break;
}
}
// 没有图形器的绘图函数:画出坐标ax,ay的雷的状态和图形
public void draw(int ax,int ay)
{
Graphics g;
g = thisgetGraphics();
draw(g,ax,ay);
}
//打开周围没有雷的地方,并且绘画所在区域点击左键触发
public int openNoMin(int ax,int ay)
{
int i,j;
if(ax<1||ay<1||ax>bx||ay>by) return 0; //鼠标点击的区域出界了
if(board[ax][ay]getStatu() != 0) return 0; //如果此区域打开了,返回
board[ax][ay]sendKey(1); //如果返回值等于-1,就说明游戏结束
draw(ax,ay);
if(board[ax][ay]getFlag() == 1)
//如果游戏结束,把所有的雷都显示出来
{
for(i = 1;i<=bx;i++)
{
for(j = 1;j <= by;j++)
{
if(board[i][j]getFlag() == 1)
{
board[i][j]sendKey(1);
draw(i,j);
}
}
}
return -1;
}
//如果游戏没有结束
if(board[ax][ay]getCount() > 0)
{
ksum ++;
return 1; //周围有雷,就不用打开周围地区
}
if(board[ax][ay]getCount() == 0 && board[ax][ay]getFlag() == 0)
//周围没有雷,打开周围地区,直到有雷的地区
{
openNoMin(ax-1,ay-1);openNoMin(ax,ay-1);openNoMin(ax+1,ay-1);
openNoMin(ax-1,ay ); openNoMin(ax+1,ay );
openNoMin(ax-1,ay+1);openNoMin(ax,ay+1);openNoMin(ax+1,ay+1);
}
ksum ++;
return 1;
}
//计算坐标x,y的周围雷的个数
public int getCount(int ai,int aj)
{
int sum = 0;
if(board[ai][aj]getFlag() == 1)
{
return sum;
}
if(ai>1&&aj>1&&ai<bx&&aj<by)
{
sum = board[ai-1][aj-1]getFlag()+ board[ai][aj-1]getFlag()+ board[ai+1][aj-1]getFlag()+
board[ai-1][aj ]getFlag()+ board[ai+1][aj ]getFlag()+
board[ai-1][aj+1]getFlag()+ board[ai][aj+1]getFlag()+ board[ai+1][aj+1]getFlag();
}
if(ai==1&&aj==1)
{
sum = board[ai+1][aj ]getFlag()+
board[ai][aj+1]getFlag()+ board[ai+1][aj+1]getFlag();
}
if(ai==1&&aj==by)
{
sum = board[ai][aj-1]getFlag()+ board[ai+1][aj-1]getFlag()+
board[ai+1][aj ]getFlag();
}
if(ai==bx&&aj==1)
{
sum = board[ai-1][aj ]getFlag()+
board[ai-1][aj+1]getFlag()+ board[ai][aj+1]getFlag();
}
if(ai==bx&&aj==by)
{
sum = board[ai-1][aj-1]getFlag()+ board[ai][aj-1]getFlag()+
board[ai-1][aj ]getFlag();
}
if(ai==1&&aj>1&&aj<by)
{
sum = board[ai][aj-1]getFlag()+ board[ai+1][aj-1]getFlag()+
board[ai+1][aj ]getFlag()+
board[ai][aj+1]getFlag()+ board[ai+1][aj+1]getFlag();
}
if(ai==bx&&aj>1&&aj<by)
{
sum = board[ai-1][aj-1]getFlag()+ board[ai][aj-1]getFlag()+
board[ai-1][aj ]getFlag()+
board[ai-1][aj+1]getFlag()+ board[ai][aj+1]getFlag();
}
if(ai>1&&ai<bx&&aj==1)
{
sum = board[ai-1][aj ]getFlag()+ board[ai+1][aj ]getFlag()+
board[ai-1][aj+1]getFlag()+ board[ai][aj+1]getFlag()+ board[ai+1][aj+1]getFlag();
}
if(ai>1&&ai<bx&&aj==by)
{
sum = board[ai-1][aj-1]getFlag()+ board[ai][aj-1]getFlag()+ board[ai+1][aj-1]getFlag()+
board[ai-1][aj ]getFlag()+ board[ai+1][aj ]getFlag();
}
return sum;
}
// 传入参数:几列,几行,宽度,雷数
public void initMin(int ax,int ay,int aw,int as)
{
int k = 1; //表明产生的第几个雷
Random r; //随机数
f = 0; //f=0表示游戏还没有结束
ksum = 0;
bx = ax;
by = ay;
bw = aw;
msum = as;
r = new Random();
//初始化底盘的值
for(i = 1;i <= bx;i++)
{
for(j=1;j<=by;j++)
{
board[i][j]setData(0,i,j,bw,0);
}
}
// 随机产生雷
while(k <= msum)
{
i = rnextInt(bx)+1;
j = rnextInt(by)+1;
if(board[i][j]getFlag() != 1)
{
board[i][j]setFlag(1);
k++;
}
}
// 非雷区的周围有几个雷,初始化其值
for(i = 1;i <= bx;i++)
{
for(j=1;j<=by;j++)
{
board[i][j]setCount(getCount(i,j));
}
}
setBackground(Colorwhite);
repaint();
}
// 构造函数
public DrawPanel(int ax,int ay,int aw,int as)
{
initMin(ax,ay,aw,as);
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
int r;
if(f != 0) return; //如果游戏结束,返回
chx = megetX();
chy = megetY();
if(megetButton() != 1)
{
board[chx/bw+1][chy/bw+1]sendKey(megetButton());
draw(chx/bw+1,chy/bw+1);
}
else if(megetButton() == 1)
{
if(openNoMin(chx/bw+1,chy/bw+1) == -1)
{
f = 1;
repaint();
}
else if ( ksum + msum == bxby )
{
f = 2;
repaint();
}
}
}
}
);
}
// 重画所有的图形,包括一些修饰的图形
public void paint(Graphics g)
{
int x,y,w;
int s;
int cx = bw/2 - 4;
int cy = bw/2 + 4;
gclearRect(0,0,600,600);
for(i=1;i<=bx;i++)
{
for(j=1;j<=by;j++)
{
draw(g,i,j);
}
}
if(f == 1)
{
Font f = new Font("11",1,70);
Font fo = ggetFont();
gsetColor(Colorwhite);
gsetFont(f);
//gsetSize();
gdrawString("Game Over",0,200);
gsetFont(fo);
}
if( f == 2 )
{
Font f = new Font("11",1,70);
Font fo = ggetFont();
gsetColor(Colorwhite);
gsetFont(f);
//gsetSize();
gdrawString("You win!",0,200);
gsetFont(fo);
}
}
};
// 主类和程序的入口
public class Mine extends JFrame implements ActionListener
{
Container cp = getContentPane();
JButton bt = new JButton("开局");
Label l1 = new Label("列:");
Label l2 = new Label("行:");
Label l3 = new Label("宽度:");
Label l4 = new Label("雷的个数:");
TextField tf1 = new TextField("10",2); //列
TextField tf2 = new TextField("10",2); //行
TextField tf3 = new TextField("40",2); //宽度
TextField tf4 = new TextField("15",2); //雷的个数
int x=10,y=10,w=40,sum=15;
DrawPanel dp = new DrawPanel(x,y,w,sum);
public Mine()
{
setBackground(Colorwhite);
cpsetLayout(null);
cpadd(dp);
cpadd(bt);
cpadd(tf1);
cpadd(tf2);
cpadd(tf3);
cpadd(tf4);
cpadd(l1);
cpadd(l2);
cpadd(l3);
cpadd(l4);
l1setBounds(20 ,10,20,20);
tf1setBounds(40,10,20,20);
l2setBounds(70,10,20,20);
tf2setBounds(90,10,20,20);
l3setBounds(120,10,40,20);
tf3setBounds(160,10,20,20);
l4setBounds(190,10,60,20);
tf4setBounds(250,10,20,20);
btsetBounds(300,10,80,20);
dpsetBounds(20,40,xw,yw);
setResizable(false);
setSize(xw+40,yw+80);
setTitle(" 扫雷");
show();
btaddActionListener(this);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{Systemexit(0);}
}
);
}
public void actionPerformed(ActionEvent e)
{
if(egetSource() == bt)
{
//x = IntegerparseInt(tf1getText());
//y = IntegerparseInt(tf2getText());
//w = IntegerparseInt(tf3getText());
sum = IntegerparseInt(tf4getText());
setSize(xw+40,yw+80);
dpsetBounds(20,40,xw,yw);
show();
dpinitMin(x,y,w,sum);
}
}
public static void main(String args[])
{
new Mine();
}
};
电脑自带的游戏扫雷程序在Windows“游戏”目录下,是可以查看的,具体查看步骤如下:
1、以Win7系统为例,点击系统左下角开始菜单;
2、在打开的页面中,点击页面右侧“游戏”目录;
3、在打开窗口页面中,找到“扫雷”游戏程序,双击该扫雷程序文件;
4、接下来,即可打开扫雷游戏主页面,可以进行游戏了;
5、还可以在开始菜单中,找到“所有程序”目录,选择“游戏-扫雷”程序也可以打开游戏界面。
以上就是关于求大神指点如何用java做扫雷小游戏 详细 ...有源代码吗全部的内容,包括:求大神指点如何用java做扫雷小游戏 详细 ...有源代码吗、现在在用C语言编写一个扫雷小游戏,里边要有成绩记录和英雄榜,请问各位高手大侠这一模块怎么写、怎么在电脑上运行Java源程序代码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)