闲的没事 写个扫雷, 算法 不太好·····凑合

<!DOCTYPE html>
<html>
<head>
<metacharset="utf-8">
<title>扫雷</title>
</head>
<body><script>functionSet(r,c,hard){vardata= newArray();for(vari=0;i<r;i++){data[i]=newArray(c);}for(vari= 0; i<r*c; i++){varran=Math.floor(Math.random()*100);data[Math.floor(i/c)][i%c]=ran<hard?1:0;    }returndata;}functionGetNewData(data,r,c){varnewdata= newArray();for(vari=0;i<r;i++){newdata[i]=newArray(c);}for(vari= 0; i<r*c; i++){if(data[Math.floor(i/c)][i%c]== 1){newdata[Math.floor(i/c)][i%c]= 9;}else{vard= 0;for(varj= 0; j< 9; j++){if(Math.floor(i/c)+(Math.floor(j/ 3)- 1)>=0 &&i%c+(j% 3 - 1)>= 0 &&Math.floor(i/c)+(Math.floor(j/ 3)- 1)<r&&i%c+(j% 3 - 1)<c&&data[Math.floor(i/c)+Math.floor(j/ 3 - 1)][i%c+(j% 3 - 1)]== 1){d++;}}newdata[Math.floor(i/c)][i%c]=d;}}returnnewdata;}functionGetRegion(rr,cc,data,list){if(data[rr][cc]!=0){return;}else{for(varj= 0; j< 9; j++){if(rr+(Math.floor(j/ 3)- 1)>=0 &&cc+(j% 3 - 1)>= 0 &&rr+(Math.floor(j/ 3)- 1)<r&&cc+(j% 3 - 1)<c&&data[rr+Math.floor(j/ 3 - 1)][cc+(j% 3 - 1)]== 0){varpr=rr+Math.floor(j/ 3 - 1);varpc=cc+(j% 3 - 1);console.log(pr,pc);if(contains(list,{r:pr,c:pc}))continue;list.push({r:pr,c:pc});GetRegion(pr,pc,data,list);}}return; }}</script><!--<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=22654542&auto=1&height=66"></iframe>--><h2>扫雷游戏</h2><!--<audio src="http://music.163.com/style/swf/widget.swf?sid=26569168" controls="controls"></audio>--><p>难度:<selectNAME="Difficulty"onchange="load(this.value)"id="Difficulty"><optionvalue></option><optionvalue="1"selected="selected">初9*9</option><optionvalue="2" >中16*16</option><optionvalue="3">高30*16</option></select><buttononclick="timedCount()">开始</button><buttononclick="stop()">停止</button><buttononclick="reload()">重置</button><buttononclick="Drawall()">显示所有</button><inputtype="text"readonly="readonly"id="time"></p><p><canvasid="myCanvas"oncontextmenu=self.event.returnvalue=falsewidth="180"height="180"style="border:1px solid #000000;background:lightgray;"></canvas></p><script>varr=9;//99 1616 3016varc=9;vardifficulty=15vartempdata;varminedata;varsigndata= newArray();varcheckSigndata=newArraydocument.write("<br/>");varDifficulty=document.getElementById("Difficulty");Difficulty.options[1].selected= true;varcanvas=document.getElementById("myCanvas");canvas.addEventListener("mousedown", doMouseDown,false); canvas.oncontextmenu=function(){return false;}load(1);redraw();//test();//Drawall();vart;vartick=0;functiontimedCount(){document.getElementById('time').value=ticktick=tick+1t=setTimeout("timedCount()",1000)}functionstop(){if(t!=null)clearTimeout(t);tick=0;}functionloadsigndata(){for(vari=0;i<r;i++){signdata[i]=newArray(c);}for(vari=0;i<r;i++){checkSigndata[i]=newArray(c);    }for(vari= 0; i<r*c; i++){switch(minedata[Math.floor(i/c)][i%c]){case 9:checkSigndata[Math.floor(i/c)][i%c]=2;break;case 0:checkSigndata[Math.floor(i/c)][i%c]=3;break;default:checkSigndata[Math.floor(i/c)][i%c]=1;break;}}}functiontest(){for(vari= 0; i<r*c; i++){document.write(minedata[Math.floor(i/c)][i%c]+"&nbsp");if(i%c==c-1)document.write("<br/>");}}functionload(v){switch(parseInt(v)){case 1:r=9;c=9;break;case 2:r=16;c=16;break;case 3:r=30;c=16;break;}reload();//Drawall();
}functionreload(){redraw();tempdata=Set(r,c,difficulty);minedata=GetNewData(tempdata,r,c);loadsigndata();}functionredraw(){canvas.setAttribute('width',c*30);canvas.setAttribute('height',r*30);varctx=canvas.getContext("2d");for(vari=0;i<r+1;i++){ctx.moveTo(0,i*30);ctx.lineTo(c*30,i*30);ctx.stroke();        }for(vari=0;i<c+1;i++){ctx.moveTo(i*30,0);ctx.lineTo(i*30,r*30);ctx.stroke();        }}functioncontains(arr, obj) {vari=arr.length;while(i--) {if(arr[i].r==obj.r&&arr[i].c==obj.c) {return true;  }  }return false;  }varplist=newArray();functiondoMouseDown(event){varbtnNum=event.button;varx=event.pageX;vary=event.pageY;varloc=getPointOnCanvas(canvas, x, y);varxx=Math.floor(loc.x/30);varyy=Math.floor(loc.y/30);if(signdata[yy]!=null&&signdata[yy][xx]!=null&&(signdata[yy][xx]==3||signdata[yy][xx]==1))return;if(btnNum==0){drawCell(xx,yy);plist.splice(0,plist.length);GetRegion(yy,xx,minedata,plist);for(varl=0;l<plist.length;l++){drawCell(plist[l].c,plist[l].r);}}else if(btnNum==2){drawCellr(xx,yy);}if(check())alert("成功:"+tick);}functioncheck(){for(vari= 0; i<r*c; i++){if(checkSigndata[Math.floor(i/c)][i%c]!=signdata[Math.floor(i/c)][i%c]){return false;}}return true;}functiondrawCell(xx,yy){varctx=canvas.getContext("2d");ctx.textAlign= "start";ctx.fillStyle= "red";ctx.font= "30px Arial";if(signdata[yy][xx]!=null &&signdata[yy][xx]==    2){ctx.fillStyle= "lightgray";ctx.fillRect(xx*30,yy*30,29,29);}ctx.fillStyle= "red";if(minedata[yy][xx]==9){//ctx.fillText("×",xx*30,(yy+1)*30);
alert("失败");Drawall();//signdata[yy][xx]=2;
}else if(minedata[yy][xx]==0){//ctx.fillText("0",xx*30,(yy+1)*30);
ctx.fillStyle= "green";ctx.fillRect(xx*30,yy*30,29,29);signdata[yy][xx]=3;}else{ctx.fillText(minedata[yy][xx].toString(),xx*30,(yy+1)*30);signdata[yy][xx]=1;}}functiondrawCellr(xx,yy){varctx=canvas.getContext("2d");ctx.textAlign= "start";ctx.fillStyle= "red";ctx.font= "30px Arial";if(signdata[yy][xx]==0||signdata[yy][xx]==null){ctx.fillText("",xx*30,(yy+1)*30);signdata[yy][xx]=2;}else{ctx.fillStyle= "lightgray";ctx.fillRect(xx*30,yy*30,29,29);//ctx.fillText("√",xx*30,(yy+1)*30);
signdata[yy][xx]=0;}ctx.stroke();}functiongetPointOnCanvas(canvas, x, y) {varbbox=canvas.getBoundingClientRect();return{ x: x-bbox.left*(canvas.width/bbox.width),  y: y-bbox.top*(canvas.height/bbox.height)  };  }functionDrawall(){redraw();varctx=canvas.getContext("2d");ctx.textAlign= "start";ctx.fillStyle= "red";ctx.font= "30px Arial";for(vari=0;i<r*c;i++){varx=i%c*30;vary=(Math.floor(i/c)+1)*30;if(minedata[Math.floor(i/c)][i%c]==9){ctx.fillText("×",x,y);}else if(minedata[Math.floor(i/c)][i%c]==0){ctx.fillText("0",x,y);}else{ctx.fillText(minedata[Math.floor(i/c)][i%c].toString(),x,y);}
}}</script>
</body></html>

转载于:https://www.cnblogs.com/onegarden/p/6739223.html

网页版扫雷游戏···相关推荐

  1. 收藏网页版小游戏:蜘蛛纸牌、扫雷、水果忍者、打地鼠、吃豆人

    学习之余当然是摸鱼了,这里分享几个不用下载直接在线玩耍的游戏.有蜘蛛纸牌网页版在线玩.在线扫雷小游戏.在线玩的水果忍者.吃豆人.打地鼠.3D模仿. 下面我将一个个列出来.欢迎体验收藏! 蜘蛛纸牌:这是 ...

  2. JavaScript网页特效-“扫雷”游戏随机布雷功能

    "扫雷"游戏是一款经典益智小游戏.游戏目标是在最短的时间内找出所有非雷格子,同时避免踩雷,踩到一个雷即全盘皆输.本节介绍"扫雷"游戏随机布雷功能的设计与实现. ...

  3. 石头剪刀布php源码,php实现的网页版剪刀石头布游戏示例

    本文实例讲述了php实现的网页版剪刀石头布游戏.分享给大家供大家参考,具体如下: /* * Created on 2016-11-25 * */ if (isset($_POST['sub'])) { ...

  4. HTML5高度还原复古24层魔塔网页版小游戏源码

    简介: HTML5高度还原复古24层魔塔网页版小游戏源码 网盘下载地址: http://kekewl.cc/OFfi6keX7OS0 图片:

  5. 网页版JS游戏五子棋单机版也可以改版成网络版

    网页版JS游戏五子棋单机版也可以改版成网络版 2个文件,界面wuziqi.html   脚本wuziqi.js 新手可以去我下载中心下, <!DOCTYPE html> <html ...

  6. 利用java开发简易版扫雷游戏

    1.简介 学了几周的Java,闲来无事,写个乞丐版的扫雷,加强一下Java基础知识. 2.编写过程 编写这个游戏,一共经历了三个阶段,编写了三个版本的游戏代码. 第一版:完成了扫雷游戏的基本雏形,实现 ...

  7. 《游戏学习》| 水果忍者HTML5网页版在线游戏 | 源码分析

    游戏介绍 这是一款由百度JS小组提供的HTML5版切水果游戏,这款基于HTML5实现的网页版切水果游戏虽然和原版的切水果游戏相比仍有美中不足之处,但也算有声有色,画面效果也十分炫目华丽. 游戏截图 主 ...

  8. c++简易版扫雷游戏

    c++简易版扫雷游戏,代码如下: #include<iostream> #include<cstdlib> #include<time.h> using names ...

  9. 前端小白写了个网页版五子棋游戏,使用原生 JS + Canvas 实现绘制棋子、棋盘

    功能模块 快来体验下吧!看下你能赢得了计算机么? 主要功能模块为: 1.人机对战功能 2.悔棋功能 3.撤销悔棋功能 老规矩,源码贴上 具体代码 (1)HTML代码 <!DOCTYPE html ...

最新文章

  1. 初识广度优先搜索与解题套路
  2. 4G LTE/EPC UE 的业务请求流程
  3. MOBA项目问题记录
  4. 网络犯罪:令牌化和区块链将终结数据盗窃行为?
  5. 软件测试——等价类划分
  6. 中国科学院院士梅宏:云计算这十年
  7. 2020 我的C++的学习之路
  8. Rake::TestTask 介绍
  9. QWaiteCondition思考3
  10. 如何在 C#9 中使用顶级程序 (top-level)
  11. 1327集群_MPT1327集群通讯系统
  12. CreateJS基础 学习笔记(上)
  13. java 随机手机验证码_基于Java随机生成手机短信验证码的实例代码|chu
  14. 大型网站技术架构之技术一览
  15. 华悦网游器软件介绍及功能介绍
  16. 手机微信html整人代码大全,2018年微信整人代码有哪些?2018年微信整人代码大全!...
  17. 全国青少年计算机考试官网,全国青少年计算机考试开考 每年组织四次
  18. 得物购买截图生成_iPhone12订单生成器app-iPhone12订单生成器网页app免费版预约 v1.0...
  19. DELL R730xd 安装PCIE SSD 后风扇转速增高的解决方案
  20. null hypothesis

热门文章

  1. 电视台为什么做不好新媒体
  2. csu:1919: 不醉不归
  3. SQL语句——生成汉字首字母拼音
  4. 18. python虚拟环境
  5. 深度学习的数学-神经单元误差和反向传播
  6. 项目总结 -- RFID 读写器上位机软件
  7. 迪文屏幕T5L平台学习笔记五:C51使用UART2打印log
  8. lingo 基础 程序结构,关系运算,逻辑运算,各类函数汇总
  9. 轻松学会spire.xls的常规操作
  10. kali系统与win10系统的文件互传