使用js開發網頁版 掃雷(附程式碼詳解)
//HTMEL <body> <p class="content"> </p></body> //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 + "<br />"; } 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 + "<br />"; } //取消 右击 菜单 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;}
相關文章:
以上是使用js開發網頁版 掃雷(附程式碼詳解)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

不同JavaScript引擎在解析和執行JavaScript代碼時,效果會有所不同,因為每個引擎的實現原理和優化策略各有差異。 1.詞法分析:將源碼轉換為詞法單元。 2.語法分析:生成抽象語法樹。 3.優化和編譯:通過JIT編譯器生成機器碼。 4.執行:運行機器碼。 V8引擎通過即時編譯和隱藏類優化,SpiderMonkey使用類型推斷系統,導致在相同代碼上的性能表現不同。

Python更適合初學者,學習曲線平緩,語法簡潔;JavaScript適合前端開發,學習曲線較陡,語法靈活。 1.Python語法直觀,適用於數據科學和後端開發。 2.JavaScript靈活,廣泛用於前端和服務器端編程。

JavaScript是現代Web開發的核心語言,因其多樣性和靈活性而廣泛應用。 1)前端開發:通過DOM操作和現代框架(如React、Vue.js、Angular)構建動態網頁和單頁面應用。 2)服務器端開發:Node.js利用非阻塞I/O模型處理高並發和實時應用。 3)移動和桌面應用開發:通過ReactNative和Electron實現跨平台開發,提高開發效率。

本文展示了與許可證確保的後端的前端集成,並使用Next.js構建功能性Edtech SaaS應用程序。 前端獲取用戶權限以控制UI的可見性並確保API要求遵守角色庫

我使用您的日常技術工具構建了功能性的多租戶SaaS應用程序(一個Edtech應用程序),您可以做同樣的事情。 首先,什麼是多租戶SaaS應用程序? 多租戶SaaS應用程序可讓您從唱歌中為多個客戶提供服務

從C/C 轉向JavaScript需要適應動態類型、垃圾回收和異步編程等特點。 1)C/C 是靜態類型語言,需手動管理內存,而JavaScript是動態類型,垃圾回收自動處理。 2)C/C 需編譯成機器碼,JavaScript則為解釋型語言。 3)JavaScript引入閉包、原型鍊和Promise等概念,增強了靈活性和異步編程能力。

JavaScript在Web開發中的主要用途包括客戶端交互、表單驗證和異步通信。 1)通過DOM操作實現動態內容更新和用戶交互;2)在用戶提交數據前進行客戶端驗證,提高用戶體驗;3)通過AJAX技術實現與服務器的無刷新通信。

JavaScript在現實世界中的應用包括前端和後端開發。 1)通過構建TODO列表應用展示前端應用,涉及DOM操作和事件處理。 2)通過Node.js和Express構建RESTfulAPI展示後端應用。
