日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

html5扫雷代码,使用js开发网页版 扫雷(附代码详解)

發布時間:2023/12/10 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 html5扫雷代码,使用js开发网页版 扫雷(附代码详解) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

//HTMEL

//css

.bottom,over { text-align: center; line-height: 0px;}

.bottom p { display: inline-block; width: 30px; height: 30px; border: 1px solid #808080; background-color: #f4a; text-align: center; font: 20px/30px arial; }

//JS

// 保存 初始雷化記錄 和 拆雷記錄 100個按鈕中 有25個雷var box = Array(10);

//初始化底層function initBottom() { const content = document.getElementsByClassName('content')[0]; const bottom = document.createElement('p'); content.appendChild(bottom); bottom.className = "bottom";

for (let i = 0; i < 10; i++) { box[i] = new Array(10); for (let j = 0; j < 10; j++) { const p = document.createElement('p'); bottom.appendChild(p); p.id = "bottom[" + i + "][" + j + "]"; box[i][j] = 0; } bottom.innerHTML = bottom.innerHTML + "
"; }

initBom();}

//初始化 25個 bom 顏色 和 innerHTMLfunction initBom() { //改變顏色 for (let i = 0; i < 25; i++) { let x = Math.floor(Math.random() * 10); let y = Math.floor(Math.random() * 10); while (box[x][y] == 1) { x = Math.floor(Math.random() * 10); y = Math.floor(Math.random() * 10); if (box[x][y] == 0) { break; } }

box[x][y] = 1; document.getElementById("bottom[" + x + "][" + y + "]").style.backgroundColor = "rgb(0, 0, 0)"; document.getElementById("bottom[" + x + "][" + y + "]").innerHTML = "x"; // 否則 對不起 ? }

// 生成底層數字 for (let i = 0; i < 10; i++) { for (let j = 0; j < 10; j++) {

if (!box[i][j] == 1) { document.getElementById("bottom[" + i + "][" + j + "]").innerHTML = getNumber(i, j); }

} }}

// 取得 周圍雷的個數function getNumber(i, j) { let num = 0, x, y; // 四周 雷的 個數統計 for (let x = i - 1; x <= i + 1; x++) { for (y = j - 1; y <= j + 1; y++) { if (x == i && y == j) { continue; } if (document.getElementById("bottom[" + x + "][" + y + "]")) { if (box[x][y] == 1) { num++; } } } } return num;}

//初始化 上層 overfunction initOver() {

const content = document.getElementsByClassName('content')[0]; const over = document.createElement('p'); content.appendChild(over); over.className = "over"; over.id = "over";

for (let i = 0; i < 10; i++) { for (let j = 0; j < 10; j++) { const p = document.createElement('p'); over.appendChild(p); p.id = "over[" + i + "][" + j + "]"; } over.innerHTML = over.innerHTML + "
"; }

//取消 右擊 菜單 over.oncontextmenu = function (e) { e.returnValue = false; }

// 點擊 做標記 over.onclick = function (e) { const target = e.srcElement; if (target.style.backgroundColor == "rgb(255, 255, 255)") { target.style.backgroundColor = "#808080"; } else { target.style.backgroundColor = "rgb(255, 255, 255)"; } }

// 雙擊拆雷 over.ondblclick = function (e) { const target = e.srcElement; const strId = target.id.substring(4, target.id.length); if (document.getElementById("bottom" + strId).style.backgroundColor == "rgb(0, 0, 0)") { document.getElementById('over').style.display = "none"; } else { target.style.opacity = 0; let i = parseInt(strId.substring(1, 2)); let j = parseInt(strId.substring(4, 5)); box[i][j] = 1; // 等于0 擴散 if (document.getElementById("bottom" + strId).innerHTML == 0) { zooming(i, j); } else { if (isGameOver()) { alert("真厲害"); } }

} }}

//擴散: 當周圍有 0時,自動顯示function zooming(i, j) {

for (let x = i - 1; x <= i + 1; x++) { for (y = j - 1; y <= j + 1; y++) { if (document.getElementById("bottom[" + x + "][" + y + "]") && document.getElementById("bottom[" + x + "][" + y + "]").innerHTML == 0) { document.getElementById("over[" + x + "][" + y + "]").style.opacity = 0; if (box[x][y] == 0) { box[x][y] = 1; zooming(x, y); } } } }}

//是否清除完 box[][] 初始化 雷時賦值 1,拆雷是賦值 1,全部為1時,完成掃雷。function isGameOver() { for (let i = 0; i < 10; i++) { for (let j = 0; j < 10; j++) { if (box[i][j] == 0) { return false; } } } return true;}

相關文章:

總結

以上是生活随笔為你收集整理的html5扫雷代码,使用js开发网页版 扫雷(附代码详解)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。