java實(shí)現(xiàn)掃雷游戲入門程序
本文實(shí)例為大家分享了java實(shí)現(xiàn)掃雷游戲入門程序的具體代碼,供大家參考,具體內(nèi)容如下
分析:
1.首先布一個(gè)10*10的雷陣,即二維數(shù)組map,每個(gè)地方都為0
2.再在雷陣中隨機(jī)選取10個(gè)位置設(shè)置為雷,雷用-1表示,即map[i][j] = -1;
3.計(jì)算雷周圍的數(shù)。這里有兩種方法。
第一種是用一個(gè)二維數(shù)組保存所有雷的位置,然后遍歷所有的雷,查看雷周圍的8個(gè)位置,如果是值-1就不做++,如果值不是-1就做++。
第二種是遍歷所有不為雷的地方,然后再計(jì)算它周圍的雷的數(shù)目,周圍的雷的數(shù)目就是該位置的值。
(個(gè)人認(rèn)為第一種方法比較好一點(diǎn),時(shí)間復(fù)雜度小一些。如果雷陣比較大,比如50*50,那么第二種方法明顯比第一種要慢很多)
還有一點(diǎn)值得注意的是,在產(chǎn)生雷的位置的隨機(jī)數(shù)的時(shí)候,要避免產(chǎn)生的隨機(jī)數(shù)產(chǎn)生重復(fù)的問(wèn)題。
我們將雷陣的每一個(gè)地方都標(biāo)號(hào),如圖:
我們用一個(gè)一維數(shù)組來(lái)保存雷陣的每一個(gè)位置的標(biāo)號(hào)indexs = [0,1,2,3.....,97,98,99].
然后產(chǎn)生隨機(jī)數(shù)的范圍為[0,100),例如第一次產(chǎn)生隨機(jī)數(shù)為22,那么這個(gè)數(shù)即為上圖標(biāo)號(hào)為22的地方,然后indexs數(shù)組里的indexs[22]保存indexs數(shù)組的最后一個(gè)數(shù)即indexs[22]=99;下一次產(chǎn)生隨機(jī)數(shù)的時(shí)候的范圍就為[0,99),此時(shí)indexs[]數(shù)組里就沒(méi)有22這個(gè)數(shù),也就不會(huì)有重復(fù)的問(wèn)題。
第一種計(jì)算雷的周圍的位置的方法的代碼如下:
/** ?* 該類用于掃雷游戲的布陣 ?*/ 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表示行數(shù) ?? ?private static int y = 10;//c表示列數(shù) ?? ?private static int n = 10;//n表示雷數(shù) ? ?? ?public static void main(String[] args) { ?? ??? ?init();//初始化 ?? ??? ?arrange();//布雷 ?? ??? ?calMines();//計(jì)算雷周圍 ?? ??? ?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++; ?? ??? ?} ?? ?} ?? ? ?? ?//該方法用于產(chǎn)生雷位置的隨機(jī)數(shù) ?? ?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() { ?? ??? ?//遍歷每一個(gè)雷 ?? ??? ?for (int i = 0; i < minePos.length; i++) { ?? ??? ??? ?int r = minePos[i][0]; ?? ??? ??? ?int c = minePos[i][1]; ?? ??? ??? ?//調(diào)用函數(shù)查看雷的周圍 ?? ??? ??? ?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(); ?? ??? ?} ?? ?} }
第二種遍歷每一個(gè)不為雷的地方然后計(jì)算周圍有多少個(gè)雷,如果沒(méi)有雷,該位置就為0,如果有一個(gè)雷,該位置就+1,代碼如下
import java.util.Random; ? /** ?* 掃雷算法 ?* @author OnTheRoad_ ?* ?*/ public class Miner2 { ?? ?private static int[][] map; ?? ?private static int[] indexs;//為雷的位置編號(hào)? ?? ?private static Random ran;//隨機(jī)數(shù)類,調(diào)用產(chǎn)生隨機(jī)數(shù) ? ?? ?public static void main(String[] args) { ?? ??? ?init();//初始化雷陣 假設(shè)10*10 ?? ??? ?arrange();//布雷 假設(shè)為10個(gè)雷 ?? ??? ?calmine();//計(jì)算雷數(shù) ?? ??? ?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個(gè) ?? ?private static void arrange() { ?? ??? ?int cnt = 0; ?? ??? ?while (cnt < 10) { ?? ??? ??? ?int index = creatIndex(indexs.length - cnt);//生成雷序列隨機(jī)數(shù) ?? ??? ??? ?int r = index / 10; ?? ??? ??? ?int c = index % 10; ?? ??? ??? ?map[r][c] = -1; ?? ??? ??? ?cnt++; ?? ??? ?} ?? ?} ? ?? ?//此方法為生成雷位置的隨機(jī)數(shù) 并且避免重復(fù) ?? ?private static int creatIndex(int right) { ?? ??? ?int index = ran.nextInt(right); ?? ??? ?int value = indexs[index]; ?? ??? ?indexs[index] = indexs[right - 1]; ?? ??? ?return value; ?? ?} ? ?? ?//遍歷每一個(gè)不是雷的地方 計(jì)算周圍的雷數(shù) ?? ?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(); ?? ??? ?} ?? ?} }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java基于面向?qū)ο髮?shí)現(xiàn)一個(gè)戰(zhàn)士小游戲
- Java使用定時(shí)器編寫(xiě)一個(gè)簡(jiǎn)單的搶紅包小游戲
- Java+JFrame實(shí)現(xiàn)貪吃蛇小游戲
- java實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲
- Java實(shí)現(xiàn)飛機(jī)小游戲
- 用java實(shí)現(xiàn)掃雷游戲
- Java實(shí)現(xiàn)掃雷游戲詳細(xì)代碼講解
- Java實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲?附完整源碼
- Java實(shí)現(xiàn)動(dòng)物換位游戲完整?過(guò)程詳解
相關(guān)文章
spring boot之SpringApplication 事件監(jiān)聽(tīng)
這篇文章主要介紹了spring boot之SpringApplication 事件監(jiān)聽(tīng),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03springboot啟動(dòng)掃描不到dao層接口的解決方案
這篇文章主要介紹了springboot啟動(dòng)掃描不到dao層接口的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Springboot通過(guò)配置WebMvcConfig處理Cors非同源訪問(wèn)跨域問(wèn)題
這篇文章主要介紹了Springboot通過(guò)配置WebMvcConfig處理Cors非同源訪問(wèn)跨域問(wèn)題,關(guān)于Cors跨域的問(wèn)題,前端有代理和jsonp的常用方式解決這種非同源的訪問(wèn)拒絕策略2023-04-04使用監(jiān)聽(tīng)器對(duì)Spring bean id進(jìn)行唯一校驗(yàn)過(guò)程解析
這篇文章主要介紹了使用監(jiān)聽(tīng)器對(duì)Spring bean id進(jìn)行唯一校驗(yàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08