


Share an example code of a web version of minesweeper game made with h5
I have nothing to do to write a minesweeper, the algorithm is not very good... It's just okay, it's really good, you can take a look
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>扫雷</title> </head> <body> <script> function Set(r,c,hard){ var data = new Array(); for(var i=0;i<r;i++){data[i]=new Array(c);} for (var i = 0; i < r*c; i++) { var ran=Math.floor(Math.random()*100); data[Math.floor(i / c)][i % c] =ran<hard?1:0; } return data; } function GetNewData(data,r,c){ var newdata = new Array(); for(var i=0;i<r;i++){newdata[i]=new Array(c);} for (var i = 0; i < r * c; i++) { if (data[Math.floor(i / c)][i % c] == 1) { newdata[Math.floor(i / c)][i % c] = 9; } else { var d = 0; for (var j = 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; } } return newdata; } function GetRegion(rr,cc,data,list){ if (data[rr][cc]!=0) { return; } else { for (var j = 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) { var pr=rr+ Math.floor(j / 3 - 1); var pc=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>难度: <select NAME="Difficulty" onchange="load(this.value)" id="Difficulty"> <option value></option> <option value="1" selected="selected">初9*9</option> <option value="2" >中16*16</option> <option value="3">高30*16</option> </select> <button onclick="timedCount()">开始</button> <button onclick="stop()">停止</button> <button onclick="reload()">重置</button> <button onclick="Drawall()">显示所有</button> <input type="text" readonly="readonly" id="time"> </p> <p> <canvas id="myCanvas" oncontextmenu=self.event.returnvalue=false width="180" height="180" style="border:1px solid #000000;background:lightgray;"></canvas> </p> <script> var r=9;//99 1616 3016 var c=9; var difficulty=15 var tempdata; var minedata; var signdata = new Array(); var checkSigndata=new Array document.write("<br/>"); var Difficulty=document.getElementById("Difficulty"); Difficulty.options[1].selected = true; var canvas=document.getElementById("myCanvas"); canvas.addEventListener("mousedown", doMouseDown, false); canvas.oncontextmenu=function(){return false;} load(1); redraw(); //test(); //Drawall(); var t; var tick=0; function timedCount(){ document.getElementById('time').value=tick tick=tick+1 t=setTimeout("timedCount()",1000) } function stop(){ if(t!=null)clearTimeout(t); tick=0; } function loadsigndata(){ for(var i=0;i<r;i++){signdata[i]=new Array(c);} for(var i=0;i<r;i++){checkSigndata[i]=new Array(c); } for (var i = 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; } } } function test(){ for (var i = 0; i < r*c; i++) { document.write(minedata[Math.floor(i / c)][i % c]+" "); if(i%c==c-1)document.write("<br/>"); } } function load(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(); } function reload(){ redraw(); tempdata=Set(r,c,difficulty); minedata=GetNewData(tempdata,r,c); loadsigndata(); } function redraw(){ canvas.setAttribute('width',c*30); canvas.setAttribute('height',r*30); var ctx=canvas.getContext("2d"); for(var i=0;i<r+1;i++) { ctx.moveTo(0,i*30); ctx.lineTo(c*30,i*30); ctx.stroke(); } for(var i=0;i<c+1;i++) { ctx.moveTo(i*30,0); ctx.lineTo(i*30,r*30); ctx.stroke(); } } function contains(arr, obj) { var i = arr.length; while (i--) { if (arr[i].r==obj.r&&arr[i].c==obj.c) { return true; } } return false; } var plist=new Array(); function doMouseDown(event){ var btnNum = event.button; var x = event.pageX; var y = event.pageY; var loc = getPointOnCanvas(canvas, x, y); var xx=Math.floor(loc.x/30); var yy=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(var l=0;l<plist.length;l++){drawCell(plist[l].c,plist[l].r);} } else if(btnNum==2) { drawCellr(xx,yy); } if(check())alert("成功:"+tick); } function check(){ for (var i = 0; i < r*c; i++) { if(checkSigndata[Math.floor(i / c)][i % c]!=signdata[Math.floor(i / c)][i % c]) { return false; } } return true; } function drawCell(xx,yy){ var ctx = 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; } } function drawCellr(xx,yy){ var ctx = 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(); } function getPointOnCanvas(canvas, x, y) { var bbox = canvas.getBoundingClientRect(); return { x: x - bbox.left * (canvas.width / bbox.width), y: y - bbox.top * (canvas.height / bbox.height) }; } function Drawall(){ redraw(); var ctx = canvas.getContext("2d"); ctx.textAlign = "start"; ctx.fillStyle = "red"; ctx.font = "30px Arial"; for(var i=0;i<r*c;i++) { var x=i%c*30; var y=(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>
[Related recommendations]
1.Free h5 online video tutorial
3. php.cn original html5 video tutorial
The above is the detailed content of Share an example code of a web version of minesweeper game made with h5. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



If Nvgpucomp64.dll is causing your game to crash frequently, the solutions provided here may help you. This problem is usually caused by outdated or corrupted graphics card drivers, corrupted game files, etc. Fixing these issues can help you deal with game crashes. The Nvgpucomp64.dll file is associated with NVIDIA graphics cards. When this file crashes, your game will crash too. This usually happens in games like LordsoftheFallen, LiesofP, RocketLeague, and ApexLegends. Nvgpucomp64.dll crashes games on Windows PC if N

The superpeople game can be downloaded through the steam client. The size of this game is about 28G. It usually takes one and a half hours to download and install. Here is a specific download and installation tutorial for you! New method to apply for global closed testing 1) Search for "SUPERPEOPLE" in the Steam store (steam client download) 2) Click "Request access to SUPERPEOPLE closed testing" at the bottom of the "SUPERPEOPLE" store page 3) After clicking the request access button, The "SUPERPEOPLECBT" game can be confirmed in the Steam library 4) Click the install button in "SUPERPEOPLECBT" and download

According to news from this website on February 23, NVIDIA updated and launched the NVIDIA application last night, providing players with a new unified GPU control center, allowing players to capture wonderful moments through the powerful recording tool provided by the in-game floating window. In this update, NVIDIA also introduced the RTXHDR function. The official introduction is attached to this site: RTXHDR is a new AI-empowered Freestyle filter that can seamlessly introduce the gorgeous visual effects of high dynamic range (HDR) into In games that do not originally support HDR. All you need is an HDR-compatible monitor to use this feature with a wide range of DirectX and Vulkan-based games. After the player enables the RTXHDR function, the game will run even if it does not support HD

Friends who have played enough AAA masterpieces and mobile games, do you want to relive the computer games of your childhood? Then let’s look for Spider Solitaire in Windows 11 together! Click the Start menu on the interface, click the "All Apps" button; click "All Apps". Find and select "MicrosoftSolitaireCollection", which is Microsoft's Solitaire series game application; Solitaire series game selection. After loading is complete, enter the selection interface and find "Spider Solitaire"; select "Spider Solitaire". Although the interface has changed slightly, it is still the same as before

According to news from this site on April 20, ASUS recently released a BIOS update, which improves instability such as crashes when running games on Intel's 13th/14th generation processors. This site previously reported that players reported problems including that when running the PC demo version of Bandai Namco's fighting game "Tekken 8", even if the computer has sufficient memory and video memory, the system will crash and prompt an error message indicating insufficient memory. Similar crashing issues have also appeared in many games such as "Battlefield 2042", "Remnant 2", "Fortnite", "Lord of the Fallen", "Hogwarts Legacy" and "The Finals". RAD published a long article in February this year, explaining that the game crash problem is a combination of BIOS settings, high clock frequency and high power consumption of Intel processors.

Various games can be purchased in the small black box, so how do you add the purchased games to Steam? Users need to activate the product on Steam in Steam, and then copy the activation code in the small black box to activate. This introduction to the method of storing purchased games into Steam will tell you the specific method. The following is a detailed introduction, hurry up Come and take a look! How to add games purchased from Little Black Box to Steam Answer: Activate the product on Steam in Steam to add it to the warehouse. Specific methods: 1. First, click the game button on Steam. 2. Click Activate product on Steam. 3. Then click Next in the window that appears. 4. Paste the purchase code in the small black box in the product activation. 5. Then click Next to add

Recently, some friends have reported that they often press the input method when playing games, which greatly affects the gaming experience. Here I will give you a detailed introduction to the method of disabling the input method when playing games in Win11. Anyone who needs it Friends can come and take a look. Disabling method: 1. Right-click the input method icon in the taskbar in the lower right corner and select "Language Preferences" in the list. 2. After entering the new interface, click the "Add preferred language" option. 3. In the pop-up window, select "English (United States)". 4. Click "Next" again. 5. Then choose whether to install some options according to your needs. 6. Then click "Install" and wait for the installation to complete. 7. Then click on the input method status bar in the lower right corner and select the "English (

According to news from this site on July 22, foreign media twistedvoxel discovered the rumored PS5 development codename "Trinity" and related image quality configuration files in the latest "World Part 1" update code of "No Man's Sky", which proves that Sony is expected to The PS5Pro model was recently launched. Although "No Man's Sky" has enhanced the graphics performance of the game in recent updates, many players still believe that this may be HelloGames paving the way for new models in advance. According to the latest graphics presets, under PS5 Pro The game's dynamic resolution scaling has been increased from 0.6 to 0.8, which means the game has a higher average resolution and some graphical details are upgraded from "High" to "Ultra" levels, but since each game
