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


猜你喜欢
- The error simply says, “you've made changes in files in your works
- Java提供一种机制叫做序列化,通过有序的格式或者字节序列持久化java对象,其中包含对象的数据,还有对象的类型,和保存在对象中
- Android底部支付弹窗实现的效果:实现的思路:1.通过继承PopupWindow自定义View来达到弹窗的弹出效果;2.通过回调将输入的
- 自用项目中统一Eclipse格式化Java、JavaScript、JSP、HTML代码设置1.Window->Preferences
- 队列的特点1.可以使用数组和链表两种方式来实现。2.遵循先入先出(FIFO)的规则,即先进入的数据先出。3.属于有序列表。图解实现过程:1.
- Android layoutAnimation详解及应用前言:最近在玩一个APP的时候,发现刚进入他的页面,他页面的子控件都是从
- 前言上一篇分析了事务注解的解析过程,本质上是将事务封装为切面加入到AOP的执行链中,因此会调用到MethodInceptor的实现类的inv
- utf-8转unicode public static String utf8ToUnicode(String inStr) {  
- 背景接上文《失踪人口回归,mybatis-plus 3.3.2 发布》[1] ,提供了一个非常实用的功能 「数据安全保护」 功能,不仅支持数
- 整理文档,java 动态增加定时任务示例,直接上代码。import org.apache.tools.ant.util.DateUtils;
- 我记得最开始接触多进程,多线程这一块的时候我不是怎么理解,为什么要有多线程啊?多线程到底是个什么鬼啊?我一个程序好好的就可以运行为什么要用到
- 前言本章是关于Java数组的最全汇总,本篇为汇总上篇,主要讲了一维数组的相关内容。数组是最常见的一种数据结构,它是相同类型的用一个标识符封装
- 目录一、需求二、步骤三、结果一、需求把以下txt中含“baidu”字符串的链接输出到一个文件,否则输出到另外一个文件。二、步骤1.LogMa
- public class MenuEx extends Activity { private static final String TAG
- jpa EntityManager复杂查询概念EntityManager:EntityManager是JPA中用于增删改查的接口,它的作用相
- 基础铺垫在java中,关于json的lib有很多,比如jackjson、fastjson、gson等等,本人都用过,但是对于我等只需要让ja
- 最近在做一个需求:从其他系统的ftp目录下载存储图片url的文件,然后读取文件中的url地址,根据地址下载图片后按天压缩成一个包,平均一个地
- java 解决异常 2 字节的 UTF-8 序列的字节 2 无效的问题  
- 本文实例讲述了Java8新增的重复注解功能。分享给大家供大家参考,具体如下:一 点睛在Java 8以前,同一个程序元素前最多只能使用一个相同
- 你是否受够了每次修改静态文件都要重启服务器?有时候在一些公司前后端的职责没有那么的明确,往往后台人员也要去写一些页面,像jsp页面,或者其他