日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

java實現掃雷游戲

瀏覽:23日期:2022-09-02 14:35:18

初學Java,寫了一個掃雷代碼來鍛煉一下自己的代碼能力。

一、代碼思路

代碼思路很重要,如果事先就想好了代碼思路,那么寫這一個代碼肯定是事半功倍,比在哪里瞎打要強不知道多少。經過思考,覺得可以創建一個二維數組來記錄情況未翻開的牌:(統一顯示 ? )數組的值 代表-1 雷0 旁邊沒有雷1 旁邊有一個雷以此類推

翻開的牌則:

if(a[x][y] == 9) System.out.print('?');if(a[x][y] == 10) System.out.print('?');if(a[x][y] == 11) System.out.print('①');if(a[x][y] == 12) System.out.print('②');if(a[x][y] == 13) System.out.print('③');if(a[x][y] == 14) System.out.print('④');if(a[x][y] == 15) System.out.print('⑤');if(a[x][y] == 16) System.out.print('⑥');if(a[x][y] == 17) System.out.print('⑦');if(a[x][y] == 18) System.out.print('⑧');

二、代碼主題部分

注意不要越界和不要重復打開

public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int x,y; int a[][]=new int[10][20]; produce(a); show(a); while(true){ x=scanner.nextInt();y=scanner.nextInt(); if(x<=0||y<=0||x>10||y>20) {System.out.println('越界!!');continue;} //防止越界 if(a[x-1][y-1]>=10) {System.out.println('已開!!!');continue;} //防止打開重復 if(bomb(a,x,y)) break; draw(a,x,y); show(a); if(All(a)){ System.out.println('你避過了所有地雷!!!');break; } } }

三、函數部分

1.顯示函數

打一個方格

public static void show(int a[][]) { int lie = 0,x =0,y=0; System.out.print(' ┃1 '); for (short i = 2; i <= 20; i++){ if(i<9)System.out.print('┃'+i+' '); else System.out.print('┃'+i); } System.out.println(); System.out.print(' '); for (short i = 0; i <= 20; i++) { //輸出第一行 if (i == 0) System.out.print('┏─'); else if (i == 20) System.out.println('┓'); else System.out.print('┳─'); } for (short i = 1; i < 2 * 10; i++) { if (i % 2 == 0) { System.out.print(' '); for (short j = 0; j <= 20; j++) { if (j == 0) System.out.print('┣─'); else if (j == 20) System.out.println('┫'); else System.out.print('╋─'); } } if (i % 2 == 1) { if(lie+1 >= 10) System.out.print(lie+1);else System.out.print(' ' + (lie+1));lie++; for (short j = 0; j <= 2*20; j++) { if (j % 2 == 0) System.out.print('┃'); else { if(a[x][y] <= 8) System.out.print('?'); if(a[x][y] == 9) System.out.print('?'); if(a[x][y] == 10) System.out.print('?'); if(a[x][y] == 11) System.out.print('①'); if(a[x][y] == 12) System.out.print('②'); if(a[x][y] == 13) System.out.print('③'); if(a[x][y] == 14) System.out.print('④'); if(a[x][y] == 15) System.out.print('⑤'); if(a[x][y] == 16) System.out.print('⑥'); if(a[x][y] == 17) System.out.print('⑦'); if(a[x][y] == 18) System.out.print('⑧'); y++; if(y>=20){ x++;y =0; } } } System.out.println(); } } System.out.print(' '); for (short k = 0; k <= 20; k++) { //輸出最后一行 if (k == 0) System.out.print('┗─'); else if (k == 20) System.out.println('┛'); else System.out.print('┻─'); }}

2.設置基本數據的函數

標有 //雷 的是指雷的數量

public static void produce(int a[][]){ int random[] = new int[25]; //雷 Random random1 = new Random(); for(short i =0;i<25;){ //雷 short j = 0; int t = random1.nextInt()%200+1; if(t<0)t=-t; for(;j<25;j++){ //雷 if(random[j]==t)break; } if(j==25){random[i]=t;i++;} //雷 } java.util.Arrays.sort(random); int x = 0; System.out.println(); for(int i = 0; i<10;i++){ //地雷配置成功 for(int j = 0 ;j<20 ;j++){ if(x == 25)break; //雷 if((i*20)+j+1 == random[x]) {a[i][j]=-1;x++;} } } //*************設置地雷周邊參數******************** for(short i = 0;i<10;i++){ for(short j = 0;j<20;j++){ if(a[i][j]==0){ int count=0; if(i!=0&&j!=0&&a[i-1][j-1]==-1 ) count++; //左上 if(i!=0&&a[i-1][j]==-1 ) count++; //上 if(i!=0&&j<=18&&a[i-1][j+1]==-1 ) count++; //右上 if(j!=0&&a[i][j-1]==-1 ) count++; //左 if(j<=18&&a[i][j+1]==-1 ) count++; //右 if(i<=8&&j!=0&&a[i+1][j-1]==-1 ) count++; //左下 if(i<=8&&a[i+1][j]==-1 ) count++; //下 if(i<=8&&j<=18&&a[i+1][j+1]==-1 ) count++; //右下 a[i][j]=count; } } }}

3.翻牌函數

這個函數很簡單,卻也是精華所在,這個函數的作用就在點開一個牌,翻開一堆符合規則的牌。

public static void draw(int a[][],int x,int y){ a[x-1][y-1]+=10; if(a[x-1][y-1]==10) { if (x - 1 > 0 && y - 1 > 0 && a[x - 2][y - 2] < 10 && a[x - 2][y - 2] != -1) draw(a, x - 1, y - 1); //左上 if (x - 1 > 0 && a[x - 2][y - 1] < 10 && a[x - 2][y - 1] != -1) draw(a, x - 1, y); //上 if (x - 1 > 0 && y - 1 < 19 && a[x - 2][y] < 10 && a[x - 2][y] != -1) draw(a, x - 1, y + 1); //右上 if (y - 1 > 0 && a[x - 1][y - 2] < 10 && a[x - 1][y - 2] != -1) draw(a, x, y - 1); //zuo if (y - 1 <= 18 && a[x - 1][y] < 10 && a[x - 1][y] != -1) draw(a, x, y + 1); //you if (x - 1 < 9 && y - 1 > 0 && a[x][y - 2] < 10 && a[x][y - 2] != -1) draw(a, x + 1, y - 1); //zuo xia if (x - 1 < 9 && a[x][y - 1] < 10 && a[x][y - 1] != -1) draw(a, x + 1, y);//xia if (x - 1 < 9 && y - 1 < 19 && a[x][y] < 10 && a[x][y] != -1) draw(a, x + 1, y + 1);//you xia }}

4.踩雷爆炸部分

public static boolean bomb(int a[][],int x ,int y){ if(a[x-1][y-1]==-1){ for(int i =0;i<10;i++){ for(int j = 0 ;j<20;j++){ if(a[i][j]==-1)a[i][j]+=10; } } show(a); System.out.println('踩雷了!!!'); return true;}else return false;}

5.判斷是否掃雷干凈部分

public static boolean All(int a[][]){ int i,j=0,t=0; for(i =0;i<10;i++){ for(j = 0 ;j<20;j++){ if(a[i][j]<10) t++; if(t>25)break; //雷 } } if(t==25)return true;else return false; //雷}

以上就是全部內容了。

下面粘貼一下效果圖和完整代碼

java實現掃雷游戲

完整代碼:

import java.util.Random;import java.util.Scanner;public class 掃雷 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int x,y; int a[][]=new int[10][20]; produce(a); show(a); while(true){ x=scanner.nextInt();y=scanner.nextInt(); if(x<=0||y<=0||x>10||y>20) {System.out.println('越界!!');continue;} //防止越界 if(a[x-1][y-1]>=10) {System.out.println('已開!!!');continue;} //防止打開重復 if(bomb(a,x,y)) break; draw(a,x,y); show(a); if(All(a)){ System.out.println('你避過了所有地雷!!!');break; } } } public static void show(int a[][]) { int lie = 0,x =0,y=0; System.out.print(' ┃1 '); for (short i = 2; i <= 20; i++){ if(i<9)System.out.print('┃'+i+' '); else System.out.print('┃'+i); } System.out.println(); System.out.print(' '); for (short i = 0; i <= 20; i++) { //輸出第一行 if (i == 0) System.out.print('┏─'); else if (i == 20) System.out.println('┓'); else System.out.print('┳─'); } for (short i = 1; i < 2 * 10; i++) { if (i % 2 == 0) { System.out.print(' '); for (short j = 0; j <= 20; j++) { if (j == 0) System.out.print('┣─'); else if (j == 20) System.out.println('┫'); else System.out.print('╋─'); } } if (i % 2 == 1) { if(lie+1 >= 10) System.out.print(lie+1);else System.out.print(' ' + (lie+1));lie++; for (short j = 0; j <= 2*20; j++) { if (j % 2 == 0) System.out.print('┃'); else { if(a[x][y] <= 8) System.out.print('?'); if(a[x][y] == 9) System.out.print('?'); if(a[x][y] == 10) System.out.print('?'); if(a[x][y] == 11) System.out.print('①'); if(a[x][y] == 12) System.out.print('②'); if(a[x][y] == 13) System.out.print('③'); if(a[x][y] == 14) System.out.print('④'); if(a[x][y] == 15) System.out.print('⑤'); if(a[x][y] == 16) System.out.print('⑥'); if(a[x][y] == 17) System.out.print('⑦'); if(a[x][y] == 18) System.out.print('⑧'); y++; if(y>=20){ x++;y =0; } } } System.out.println(); } } System.out.print(' '); for (short k = 0; k <= 20; k++) { //輸出最后一行 if (k == 0) System.out.print('┗─'); else if (k == 20) System.out.println('┛'); else System.out.print('┻─'); } } public static void produce(int a[][]){ int random[] = new int[25]; //雷 Random random1 = new Random(); for(short i =0;i<25;){ //雷 short j = 0; int t = random1.nextInt()%200+1; if(t<0)t=-t; for(;j<25;j++){ //雷 if(random[j]==t)break; } if(j==25){random[i]=t;i++;} //雷 } java.util.Arrays.sort(random); int x = 0; System.out.println(); for(int i = 0; i<10;i++){ //地雷配置成功 for(int j = 0 ;j<20 ;j++){ if(x == 25)break; //雷 if((i*20)+j+1 == random[x]) {a[i][j]=-1;x++;} } } //*************設置地雷周邊參數******************** for(short i = 0;i<10;i++){ for(short j = 0;j<20;j++){ if(a[i][j]==0){ int count=0; if(i!=0&&j!=0&&a[i-1][j-1]==-1 ) count++; //左上 if(i!=0&&a[i-1][j]==-1 ) count++; //上 if(i!=0&&j<=18&&a[i-1][j+1]==-1 ) count++; //右上 if(j!=0&&a[i][j-1]==-1 ) count++; //左 if(j<=18&&a[i][j+1]==-1 ) count++; //右 if(i<=8&&j!=0&&a[i+1][j-1]==-1 ) count++; //左下 if(i<=8&&a[i+1][j]==-1 ) count++; //下 if(i<=8&&j<=18&&a[i+1][j+1]==-1 ) count++; //右下 a[i][j]=count; } } } } //*******************************翻牌****************************8 public static void draw(int a[][],int x,int y){ a[x-1][y-1]+=10; if(a[x-1][y-1]==10) { if (x - 1 > 0 && y - 1 > 0 && a[x - 2][y - 2] < 10 && a[x - 2][y - 2] != -1) draw(a, x - 1, y - 1); //左上 if (x - 1 > 0 && a[x - 2][y - 1] < 10 && a[x - 2][y - 1] != -1) draw(a, x - 1, y); //上 if (x - 1 > 0 && y - 1 < 19 && a[x - 2][y] < 10 && a[x - 2][y] != -1) draw(a, x - 1, y + 1); //右上 if (y - 1 > 0 && a[x - 1][y - 2] < 10 && a[x - 1][y - 2] != -1) draw(a, x, y - 1); //zuo if (y - 1 <= 18 && a[x - 1][y] < 10 && a[x - 1][y] != -1) draw(a, x, y + 1); //you if (x - 1 < 9 && y - 1 > 0 && a[x][y - 2] < 10 && a[x][y - 2] != -1) draw(a, x + 1, y - 1); //zuo xia if (x - 1 < 9 && a[x][y - 1] < 10 && a[x][y - 1] != -1) draw(a, x + 1, y);//xia if (x - 1 < 9 && y - 1 < 19 && a[x][y] < 10 && a[x][y] != -1) draw(a, x + 1, y + 1);//you xia } } //*******************************爆炸****************************** public static boolean bomb(int a[][],int x ,int y){ if(a[x-1][y-1]==-1){ for(int i =0;i<10;i++){ for(int j = 0 ;j<20;j++){ if(a[i][j]==-1)a[i][j]+=10; } } show(a); System.out.println('踩雷了!!!'); return true;}else return false; } //*******************************全翻了******************** public static boolean All(int a[][]){ int i,j=0,t=0; for(i =0;i<10;i++){ for(j = 0 ;j<20;j++){ if(a[i][j]<10) t++; if(t>25)break; //雷 } } if(t==25)return true;else return false; //雷 }}

更多精彩游戲,請參考專題《java經典小游戲》

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
免费成人性网站| 视频国产精品| 精品国产a一区二区三区v免费| 日韩精品导航| 一区二区三区四区在线观看国产日韩| 午夜精品网站| 日产欧产美韩系列久久99| 一区二区三区国产在线| 亚洲日本免费电影| 欧美aa在线视频| 日韩在线综合| 另类av一区二区| 国产精品久久乐| 日本韩国欧美超级黄在线观看| 欧美亚洲激情| 日韩国产欧美三级| 亚洲综合电影| 爽好多水快深点欧美视频| 欧美日韩99| 精品欧美视频| 悠悠资源网久久精品| 日韩黄色免费网站| 中文字幕在线视频久| 美女久久网站| sm捆绑调教国产免费网站在线观看| 99久久久久国产精品| 日韩和欧美一区二区| 欧洲av一区二区| 国产日产高清欧美一区二区三区| 国产videos久久| 日本中文字幕不卡| 蜜桃成人精品| 日韩av网站在线免费观看| 久久天堂成人| 日韩成人精品一区二区三区| 日韩电影免费网站| 久久狠狠久久| 国产精品免费看| 日韩在线观看一区| 国产免费av国片精品草莓男男| 久久亚洲在线| 久久uomeier| 欧美1区2区3| 日韩一区二区三区高清在线观看| 日韩和的一区二在线| 麻豆91精品91久久久的内涵| 视频一区国产视频| 免费观看不卡av| 成人看片网站| 免费在线观看一区| 91欧美日韩在线| 日本不卡在线视频| 日本v片在线高清不卡在线观看| 亚洲一区二区三区高清不卡| 久久久噜噜噜| 欧美日韩免费观看视频| 黄色网一区二区| 精品国产乱码| 色一区二区三区四区| 日韩88av| 99久久激情| 好吊一区二区三区| 中文日韩在线| 蜜桃视频一区二区三区| 欧美日韩国产高清| 国产亚洲精品v| 日本亚洲最大的色成网站www| 亚洲另类av| 欧美日韩夜夜| 精品中文字幕一区二区三区四区| 麻豆一区二区在线| 黑人精品一区| 激情欧美一区| 中文一区一区三区免费在线观 | 美女国产一区| 久久亚洲影院| 亚洲精品黄色| 久久99久久人婷婷精品综合| 精品99在线| 久久久久美女| 亚洲不卡视频| 国产成人精选| 9色精品在线| 欧美一级网站| 波多野结衣久久精品| 蜜臀精品一区二区三区在线观看| 欧美日本三区| 欧美xxxx中国| 免费日本视频一区| 美女久久精品| 日韩在线一二三区| 国产精品成人3p一区二区三区| 亚洲伦乱视频| 国产伦精品一区二区三区视频| 成人自拍av| 欧美亚洲综合视频| 国产精品av一区二区| 国产精品一区亚洲| 国产精品老牛| 秋霞国产精品| 久久一区欧美| 亚洲精品黄色| 九色porny丨国产首页在线| 国产日韩亚洲欧美精品| 国产精品白浆| 日韩在线观看中文字幕| 精品久久影院| 日韩av三区| 日韩午夜黄色| 婷婷国产精品| 蜜桃视频在线网站| 免费日韩一区二区三区| 日韩精品社区| 亚州av一区| 性欧美长视频| av不卡免费看| 国产精品7m凸凹视频分类| 91视频精品| 日本а中文在线天堂| 久久精品国产亚洲aⅴ| 国产日产精品_国产精品毛片 | 精品国产a一区二区三区v免费| 亚洲专区视频| 亚洲人www| 日韩欧美中文字幕一区二区三区| 99日韩精品| 视频在线在亚洲| 一区二区精彩视频| 亚洲三级国产| 久久国产三级| 久久精品国产999大香线蕉| 久久影院一区二区三区| 国产精品伊人| 久久伊人国产| 麻豆理论在线观看| 免费视频亚洲| 亚洲一级淫片| 国产精品www.| 日韩成人精品一区| 四虎4545www国产精品| 日韩一区二区久久| 日本三级亚洲精品| 国产精品videossex| 色一区二区三区四区| 999久久久91| 视频一区免费在线观看| 欧美三区不卡| 成人亚洲一区二区| 激情综合网站| 国产乱人伦丫前精品视频| 国内自拍视频一区二区三区| 久久久成人网| 日韩精品久久久久久| 日韩成人精品一区| 乱人伦精品视频在线观看| 国产精品麻豆成人av电影艾秋| 日韩精品影视| 欧美综合精品| 99热国内精品| 国产精品白丝av嫩草影院| 国内精品福利| 欧美国产极品| 蜜臀av一区二区在线免费观看 | 精品色999| 日韩制服丝袜先锋影音| 国产白浆在线免费观看| | 欧美少妇精品| 国产亚洲一区二区三区不卡| 婷婷丁香综合| 理论片午夜视频在线观看| 日韩国产精品久久久久久亚洲| 久久精品动漫| 国产精品久久久久久久免费软件| 国精品一区二区三区| 国产一区2区| 国产精品分类| 国产亚洲一区| 日韩国产91| 蜜臀91精品一区二区三区| 久久免费高清| 日韩中文视频| 超碰99在线| 国产999精品在线观看| 国产精品一线天粉嫩av| 亚欧成人精品| 日本不卡视频一二三区| 国产精品国产三级在线观看| 亚洲精品护士| 亚洲精品少妇| 日本va欧美va精品发布| 涩涩涩久久久成人精品| 国产一区日韩一区| 欧美日韩一区二区综合| 欧美 日韩 国产一区二区在线视频| 福利片在线一区二区| 国产成人精品三级高清久久91| 粉嫩av一区二区三区四区五区 | 久久女人天堂| 精品日产乱码久久久久久仙踪林|