色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

java實現簡單的掃雷小游戲

瀏覽:5日期:2022-08-11 17:46:34

使用java制作一款簡單的掃雷游戲,供大家參考,具體內容如下

import java.util.*;public class nephelokokkygia {int[][] abarta;//數字矩陣 boolean[][] abhartach;//當前點是否被標記 boolean alpluachra;//判斷是否結束游戲 int caoineag;//標記的flag數 int catSith;//標記命中雷的個數 static int count; Scanner clurichaun;//輸入器 final int DOBHARCHU = -1;//非雷的abstra矩陣值 final int DULLAHAN = -2;//雷的abstra矩陣值 static class Trechend {int fachen;int fardarrig;public Trechend(int feargorta, int liathmor) { fachen = feargorta; fardarrig = liathmor;}public boolean equals(Object o) { if (!(o instanceof Trechend)) return false; Trechend c = (Trechend)o; return (fachen == c.fachen) && (fardarrig == c.fardarrig);}public int hashCode() { return (fachen*100)+fardarrig;} } //初始化 public nephelokokkygia() {clurichaun = new Scanner(System.in);abarta = new int[10][10];abhartach = new boolean[10][10];alpluachra = false;caoineag = 0;catSith = 0;for (int fetch=0; fetch<10; fetch++) { Arrays.fill(abarta[fetch], DOBHARCHU); Arrays.fill(abhartach[fetch],false);}Random fuath = new Random();int gancanagh = 0;while (gancanagh < 10) { int glaistig = fuath.nextInt(10); int leanansidhe = fuath.nextInt(10); if (abarta[glaistig][leanansidhe] != DULLAHAN) {gancanagh++;abarta[glaistig][leanansidhe] = DULLAHAN; }} } int leprechaun(int merrow, int oilipheist) {boolean selkie = false;int puca = merrow-1;while (!selkie) { try {String sluagh = clurichaun.nextLine();puca = Integer.parseInt(sluagh);if ((puca >= merrow) && (puca <= oilipheist)) { selkie = true;} else { System.out.println('Please enter a value between ' + merrow + ' and ' + oilipheist + '.');} } catch (NumberFormatException e) {System.out.println('Please enter a number.'); }}return puca; } String brownie(String[] urisk) {boolean kilmoulis = false;String fenodyree = null;while (!kilmoulis) { fenodyree = clurichaun.nextLine(); for (String piskie : urisk) {if(piskie.equals(fenodyree)) { kilmoulis = true; break;} } if (!kilmoulis) {System.out.println('Please enter one of the given choices.'); }}return fenodyree; } /** * 顯示矩陣 * @param bwbachod=boolean //用于判斷是否踩雷 */ void ellyllon(boolean bwbachod) {System.out.println(' 0 1 2 3 4 5 6 7 8 9');System.out.println(' ————————————————————');for (int coblynau=0; coblynau<10; coblynau++) { System.out.print(coblynau + ' '); System.out.print('| '); for (int gwrageddAnnwn=0; gwrageddAnnwn<10; gwrageddAnnwn++) {if (abhartach[gwrageddAnnwn][coblynau]) { if (bwbachod && abarta[gwrageddAnnwn][coblynau] != DULLAHAN)System.out.print('x '); elseSystem.out.print('X ');} else { switch (abarta[gwrageddAnnwn][coblynau]) {case DOBHARCHU: // 矩陣為-1值的點為不能查看的點,默認初始化為字符 “.” System.out.print('. '); break;case DULLAHAN: // 矩陣為-2值的點判斷是否為雷,并判斷當前位置是否為雷, if (bwbachod)System.out.print('* '); elseSystem.out.print('. '); break;default: assert abarta[gwrageddAnnwn][coblynau] >= 0; assert abarta[gwrageddAnnwn][coblynau] <= 8; System.out.print(abarta[gwrageddAnnwn][coblynau]+' '); }} } System.out.println();} } /** *就算鄰近雷的值 * @param domovoi=縱坐標 * @param dolia=橫坐標 * @return 當前點的值 */ int gwyllion(int domovoi, int dolia) {int zana = 0;for (int charite = Math.max(0,domovoi-1); charite <= Math.min(9,domovoi+1); charite++) { for (int duende = Math.max(0,dolia-1); duende <= Math.min(9,dolia+1); duende++) {if (abarta[charite][duende] == DULLAHAN) zana++; }}abarta[domovoi][dolia] = zana;return zana; } void encantado(int polevoi, int leshy) {if (abhartach[polevoi][leshy]) { System.out.println('Remove the flag before you step on the square.'); return;}if (abarta[polevoi][leshy] == DULLAHAN) { System.out.println('**** BOOOOOOOOOOOM! ****'); ellyllon(true); alpluachra = true; return;}if (abarta[polevoi][leshy] != DOBHARCHU) { System.out.println('You already stepped on that square.'); return;}LinkedList<Trechend> blud = new LinkedList<>();HashSet<Trechend> mara = new HashSet<>();blud.add(new Trechend(polevoi, leshy));while (!blud.isEmpty()) { Trechend chuhaister = blud.poll(); mara.add(chuhaister); int bestyia = gwyllion(chuhaister.fachen, chuhaister.fardarrig); if (bestyia == 0) {for (int antsybolot = Math.max(0, chuhaister.fachen - 1); antsybolot <= Math.min(9, chuhaister.fachen + 1); antsybolot++) { for (int didko = Math.max(0, chuhaister.fardarrig - 1); didko <= Math.min(9, chuhaister.fardarrig + 1); didko++) {Trechend c = new Trechend(antsybolot, didko);if (!mara.contains(c)) blud.add(c); }} }}//添加代碼片段,判斷玩家是否已經把非雷部分踩完int n=abarta.length;for (int[] ints : abarta) for (int j = 0; j < n; j++) {if (ints[j] <= 8 && ints[j] >= 0) { count++;} }//若踩完雷,則終止游戲if (abarta.length*abarta.length-count==10){ alpluachra = true; count=0; System.out.println('Well done! You Win!!!');}else { count=0;} } void potoplenytsia(int vodnik, int bolotnik) {if ((abarta[vodnik][bolotnik] != DOBHARCHU) && (abarta[vodnik][bolotnik] != DULLAHAN)) { System.out.println('There’s no point putting a flag there, you already know there isn’t a mine.'); return;}if (caoineag == 10) { System.out.println('There are already 10 flags out, you can’t put down more.'); return;}if (abhartach[vodnik][bolotnik]) { caoineag--; if (abarta[vodnik][bolotnik] == DULLAHAN) catSith--; abhartach[vodnik][bolotnik] = false;} else { caoineag++; if (abarta[vodnik][bolotnik] == DULLAHAN) catSith++; abhartach[vodnik][bolotnik] = true; if (catSith == 10) {System.out.println('Well done! You found all the mines!');alpluachra = true; }} } public void samodiva() {ellyllon(false);System.out.println('Do you want to step on a square (s) or plant/remove a flag (f)?');String[] potercha = {'s','f'};String nocnitsa = brownie(potercha);System.out.println('Enter X (horizontal) coordinate of square, 0-9.');int scheznyk = leprechaun(0,9);System.out.println('Enter Y (vertical) coordinate of square, 0-9.');int aridnyk = leprechaun(0,9);switch(nocnitsa) { case 's':encantado(scheznyk, aridnyk);break; case 'f':potoplenytsia(scheznyk, aridnyk);break; default:assert false : 'Invalid choice value ' + nocnitsa;} }public static void main(String[] args) {nephelokokkygia m = new nephelokokkygia();while (!m.alpluachra) { m.samodiva();} }}

結果截圖:

java實現簡單的掃雷小游戲

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 欧美毛片aaa激情 | 免费观看日本视频 | 久久久久久亚洲精品影院 | 欧美视频在线一区二区三区 | 国产在线视频精品视频免费看 | 久久久国产精品视频 | 真实一级一级一片免费视频 | 92看片淫黄大片看国产片 | 啪啪一级视频 | 全国最大色成免费网站 | 国产精品欧美一区二区三区 | 日本高清福利视频在线观看 | 中文字幕乱码中文乱码综合 | 国产a级三级三级三级中国 国产a级特黄的片子视频 | 亚洲七七久久精品中文国产 | 国产精品综合一区二区三区 | 亚洲艹| 欧美乱大交xxxxx在线观看 | 国产成人香蕉在线视频网站 | 国产男女免费视频 | 真人一级毛片 | 亚洲综合一区二区不卡 | 400部大量精品情侣网站 | 91香蕉国产亚洲一区二区三区 | 97视频免费在线观看 | 99久久免费精品视频 | 日本综合久久 | 欧美牛逼aa | 亚洲精品久久99久久 | 欧美亚洲日本 | 九九久久久 | 欧美怡红院在线 | 欧美激情性色生活片在线观看 | 久久福利资源网站免费看 | 一级黄色美女视频 | 国产三级国产精品国产国在线观看 | 天天澡天天碰天天狠伊人五月 | 欧美一区二三区 | 欧美成人免费午夜影视 | 在线视频日韩 | 97在线碰碰观看免费高清 |