简单的java扫雷程序,已运行成功。自己想出来的,有个问题很困惑有望高手能解答。
来源:互联网 发布:js 右键菜单 编辑:程序博客网 时间:2024/06/13 01:06
SeepThunder
简单的扫雷程序:我的方法很简单通过判断9个位置来放雷a[0][0],a[0][9],a[9][0],a[9][9],a[0~9][0],a[0~9][9],a[0][0~9],
a[9][0~9],a[0~9][0~9],
我大致讲一下我的程序:
定义了一个10*10的二维数组,随机产生10个雷(数字9代表着雷)
有个问题我很困惑,这个程序编完我都没有想明白,问题如下:
在随机产生的雷中,我用下面的结果来说明:
1 1 1
1 9 1
1 1 1
如果a[i][j]==9
那么相应的
a[i-1][j-1] a[i-1][j] a[i-1][j+1]
a[i][j-1] a[i][j+1]
a[i+1][j-1] a[i+1][j] a[i+1][j+1]
他们的值+1
本来我是要写在一个条件里面的,运行后的结果却发现为,9上面的值没有+1,很奇怪,这个我困惑的很,为什么没有+1?
0 0 0
0 9 1
1 1 1
所以现在我分成了两个部分,
part1和part2,part1是给9上面的一半赋值,part2给9下面的赋值,这样运行的结果才正确,这是什么情况,有望高手解释一下。
我想了一下,这个程序只是简单的代码堆砌而成,有没有更简单的面向对象的处理方法,希望能改进一下。
代码如下:
package com.arithmetic.show;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
public class SweepThunder {
/**
* @param args
* SweepThunder
*This is a simple small program about SweepThunder
*Its function including:
*it has eight position need to clarify
*a[i-1][j-1] a[i-1][j] a[i-1][j+1]
*a[i][j-1] a[i][j] a[i][j+1]
*a[i+1][j-1] a[i+1][j] a[i+1][j+1]
*There are nine coordinate need to judge
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int count = 0;
int a[][] = new int[10][10];
Random r = new Random();
int m = 0;
int n = 0;
Set<Integer> set = new HashSet<Integer>();
// There are 10 number random generate in 10*10 array and its values is equals 9
while(true) {
set.add(r.nextInt(10));
if(set.size()==10) {
break;
}
}
Iterator<Integer> it = set.iterator();
while(it.hasNext()) {
m = (Integer) it.next();
n = r.nextInt(10);
a[m][n] = 9;
}
//part1 is to solve the voluation up a[i][j]=9
for(int i=0;i<a.length;i++) {
//System.out.print(" ");
for(int j=0;j<a[i].length;j++) {
//System.out.print(" ");
//when the Thunder equals "a[9][0]"
if(i==9&&j==0&&a[i][j]==9) {
if(a[i-1][j]!=9) {
a[i-1][j]++;
}
if(a[i-1][j+1]!=9) {
a[i-1][j+1]++;
}
}
//when the Thunder equals "a[0][9]"
if(i==0&&j==9&&a[i][j]==9) {
if(a[i][j-1]!=9) {
a[i][j-1]++;
}
}
//when the Thunder equals "a[9][9]"
if(i==9&&j==9&&a[i][j]==9) {
if(a[i][j-1]!=9) {
a[i][j-1]++;
}
if(a[i-1][j-1]!=9) {
a[i-1][j-1]++;
}
if(a[i-1][j]!=9) {
a[i-1][j]++;
}
}
//when the Thunder equals "a[0~9][0]"
if(i>0&&i<9&&j==0&&a[i][j]==9) {
if(a[i-1][j]!=9) {
a[i-1][j]++;
}
if(a[i-1][j+1]!=9) {
a[i-1][j+1]++;
}
}
//when the Thunder equals "a[0~9][9]"
if(i>0&&i<9&&j==9&&a[i][j]==9) {
if(a[i-1][j]!=9) {
a[i-1][j]++;
}
if(a[i-1][j-1]!=9) {
a[i-1][j-1]++;
}
if(a[i][j-1]!=9) {
a[i][j-1]++;
}
}
//when the Thunder equals "a[0][0~9]"
if(i==0&&j>0&&j<9&&a[i][j]==9) {
if(a[i][j-1]!=9) {
a[i][j-1]++;
}
}
//when the Thunder equals "a[9][0~9]"
if(i==9&&j>0&&j<9&&a[i][j]==9) {
if(a[i][j-1]!=9) {
a[i][j-1]++;
}
if(a[i-1][j-1]!=9) {
a[i-1][j-1]++;
}
if(a[i-1][j]!=9) {
a[i-1][j]++;
}
if(a[i-1][j+1]!=9) {
a[i-1][j+1]++;
}
}
//when the Thunder equals "a[0~9][0~9]"
if(i>0&&i<9&&j>0&&j<9&&a[i][j]==9) {
if(a[i][j-1]!=9) {
a[i][j-1]++;
}
if(a[i-1][j-1]!=9) {
a[i-1][j-1]++;
}
if(a[i-1][j]!=9) {
a[i-1][j]++;
}
if(a[i-1][j+1]!=9) {
a[i-1][j+1]++;
}
}
//System.out.print(a[i][j]);
}
//System.out.println();
}
//part2 is to solve the voluation down a[i][j]=9
for(int i=0;i<a.length;i++) {
System.out.print(" ");
for(int j=0;j<a[i].length;j++) {
System.out.print(" ");
//when the Thunder equals "a[0][0]"
if(i==0&&j==0&&a[i][j]==9) {
if(a[i][j+1]!=9) {
a[i][j+1]++;
}
if(a[i+1][j+1]!=9) {
a[i+1][j+1]++;
}
if(a[i+1][j]!=9) {
a[i+1][j]++;
}
}
//when the Thunder equals "a[9][0]"
if(i==9&&j==0&&a[i][j]==9) {
if(a[i][j+1]!=9) {
a[i][j+1]++;
}
}
//when the Thunder equals "a[0][9]"
if(i==0&&j==9&&a[i][j]==9) {
if(a[i][j-1]!=9) {
a[i][j-1]++;
}
if(a[i+1][j-1]!=9) {
a[i+1][j-1]++;
}
if(a[i+1][j]!=9) {
a[i+1][j]++;
}
}
//when the Thunder equals "a[0~9][0]"
if(i>0&&i<9&&j==0&&a[i][j]==9) {
if(a[i-1][j]!=9) {
a[i-1][j]++;
}
if(a[i-1][j+1]!=9) {
a[i-1][j+1]++;
}
if(a[i+1][j]!=9) {
a[i+1][j]++;
}
if(a[i+1][j+1]!=9) {
a[i+1][j+1]++;
}
if(a[i][j+1]!=9) {
a[i][j+1]++;
}
}
//when the Thunder equals "a[0~9][9]"
if(i>0&&i<9&&j==9&&a[i][j]==9) {
if(a[i+1][j]!=9) {
a[i+1][j]++;
}
if(a[i+1][j-1]!=9) {
a[i+1][j-1]++;
}
}
//when the Thunder equals "a[0][0~9]"
if(i==0&&j>0&&j<9&&a[i][j]==9) {
if(a[i][j+1]!=9) {
a[i][j+1]++;
}
if(a[i+1][j-1]!=9) {
a[i+1][j-1]++;
}
if(a[i+1][j]!=9) {
a[i+1][j]++;
}
if(a[i+1][j+1]!=9) {
a[i+1][j+1]++;
}
}
//when the Thunder equals "a[9][0~9]"
if(i==9&&j>0&&j<9&&a[i][j]==9) {
if(a[i][j+1]!=9) {
a[i][j+1]++;
}
}
//when the Thunder equals "a[0~9][0~9]"
if(i>0&&i<9&&j>0&&j<9&&a[i][j]==9) {
if(a[i][j+1]!=9) {
a[i][j+1]++;
}
if(a[i+1][j+1]!=9) {
a[i+1][j+1]++;
}
if(a[i+1][j]!=9) {
a[i+1][j]++;
}
if(a[i+1][j-1]!=9) {
a[i+1][j-1]++;
}
}
System.out.print(a[i][j]);
}
System.out.println();
}
// count "9" when it exist
for(int i=0;i<a.length;i++) {
for(int j=0;j<a[i].length;j++) {
if(a[i][j]==9) {
count++;
}
}
}
System.out.println("产生雷的个数为:"+count);
}
}
---------------------------------------------
9 1 0 0 1 1 1 0 0 0
1 1 0 1 2 9 1 0 0 0
0 0 0 1 9 3 2 0 0 0
1 1 0 1 2 9 1 0 0 0
9 1 0 0 1 1 1 0 1 1
1 1 0 0 0 0 0 1 2 9
1 1 1 0 0 0 0 1 9 2
2 9 1 0 0 0 0 1 1 1
9 2 1 0 0 0 0 0 1 1
1 1 0 0 0 0 0 0 1 9
产生雷的个数为:10
- 简单的java扫雷程序,已运行成功。自己想出来的,有个问题很困惑有望高手能解答。
- 出一道自己偶然想出来的JAVA题,看似很简单
- 写自己想出来的东西
- 网上找的约瑟夫环~~自己写的时候有个地方没想出来~~哎~~~~就出来了上面的那个笨方法~~~~
- 高手看过来,java/c++/设计模式的困惑
- 自己想出的算法!
- 出一道自己偶然想出来的JAVA题 延伸一
- 出一道自己偶然想出来的JAVA题 延伸二
- 今天一个数据结构的代码自己想出来,开心!!
- zjut1547——自己想出来的动态规划
- java困惑我1个多礼拜的问题,希望大家能帮我解决!
- 我有个问题想请高手解答一下!是C语言的!谢谢了!我有急用
- 困惑已久的问题
- 扫雷的java程序
- “创意不是想出来的”
- 财富是想出来的
- 一个简单的扫雷程序
- 《php------简单的扫雷程序》
- libusb介绍和使用例子
- Win7 64位编译OGRE
- iframe载入完成时的事件监听
- 文本转语音库flite与festival
- jdk环境变量
- 简单的java扫雷程序,已运行成功。自己想出来的,有个问题很困惑有望高手能解答。
- android surfaceflinger研究----显示系统
- 让孩子踏上阅读快车道
- 网站SEO技巧:搭建一个强壮的内部结构设计
- php正则匹配汉字!
- PHP源码分析-变量的引用计数、写时复制(Reference counting & Copy-on-Write)
- VB没有提供直接读写I/O口的方法
- Linux防火墙设置
- js中的变量封装