java实现简单的扫雷小游戏
作者:boogie_liu 发布时间:2022-09-14 19:23:24
标签: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();
}
}
}
结果截图:
来源:https://blog.csdn.net/weixin_45652057/article/details/117121495
0
投稿
猜你喜欢
- 一、堆的概念堆的定义:n个元素的序列{k1 , k2 , … , kn}称之为堆,当且仅当满足以下条件时:(1)ki
- Android之文件数据存储一、文件保存数据介绍Activity提供了openFileOutput()方法可以用于把数据输出到文件中,具体的
- 什么是RabbitMQ?RabbitMQ是由erlang语言开发的一个基于AMQP(Advanced Message Queuing Pro
- 前言在我们日常的开发中,很多时候,定时任务都不是写死的,而是写到数据库中,从而实现定时任务的动态配置,下面就通过一个简单的示例,来实现这个功
- 之前使用springMVC+spring+mybatis,总是被一些繁琐的xml配置,有时候如果配置出错,还要检查各种xml配置,偶然接触到
- 抽象类和抽象方法常用知识点:(1)抽象类作为被继承类,子类必须实现抽象类中的所有抽象方法,除非子类也为抽象类。也就是说,如果子类也为抽象类,
- LRU:Least Recently Used最近最少使用,当缓存容量不足时,先淘汰最近最少使用的数据。就像JVM垃圾回收一样,希望将存活的
- 前言LocalDateTime、LocalDate、LocalTime 是 Java8 全新的日期框架,加强了对时间的管理,有很多特别好用的
- IntelliJ IDEA中实现跟eclipse一样的outline方法,查看文件内所有已经声明的方法。mac的可以在key map 里搜索
- springboot项目还是ssm等java常用框架都会有这样的问题,解决办法通用问题场景前端发送Post请求,前端返回400 Bad Re
- 1. 算法分析根据概率将奖品划分区间,每个区间代表一个奖品,然后抽取 随机数,反查落在那个区间上,即为所抽取的奖品。2. 代码核心
- 1、配置maven环境变量,将maven安装的bin⽬录添加到path路径中(此电脑->属性->高级系统设置->环境变量-
- 在为什么阿里巴巴不建议在for循环中使用”+”进行字符串拼接一文中,我们介绍了几种Java中字符串拼接的方式,以及优缺点。其中还有一个重要的
- 本文实例讲述了Java基于正则实现的日期校验功能。分享给大家供大家参考,具体如下:private void checkDate() thro
- 目录一.什么是负载均衡二.负载均衡的简单分类三.为什么需要做负载均衡四.springCloud如何开启负载均衡五.IRule1.Random
- 前言本文主要介绍了如果通过输入年份月份输出天数的相关内容,下面话不多说了,来一起看看详细的介绍吧示例代码package com.ambow.
- 抽象类(abstract):抽象类不能创建实例,它只能作为父类被继承。抽象类是从多个具体类中抽象出来的父类,它具有更高层次的抽象。从多个具有
- 最近在鼓捣spring -boot ,真好用,学习到jpa.通过生成Entity 文件,能够快速的生成数据库,并且使用JpaReposito
- 本文实例讲述了java实现mp3合并的方法。分享给大家供大家参考。具体实现方法如下:package test;import java.io.
- 用来记录自己所用到的知识前两天在做项目的时候发现有时候在访问网络数据的时候由于后台要做的工作较多,给我们返回数据的时间较长,所以老大叫我加了