数独游戏,前端实现

在这里插入图片描述
代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<script>
    window.onload = function () {

        var arr = ["1","2","3","4","5","6","7","8","9"];

        var flag = true;

        var buten = document.getElementById("butten");

        var elementsByTagName = document.getElementsByTagName("input");
        //判断输入是否合法
        for (let i = 0; i < elementsByTagName.length; i++) {
            /*elementsByTagName[i].setAttribute("value","5")*/
            elementsByTagName[i].onblur = function () {
                this.style.color = "red";
                if (arr.indexOf(this.value) == -1 && this.value.length>0){
                    if (this != buten){
                        alert("您输入有错误,重新输入");
                        this.value = null;
                    }
                }
            }
        }
        //检验是否能够构成数独
        function get_hen_shudu() {

            function f(j) {
                var panduna_arr = new Array();
                for (let i = 0; i < 9; i++) {
                    panduna_arr.push(elementsByTagName[j*9+i].value);
                }
                flag = if_repetition(panduna_arr)
            }
            for (let j = 0; j < 9; j++) {
                f(j)
            }

        }

        function get_shu_shudu() {
            function f(j) {
                var panduna_arr = new Array();
                for (var i = 0; i < 9; i++) {
                    panduna_arr.push(elementsByTagName[j+i*9].value)
                }
                flag = if_repetition(panduna_arr)
            }
            for (let i = 0; i < 9; i++) {
                f(i)
            }
        }

        function get_fang_shudu() {
            function f(i) {
                var panduna_arr = new Array();
                for (let j = 0; j < 3; j++) {
                    panduna_arr.push(elementsByTagName[i+j].value)
                    panduna_arr.push(elementsByTagName[i+j+9].value)
                    panduna_arr.push(elementsByTagName[i+j+18].value)
                }
                flag = if_repetition(panduna_arr)
            }
            for (let i = 0; i < 3; i++) {
                for (let j = 0; j < 3; j++) {
                    f(i*27+j*3)
                }
            }

        }

        function if_repetition(arr) {
            var biaoji = 0;
            /*alert("判断")*/
            for (let i = 0; i < arr.length; i++) {
                for (let j = 0; j < arr.length; j++) {
                    if (arr[i] == arr[j]){
                        biaoji++;
                    }
                }
            }
            if (biaoji == 9){
                return true;
            }else {
                return false;
            }
        /*alert("判断结束")*/
        }
        //绑定点击事件
        buten.onclick = function () {
            var show = true
            for (let i = 0; i < elementsByTagName.length; i++) {
                if (!(elementsByTagName[i].value > 0)){
                    show = false;
                }
            }
            if (show == true){
                get_hen_shudu()
                get_shu_shudu()
                get_fang_shudu()
                if (flag == true){
                    document.getElementsByTagName("h2")[0].innerHTML = "恭喜!!!"
                }else {
                    /*alert("失败")*/
                    document.getElementsByTagName("h2")[0].innerHTML = "出错了!!!"
                }
            }else {
                alert("您还没有输入完")
            }
        }

        function doSelf() {
            
        }
    }
</script>
<style>
    div:first-child{
        text-align: center;
        font-size: 50px;
    }
    h2{
        float: right;
        color: #FFD026;
        font-size: 60px;
    }
    div:last-child{
        margin-top: 70px;
        text-align: center;
    }
    table{
        margin: auto;
        border: 2px solid gray;
    }
    input{
        text-align: center;
        width: 40px;
        height: 40px;
        font-size: 30px;
    }
    #butten{
        color: black;
        font-size: 30px;
    }
</style>
<body>
<div>
    <strong>
        数独
    </strong>
</div>
<div>
    <table>
        <tr>
            <td>
                <input type="text">
                <input type="text" value="6" disabled>
                <input type="text" value="1" disabled>
                <input type="text">
                <input type="text" value="3" disabled>
                <input type="text">
                <input type="text">
                <input type="text" value="2" disabled>
                <input type="text">
            </td>
        </tr>
        <tr>
            <td>
                <input type="text">
                <input type="text" value="5" disabled>
                <input type="text">
                <input type="text">
                <input type="text">
                <input type="text" value="8" disabled>
                <input type="text" value="1" disabled>
                <input type="text">
                <input type="text" value="7" disabled>
            </td>
        </tr>
        <tr>
            <td>
                <input type="text">
                <input type="text">
                <input type="text">
                <input type="text">
                <input type="text">
                <input type="text" value="7" disabled>
                <input type="text">
                <input type="text" value="3" disabled>
                <input type="text" value="4" disabled>
            </td>
        </tr>
        <tr>
            <td>
                <input type="text">
                <input type="text">
                <input type="text" value="9" disabled>
                <input type="text">
                <input type="text">
                <input type="text" value="6" disabled>
                <input type="text">
                <input type="text" value="7" disabled>
                <input type="text" value="8" disabled>
            </td>
        </tr>
        <tr>
            <td>
                <input type="text">
                <input type="text">
                <input type="text" value="3" disabled>
                <input type="text" value="2" disabled>
                <input type="text">
                <input type="text" value="9" disabled>
                <input type="text" value="5" disabled>
                <input type="text">
                <input type="text">
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" value="5" disabled>
                <input type="text" value="7" disabled>
                <input type="text">
                <input type="text" value="3" disabled>
                <input type="text">
                <input type="text">
                <input type="text" value="9" disabled>
                <input type="text">
                <input type="text">
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" value="1" disabled>
                <input type="text" value="9" disabled>
                <input type="text">
                <input type="text" value="7" disabled>
                <input type="text">
                <input type="text">
                <input type="text">
                <input type="text">
                <input type="text">
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" value="8" disabled>
                <input type="text">
                <input type="text" value="2" disabled>
                <input type="text" value="4" disabled>
                <input type="text">
                <input type="text">
                <input type="text">
                <input type="text" value="6" disabled>
                <input type="text">
            </td>
        </tr>
        <tr>
            <td>
                <input type="text">
                <input type="text" value="4" disabled>
                <input type="text">
                <input type="text">
                <input type="text" value="1" disabled>
                <input type="text">
                <input type="text" value="2" disabled>
                <input type="text" value="5" disabled>
                <input type="text">
            </td>
        </tr>
    </table>
</div>

<a href="javascript:void 0" id="butten">提交</a>
<h2>
    <!--恭喜!!!-->
</h2>
</body>
</html>
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值