本文实例为大家分享了java实现扫雷游戏入门程序的具体代码,供大家参考,具体内容如下
分析:
1.首先布一个10*10的雷阵,即二维数组map,每个地方都为0
2.再在雷阵中随机选取10个位置设置为雷,雷用-1表示,即map[i][j] = -1;
3.计算雷周围的数。这里有两种方法。
第一种是用一个二维数组保存所有雷的位置,然后遍历所有的雷,查看雷周围的8个位置,如果是值-1就不做++,如果值不是-1就做++。
第二种是遍历所有不为雷的地方,然后再计算它周围的雷的数目,周围的雷的数目就是该位置的值。
(个人认为第一种方法比较好一点,时间复杂度小一些。如果雷阵比较大,比如50*50,那么第二种方法明显比第一种要慢很多)
还有一点值得注意的是,在产生雷的位置的随机数的时候,要避免产生的随机数产生重复的问题。
我们将雷阵的每一个地方都标号,如图:
我们用一个一维数组来保存雷阵的每一个位置的标号indexs = [0,1,2,3.....,97,98,99].
然后产生随机数的范围为[0,100),例如第一次产生随机数为22,那么这个数即为上图标号为22的地方,然后indexs数组里的indexs[22]保存indexs数组的最后一个数即indexs[22]=99;下一次产生随机数的时候的范围就为[0,99),此时indexs[]数组里就没有22这个数,也就不会有重复的问题。
第一种计算雷的周围的位置的方法的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
/** * 该类用于扫雷游戏的布阵 */ import java.util.Random;
public class Miner_1 { private static int [][] map; private static Random ran = new Random(); private static int [] indexs; private static int [][] minePos; //用于保存所有雷的位置 private static int x = 10 ; //c表示行数 private static int y = 10 ; //c表示列数 private static int n = 10 ; //n表示雷数
public static void main(String[] args) { init(); //初始化 arrange(); //布雷 calMines(); //计算雷周围 disp(); }
private static void init() { map = new int [x][y]; indexs = new int [x * y]; for ( int i = 0 ; i < indexs.length; i++) { indexs[i] = i; } minePos = new int [n][ 2 ]; }
private static void arrange() { int cnt = 0 ; while (cnt < n) { int index = creatIndex(indexs.length - cnt); int r = index / map[ 0 ].length; int c = index % map[ 0 ].length; map[r][c] = - 1 ; //记录雷的位置 minePos[cnt][ 0 ] = r; minePos[cnt][ 1 ] = c; cnt++; } }
//该方法用于产生雷位置的随机数 private static int creatIndex( int right) { int index = ran.nextInt(right); int value = indexs[index]; indexs[index] = indexs[right - 1 ]; return value; }
private static void calMines() { //遍历每一个雷 for ( int i = 0 ; i < minePos.length; i++) { int r = minePos[i][ 0 ]; int c = minePos[i][ 1 ]; //调用函数查看雷的周围 for ( int j = r - 1 ; j <= r + 1 ; j++) { for ( int k = c - 1 ; k <= c + 1 ; k++) { if (checkIndex(j, k) && map[j][k] != - 1 ) { map[j][k]++; } } } } }
private static boolean checkIndex( int r, int c) { return (r >= 0 && r < map.length) && (c >= 0 && c < map[r].length); }
private static void disp() { for ( int i = 0 ; i < map.length; i++) { for ( int j = 0 ; j < map[i].length; j++) { System.out.printf( "%-3d" , map[i][j]); } System.out.println(); } } } |
第二种遍历每一个不为雷的地方然后计算周围有多少个雷,如果没有雷,该位置就为0,如果有一个雷,该位置就+1,代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
import java.util.Random;
/** * 扫雷算法 * @author OnTheRoad_ * */ public class Miner2 { private static int [][] map; private static int [] indexs; //为雷的位置编号 private static Random ran; //随机数类,调用产生随机数
public static void main(String[] args) { init(); //初始化雷阵 假设10*10 arrange(); //布雷 假设为10个雷 calmine(); //计算雷数 disp(); //打印 }
private static void init() { ran = new Random(); map = new int [ 10 ][ 10 ]; indexs = new int [ 100 ]; for ( int i = 0 ; i < indexs.length; i++) { indexs[i] = i; } }
//布雷 10个 private static void arrange() { int cnt = 0 ; while (cnt < 10 ) { int index = creatIndex(indexs.length - cnt); //生成雷序列随机数 int r = index / 10 ; int c = index % 10 ; map[r][c] = - 1 ; cnt++; } }
//此方法为生成雷位置的随机数 并且避免重复 private static int creatIndex( int right) { int index = ran.nextInt(right); int value = indexs[index]; indexs[index] = indexs[right - 1 ]; return value; }
//遍历每一个不是雷的地方 计算周围的雷数 private static void calmine() { for ( int i = 0 ; i < map.length; i++) { for ( int j = 0 ; j < map[i].length; j++) { if (map[i][j] != - 1 ) { map[i][j] = calRound(i, j); } } } }
private static int calRound( int r, int c) { int cnt = 0 ; for ( int i = r - 1 ; i <= r + 1 ; i++) { for ( int j = c - 1 ; j <= c + 1 ; j++) { if (checkIndex(i, j) && map[i][j] == - 1 ) { cnt++; } } } return cnt; }
private static boolean checkIndex( int r, int c) { return (r >= 0 && r < 10 ) && (c >= 0 && c < 10 ); }
private static void disp() { for ( int i = 0 ; i < map.length; i++) { for ( int j = 0 ; j < map[i].length; j++) { System.out.printf( "%3d" , map[i][j]); } System.out.println(); } } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://blog.csdn.net/Ontheroad_/article/details/78115747