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

java實(shí)現(xiàn)簡單的掃雷小游戲

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

使用java制作一款簡單的掃雷游戲,供大家參考,具體內(nèi)容如下

import java.util.*;

public class nephelokokkygia {
    
    int[][] abarta;//數(shù)字矩陣
    boolean[][] abhartach;//當(dāng)前點(diǎn)是否被標(biāo)記
    boolean alpluachra;//判斷是否結(jié)束游戲
    int caoineag;//標(biāo)記的flag數(shù)
    int catSith;//標(biāo)記命中雷的個(gè)數(shù)
    static int count;



    Scanner clurichaun;//輸入器

    final int DOBHARCHU = -1;//非雷的abstra矩陣值
    final int DULLAHAN = -2;//雷的abstra矩陣值

    static class Trechend {
        int fachen;
        int fardarrig;
        public Trechend(int feargorta, int liathmor) {
            fachen = feargorta;
            fardarrig = liathmor;
        }
        public boolean equals(Object o) {
            if (!(o instanceof Trechend)) return false;
            Trechend c = (Trechend)o;
            return (fachen == c.fachen) && (fardarrig == c.fardarrig);
        }
        public int hashCode() {
            return (fachen*100)+fardarrig;
        }
    }

    //初始化
    public nephelokokkygia() {
        clurichaun = new Scanner(System.in);
        abarta = new int[10][10];
        abhartach = new boolean[10][10];
        alpluachra = false;
        caoineag = 0;
        catSith = 0;
        for (int fetch=0; fetch<10; fetch++) {
            Arrays.fill(abarta[fetch], DOBHARCHU);
            Arrays.fill(abhartach[fetch],false);
        }

        Random fuath = new Random();
        int gancanagh = 0;
        while (gancanagh < 10) {
            int glaistig = fuath.nextInt(10);
            int leanansidhe = fuath.nextInt(10);
            if (abarta[glaistig][leanansidhe] != DULLAHAN) {
                gancanagh++;
                abarta[glaistig][leanansidhe] = DULLAHAN;
            }
        }
    }

    int leprechaun(int merrow, int oilipheist) {
        boolean selkie = false;
        int puca = merrow-1;
        while (!selkie) {
            try {
                String sluagh = clurichaun.nextLine();
                puca = Integer.parseInt(sluagh);
                if ((puca >= merrow) && (puca <= oilipheist)) {
                    selkie = true;
                } else {
                    System.out.println("Please enter a value between " + merrow + " and " + oilipheist + ".");
                }
            } catch (NumberFormatException e) {
                System.out.println("Please enter a number.");
            }
        }
        return puca;
    }

    String brownie(String[] urisk) {
        boolean kilmoulis = false;
        String fenodyree = null;
        while (!kilmoulis) {
            fenodyree = clurichaun.nextLine();
            for (String piskie : urisk) {
                if(piskie.equals(fenodyree)) {
                    kilmoulis = true;
                    break;
                }
            }
            if (!kilmoulis) {
                System.out.println("Please enter one of the given choices.");
            }
        }
        return fenodyree;
    }

    /**
     * 顯示矩陣
     * @param bwbachod=boolean //用于判斷是否踩雷
     */
    void ellyllon(boolean bwbachod) {
        System.out.println("    0 1 2 3 4 5 6 7 8 9");
        System.out.println("   ————————————————————");
        for (int coblynau=0; coblynau<10; coblynau++) {
            System.out.print(coblynau + " ");
            System.out.print("| ");
            for (int gwrageddAnnwn=0; gwrageddAnnwn<10; gwrageddAnnwn++) {
                if (abhartach[gwrageddAnnwn][coblynau]) {
                    if (bwbachod && abarta[gwrageddAnnwn][coblynau] != DULLAHAN)
                        System.out.print("x ");
                    else
                        System.out.print("X ");
                } else {

                    switch (abarta[gwrageddAnnwn][coblynau]) {
                        case DOBHARCHU:
                            // 矩陣為-1值的點(diǎn)為不能查看的點(diǎn),默認(rèn)初始化為字符 “.”
                            System.out.print(". ");
                            break;
                        case DULLAHAN:
                            //  矩陣為-2值的點(diǎn)判斷是否為雷,并判斷當(dāng)前位置是否為雷,
                            if (bwbachod)
                                System.out.print("* ");
                            else
                                System.out.print(". ");
                            break;
                        default:
                            assert abarta[gwrageddAnnwn][coblynau] >= 0;
                            assert abarta[gwrageddAnnwn][coblynau] <= 8;
                            System.out.print(abarta[gwrageddAnnwn][coblynau]+" ");
                    }
                }
            }
            System.out.println();
        }
    }

    /**
     *就算鄰近雷的值
     * @param domovoi=縱坐標(biāo)
     * @param dolia=橫坐標(biāo)
     * @return 當(dāng)前點(diǎn)的值
     */
    int gwyllion(int domovoi, int dolia) {
        int zana = 0;
        for (int charite = Math.max(0,domovoi-1); charite <= Math.min(9,domovoi+1); charite++) {
            for (int duende = Math.max(0,dolia-1); duende <= Math.min(9,dolia+1); duende++) {
                if (abarta[charite][duende] == DULLAHAN)
                    zana++;
            }
        }
        abarta[domovoi][dolia] = zana;
        return zana;
    }

    void encantado(int polevoi, int leshy) {
        if (abhartach[polevoi][leshy]) {
            System.out.println("Remove the flag before you step on the square.");
            return;
        }
        if (abarta[polevoi][leshy] == DULLAHAN) {
            System.out.println("**** BOOOOOOOOOOOM! ****");
            ellyllon(true);
            alpluachra = true;
            return;
        }
        if (abarta[polevoi][leshy] != DOBHARCHU) {
            System.out.println("You already stepped on that square.");
            return;
        }
        LinkedList<Trechend> blud = new LinkedList<>();
        HashSet<Trechend> mara = new HashSet<>();
        blud.add(new Trechend(polevoi, leshy));
        while (!blud.isEmpty()) {
            Trechend chuhaister = blud.poll();
            mara.add(chuhaister);
            int bestyia = gwyllion(chuhaister.fachen, chuhaister.fardarrig);
            if (bestyia == 0) {
                for (int antsybolot = Math.max(0, chuhaister.fachen - 1); antsybolot <= Math.min(9, chuhaister.fachen + 1); antsybolot++) {
                    for (int didko = Math.max(0, chuhaister.fardarrig - 1); didko <= Math.min(9, chuhaister.fardarrig + 1); didko++) {
                        Trechend c = new Trechend(antsybolot, didko);
                        if (!mara.contains(c))
                            blud.add(c);
                    }
                }
            }
        }

        //添加代碼片段,判斷玩家是否已經(jīng)把非雷部分踩完
        int n=abarta.length;
        for (int[] ints : abarta)
            for (int j = 0; j < n; j++) {
                if (ints[j] <= 8 && ints[j] >= 0) {
                    count++;
                }
            }

        //若踩完雷,則終止游戲
        if (abarta.length*abarta.length-count==10){
            alpluachra = true;
            count=0;
            System.out.println("Well done! You Win!!!");
        }
        else {
            count=0;
        }

    }

    void potoplenytsia(int vodnik, int bolotnik) {
        if ((abarta[vodnik][bolotnik] != DOBHARCHU) && (abarta[vodnik][bolotnik] != DULLAHAN)) {
            System.out.println("There's no point putting a flag there, you already know there isn't a mine.");
            return;
        }
        if (caoineag == 10) {
            System.out.println("There are already 10 flags out, you can't put down more.");
            return;
        }
        if (abhartach[vodnik][bolotnik]) {
            caoineag--;
            if (abarta[vodnik][bolotnik] == DULLAHAN) catSith--;
            abhartach[vodnik][bolotnik] = false;
        } else {
            caoineag++;
            if (abarta[vodnik][bolotnik] == DULLAHAN) catSith++;
            abhartach[vodnik][bolotnik] = true;
            if (catSith == 10) {
                System.out.println("Well done! You found all the mines!");
                alpluachra = true;
            }

        }

    }

    public void samodiva() {
        ellyllon(false);
        System.out.println("Do you want to step on a square (s) or plant/remove a flag (f)?");
        String[] potercha = {"s","f"};
        String nocnitsa = brownie(potercha);
        System.out.println("Enter X (horizontal) coordinate of square, 0-9.");
        int scheznyk = leprechaun(0,9);
        System.out.println("Enter Y (vertical) coordinate of square, 0-9.");
        int aridnyk = leprechaun(0,9);
        switch(nocnitsa) {
            case "s":
                encantado(scheznyk, aridnyk);

                break;
            case "f":

                potoplenytsia(scheznyk, aridnyk);
                break;
            default:
                assert false : "Invalid choice value " + nocnitsa;
        }
    }
    
    public static void main(String[] args) {
        nephelokokkygia m = new nephelokokkygia();

        while (!m.alpluachra) {

            m.samodiva();
        }
    }

}

結(jié)果截圖:

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

相關(guān)文章

  • Spring boot實(shí)現(xiàn)文件上傳實(shí)例(多文件上傳)

    Spring boot實(shí)現(xiàn)文件上傳實(shí)例(多文件上傳)

    本篇文章主要介紹了Spring boot實(shí)現(xiàn)文件上傳實(shí)例(多文件上傳),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • maven私有鏡像倉庫nexus部署使用

    maven私有鏡像倉庫nexus部署使用

    Nexus在企業(yè)開發(fā)中還是比較常用的私有倉庫管理工具,本文主要介紹了maven私有鏡像倉庫nexus部署使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-07-07
  • SpringBoot打印詳細(xì)啟動(dòng)異常信息

    SpringBoot打印詳細(xì)啟動(dòng)異常信息

    這篇文章主要介紹了SpringBoot打印詳細(xì)啟動(dòng)異常信息,本文包含了詳細(xì)的過程解析與案例,概要的說明了如何去使用打印啟動(dòng)異常信息,需要的朋友可以參考下
    2021-06-06
  • Java8新特性詳解與實(shí)戰(zhàn)分享

    Java8新特性詳解與實(shí)戰(zhàn)分享

    Java 8作為Java語言的一個(gè)重要更新,引入了一系列新特性,這些特性不僅提高了代碼的可讀性和可維護(hù)性,還增強(qiáng)了程序的性能,本文將詳細(xì)介紹Java 8中的幾個(gè)關(guān)鍵使用技巧,并通過代碼案例來展示它們的應(yīng)用,需要的朋友可以參考下
    2024-06-06
  • Java文件處理之使用itextpdf實(shí)現(xiàn)excel轉(zhuǎn)pdf

    Java文件處理之使用itextpdf實(shí)現(xiàn)excel轉(zhuǎn)pdf

    在文件處理中,經(jīng)常有文件類型轉(zhuǎn)換的使用場景,本文主要介紹了如何使用poi以及itextpdf完成excel轉(zhuǎn)pdf的操作,需要的小伙伴可以參考一下
    2024-02-02
  • 基于JDK8總結(jié)java中的interrupt

    基于JDK8總結(jié)java中的interrupt

    本文是基于JDK8總結(jié)java中的interrupt知識,需要的朋友可以參考下
    2017-12-12
  • Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換的實(shí)踐指南

    Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換的實(shí)踐指南

    在 Java 開發(fā)中,許多場景需要訪問多個(gè)數(shù)據(jù)庫,例如多租戶系統(tǒng)或讀寫分離架構(gòu),為了靈活高效地管理這些場景,動(dòng)態(tài)數(shù)據(jù)源切換技術(shù)應(yīng)運(yùn)而生,所以本文給大家介紹了Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換的實(shí)踐指南,需要的朋友可以參考下
    2025-03-03
  • Java讀取PDF中的表格的方法示例

    Java讀取PDF中的表格的方法示例

    本文主要介紹了Java讀取PDF中的表格的方法示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • Java Enum的簡單使用

    Java Enum的簡單使用

    這篇文章主要為大家詳細(xì)介紹了Java Enum的簡單使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • Mac電腦安裝多個(gè)JDK版本的詳細(xì)圖文教程

    Mac電腦安裝多個(gè)JDK版本的詳細(xì)圖文教程

    目前使用的主流版本還是JDK 8,但偶爾會(huì)想體驗(yàn)下新版本(或者舊版本),如果能裝多個(gè)版本的JDK,而且很方便的切換就好了,這篇文章主要給大家介紹了關(guān)于Mac電腦安裝多個(gè)JDK版本的相關(guān)資料,需要的朋友可以參考下
    2024-03-03

最新評論

胶南市| 综艺| 莱州市| 彭阳县| 攀枝花市| 长顺县| 广州市| 通江县| 琼海市| 平原县| 通城县| 开封市| 阳谷县| 股票| 页游| 富锦市| 通州区| 安岳县| 固安县| 新化县| 沽源县| 敖汉旗| 宜良县| 邵东县| 铜鼓县| 霸州市| 太湖县| 麻江县| 天全县| 班玛县| 东至县| 龙川县| 巴中市| 陇西县| 龙岩市| 丹巴县| 巫溪县| 青川县| 漾濞| 彩票| 车致|