java实现简单的扫雷小游戏

 更新时间:2021年05月26日 10:07:12   作者:boogie_liu  
这篇文章主要为大家详细介绍了java实现简单的扫雷小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

使用java制作一款简单的扫雷游戏,供大家参考,具体内容如下

import java.util.*;

public class nephelokokkygia {
    
    int[][] abarta;//数字矩阵
    boolean[][] abhartach;//当前点是否被标记
    boolean alpluachra;//判断是否结束游戏
    int caoineag;//标记的flag数
    int catSith;//标记命中雷的个数
    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值的点为不能查看的点,默认初始化为字符 “.”
                            System.out.print(". ");
                            break;
                        case DULLAHAN:
                            //  矩阵为-2值的点判断是否为雷,并判断当前位置是否为雷,
                            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=纵坐标
     * @param dolia=横坐标
     * @return 当前点的值
     */
    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);
                    }
                }
            }
        }

        //添加代码片段,判断玩家是否已经把非雷部分踩完
        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();
        }
    }

}

结果截图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 解决maven build 无反应,直接terminated的问题

    解决maven build 无反应,直接terminated的问题

    下面小编就为大家带来一篇解决maven build 无反应,直接terminated的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • Java设计模式之桥模式(Bridge模式)介绍

    Java设计模式之桥模式(Bridge模式)介绍

    这篇文章主要介绍了Java设计模式之桥模式(Bridge模式)介绍,本文讲解了为什么使用桥模式、如何实现桥模式、Bridge模式在EJB中的应用等内容,需要的朋友可以参考下
    2015-03-03
  • Java静态方法不具有多态性详解

    Java静态方法不具有多态性详解

    下面小编就为大家带来一篇Java静态方法不具有多态性详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-06-06
  • 分布式难题ElasticSearch解决大数据量检索面试

    分布式难题ElasticSearch解决大数据量检索面试

    这篇文章主要为大家介绍了分布式面试难题,ElasticSearch解决大数据量检索的问题分析回答,让面试官无话可说,帮助大家实现面试开薪自由
    2022-03-03
  • springboot项目如何打war包流程的方法

    springboot项目如何打war包流程的方法

    这篇文章主要介绍了springboot项目如何打war包流程的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • springboot+mybatis如何屏蔽掉mybatis日志

    springboot+mybatis如何屏蔽掉mybatis日志

    这篇文章主要介绍了springboot+mybatis如何屏蔽掉mybatis日志问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • java后端解决跨域的几种问题解决

    java后端解决跨域的几种问题解决

    这篇文章主要介绍了java后端解决跨域的几种问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • Java中几个Reference常见的作用详解

    Java中几个Reference常见的作用详解

    这篇文章主要给大家介绍了Java中关于Reference多个作用的相关资料,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编一起来学习学习吧。
    2017-06-06
  • springboot定时任务备份mysql数据库的实现示例

    springboot定时任务备份mysql数据库的实现示例

    为了防止数据库被清库或者误删数据库的情况,所以需要定时将mysql数据库中的数据进行备份,本文主要介绍了springboot定时任务备份mysql数据库的实现示例,需要的朋友们下面随着小编来一起学习学习吧
    2024-03-03
  • Spring Boot启动端口修改方法

    Spring Boot启动端口修改方法

    spring boot是个好东西,可以不用容器直接在main方法中启动,而且无需配置文件,方便快速搭建环境。下面通过本文给大家分享Spring Boot修改启动端口的方法,感兴趣的的朋友一起看看吧
    2017-07-07

最新评论