美文网首页
扫雷问题

扫雷问题

作者: 新手村的0级玩家 | 来源:发表于2018-11-08 19:43 被阅读0次

0.前言

今天一同学让帮忙写一个打印扫雷代码某两个 状态的程序,感觉还挺有意思的,特此 整理如下:

1.问题描述

默认10 × 10的扫雷板,默认数值是0,读取一个描述“雷”坐标的txt文件,文件第一行表示“雷”的个数n,接下来n行标明n个“雷”的横纵坐标

“雷”周围八个方向(左、左上、上、右上、右、右下、下、左下)中的任何一个方向的空间都标明了周围“雷的个数”,“雷”用 -1表示,每个没有地雷的空间周围多一个“雷”那么该空间的数字+1

输出放置“雷”后的整个扫雷板,以及改变每个没有地雷的空间后的扫雷板

2.分析

读取文件,切割字符串后,可得某个“地雷”的坐标

第一个状态:直接向扫雷板中“放置地雷”即可;

第二个状态:遍历每个地雷,判断地雷周围八个方向(左、左上、上、右上、右、右下、下、左下)是否能“改变”(是否越界,是否已经放置地雷)
如果可以“改变”,自增即可

3.代码实现


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;

/**
 * @author Stone6762
 *
 */
public class Main {

    /**
     * 读取文件
     * 
     * @param file
     * @return
     */
    public static String txt2String(File file) {
        StringBuilder result = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));// 构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) {// 使用readLine方法,一次读一行
                result.append(s + System.lineSeparator());
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    /**
     * 打印数组
     * 
     * @param array
     */
    public static void print(int[][] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length; j++) {
                //默认占两位,不足补空格,放置出现两位数或者-1,对不齐的情况
                String format = String.format("% 2d", array[i][j]);
                System.out.print("[" + format + "]");
            }
            System.out.println();
        }
    }

    /**
     * 改变炸弹周围的数据
     * 
     * @param array
     * @param x
     *            炸弹横坐标
     * @param y
     *            炸弹纵坐标
     */
    public static void changBoomNeighbor(int[][] array, int x, int y) {
        // 该炸弹左、左上、上、右上、右、右下、下、左下 八个地方,非炸弹的位置+1
        // 左上
        int temp_x = x - 1;
        int temp_y = y - 1;
        if (isCanChange(array, temp_x, temp_y)) {
            array[temp_x][temp_y]++;
        }
        // 上
        temp_x = x;
        temp_y = y - 1;
        if (isCanChange(array, temp_x, temp_y)) {
            array[temp_x][temp_y]++;
        }
        // 右上
        temp_x = x + 1;
        temp_y = y - 1;
        if (isCanChange(array, temp_x, temp_y)) {
            array[temp_x][temp_y]++;
        }
        // 左
        temp_x = x - 1;
        temp_y = y;
        if (isCanChange(array, temp_x, temp_y)) {
            array[temp_x][temp_y]++;
        }
        // 右
        temp_x = x + 1;
        temp_y = y;
        if (isCanChange(array, temp_x, temp_y)) {
            array[temp_x][temp_y]++;
        }
        // 左下
        temp_x = x - 1;
        temp_y = y + 1;
        if (isCanChange(array, temp_x, temp_y)) {
            array[temp_x][temp_y]++;
        }
        // 下
        temp_x = x;
        temp_y = y + 1;
        if (isCanChange(array, temp_x, temp_y)) {
            array[temp_x][temp_y]++;
        }
        // 右下
        temp_x = x + 1;
        temp_y = y + 1;
        if (isCanChange(array, temp_x, temp_y)) {
            array[temp_x][temp_y]++;
        }
    }

    /**
     * 判断某个地方是否可以被改变
     * 
     * @param array
     * @param x
     * @param y
     * @return
     */
    public static boolean isCanChange(int[][] array, int x, int y) {
        if (x < 0 || x > 9) {
            return false;
        }
        if (y < 0 || y > 9) {
            return false;
        }
        if (array[x][y] == -1) {
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入文件名如:D:/ct/index.txt (路径名不能有中文)");
        while (scan.hasNext()) {
            int[][] array = new int[10][10];
            String fileName = scan.next();
            File file = new File(fileName);
            String txt_str = txt2String(file);
            String[] txt_datas = txt_str.split(System.lineSeparator());
            int num = Integer.parseInt(txt_datas[0]);// 有多少地雷
            int[][] boom = new int[num][2];
            for (int i = 1; i < txt_datas.length; i++) {
                String[] temp_strs = txt_datas[i].split(" ");
                int x = Integer.parseInt(temp_strs[0]);
                int y = Integer.parseInt(temp_strs[1]);
                boom[i - 1][0] = x;
                boom[i - 1][1] = y;
                array[x][y] = -1;
            }
            // 放置炸弹后的情况
            print(array);

            for (int i = 0; i < boom.length; i++) {
                changBoomNeighbor(array, boom[i][0], boom[i][1]);
            }
            System.out.println("------改变周边后-------");
            // 处理炸弹 改变周边后的情况
            print(array);
        }
    }
}

相关文章

  • 扫雷问题

    0.前言 今天一同学让帮忙写一个打印扫雷代码某两个 状态的程序,感觉还挺有意思的,特此 整理如下: 1.问题描述 ...

  • 小赚红包扫雷平台开发

    小赚红包扫雷系统app开发,小赚红包扫雷源码搭建,小赚社区红包扫雷,小赚扫雷系统app,提供小赚红包扫雷服务器、域...

  • 扫雷红包飞信扫雷系统模式现成开发

    扫雷红包飞信扫雷系统模式现成开发、虾聊扫雷系统app源码开发,樱桃红包扫雷系统app源码开发、博友红包扫雷系统ap...

  • Web版扫雷开发小记(3)

    前篇: web版扫雷开发小记(1)web版扫雷开发小记(2)web版扫雷开发小记(3)web版扫雷开发小记(4) ...

  • Web扫雷开发小记(1)

    目录Web扫雷开发小记(2)Web扫雷开发小记(3)Web扫雷开发小记(4) 刚好今天做阿里前端笔试问到扫雷了,那...

  • web版扫雷开发小记(4)

    目录:Web扫雷开发小记(1)Web扫雷开发小记(2)Web扫雷开发小记(3) 其实在完成上篇的功能之后,一个扫雷...

  • 飞信红包扫雷系统源码平台

    飞信红包扫雷系统app源码开发、腾信红包扫雷系统app源码、诚信红包扫雷系统app开发、提供红包扫雷系统服务器、域...

  • 虾聊社区红包扫雷接龙平台模式开发

    樱桃红包扫雷系统app源码开发、虾聊社区红包扫雷接龙平台模式开发、博友红包扫雷系统app开发、腾信红包扫雷系统ap...

  • 哇!踩到雷了

    这一段时间迷上了一个年头可能比我大的游戏--扫雷。人家追剧我在扫雷,人家游戏我在扫雷,累了,困了,扫一扫雷。真心爱...

  • 两分钟教你用C++修改"扫雷"游戏的设置,有

    C++ 6.0 修改"扫雷"游戏的设置,可以对扫雷游戏的界面进行设置,设置 黑白界面和彩色界面,可修改扫雷游戏英雄...

网友评论

      本文标题:扫雷问题

      本文链接:https://www.haomeiwen.com/subject/jfcgxqtx.html