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ù)的問題。
我們將雷陣的每一個(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ù)組里就沒有22這個(gè)數(shù),也就不會(huì)有重復(fù)的問題。
第一種計(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è)雷,如果沒有雷,該位置就為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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換的方法總結(jié)
項(xiàng)目開發(fā)中經(jīng)常會(huì)遇到多數(shù)據(jù)源同時(shí)使用的場(chǎng)景,比如冷熱數(shù)據(jù)的查詢等情況,所以接下來(lái)本文就來(lái)介紹一下如何使用實(shí)現(xiàn)自定義注解的形式來(lái)實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換吧2023-12-12
JAVA環(huán)境搭建之MyEclipse10+jdk1.8+tomcat8環(huán)境搭建詳解
本文詳細(xì)講解了MyEclipse10+jdk1.8+tomcat8的JAVA環(huán)境搭建方法,希望能幫助到大家2018-10-10
SpringBoot項(xiàng)目執(zhí)行腳本 自動(dòng)拉取最新代碼并重啟的實(shí)例內(nèi)容
在本篇文章里小編給大家整理的是一篇關(guān)于SpringBoot項(xiàng)目執(zhí)行腳本 自動(dòng)拉取最新代碼并重啟的實(shí)例內(nèi)容,有需要的朋友們參考下。2019-12-12
Mybatis的mapper.xml中if標(biāo)簽test判斷的用法說(shuō)明
這篇文章主要介紹了Mybatis的mapper.xml中if標(biāo)簽test判斷的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Spring MVC+FastJson+hibernate-validator整合的完整實(shí)例教程
這篇文章主要給大家介紹了關(guān)于Spring MVC+FastJson+hibernate-validator整合的完整實(shí)例教程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04
Lombok注解之@SuperBuilder--解決無(wú)法builder父類屬性問題
這篇文章主要介紹了Lombok注解之@SuperBuilder--解決無(wú)法builder父類屬性問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09

