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

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

java實現(xiàn)掃雷游戲

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

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

一、代碼思路

代碼思路很重要,如果事先就想好了代碼思路,那么寫這一個代碼肯定是事半功倍,比在哪里瞎打要強不知道多少。經(jīng)過思考,覺得可以創(chuàng)建一個二維數(shù)組來記錄情況未翻開的牌:(統(tǒng)一顯示 ? )數(shù)組的值 代表-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; } } }

三、函數(shù)部分

1.顯示函數(shù)

打一個方格

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.設置基本數(shù)據(jù)的函數(shù)

標有 //雷 的是指雷的數(shù)量

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++;} } } //*************設置地雷周邊參數(shù)******************** 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.翻牌函數(shù)

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

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; //雷}

以上就是全部內(nèi)容了。

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

java實現(xiàn)掃雷游戲

完整代碼:

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++;} } } //*************設置地雷周邊參數(shù)******************** 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經(jīng)典小游戲》

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

標簽: Java
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
青草av.久久免费一区| 免费观看亚洲天堂| 久久精品播放| 国产一区二区三区自拍| 午夜视频精品| 伊人久久亚洲| 国产日韩一区二区三免费高清| 国产欧美日韩视频在线| 精品国产鲁一鲁****| 婷婷综合六月| 午夜日本精品| 欧美日韩亚洲一区三区| 国产精品一区二区三区www| 精品91福利视频| 99久久亚洲精品蜜臀| 模特精品在线| 国产精品毛片久久久| 亚洲天堂资源| 最新国产精品视频| 另类综合日韩欧美亚洲| 日韩三区免费| 日韩欧美中文字幕一区二区三区| 六月丁香综合在线视频| 免费精品国产的网站免费观看| 婷婷综合国产| 伊人久久国产| 亚洲影院天堂中文av色| 国产亚洲精品精品国产亚洲综合| 国产成人精品一区二区三区视频 | 欧美韩一区二区| 久久视频一区| 日韩欧美久久| 色婷婷狠狠五月综合天色拍| 中文字幕一区二区三区在线视频| 成人高清一区| 免费黄网站欧美| sm久久捆绑调教精品一区| 国产综合亚洲精品一区二| 欧美久久精品| 不卡视频在线| 国产一区二区亚洲| 免费观看久久久4p| 日韩不卡在线| 久久精品72免费观看| 亚洲一级黄色| 里番精品3d一二三区| 亚洲精品麻豆| 亚洲午夜精品久久久久久app| 免费在线日韩av| 亚洲综合五月| 99久久婷婷这里只有精品| 国产精品成人一区二区网站软件| 99成人在线| 久久一区视频| 亚洲免费一区三区| 日韩精品一卡| 精品国产中文字幕第一页| 美女网站久久| 亚洲二区免费| 四虎国产精品免费观看| 欧美久久久网站| 日韩综合一区二区| 亚洲国产综合在线看不卡| 精品视频网站| 91国内精品| 天堂va蜜桃一区二区三区| 欧美日韩水蜜桃| 成人精品视频| 国产欧美日韩精品一区二区三区| 久久亚洲一区| 狠狠操综合网| 久久久久99| 亚洲午夜天堂| 成人国产精选| 精品国产成人| 国产一区二区三区不卡av| 国产精品日本一区二区三区在线| 日韩精品一区二区三区免费视频 | 国产欧美一区| 亚洲免费影视| 午夜亚洲精品| 国产一区91| 久久99伊人| 久久国产99| 亚洲网址在线观看| 综合国产精品| 日韩综合小视频| 亚洲精品护士| 亚洲精品视频一二三区| 亚洲精品日韩久久| 日韩高清三区| 91欧美极品| 欧美日韩黄网站| 国产精品视频一区二区三区综合| 日韩精品高清不卡| 日韩avvvv在线播放| 91精品啪在线观看国产爱臀| 欧美天堂一区| 精品一区av| 福利精品在线| 欧美.日韩.国产.一区.二区| 99热精品在线| 日韩在线麻豆| 国产精品白丝av嫩草影院| 久久久91麻豆精品国产一区| 成人在线黄色| 99精品综合| 视频一区二区中文字幕| 日韩欧美三区| 国产精品久久久久久久免费软件| 亚洲精品小说| 亚洲福利国产| 国产视频一区在线观看一区免费| 蜜桃久久av| 777久久精品| 国产一区二区三区亚洲综合| 香蕉视频亚洲一级| 性色一区二区| 国产欧美一区| 日韩和的一区二在线| 国产一级久久| 国产日韩一区| 在线中文字幕播放| 日韩一级欧洲| 亚洲深深色噜噜狠狠爱网站| 国产日韩中文在线中文字幕| 特黄毛片在线观看| 美女久久网站| 国产劲爆久久| av一区在线| 日本中文字幕一区二区| 国产拍在线视频| 久久国产精品毛片| 久久永久免费| 亚洲精品网址| 欧美激情麻豆| 黄色亚洲大片免费在线观看| 久久国产尿小便嘘嘘| 日韩中文在线播放| 国产中文一区| 久久最新视频| 日韩av片子| 99视频精品全国免费| 丝袜国产日韩另类美女| 国产精久久久| 午夜在线播放视频欧美| 麻豆成人91精品二区三区| 蜜桃一区二区三区| 国产精品视频首页| 野花国产精品入口| 久久伊人久久| 鲁大师成人一区二区三区| 国产在视频一区二区三区吞精| 99在线|亚洲一区二区| 日韩1区2区日韩1区2区| 亚洲二区精品| 精品三级av| 日韩在线视频一区二区三区| 欧美日韩色图| 福利一区二区| 欧美日韩99| 午夜在线视频观看日韩17c| 黄色在线观看www| 国产探花一区在线观看| 美日韩精品视频| 91精品一区二区三区综合在线爱| 久久国产精品美女| 色狠狠一区二区三区| 欧美特黄一区| 最近高清中文在线字幕在线观看1| 久久久蜜桃一区二区人| 久久国产麻豆精品| 精品中文一区| 在线天堂中文资源最新版| 国产亚洲一区| 亚洲精品欧美| 亚洲综合不卡| 久久国产欧美| 在线手机中文字幕| 久久精品亚洲| 国产欧美日韩精品一区二区免费| 亚洲人妖在线| 蜜芽一区二区三区| 九九在线精品| 日韩精品91| 老司机精品在线| 国产美女视频一区二区| 日韩精品一区二区三区中文在线| 免费看日韩精品| 每日更新成人在线视频| av不卡在线看| 午夜亚洲精品| 久热精品在线| 久热精品在线| 手机精品视频在线观看| 99成人在线| 国产亚洲毛片| 首页欧美精品中文字幕| 麻豆精品网站| 香蕉久久久久久久av网站|