Java模拟实现扫雷功能
2024-09-28 14:59:29
//棋子
public class Chess {
private boolean isBoomb=false;
private int id;//下标 //点击方法
public int click(IChessBoard cb) {
if (isBoomb) {
System.out.println("触雷");
return -1;
} //获取自己的所有邻居,把自己的ID传过去,然后进行判断每个邻居是否是炸弹
ArrayList<Chess> neig = cb.getNei(id); //cb.getNei();回调函数
int cnt = 0;
for (Chess c : neig) {
if (c.isBoomb) {
cnt++;//如果是雷
}
}
//
return cnt;
} //构造方法
public Chess(int id) {
this.id = id;
} public boolean isBoomb() {
return isBoomb;
} public void setBoomb(boolean boomb) {
isBoomb = boomb;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
}
}
/**
* //得到一个参数,把此参数的所有邻居放到数组里
*
* @author liuwenlong
* @create 2020-07-21 13:57:14
*/
//棋盘类
@SuppressWarnings("all")
public class ChessBoard implements IChessBoard {
private ArrayList<Chess> board = new ArrayList<>();//构造一个随机数的地雷画板
private int maxx;
private int miny;
private int boomNum; public ChessBoard(int maxx, int miny, int boomNum) {
this.maxx = maxx;
this.miny = miny;
this.boomNum = boomNum;
initBoard();//初始化要调用一下
} //-------------------------------------
//获取一个棋子
public Chess getChess(int x, int y) {
return getChess(y * maxx + x);
} public Chess getChess(int id) {
return board.get(id);
}
//----------------------------------- public int getBoomNum() {
return boomNum;
} public void setBoomNum(int boomNum) {
this.boomNum = boomNum;
} public int getMaxx() {
return maxx;
} public void setMaxx(int maxx) {
this.maxx = maxx;
} public int getMiny() {
return miny;
} public void setMiny(int miny) {
this.miny = miny;
} public void initBoard() {
//构造棋子
for (int i = 0; i < (maxx * miny); i++) {
board.add(new Chess(i)); //在棋子构造方法内使用static自增
} //生成一些雷
//生成15个不重复的随机数,ArrayList<Integer> mineIds = 。。
// mineIds = ChessBoard.getRandom(mineIds, 0, maxx * miny);
// for (int i = 0; i < boomNum; i++) { //使用foreach
// board.[i].setBoomb(true);
// //board.get(i).setBoomb(true);
// }
ArrayList<Integer> mineIds = getRandom(0, 99, 15);
// for (int i = 0; i < boomNum; i++) {
// board.get(i).setBoomb(true);
// }
for (int i : mineIds) {
board.get(i).setBoomb(true);
}
} // int id = y*maxx+x;下标
public void showBoard() {
for (int i = 0; i < maxx; i++) {
String line = "";
for (int j = 0; j < maxx; j++) {
int id = i * maxx + j;
Chess c = board.get(id);
if (c.isBoomb()) {
line += 1 + " ";
} else {
line += 0 + " ";
}
}
System.out.println(line);
}
} /**
* 随机生成 N--M,N个不重复随机数 使用ArrayList
*
* @param startRange 起始数字
* @param endRange 终止数字
* @param count 个数
*/
public static ArrayList<Integer> getRandom(int startRange, int endRange, int count) {
ArrayList<Integer> arr = new ArrayList<>();
for (int i = 0; i < count; i++) {
arr.add(((int) (Math.random() * (endRange - startRange + 1) + startRange)));
for (int j = 0; j < i; j++) {
if (arr.get(i) == arr.get(j)) {
arr.remove(i);
i--;
break;
}
}
}
return arr;
} //给一个下标,算出该id周围所有邻居
@Override
public ArrayList<Chess> getNei(int id) { //根据下标转换坐标
//整除 id/maxx--y坐标
//取余 id%max --x 坐标
int x0 = id % maxx;
int y0 = id / maxx; //邻居列表
ArrayList<Chess> nei = new ArrayList<>();
for (int ydlt = -1; ydlt < 2; ydlt++) {
int y = y0 + ydlt;
if (y < 0 || y >= miny) { //miny
continue;
}
for (int xdlt = -1; xdlt < 2; xdlt++) {
int x = x0 + xdlt;
if (x < 0 || x >= maxx || (xdlt == 0 && ydlt == 0)) {
continue;
} //这个棋子是邻居,根据坐标换成下标
int cid = y * maxx + x;
nei.add(board.get(cid));
}
}
return nei;
} public ArrayList<Chess> getNei(int x, int y) { return getNei(y * maxx + x); //坐标转下标
}
}
/**
* @author liuwenlong
* @create 2020-07-21 14:01:38
*/
@SuppressWarnings("all")
public interface IChessBoard {
//告诉我哪一个棋子
public ArrayList<Chess> getNei(int id);
}
/**
* @author liuwenlong
* @create 2020-07-21 14:28:09
*/
@SuppressWarnings("all")
public class Test {
public static void main(String[] args) {
ChessBoard cb = new ChessBoard(10, 10, 15);
cb.showBoard();
System.out.println("点击的'X'坐标为:");
int x = new Scanner(System.in).nextInt();
System.out.println("点击的'Y'坐标为:");
int y = new Scanner(System.in).nextInt();
int number = cb.getChess(x, y).click(cb);
if (number>=0){
System.out.println("共有"+number+"个雷");
}
}
}
最新文章
- laravel 在windows中使用一键安装包步骤
- 不在折腾---storm-0.9.2-incubating分布式安装
- 关于LR中的EXTRARES
- 无法在Web服务器上启动调试,与Web服务器通信时出现身份验证错误
- JavaScript学习笔记(7)——JavaScript语法之函数
- RMAN常用备份恢复命令汇总
- LinkedList的分析(转)
- a=&#39;1,2,3,4,5&#39;如何转换为[&#39;1&#39;,&#39;2&#39;,&#39;3&#39;,&#39;4&#39;,&#39;5&#39;]
- Docker集群实验环境布署--swarm【5 容器启动组件--node】
- 移动应用开发者最应该知道的8款SDK
- 解决sqlite 删除记录后数据库文件大小不变
- leetcode — symmetric-tree
- PHP artisan migrate 报错显示 could not find driver ,怎么办?
- linux回顾
- 于bugku中游荡意外得到关于CBC翻转攻击思路
- 《EM-PLANT仿真技术教程》读书笔记
- Codeforces round 1103
- 原 用Tomcat服务器配置https双向认证过程实战
- Bootstrap-Other:v2 教程
- SQL SERVER SQLOS的任务调度--微软亚太区数据库技术支持组 官方博客
热门文章
- java Iterator迭代器
- Vuex mapAction的基本使用
- error PRJ0003 : 生成“cmd.exe”时出错 2010-01-19 22:26
- int ,long , long long , __int64类型的范围
- python re之search/match差别
- 笔记:Ubuntu安装LAMP环境
- springboot + kafka 入门实例 入门demo
- Hyperledger Fabric 手动搭建【区块链学习三】
- 极简 Node.js 入门 - 3.3 文件写入
- OpenStack虚拟机virtaulinterfance 网络设备在libvirt的代码梳理