网页扫雷的问题

扫雷

我做了一款扫雷

代码结构:

在这里插入图片描述
BoomIndex.html代码:

<!doctype html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>扫雷</title>
    <script src="Script.js" rel="script"></script>
    <link href="StyleSheet.css" rel="stylesheet" />
</head>
<body style="background-color: black">
<div id="root">
    <table class="up_block">
        <tr>
            <td id="ub_fin_boom" >
                <button id="free_normal_mode" onclick="update_mode('FREE')">普通模式</button>
                <button id="flag_mode" onclick="update_mode('FLAG')">插旗模式</button>
            </td>
            <td id="ub_slt_boom">
                <button class="Title_Button" id="ub_slt_boom_button" onclick="update_bmp_func()">
                    <img src="img/smile.bmp" alt="SMILE"/>
                </button>
            </td>
            <td id="ub_scr_boom">
                分数:<span id="ub_scr_boom_data">0</span></td>
        </tr>
    </table>
    <div id="boom_block">
        <div id="boom_table">
            <script type="text/javascript">
                for(let x=1;x<=15;x++){
                    for(let y=1;y<=15;y++){
                        document.write(`<button class="box" id="box${x}_${y}" οnclick="click_box(${x},${y})">N</button>`)
                    }
                    document.write(`<br/>`)
                }
            </script>
        </div>
    </div>
</div>
</body>
</html>

StyleSheetCSS代码:

#root{
    position: absolute;
    width: 600px;
    height: 100%;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
    background-color: #FFFF00;
}
#up_block{
    height: 25%;
    width : 100%;
    background-color: #000000;
}
#boom_block{
    height: 75%;
    width: 100%;
    background-color: #FFFF00;
}
#ub_fin_boom{
    height: 100px;
    width: 200px;
    border: 5px solid #000000;
    padding-left: 8px;
    padding-right: 8px;
    text-align: center;
    vertical-align: center;
    background-color: #FFFF00;
}
#ub_slt_boom{
    height: 100px;
    width: 200px;
    border: 5px solid #000000;
    background-color: #FFFF00;
    padding-left: 8px;
    padding-right: 8px;
    text-align: center;
    vertical-align: center;
}
#ub_scr_boom{
    height: 100px;
    width: 200px;
    border : 5px solid #000000;
    background-color: #FFFF00;
    padding-left: 8px;
    padding-right: 8px;
    text-align: center;
    vertical-align: center;
}
.Title_Button{
    border: 3px outset #FFFFFF;
    background-color: #FFFFFF;
    transition-duration: 0.05s;
}
.Title_Button:hover{
    border: 3px outset #777777;
    background-color: #777777;
}
.box{
    border: 5px outset #777777;
    background-color: #777777;
    width: 30px;
    height : 30px;
    text-align: center;
    vertical-align: center;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    transition-duration: 0.05s;
}
.box:hover{
    border: 5px outset #444444;
    background-color: #444444;
}
.lose_box{
    border: crimson;
    background-color: crimson;
}
#boom_table{
    text-align: center;
    /*vertical-align: center;*/
    /*margin-top: 20px;*/
}

Script.js代码

let ClickMode;
let BoomList;
let NumList;
let dis;
let cnt;
function game_init(){
    ClickMode = 'FREE'
    BoomList = new Array();
    NumList = new Array();
    for(let i=0;i<=16;i++){
        BoomList[i] = new Array();
        NumList[i] = new Array();
        dis[i] = new Array();
        for(let j=0;j<16;j++){
            BoomList[i][j] = false;
            NumList[i][j] = 0;
            dis[i][j]=false;
        }
    }
    for(let i=1;i<=15;i++){
        for(let j=1;j<=15;j++){
            let element=document.getElementById(`box${i}_${j}`)
            element.innerHTML = 'N'
            element.className = 'box'
        }
    }
    for(let i=0;i<35;i++){
        let tx = Math.floor(Math.random() * 14) + 1;
        let ty = Math.floor(Math.random() * 14) + 1;
        if(BoomList[tx][ty]==true){
            i--;
            continue;
        }
        BoomList[tx][ty] = true;
    }
    for(let i=1;i<=15;i++){
        for(let j=1;j<=15;j++){
            if(BoomList[i][j]){
                NumList[i][j] = Infinity;
            }else{
                let dx = [0,0,1,-1,1,1,-1,-1]
                let dy = [1,-1,0,0,1,-1,1,-1]
                let sum = 0
                for(let x=0;x<8;x++){
                    if(BoomList[i+dx[x]][j+dy[x]]){
                        sum++;
                    }
                }
                NumList[i][j] = sum;
            }
        }
    }
}
function over_game(bombx,bomby){
    for(let i=1;i<=15;i++){
        for(let j=1;j<=15;j++){
            if(BoomList[i][j]){
                let element = document.getElementById(`box${i}_${j}`)
                element.className = 'lose_box'
                element.innerHTML = 'B'
            }
        }
    }
}
function update_bmp_func(){
    let element = document.getElementById("ub_slt_boom_button")
    if(element.innerHTML==='<img src="img/bad.bmp" alt="LOST" />'){
        element.innerHTML='<img src="img/smile.bmp" alt="SMILE"/>'
        game_init()
        return;
    }
    if(element.innerHTML==='<img src="img/smile.bmp" alt="SMILE"/>'){
        element.innerHTML = '<img src="img/smileClick.bmp" alt="SMILE_CLICK" />'
        setTimeout(function(){
            element.innerHTML = '<img src="img/smile.bmp" alt="SMILE"/>'
        },200)
    }
};
let update_mode = (mode_string) => {
    if(mode_string === 'FREE'){
        ClickMode = 'FREE'
    }else{
        ClickMode = 'FLAG'
    }
}
function is_win(){
    let flag = true;
    for(let i=1;i<=15;i++){
        for(let j=1;j<=15;j++){
            let element = document.getElementById(`box${i}_${j}`)
            if(BoomList[i][j]){
                if(element.innerHTML !== 'F'){
                    flag = false;
                }
            }
        }
    }
    return flag;
}
function click_box(cx,cy){
    let element = document.getElementById(`box${cx}_${cy}`)
    let dx = [0,0,-1,1]
    let dy = [-1,1,0,0]
    let dfs_dis = new Array();
    for(let i=1;i<=15;i++){
        dfs_dis[i] = new Array();
        for (let j=1;j<=15;j++){
            dfs_dis[i][j] = false;
        }
    }
    let dfs = (x,y) => {
        dfs_dis[i] = true;
        element.innerHTML = (NumList[x][y]!=0)?(`${NumList[x][y]}`):(' ')
        dis[x][y] = true;
        for(let i=0;i<4;i++){
            let tx = x+dx[i];
            let ty = y+dy[i];
            if(tx<1 || tx>15 || ty<1 || ty>15 || (element.innerHTML==0 && dfs_dis[tx][ty]==true)){
                continue;
            }
            dfs(tx,ty)
        }
    }
    if(ClickMode==='FLAG'){
        if(element.innerHTML==='F'){
            element.innerHTML = 'N'
        }else{
            element.innerHTML = 'F'
        }
    }else{
        if(BoomList[cx][cy]){
            over_game(cx,cy)
        }else{

            dfs(cx,cy);
            if(is_win()){
                let element_win = document.getElementById("ub_slt_boom_button")
                element_win.innerHTML = 'YOUWIN!'
            }
        }
    }
}

图片资源:
bad.bmp
在这里插入图片描述
smile.bmp
在这里插入图片描述
smileClick.bmp
在这里插入图片描述
效果图
在这里插入图片描述

不得不说,真的有一点丑,但功能还是有的

问题

  1. ub_slt_boom_button点击后没有反应 (指有切换图片效果的)
  2. 雷区正常模式点击无反应

望评论区中的大神教一下,到底哪里出了问题。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值