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

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

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
相關文章:
主站蜘蛛池模板: 微型气泵-真空-蠕动-水泵-厂家-深圳市品亚科技有限公司 | PVC地板|PVC塑胶地板|PVC地板厂家|地板胶|防静电地板-无锡腾方装饰材料有限公司-咨询热线:4008-798-128 | 数显恒温培养摇床-卧式/台式恒温培养摇床|朗越仪器 | 小港信息港-鹤壁信息港 鹤壁老百姓便民生活信息网站 | 广东青藤环境科技有限公司-水质检测| 成都热收缩包装机_袖口式膜包机_高速塑封机价格_全自动封切机器_大型套膜机厂家 | 预制舱-电力集装箱预制舱-模块化预制舱生产厂家-腾达电器设备 | 工业铝型材生产厂家_铝合金型材配件批发精加工定制厂商 - 上海岐易铝业 | pbootcms网站模板|织梦模板|网站源码|jquery建站特效-html5模板网 | 冰晶石|碱性嫩黄闪蒸干燥机-有机垃圾烘干设备-草酸钙盘式干燥机-常州市宝康干燥 | 布袋除尘器|除尘器设备|除尘布袋|除尘设备_诺和环保设备 | 沈阳液压泵_沈阳液压阀_沈阳液压站-沈阳海德太科液压设备有限公司 | 模具硅橡胶,人体硅胶,移印硅胶浆厂家-宏图硅胶科技 | 北京京云律师事务所| 球磨机,节能球磨机价格,水泥球磨机厂家,粉煤灰球磨机-吉宏机械制造有限公司 | 动库网动库商城-体育用品专卖店:羽毛球,乒乓球拍,网球,户外装备,运动鞋,运动包,运动服饰专卖店-正品运动品网上商城动库商城网 - 动库商城 | 武汉天安盾电子设备有限公司 - 安盾安检,武汉安检门,武汉安检机,武汉金属探测器,武汉测温安检门,武汉X光行李安检机,武汉防爆罐,武汉车底安全检查,武汉液体探测仪,武汉安检防爆设备 | 硬质合金模具_硬质合金非标定制_硬面加工「生产厂家」-西迪技术股份有限公司 | 定制防伪标签_防伪标签印刷_防伪标签厂家-510品保防伪网 | KBX-220倾斜开关|KBW-220P/L跑偏开关|拉绳开关|DHJY-I隔爆打滑开关|溜槽堵塞开关|欠速开关|声光报警器-山东卓信有限公司 | 无痕胶_可移胶_无痕双面胶带_可移无痕胶厂家-东莞凯峰 | 下水道疏通_管道疏通_马桶疏通_附近疏通电话- 立刻通 | 锤式粉碎机,医药粉碎机,锥式粉碎机-无锡市迪麦森机械制造有限公司 | 不锈钢监控杆_监控立杆厂家-廊坊耀星光电科技有限公司 | 自动气象站_农业气象站_超声波气象站_防爆气象站-山东万象环境科技有限公司 | 便携式XPDM露点仪-在线式防爆露点仪-增强型烟气分析仪-约克仪器 冰雕-冰雪世界-大型冰雕展制作公司-赛北冰雕官网 | 南京欧陆电气股份有限公司-风力发电机官网 | 针焰试验仪,灼热丝试验仪,漏电起痕试验仪,水平垂直燃烧试验仪 - 苏州亚诺天下仪器有限公司 | 奥运星-汽车性能网评-提供个性化汽车资讯 | 空调风机,低噪声离心式通风机,不锈钢防爆风机,前倾皮带传动风机,后倾空调风机-山东捷风风机有限公司 | 数年网路-免费在线工具您的在线工具箱-shuyear.com | 上海瑶恒实业有限公司|消防泵泵|离心泵|官网 | 北京模型公司-工业模型-地产模型-施工模型-北京渝峰时代沙盘模型制作公司 | 鄂泉泵业官网|(杭州、上海、全国畅销)大流量防汛排涝泵-LW立式排污泵 | PC阳光板-PC耐力板-阳光板雨棚-耐力板雨棚,厂家定制[优尼科板材] | 行吊_电动单梁起重机_双梁起重机_合肥起重机_厂家_合肥市神雕起重机械有限公司 | 【同风运车官网】一站式汽车托运服务平台,验车满意再付款 | 华禹护栏|锌钢护栏_阳台护栏_护栏厂家-华禹专注阳台护栏、楼梯栏杆、百叶窗、空调架、基坑护栏、道路护栏等锌钢护栏产品的生产销售。 | VOC检测仪-甲醛检测仪-气体报警器-气体检测仪厂家-深恒安科技有限公司 | 雷冲击高压发生器-水内冷直流高压发生器-串联谐振分压器-武汉特高压电力科技有限公司 | 武汉天安盾电子设备有限公司 - 安盾安检,武汉安检门,武汉安检机,武汉金属探测器,武汉测温安检门,武汉X光行李安检机,武汉防爆罐,武汉车底安全检查,武汉液体探测仪,武汉安检防爆设备 |