Java模拟实现扫雷功能
- //棋子
- 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+"个雷");
- }
- }
- }
Java模拟实现扫雷功能的更多相关文章
- Java基础面试操作题:线程同步代码块 两个客户往一个银行存钱,每人存三十次一次存一百。 模拟银行存钱功能,时时银行现金数。
package com.swift; public class Bank_Customer_Test { public static void main(String[] args) { /* * 两 ...
- java模拟浏览器包selenium整合了htmlunit,火狐浏览器,IE浏览器,opare浏览器驱
//如果网页源码中有些内容是js渲染过来的,那你通过HttpClient直接取肯定取不到,但是这些数据一般都是通过异步请求传过来的(一般都是通过ajax的get或者post方式).那么你可以通过火狐浏 ...
- 使用Java模拟一个简单的Dos学生成绩管理系统:
使用Java模拟学生成绩管理系统... ------------------- 学生成绩管理系统:需要实现的功能:1.录入学生的姓名和成绩2.显示列表.列表中包括学生姓名与成绩3.显示最高分.最低分的 ...
- 浏览器与服务器交互原理以及用java模拟浏览器操作v
浏览器应用服务器JavaPHPApache * 1,在HTTP的WEB应用中, 应用客户端和服务器之间的状态是通过Session来维持的, 而Session的本质就是Cookie, * 简单的讲,当浏 ...
- Java 基本数据类型 sizeof 功能
Java 基本数据类型 sizeof 功能 来源 https://blog.csdn.net/ithomer/article/details/7310008 Java基本数据类型int 32b ...
- ListView模拟微信好友功能
ListView模拟微信好友功能 效果图: 分析: 1.创建listView 2.创建数据 3.创建适配器 将数据放到呈现数据的容器里面. 将这个容器(带数据)连接适配器. 其实是直接在我们自己写的a ...
- TCP模拟QQ聊天功能
需求: 模拟qq聊天功能:实现客户端与服务器(一对一)的聊天功能,客户端首先发起聊天,输入的内容在服务器端和客户端显示,然后服务器端也可以输入信息,同样信息在客户端和服务端显示. 提示: 客户端 1) ...
- java模拟post请求发送json
java模拟post请求发送json,用两种方式实现,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请求, 方法一: package main ...
- java 模拟qq源码
java 模拟qq源码: http://files.cnblogs.com/files/hujunzheng/QQ--hjzgg.zip
随机推荐
- C#LeetCode刷题之#852-山脉数组的峰顶索引(Peak Index in a Mountain Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4003 访问. 我们把符合下列属性的数组 A 称作山脉: A.le ...
- Vue Element-UI 中列表单选的实现
el-table中单选的实现 引用场景: 选择单条数据进行业务操作 实现方式: 给el-table-column设置el-radio Template 代码 <div class="r ...
- Flutter 容器 (1) - Center
Center容器用来居中widget import 'package:flutter/material.dart'; class AuthList extends StatelessWidget { ...
- next()与nextLine()的区别
abc def ghij kl mno pqr st uvw xyz 你用next(),第一次取的是abc,第二次取的是def,第三次取的是ghij 你用nextLine(),第一次取的是abc de ...
- 通过http、https域名访问静态网页、nginx配置负载均衡(nginx配置)
很多场景下需要可以通过浏览器访问静态网页,不想把服务器ip地址直接暴露出来,通过nginx可以解决这个问题. 实现http域名访问静态网页 1.域名解析配置(本文都是以阿里云为例,其他平台,操作步骤类 ...
- Spring Boot 教程 - 文件上传下载
在日常的开发工作中,基本上每个项目都会有各种文件的上传和下载,大多数文件都是excel文件,操作excel的JavaAPI我用的是apache的POI进行操作的,POI我之后会专门讲到.此次我们不讲如 ...
- Java代替if和switch的方法(记录一下)
package xcc.mapTest; /** * @Decription: 接口 * @Author: * @Date: * @Email: **/ public interface Functi ...
- 正则表达式截取xml
$str = '<Ips><GateWayRsp><head><ReferenceID>123</ReferenceID><RspCo ...
- 算法-deque双端队列
Python的deque模块,它是collections库的一部分.deque实现了双端队列,意味着你可以从队列的两端加入和删除元素 1.基本介绍 # 实例化一个deque对象d = deque()d ...
- nvm -- node 多版本管理器
Node.js 越来越热,应用的场景也越来越多. 在开发中,我们可能同时在进行多个 node 项目,而这些不同的项目所使用的 node 版本又是不一样的,或者是要用更新的 node 版本进行试验和学习 ...