电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

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

java實現簡單的掃雷小游戲

瀏覽:6日期: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
相關文章:
主站蜘蛛池模板: 螺旋叶片_螺旋叶片成型机_绞龙叶片_莱州源泽机械制造有限公司 | 杰恒蠕动泵-蠕动泵专业厂家-19年专注蠕动泵| 油罐车_加油机_加油卷盘_加油机卷盘_罐车人孔盖_各类球阀_海底阀等车用配件厂家-湖北华特专用设备有限公司 | 网站建设-高端品牌网站设计制作一站式定制_杭州APP/微信小程序开发运营-鼎易科技 | 药品冷藏箱厂家_低温冰箱_洁净工作台-济南欧莱博电子商务有限公司官网 | 玻璃钢罐_玻璃钢储罐_盐酸罐厂家-河北华盛节能设备有限公司 | 二手光谱仪维修-德国OBLF光谱仪|进口斯派克光谱仪-热电ARL光谱仪-意大利GNR光谱仪-永晖检测 | 能量回馈_制动单元_电梯节能_能耗制动_深圳市合兴加能科技有限公司 | 玉米深加工机械,玉米加工设备,玉米加工机械等玉米深加工设备制造商-河南成立粮油机械有限公司 | 钢格栅板_钢格板网_格栅板-做专业的热镀锌钢格栅板厂家-安平县迎瑞丝网制造有限公司 | 婚博会2024时间表_婚博会门票领取_婚博会地址-婚博会官网 | 西安烟道厂家_排气道厂家_包立管厂家「陕西西安」推荐西安天宇烟道 | 礼堂椅厂家|佛山市艺典家具有限公司| 金刚网,金刚网窗纱,不锈钢网,金刚网厂家- 河北萨邦丝网制品有限公司 | 冰雕-冰雪世界-大型冰雕展制作公司-赛北冰雕官网 | 精益专家 - 设备管理软件|HSE管理系统|设备管理系统|EHS安全管理系统 | 钢格板|镀锌钢格板|热镀锌钢格板|格栅板|钢格板|钢格栅板|热浸锌钢格板|平台钢格板|镀锌钢格栅板|热镀锌钢格栅板|平台钢格栅板|不锈钢钢格栅板 - 专业钢格板厂家 | 找果网 | 苹果手机找回方法,苹果iPhone手机丢了找回,认准找果网! | 洛阳防爆合格证办理-洛阳防爆认证机构-洛阳申请国家防爆合格证-洛阳本安防爆认证代办-洛阳沪南抚防爆电气技术服务有限公司 | 点焊机-缝焊机-闪光对焊机-电阻焊设备生产厂家-上海骏腾发智能设备有限公司 | 石英砂矿石色选机_履带辣椒色选机_X光异物检测机-合肥幼狮光电科技 | 广西绿桂涂料--承接隔热涂料、隔音涂料、真石漆、多彩仿石漆等涂料工程双包施工 | 间甲酚,间甲酚厂家-山东祥东新材料 | 济南侦探调查-济南调查取证-山东私家侦探-山东白豹调查咨询公司 密集架|电动密集架|移动密集架|黑龙江档案密集架-大量现货厂家销售 | 冰晶石|碱性嫩黄闪蒸干燥机-有机垃圾烘干设备-草酸钙盘式干燥机-常州市宝康干燥 | 钢结构-钢结构厂房-钢结构工程[江苏海逵钢构厂] | 杭州ROHS检测仪-XRF测试仪价格-百科 | 智能案卷柜_卷宗柜_钥匙柜_文件流转柜_装备柜_浙江福源智能科技有限公司 | 建筑资质代办-建筑企业资质代办机构-建筑资质代办公司 | 润滑脂-高温润滑脂-轴承润滑脂-食品级润滑油-索科润滑油脂厂家 | 高尔夫球杆_高尔夫果岭_高尔夫用品-深圳市新高品体育用品有限公司 | 河南正规膏药生产厂家-膏药贴牌-膏药代加工-修康药业集团官网 | 上海冠顶工业设备有限公司-隧道炉,烘箱,UV固化机,涂装设备,高温炉,工业机器人生产厂家 | 膜片万向弹性联轴器-冲压铸造模具「沧州昌运模具」 | 铝箔袋,铝箔袋厂家,东莞铝箔袋,防静电铝箔袋,防静电屏蔽袋,防静电真空袋,真空袋-东莞铭晋让您的产品与众不同 | 山东彩钢板房,山东彩钢活动房,临沂彩钢房-临沂市贵通钢结构工程有限公司 | 海外整合营销-独立站营销-社交媒体运营_广州甲壳虫跨境网络服务 焊管生产线_焊管机组_轧辊模具_焊管设备_焊管设备厂家_石家庄翔昱机械 | 无线遥控更衣吊篮_IC卡更衣吊篮_电动更衣吊篮配件_煤矿更衣吊篮-力得电子 | 步进电机_agv电机_伺服马达-伺服轮毂电机-和利时电机 | 长春网站建设,五合一网站设计制作,免费优化推广-长春网站建设 | 超细粉碎机|超微气流磨|气流分级机|粉体改性设备|超微粉碎设备-山东埃尔派粉碎机厂家 |