格子游戲

<!DOCTYPE html>
<head>
<title> 16宮格小游戲 </title>
<style type="text/css">
body {background-color:white;}
p {color:blue;}
td {
width: 50px;
height: 50px;
background-color: black;
}
table {size: 400px;}
</style>
</head>
<body>

<script>
    var step = 0

    function start()
    {
        step = 0
        document.getElementById("display_label").textContent = "開始游戲";
        for (var i = 1; i <= 16; i++) {
            document.getElementById(i).style.backgroundColor="#000000";
        }
    }
    function next() {
        //clear color
        for (var i = 1; i <= 16; i++) {
            document.getElementById(i).style.backgroundColor="#000000";
        }
        //next
        step += 1
        if (step > 8) {
            document.getElementById("display_label").textContent = "游戲結(jié)束! 恭喜您完成全部挑戰(zhàn)!";
        } else {
            document.getElementById("display_label").textContent = "恭喜您已進入第\(" + step + ")級游戲";
            //隨機數(shù)
            var randomIds = []
            console.log(randomIds.length)
            var length = 0
            while (length < step) {
                console.log(length)
                let random = Math.floor(Math.random() * 16)+1;
                var has = false;
                for (const i in randomIds) {
                    if (randomIds[i] == random) {
                        has = true;
                        break;
                    }
                }
                if (!has) {
                    length +=1;
                    document.getElementById(random).style.backgroundColor="#ffffff";
                    randomIds.push(random);
                }
            }
        }
    }

</script>

<label id="display_label" style="font-size: xx-large;">小游戲</label>
<br />
<button id="start_button" onclick="start()" style="background-color:rgb(255,0,0)">啟動</button>
<button id="next_button" onclick="next()">下一步</button>
<br />
<br />
<div id="content">
    <table border="1">
        <tr>
            <td id="1"></td>
            <td id="2"></td>
            <td id="3"></td>
            <td id="4"></td>
        </tr>
        <tr>
            <td id="5"></td>
            <td id="6"></td>
            <td id="7"></td>
            <td id="8"></td>
        </tr>
        <tr>
            <td id="9"></td>
            <td id="10"></td>
            <td id="11"></td>
            <td id="12"></td>
        </tr>
        <tr>
            <td id="13"></td>
            <td id="14"></td>
            <td id="15"></td>
            <td id="16"></td>
        </tr>
    </table>

</div>

</body>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容