java实现简单的扫雷小游戏
作者:boogie_liu 时间:2022-09-14 19:23:24
使用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
标签:java,扫雷
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
教你怎么用java一键自动生成数据库文档
2021-08-01 02:34:36
![](https://img.aspxhome.com/file/2023/1/61321_0s.png)
通过Mybatis实现单表内一对多的数据展示示例代码
2021-12-20 12:34:31
![](https://img.aspxhome.com/file/2023/1/64011_0s.png)
Java加载property文件配置过程解析
2023-10-07 07:53:03
C#控制台程序使用Log4net日志组件详解
2023-02-15 19:44:26
![](https://img.aspxhome.com/file/2023/2/80052_0s.png)
springboot自定义Starter的具体流程
2022-01-26 05:08:06
Zookeeper和Eureka哪个更好?
2023-11-10 02:57:35
java——多线程基础
2021-08-08 04:14:25
![](https://img.aspxhome.com/file/2023/9/89939_0s.png)
Java swing实现酒店管理系统
2021-08-08 02:24:12
Android如何让WebView中的HTML5页面实现视频全屏播放
2023-07-29 00:32:06
![](https://img.aspxhome.com/file/2023/1/82401_0s.jpg)
Spring定时任务使用及如何使用邮件监控服务器
2023-01-12 16:38:58
c#动态加载卸载DLL的方法
2023-06-08 08:18:08
maven profile动态选择配置文件详解
2023-11-05 22:05:20
![](https://img.aspxhome.com/file/2023/4/64514_0s.png)
Spring如何处理注解的深入理解
2023-09-09 14:35:49
Android的RV列表刷新详解Payload与Diff方式异同
2023-07-05 13:17:29
Jackson 反序列化时实现大小写不敏感设置
2021-11-18 06:17:18
Java实现双色球抽奖随机算法示例
2023-04-25 12:15:38
Springboot如何设置过滤器及重复读取request里的body
2022-05-26 04:59:20
![](https://img.aspxhome.com/file/2023/6/74996_0s.jpg)
Java 中的变量类型
2022-05-27 20:00:19
java简单实现复制 粘贴 剪切功能代码分享
2023-10-14 05:08:23
maven基础教程——简单了解maven的特点与功能
2023-09-07 03:43:02