简单的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

原创粉丝点击