導入Java . util . *;
導入Java . io . *;
導入javax . swing . *;
導入javax . swing . timer;
導入Java . awt . event . *;
導入javax . swing . border . *;
/**
* & ltp & gt標題:掃雷
*
* & ltp & gt描述:學JAVA以來玩的第壹個遊戲,程序可能有壹些bug。我希望妳能提出來討論。
*如果要測試記錄文件,可以把地雷數量改的少壹點。
* while(陸上
*如果((landmineNum-1)==0),有三位,表示剩余雷霆數。
* for(int I = 0;我& lt99;i++)& lt;/p & gt;
* & ltp & gt版權所有:版權所有(c)2006 & lt;/p & gt;
*
* & ltp & gt公司:私人& lt/p & gt;
*
* @作者cqp
* @版本演示
*/
公共類邵磊擴展JFrame {
/* *類的屬性和控件的實例化*/
ImageIcon按鈕圖標;//按鈕的圖片;
HashMap map = new HashMap();//雷霆和數字的狀態,鍵是位置(0-479),值是狀態,0-6是數字,7是雷霆;
HashMap flag _ minel = new HashMap();//按鈕上標註的標記,如問號、復選標記、取消,8標註雷霆,9標註問號,10默認為空;
JMenuBar file = new JMenuBar();//菜單欄;
JMenu game = new JMenu();//菜單按鈕;
JMenuItem start = new JMenuItem();//菜單項;
JMenuItem record = new JMenuItem();//菜單項;
JMenuItem quit = new JMenuItem();//菜單項;
JMenuItem clearReocrd = new JMenuItem();//菜單項;
JMenu help = new JMenu();//菜單按鈕;
JButton[]cardsBtn = new JButton[480];//480個按鈕;
JButton begin BTN = new JButton();//開始按鈕;
JPanel pane = new JPanel();//雷區面板;
JPanel paneTime = new JPanel();//計數器所在的面板;
JOptionPane save record = new JOptionPane();//保存記錄對話框;
JTextField landmineTally = new JTextField(" 99 ");//剩余地雷計數器;
JTextField time tly = new JTextField(" 0 ");//時間計數器;
GridLayout GridLayout 1 = new GridLayout();//網格布局;
定時器timer;//線程設施;
String[]mine = new String[99];//存儲地雷的位置,用來判斷地雷的位置是否重復;
sl frame _ button _ actionAdatper[]button click = new sl frame _ button _ actionAdatper[480];//雷區按鈕的事件類;
int mouse key = 0;//獲取鼠標先按了哪個鍵,用來判斷鼠標是否同時按了左右鍵;
int time count = 0;//時間計數器;
/* *施工方法*/
公共邵磊(){
嘗試{
JB init();
} catch(異常異常){
exception . printstacktrace();
}
}
/* *接口設置*/
私有void jbInit()拋出異常{
getContentPane()。set layout(null);
this.setJMenuBar(文件);
game . settext(" game ");
start . settext(" opening ");
start . addactionlistener(new sl frame _ start _ action adapter(this));
Record.setText("排行榜");
record . addactionlistener(new sl frame _ record _ action adapter(this));
quit . settext(" exit ");
quit . addactionlistener(new sl frame _ quit _ action adapter(this));
help . settext(" help ");
ClearReocrd.setText("清除記錄");
clearReocrd . addactionlistener(new sl frame _ clearReocrd _ actionAdapter(this));
landmineTally.setBounds(新矩形(5,5,40,25));
landmineTally.setBackground(新顏色(0,0,0));
landmineTally.setForeground(新顏色(255,0,0));
landminetally . set Font(New Java . awt . Font(" Times New Roman ",字體。粗體,20));
landminetally . set border(border factory . createbevelborder(1));
landminetally . set editable(false);
timeTally.setBounds(新矩形(520,5,50,25));
timeTally.setBackground(新顏色(0,0,0));
timeTally.setForeground(新顏色(255,0,0));
time tally . sethorizontal alignment(4);
time tally . set Font(New Java . awt . Font(" Times New Roman ",字體。粗體,20));
time tally . set border(border factory . createbevelborder(0));
time tally . set editable(false);
beginBtn.setBounds(新矩形(250,5,25,25));
begin BTN . set border(border factory . createbevelborder(0));
begin BTN . addactionlistener(new sl frame _ begin BTN _ actionAdatper(this));
begin BTN . seticon(createImageIcon(" images/laugh . jpg "));
paneTime.setBounds(新矩形(0,0,585,35));
pane time . set border(border factory . createetchedborder());
paneTime.setLayout(空);
panetime . add(landmineTally);
paneTime.add(時間上);
panetime . add(begin BTN);
pane.setBounds(新矩形(0,35,590,320));
pane . set layout(gridlayout 1);
gridlayout 1 . set columns(30);
gridlayout 1 . set rows(16);
file.add(遊戲);
file.add(幫助);
game.add(開始);
game.add(記錄);
game.add(退出);
help . add(clearReocrd);
this.getContentPane()。添加(窗格);
this.getContentPane()。add(paneTime);
action listener listener = new action listener(){//自定義線程
public void action performed(action event e){
time count++;
time tally . settext(integer . tostring(time count));
}
};
timer = new Timer(1000,監聽器);//添加線程,每1秒執行壹次;
for(int I = 0;我& lt480;I++) //實例化480個小按鈕並添加到面板窗格中
{
cards BTN[I]= new JButton();
卡片Btn[i]。setText(" ");//按鈕上的文本被刪除;
卡片Btn[i]。set border(null);//按鈕的邊框被移除;
pane . add(cardsBtn[I]);
}
}
/* *主方法*/
公共靜態void main(String[] args) {
邵磊幀=新邵磊();
frame.setSize(580,410);
Frame.setTitle("掃雷");
frame . show();
frame . setresizable(false);//不能修改表單大小。
frame . setdefaultcloseoperation(JFrame。EXIT _ ON _ CLOSE);//單擊關閉按鈕時呈直線。
}
/* *用戶定義的向按鈕添加圖片的方法*/
受保護的靜態圖像圖標createImageIcon(字符串路徑){
Java . net . URL imgURL = Shao lei . class . get resource(路徑);
如果(imgURL!= null) {
返回新的image icon(imgURL);
}否則{
System.err.println("找不到文件:"+path);
返回null
}
}
/* *菜單按鈕事件,開始遊戲*/
public void start _ action performed(action event e){
start();//初始化;
算術();//計算地雷的位置;
計算();//計算地雷的分布;
timer . start();//線程開始的時間;
}
/* *開始遊戲按鈕的事件*/
public void begin BTN _ mouse clicked(action event e){
start _ action performed(e);//直接調用菜單的事件;
}
/* *用戶自定義的方法,遊戲開始的地方,其中按鈕的屬性和狀態被初始化;*/
void start(){
time count = 0;//時間從0開始;
landminetally . settext(" 99 ");//剩余礦數99;
for(int I = 0;我& lt480;i++){
卡片Btn[i]。setIcon(null);//清除按鈕上的圖片;
map.put( Integer.toString(i),integer . tostring(10));//分布狀態都是10,表示為空;
flag _ Ming . put(integer . tostring(I),integer . tostring(10));//標記的狀態都是10;
卡片Btn[i]。remove mouse listener(button click[I]);//移除雷區所有按鈕的鼠標事件;
}
}
/* *計算雷電分布位置的自定義方法*/
void算術(){
calendar time = calendar . getinstance();//Calendar類獲取當前時間;
int leed = time.get(日歷。第二);//獲取當前時間的秒;
Random rand = new Random(LEED);//使用秒作為隨機數的種子;
int tempRand//臨時隨機數;
int landmintally = 0;//得到壹個多少雷的計數器;
布爾標誌=假;//標記是否重復;
int tempNum
while(landmintally & lt;99){ //最多只能有99個礦;
tempRand =(int)(rand . next float()* 480);//獲取壹個隨機數;
tempNum = integer . parse int(map . get(integer . tostring(tempRand))。toString());
if (tempNum == 7)繼續;//如果壹個數重復執行;
地雷[landmintally]= integer . tostring(tempRand);//將獲取的位置放入字符串中;
map . put(integer . tostring(tempRand),integer . tostring(7));//將獲取的位置放入地圖集中,值為7,表示有雷;
landmintally++;//計數器加1;
}
}
/* *計算雷的劃分,表示壹個按鈕周圍有多少個雷;*/
void計算()
{
int num//按鈕的狀態;
int sum = 0;//計數器,統計周圍有多少雷;
int leftUp,Up,rightUp,left,right,leftDown,Down,rightDown//定義了8個位置。
for(int I = 0;我& lt480;i++)
{
left up = I-31;
up = I-30;
right up = I-29;
left = I-1;
右= I+1;
left down = I+29;
down = I+30;
right down = I+31;
卡片Btn[i]。set border(border factory . createbevelborder(0));//設置按鈕的邊框樣式;
button click[I]= new sl frame _ button _ actionAdatper(this,I);//實例化事件類;
卡片Btn[i]。addmouse listener(button click[I]);//向當前按鈕添加鼠標事件;
num = integer . parse int(map . get(integer . tostring(I))。toString());//獲取當前按鈕的狀態;
if (num == 7){
繼續;//如果這個按鈕的狀態是雷霆,跳到下壹個按鈕;
}
If (i == 0) {//左上角第壹雷;
num = integer . parse int(map . get(integer . tostring(I))。toString());
if(integer . parse int(map . get(integer . tostring(right))。toString())= = 7)sum++;//如果是雷霆計數器,加1;
if(integer . parse int(map . get(integer . tostring(down))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(right down))。toString())= = 7)sum++;
map.put(Integer.toString(0),integer . tostring(sum));//將獲取的數字放在當前位置;
sum = 0;//計數器清零;
繼續;//下壹步按鈕;
}else if (i == 29) {//右上角第壹雷;
if(integer . parse int(map . get(integer . tostring(left))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(left down))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(down))。toString())= = 7)sum++;
map.put(Integer.toString(i),integer . tostring(sum));
sum = 0;
繼續;
}else if (i == 450) {//左下角第壹雷;
if(integer . parse int(map . get(integer . tostring(right))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(up))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(right up))。toString())= = 7)sum++;
map.put(Integer.toString(i),integer . tostring(sum));
sum = 0;
繼續;
}else if (i == 479) {//右下角第壹雷;
if(integer . parse int(map . get(integer . tostring(left))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(leftUp))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(up))。toString())= = 7)sum++;
map.put(Integer.toString(i),integer . tostring(sum));
sum = 0;
返回;
} else if(I & lt;29){ //第壹行;
if(integer . parse int(map . get(integer . tostring(left))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(left down))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(down))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(right))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(right down))。toString())= = 7)sum++;
map.put(Integer.toString(i),integer . tostring(sum));
sum = 0;
繼續;
} else if(I & gt;450){ //最後壹行;
if(integer . parse int(map . get(integer . tostring(leftUp))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(up))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(right up))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(left))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(right))。toString())= = 7)sum++;
map.put(Integer.toString(i),integer . tostring(sum));
sum = 0;
繼續;
}else if ((i%30) == 0 ){ //第壹列;
if(integer . parse int(map . get(integer . tostring(up))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(right up))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(right))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(down))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(right down))。toString())= = 7)sum++;
map.put(Integer.toString(i),integer . tostring(sum));
sum = 0;
繼續;
} else if((I+1)% 30)= = 0){//最後壹列;
if(integer . parse int(map . get(integer . tostring(leftUp))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(up))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(left))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(left down))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(down))。toString())= = 7)sum++;
map.put(Integer.toString(i),integer . tostring(sum));
sum = 0;
繼續;
}else{ //去掉周圍剩下的;
if(integer . parse int(map . get(integer . tostring(leftUp))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(up))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(right up))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(left))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(right))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(left down))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(down))。toString())= = 7)sum++;
if(integer . parse int(map . get(integer . tostring(right down))。toString())= = 7)sum++;
map.put(Integer.toString(i),integer . tostring(sum));
sum = 0;
繼續;
}
}
}
/* *鼠標點擊事件,參數I是點擊按鈕的位置*/
public void button _ mouse clicked(mouse event e,int i){
int mKey = e . get button();//單擊了哪個鍵;
int landmineNum = integer . parse int(landminetally . gettext()。toString());//剩下的礦數;
int num = integer . parse int(map . get(integer . tostring(I))。toString());//當前按鈕的狀態;
int flag = integer . parse int(flag _ Ming . get(integer . tostring(I))。toString());//當前按鈕的標記狀態;
if((mKey = = 3)& amp;& amp(cardsBtn[i]。getBorder()!= null)){ //點擊鼠標右鍵,邊框不為空(空表示已經高亮顯示);
If (flag == 10){ //不標記,就標記;
flag _ Ming . put(integer . tostring(I),integer . tostring(8));
button icon = createImageIcon(" images/8 . jpg ");
卡片Btn[i]。setIcon(button icon);
landminetally . settext(integer . tostring(landmineNum-1));
If ((landmineNum-1) == 0) //如果標記的礦號為99;
completeGame()。//完成遊戲;
}else if (flag == 8){ //如果標記,則改為問號;
flag _ Ming . put(integer . tostring(I),integer . tostring(9));
button icon = createImageIcon(" images/9 . jpg ");
卡片Btn[i]。setIcon(button icon);
landminetally . settext(integer . tostring(landmineNum+1));
If ((landmineNum+1) == 0) //如果標記的礦號為99;
completeGame()。//完成遊戲;
}else if (flag == 9){ //如果是問號,則不標記;
flag _ Ming . put(integer . tostring(I),integer . tostring(10));
卡片Btn[i]。setIcon(null);
}
}else if (mKey == 1){ //如果點擊鼠標左鍵;
flag _ Ming . put(integer . tostring(I),integer . tostring(10));//首先清除被點擊按鈕的標記狀態;
If ((landmineNum+1) == 0) //如果標記的礦號為99;
completeGame()。//完成遊戲;
If (num == 7){ //如果銨鈕狀態為雷霆,遊戲結束;
過度遊戲(壹);
}else if (num == 0){ //如果雷霆號為空。
If (flag == 8 ){ //如果已經標記為迅雷,計數器加1;
landminetally . settext(integer . tostring(landmineNum+1));
}
button icon = createImageIcon(" images/0 . jpg ");
卡片Btn[i]。setIcon(button icon);
卡片Btn[i]。set border(null);
展示(壹);//打開周圍的按鈕;
}else {//數字在1-6之間。打開按鈕,顯示數字對應的圖片;
If (flag == 8 ){ //如果已經標記為迅雷,計數器加1;
landminetally . settext(integer . tostring(landmineNum+1));
}
button icon = createImageIcon(" images/"+num+"。jpg”);
卡片Btn[i]。setIcon(button icon);
卡片Btn[i]。set border(null);
}
}
if((mouse key = = 1 & amp;& ampmKey = = 3)| |(mouse key = = 3 & amp;& ampMKey == 1) ){ //同時點擊鼠標左右鍵;
打開(壹);//打開周圍按鈕(先判斷);
}
mouse key = 0;
}
/* *用戶自定義方法,用於判斷是否點亮周圍的按鈕*/
void open(int i){
int landmine amount = 0;//實際地雷數量;
int flag amount = 0;//標記地雷的數量;
int地雷_左起=0,地雷_上起=0,地雷_右起=0,地雷_左起=0,地雷_右起=0,
地雷_左下=0,地雷_下=0,地雷_右下= 0;//定義了實際地雷的8個位置。
int flag_leftUp=0,flag_up=0,flag_rightUp=0,flag_left=0,flag_right=0,
flag_leftDown=0,flag_down=0,flag _ right down = 0;//定義了8個標記地雷的位置。
//實際地雷所在的八個位置和標記地雷的八個位置。如果不判斷,hashMap集合就會越線;
如果(i & gt31)mineral _ left up = integer . parse int(map . get(integer . tostring(I-31))。toString());
如果(i & gt30)mine _ up = integer . parse int(map . get(integer . tostring(I-30))。toString());
如果(i & gt29)mineral _ right up = integer . parse int(map . get(integer . tostring(I-29))。toString());
如果(i & gt1)mineral _ left = integer . parse int(map . get(integer . tostring(I-1))。toString());
如果(我& lt479)mineral _ right = integer . parse int(map . get(integer . tostring(I+1))。toString());
如果(我& lt450)mineral _ left down = integer . parse int(map . get(integer . tostring(I+29))。toString());
如果(我& lt449)mine _ down = integer . parse int(map . get(integer . tostring(I+30))。toString());
如果(我& lt448)mineral _ right down = integer . parse int(map . get(integer . tostring(I+31))。toString());
如果(i & gt31)flag _ left up = integer . parse int(flag _ Ming . get(integer . tostring(I-31))。toString());
如果(i & gt30)flag _ up = integer . parse int(flag _ Ming . get(integer . tostring(I-30))。toString());
如果(i & gt29)flag _ right up = integer . parse int(flag _ mine . get(integer . tostring(I-29))。toString());
如果(i & gt1)flag _ left = integer . parse int(flag _ Ming . get(integer . tostring(I-1))。toString());
如果(我& lt479)flag _ right = integer . parse int(flag _ Ming . get
太長了,說不完。我會把壓縮包發給妳。請撥打49905518查詢。