1. //棋子
  2. public class Chess {
  3. private boolean isBoomb=false;
  4. private int id;//下标
  5.  
  6. //点击方法
  7. public int click(IChessBoard cb) {
  8. if (isBoomb) {
  9. System.out.println("触雷");
  10. return -1;
  11. }
  12.  
  13. //获取自己的所有邻居,把自己的ID传过去,然后进行判断每个邻居是否是炸弹
  14. ArrayList<Chess> neig = cb.getNei(id); //cb.getNei();回调函数
  15. int cnt = 0;
  16. for (Chess c : neig) {
  17. if (c.isBoomb) {
  18. cnt++;//如果是雷
  19. }
  20. }
  21. //
  22. return cnt;
  23. }
  24.  
  25. //构造方法
  26. public Chess(int id) {
  27. this.id = id;
  28. }
  29.  
  30. public boolean isBoomb() {
  31. return isBoomb;
  32. }
  33.  
  34. public void setBoomb(boolean boomb) {
  35. isBoomb = boomb;
  36. }
  37.  
  38. public int getId() {
  39. return id;
  40. }
  41.  
  42. public void setId(int id) {
  43. this.id = id;
  44. }
  45. }
  1. /**
  2. * //得到一个参数,把此参数的所有邻居放到数组里
  3. *
  4. * @author liuwenlong
  5. * @create 2020-07-21 13:57:14
  6. */
  7. //棋盘类
  8. @SuppressWarnings("all")
  9. public class ChessBoard implements IChessBoard {
  10. private ArrayList<Chess> board = new ArrayList<>();//构造一个随机数的地雷画板
  11. private int maxx;
  12. private int miny;
  13. private int boomNum;
  14.  
  15. public ChessBoard(int maxx, int miny, int boomNum) {
  16. this.maxx = maxx;
  17. this.miny = miny;
  18. this.boomNum = boomNum;
  19. initBoard();//初始化要调用一下
  20. }
  21.  
  22. //-------------------------------------
  23. //获取一个棋子
  24. public Chess getChess(int x, int y) {
  25. return getChess(y * maxx + x);
  26. }
  27.  
  28. public Chess getChess(int id) {
  29. return board.get(id);
  30. }
  31. //-----------------------------------
  32.  
  33. public int getBoomNum() {
  34. return boomNum;
  35. }
  36.  
  37. public void setBoomNum(int boomNum) {
  38. this.boomNum = boomNum;
  39. }
  40.  
  41. public int getMaxx() {
  42. return maxx;
  43. }
  44.  
  45. public void setMaxx(int maxx) {
  46. this.maxx = maxx;
  47. }
  48.  
  49. public int getMiny() {
  50. return miny;
  51. }
  52.  
  53. public void setMiny(int miny) {
  54. this.miny = miny;
  55. }
  56.  
  57. public void initBoard() {
  58. //构造棋子
  59. for (int i = 0; i < (maxx * miny); i++) {
  60. board.add(new Chess(i)); //在棋子构造方法内使用static自增
  61. }
  62.  
  63. //生成一些雷
  64. //生成15个不重复的随机数,ArrayList<Integer> mineIds = 。。
  65. // mineIds = ChessBoard.getRandom(mineIds, 0, maxx * miny);
  66. // for (int i = 0; i < boomNum; i++) { //使用foreach
  67. // board.[i].setBoomb(true);
  68. // //board.get(i).setBoomb(true);
  69. // }
  70. ArrayList<Integer> mineIds = getRandom(0, 99, 15);
  71. // for (int i = 0; i < boomNum; i++) {
  72. // board.get(i).setBoomb(true);
  73. // }
  74. for (int i : mineIds) {
  75. board.get(i).setBoomb(true);
  76. }
  77. }
  78.  
  79. // int id = y*maxx+x;下标
  80. public void showBoard() {
  81. for (int i = 0; i < maxx; i++) {
  82. String line = "";
  83. for (int j = 0; j < maxx; j++) {
  84. int id = i * maxx + j;
  85. Chess c = board.get(id);
  86. if (c.isBoomb()) {
  87. line += 1 + " ";
  88. } else {
  89. line += 0 + " ";
  90. }
  91. }
  92. System.out.println(line);
  93. }
  94. }
  95.  
  96. /**
  97. * 随机生成 N--M,N个不重复随机数 使用ArrayList
  98. *
  99. * @param startRange 起始数字
  100. * @param endRange 终止数字
  101. * @param count 个数
  102. */
  103. public static ArrayList<Integer> getRandom(int startRange, int endRange, int count) {
  104. ArrayList<Integer> arr = new ArrayList<>();
  105. for (int i = 0; i < count; i++) {
  106. arr.add(((int) (Math.random() * (endRange - startRange + 1) + startRange)));
  107. for (int j = 0; j < i; j++) {
  108. if (arr.get(i) == arr.get(j)) {
  109. arr.remove(i);
  110. i--;
  111. break;
  112. }
  113. }
  114. }
  115. return arr;
  116. }
  117.  
  118. //给一个下标,算出该id周围所有邻居
  119. @Override
  120. public ArrayList<Chess> getNei(int id) {
  121.  
  122. //根据下标转换坐标
  123. //整除 id/maxx--y坐标
  124. //取余 id%max --x 坐标
  125. int x0 = id % maxx;
  126. int y0 = id / maxx;
  127.  
  128. //邻居列表
  129. ArrayList<Chess> nei = new ArrayList<>();
  130. for (int ydlt = -1; ydlt < 2; ydlt++) {
  131. int y = y0 + ydlt;
  132. if (y < 0 || y >= miny) { //miny
  133. continue;
  134. }
  135. for (int xdlt = -1; xdlt < 2; xdlt++) {
  136. int x = x0 + xdlt;
  137. if (x < 0 || x >= maxx || (xdlt == 0 && ydlt == 0)) {
  138. continue;
  139. }
  140.  
  141. //这个棋子是邻居,根据坐标换成下标
  142. int cid = y * maxx + x;
  143. nei.add(board.get(cid));
  144. }
  145. }
  146. return nei;
  147. }
  148.  
  149. public ArrayList<Chess> getNei(int x, int y) {
  150.  
  151. return getNei(y * maxx + x); //坐标转下标
  152. }
  153. }
  1. /**
  2. * @author liuwenlong
  3. * @create 2020-07-21 14:01:38
  4. */
  5. @SuppressWarnings("all")
  6. public interface IChessBoard {
  7. //告诉我哪一个棋子
  8. public ArrayList<Chess> getNei(int id);
  9. }
  1. /**
  2. * @author liuwenlong
  3. * @create 2020-07-21 14:28:09
  4. */
  5. @SuppressWarnings("all")
  6. public class Test {
  7. public static void main(String[] args) {
  8. ChessBoard cb = new ChessBoard(10, 10, 15);
  9. cb.showBoard();
  10. System.out.println("点击的'X'坐标为:");
  11. int x = new Scanner(System.in).nextInt();
  12. System.out.println("点击的'Y'坐标为:");
  13. int y = new Scanner(System.in).nextInt();
  14. int number = cb.getChess(x, y).click(cb);
  15. if (number>=0){
  16. System.out.println("共有"+number+"个雷");
  17. }
  18. }
  19. }

Java模拟实现扫雷功能的更多相关文章

  1. Java基础面试操作题:线程同步代码块 两个客户往一个银行存钱,每人存三十次一次存一百。 模拟银行存钱功能,时时银行现金数。

    package com.swift; public class Bank_Customer_Test { public static void main(String[] args) { /* * 两 ...

  2. java模拟浏览器包selenium整合了htmlunit,火狐浏览器,IE浏览器,opare浏览器驱

    //如果网页源码中有些内容是js渲染过来的,那你通过HttpClient直接取肯定取不到,但是这些数据一般都是通过异步请求传过来的(一般都是通过ajax的get或者post方式).那么你可以通过火狐浏 ...

  3. 使用Java模拟一个简单的Dos学生成绩管理系统:

    使用Java模拟学生成绩管理系统... ------------------- 学生成绩管理系统:需要实现的功能:1.录入学生的姓名和成绩2.显示列表.列表中包括学生姓名与成绩3.显示最高分.最低分的 ...

  4. 浏览器与服务器交互原理以及用java模拟浏览器操作v

    浏览器应用服务器JavaPHPApache * 1,在HTTP的WEB应用中, 应用客户端和服务器之间的状态是通过Session来维持的, 而Session的本质就是Cookie, * 简单的讲,当浏 ...

  5. Java 基本数据类型 sizeof 功能

    Java 基本数据类型 sizeof 功能 来源 https://blog.csdn.net/ithomer/article/details/7310008 Java基本数据类型int     32b ...

  6. ListView模拟微信好友功能

    ListView模拟微信好友功能 效果图: 分析: 1.创建listView 2.创建数据 3.创建适配器 将数据放到呈现数据的容器里面. 将这个容器(带数据)连接适配器. 其实是直接在我们自己写的a ...

  7. TCP模拟QQ聊天功能

    需求: 模拟qq聊天功能:实现客户端与服务器(一对一)的聊天功能,客户端首先发起聊天,输入的内容在服务器端和客户端显示,然后服务器端也可以输入信息,同样信息在客户端和服务端显示. 提示: 客户端 1) ...

  8. java模拟post请求发送json

    java模拟post请求发送json,用两种方式实现,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请求, 方法一: package main ...

  9. java 模拟qq源码

    java 模拟qq源码: http://files.cnblogs.com/files/hujunzheng/QQ--hjzgg.zip

随机推荐

  1. C#LeetCode刷题之#852-山脉数组的峰顶索引(Peak Index in a Mountain Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4003 访问. 我们把符合下列属性的数组 A 称作山脉: A.le ...

  2. Vue Element-UI 中列表单选的实现

    el-table中单选的实现 引用场景: 选择单条数据进行业务操作 实现方式: 给el-table-column设置el-radio Template 代码 <div class="r ...

  3. Flutter 容器 (1) - Center

    Center容器用来居中widget import 'package:flutter/material.dart'; class AuthList extends StatelessWidget { ...

  4. next()与nextLine()的区别

    abc def ghij kl mno pqr st uvw xyz 你用next(),第一次取的是abc,第二次取的是def,第三次取的是ghij 你用nextLine(),第一次取的是abc de ...

  5. 通过http、https域名访问静态网页、nginx配置负载均衡(nginx配置)

    很多场景下需要可以通过浏览器访问静态网页,不想把服务器ip地址直接暴露出来,通过nginx可以解决这个问题. 实现http域名访问静态网页 1.域名解析配置(本文都是以阿里云为例,其他平台,操作步骤类 ...

  6. Spring Boot 教程 - 文件上传下载

    在日常的开发工作中,基本上每个项目都会有各种文件的上传和下载,大多数文件都是excel文件,操作excel的JavaAPI我用的是apache的POI进行操作的,POI我之后会专门讲到.此次我们不讲如 ...

  7. Java代替if和switch的方法(记录一下)

    package xcc.mapTest; /** * @Decription: 接口 * @Author: * @Date: * @Email: **/ public interface Functi ...

  8. 正则表达式截取xml

    $str = '<Ips><GateWayRsp><head><ReferenceID>123</ReferenceID><RspCo ...

  9. 算法-deque双端队列

    Python的deque模块,它是collections库的一部分.deque实现了双端队列,意味着你可以从队列的两端加入和删除元素 1.基本介绍 # 实例化一个deque对象d = deque()d ...

  10. nvm -- node 多版本管理器

    Node.js 越来越热,应用的场景也越来越多. 在开发中,我们可能同时在进行多个 node 项目,而这些不同的项目所使用的 node 版本又是不一样的,或者是要用更新的 node 版本进行试验和学习 ...