做了一个基本的扫雷游戏,但需要一些方向与清除盒周围没有炸弹的数字

时间:2014-11-19 17:37:28

标签: javascript minesweeper

只是一个基本的扫雷游戏,但无法弄清楚当我打开一个盒子时如何移动或清除盒子。这是一个8x8阵列,炸弹放置在随机的地方。所以,如果有人能指导我如何开始这将是伟大的

//Global 
//store the value of each square
    var gaValue = new Array(8)
    for (i = 0; i <= 8; i++)
        gaValue[i] = new Array(8)
    for (i = 0; i <= 8; i++)
    {
        //loop through each item in those row
        for (j = 0; j <= 8 ; j++)
            gaValue[i][j] = 0
    }
    //Store the status of each square
    var gaSquare = new Array(8)
    for (j = 0; j <= 8; j++)
        gaSquare[j] = new Array(8)
    for (j = 0; j <= 8; j++)
    {
        //loop through each item in those row
        for (i = 0; i <= 8 ; i++)
            gaSquare[i][j] = "C"        
    }
    //Track of whether the game is over or not (starts this with false)
    var gbGameOver = false

    function vInit()
    {
        var strHTML
        var i
        var j

        strHTML = "<table style='margin-left:auto;margin-right:auto'>"
        strHTML += "<tr><td colspan='8' style='text-align:center'>MineSweeper</td></tr>"
        strHTML += "<tr><td colspan='8' style='text-align:center'><input type='button' id='btnNew' value='New Game' onclick='vNewGame()'></td></tr>"

        //Loop through the rows to build the table of tiles
        for (i = 0; i < 8; i++)
        {
            strHTML += "<tr>"
            for (j = 0; j < 8; j++)

                strHTML += '<td><img src="images/sqt0.png" id="square' + i + ', ' + j + '" onclick="vClick(' + i + ', ' + j + ')"/></td>';

            strHTML += "<tr>";
        }
        strHTML += '<tr><td colspan="8" style="text-align:center"><textarea id="taOut" cols="18" rows="10"></textarea></td></tr>'
        strHTML += "</table>"
        frmGrid.innerHTML = strHTML    

//Place bombs
            var iBomb = 0
            var iRow = Math.floor(Math.random() * 8)
            var iCol = Math.floor(Math.random() * 8)

        while (iBomb < 8)
        {   
            while(gaValue[iRow][iCol] == 9)
            {
                iRow = Math.floor(Math.random() * 8)
                iCol = Math.floor(Math.random() * 8)
            }
            gaValue[iRow][iCol] = 9
            iBomb++
        }
        //Calculate clue values around mines
        var iMine = 0        
        for (i = 0; i < 8; i++)
        {
            for (j = 0; j < 8; j++)
            {
                for (k = (i - 1) ; k <= (i + 1) ; k++)
                    for (m = (j - 1) ; m <= (j + 1) ; m++)
                        if (k >= 0 && k <= 9 && j >= 0 && j <= 9)
                            if (gaValue[k][m] == 9)
                                iMine++
                if (gaValue[i][j] != 9)
                    gaValue[i][j] = iMine
                iMine = 0
            }
        }
    }
    //Get the ID of the image I need to change
        function vClick(iRow, iCol)
        {      
             var strSquare = "square" + iRow + ", " + iCol
             gaValue[iRow][iCol] == 'o';
             document.getElementById(strSquare).src = "images/" + gaValue[iRow][iCol] + ".png"
        }
    //Start new game
        function vNewGame()
        {
          vInit()
        }

0 个答案:

没有答案