最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年04月23日 11:20:22   作者:米米奇  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

初學(xué)Java,寫(xiě)了一個(gè)掃雷代碼來(lái)鍛煉一下自己的代碼能力。

一、代碼思路

代碼思路很重要,如果事先就想好了代碼思路,那么寫(xiě)這一個(gè)代碼肯定是事半功倍,比在哪里瞎打要強(qiáng)不知道多少。
經(jīng)過(guò)思考,覺(jué)得可以創(chuàng)建一個(gè)二維數(shù)組來(lái)記錄情況
未翻開(kāi)的牌:(統(tǒng)一顯示 █ )
數(shù)組的值 代表
-1 雷
0 旁邊沒(méi)有雷
1 旁邊有一個(gè)雷
以此類推

翻開(kāi)的牌則:

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("⑧");

二、代碼主題部分

注意不要越界和不要重復(fù)打開(kāi)

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("已開(kāi)?。?!");continue;} //防止打開(kāi)重復(fù)
  if(bomb(a,x,y)) break;
  draw(a,x,y);
  show(a);
  if(All(a)){ System.out.println("你避過(guò)了所有地雷!??!");break; }
 }
 }

三、函數(shù)部分

1.顯示函數(shù)

打一個(gè)方格

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

標(biāo)有 //雷 的是指雷的數(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è)置地雷周邊參數(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ù)

這個(gè)函數(shù)很簡(jiǎn)單,卻也是精華所在,這個(gè)函數(shù)的作用就在點(diǎn)開(kāi)一個(gè)牌,翻開(kāi)一堆符合規(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; //雷
}

以上就是全部?jī)?nèi)容了。

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

完整代碼:

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("已開(kāi)?。?!");continue;} //防止打開(kāi)重復(fù)
 if(bomb(a,x,y)) break;
 draw(a,x,y);
 show(a);
 if(All(a)){ System.out.println("你避過(guò)了所有地雷!??!");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è)置地雷周邊參數(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; //雷
 }
}

更多精彩游戲,請(qǐng)參考專題《java經(jīng)典小游戲》

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java截取圖片示例

    java截取圖片示例

    這篇文章主要介紹了java截取圖片示例,把代碼中的圖片路徑改成自己的圖片,運(yùn)行就可以看到效果了,需要的朋友可以參考下
    2014-03-03
  • Spring?refresh()源碼解析

    Spring?refresh()源碼解析

    這篇文章主要為大家介紹了Spring?refresh()源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • 解析SpringBoot自定義參數(shù)校驗(yàn)注解

    解析SpringBoot自定義參數(shù)校驗(yàn)注解

    這篇文章主要介紹了SpringBoot自定義參數(shù)校驗(yàn)注解,引入依賴,spring validation是在hibernate-validator上做了一層封裝,文中提到了定義參數(shù)校驗(yàn)注解與處理器的示例代碼,感興趣的朋友跟隨小編一起看看吧
    2023-10-10
  • Java多線程及線程安全實(shí)現(xiàn)方法解析

    Java多線程及線程安全實(shí)現(xiàn)方法解析

    這篇文章主要介紹了Java多線程及線程安全實(shí)現(xiàn)方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Java劍指offer之刪除鏈表的節(jié)點(diǎn)

    Java劍指offer之刪除鏈表的節(jié)點(diǎn)

    這篇文章主要介紹了Java劍指offer之刪除鏈表的節(jié)點(diǎn),給定單向鏈表的頭指針和一個(gè)要?jiǎng)h除的節(jié)點(diǎn)的值,定義一個(gè)函數(shù)刪除該節(jié)點(diǎn)。返回刪除后的鏈表的頭節(jié)點(diǎn),下文更多相關(guān)內(nèi)容介紹,需要的小伙伴可以參考一下
    2022-04-04
  • Spring實(shí)戰(zhàn)之XML與JavaConfig的混合配置詳解

    Spring實(shí)戰(zhàn)之XML與JavaConfig的混合配置詳解

    大家都知道Spring的顯示配置方式有兩種,一種是基于XML配置,一種是基于JavaConfig的方式配置。那么下這篇文章主要給大家分別介紹如何在JavaConfig中引用XML配置的bean以及如何在XML配置中引用JavaConfig,需要的朋友可以參考下。
    2017-07-07
  • Java基礎(chǔ)-封裝和繼承

    Java基礎(chǔ)-封裝和繼承

    這篇文章主要介紹了Java面向?qū)ο缶幊蹋ǚ庋b/繼承/多態(tài))實(shí)例解析的相關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下希望可以幫助到你
    2021-07-07
  • SpringBoot WebSocket實(shí)時(shí)監(jiān)控異常的詳細(xì)流程

    SpringBoot WebSocket實(shí)時(shí)監(jiān)控異常的詳細(xì)流程

    最近做了一個(gè)需求,消防的設(shè)備巡檢,如果巡檢發(fā)現(xiàn)異常,通過(guò)手機(jī)端提交,后臺(tái)的實(shí)時(shí)監(jiān)控頁(yè)面實(shí)時(shí)獲取到該設(shè)備的信息及位置,然后安排員工去處理。這篇文章主要介紹了SpringBoot WebSocket實(shí)時(shí)監(jiān)控異常的全過(guò)程,感興趣的朋友一起看看吧
    2021-10-10
  • FastJSON的0day漏洞的解決

    FastJSON的0day漏洞的解決

    本文主要介紹了FastJSON的0day漏洞的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • Springboot整合多數(shù)據(jù)源配置流程詳細(xì)講解

    Springboot整合多數(shù)據(jù)源配置流程詳細(xì)講解

    這篇文章主要介紹了Springboot整合多數(shù)據(jù)源配置流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧
    2023-03-03

最新評(píng)論

潜山县| 乌拉特前旗| 常德市| 左权县| 土默特左旗| 泸西县| 呼和浩特市| 阳高县| 成武县| 萨迦县| 西丰县| 渑池县| 罗平县| 公安县| 兴仁县| 拜城县| 通化县| 额尔古纳市| 宁德市| 贵阳市| 和静县| 合山市| 独山县| 萨嘎县| 乡城县| 财经| 河北区| 建宁县| 耒阳市| 新丰县| 武冈市| 古交市| 阿拉善左旗| 夏邑县| 阳原县| 峨边| 高平市| 大埔县| 奈曼旗| 靖边县| 商水县|