欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)掃雷游戲入門程序

 更新時(shí)間:2022年06月06日 15:35:29   作者:Ontheroad_  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)掃雷游戲入門程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • spring boot之SpringApplication 事件監(jiān)聽(tīng)

    spring boot之SpringApplication 事件監(jiān)聽(tīng)

    這篇文章主要介紹了spring boot之SpringApplication 事件監(jiān)聽(tīng),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-03-03
  • java輸入空行結(jié)束問(wèn)題

    java輸入空行結(jié)束問(wèn)題

    這篇文章主要介紹了java輸入空行結(jié)束問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • springboot啟動(dòng)掃描不到dao層接口的解決方案

    springboot啟動(dòng)掃描不到dao層接口的解決方案

    這篇文章主要介紹了springboot啟動(dòng)掃描不到dao層接口的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java排序算法總結(jié)之歸并排序

    Java排序算法總結(jié)之歸并排序

    這篇文章主要介紹了Java排序算法總結(jié)之歸并排序,較為詳細(xì)的分析了歸并排序的原理與java實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2015-05-05
  • java split()使用方法解析

    java split()使用方法解析

    這篇文章主要介紹了java split()使用方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • Java 中的變量類型

    Java 中的變量類型

    這篇文章主要介紹了Java 中的變量類型,一般包括局部變量、成員變量、類變量,下面文章對(duì)這三種內(nèi)容的變量做了一個(gè)詳細(xì)介紹,需要的朋友可以參考一下
    2021-11-11
  • Springboot通過(guò)配置WebMvcConfig處理Cors非同源訪問(wèn)跨域問(wèn)題

    Springboot通過(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ò)程解析

    這篇文章主要介紹了使用監(jiān)聽(tīng)器對(duì)Spring bean id進(jìn)行唯一校驗(yàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • 淺談foreach寫(xiě)失效的問(wèn)題

    淺談foreach寫(xiě)失效的問(wèn)題

    下面小編就為大家?guī)?lái)一篇淺談foreach寫(xiě)失效的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07
  • Swagger屏蔽某些接口顯示的操作

    Swagger屏蔽某些接口顯示的操作

    這篇文章主要介紹了Swagger屏蔽某些接口顯示的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06

最新評(píng)論