javascript游戏(3行)行检查逻辑

时间:2014-05-24 02:21:28

标签: javascript

自从昨天以来,我一直在努力解决这个问题,我担心自己已​​经获得了隧道视野。

游戏:

第一个获得3种线(xxx或000)的玩家获胜。 http://jsfiddle.net/brunobliss/YANAW/

捕获:

只有第一条水平线正在工作! 我可以使用大量的IFS来完成所有工作但是一遍又一遍地重复相同的代码往往是一个很好的指标,我正在做一些错误的

问题:

bruno.checkWin();将检查是否有一条线,向我展示这个游戏的人chalenge告诉我,有可能用for循环检查线路,我应该使用它而不是IFS。没有IFS,我无法解决这个问题......

<!doctype html>
<html>
    <head>
        <meta charset="iso-8859-1">
        <title> </title>
        <style>
            #jogo {
                border: #000 1px solid;
                width: 150px;
                position: absolute;
                left: 50%;
                top: 50%;
                margin-left: -75px;
                margin-top: -75px;
            }
            #jogo div {
                display: inline-block;
                vertical-align: top;
                width: 28px;
                height: 28px;
                padding: 10px;
                font-size: 20px;
                border: #000 1px solid;
                border-collapse: collapse;
                text-align: center;
            }
            #reset {
                font-family: Verdana;
                width: 153px;
                height: 30px;
                background-color: black;
                color: white;
                text-align: center;
                cursor: pointer;
                left: 50%;
                top: 50%;
                position: absolute;
                margin-left: -76px;
                margin-top: 100px;
            }
        </style>
        <script> </script>
    </head>
    <body>
        <div id="jogo"> </div>
        <div id="reset"> RESET </div>
        <script>
        var ultimo = "0";
        var reset = document.getElementById('reset');
                    var jogo = document.getElementById('jogo');
            var cell = jogo.getElementsByTagName('div');
        var bruno = {
            init: function () {
                var jogo = document.getElementById('jogo');
                for ( i = 0 ; i < 9 ; i++ ) {
                    var cell = document.createElement('div');
                    cell.onclick = function () {
                        // variavel publica dentro do obj?
                        ultimo = (ultimo == "x") ? 0 : "x";
                        this.innerHTML = ultimo;
                        bruno.checkWin();   
                    };
                    jogo.appendChild(cell);
        }
            },
            checkWin: function () {
                var jogo = document.getElementById('jogo');
                var cell = jogo.getElementsByTagName('div');
                // as diagonais nao verificar por loop
                for ( i = 0 ; i < cell.length ; i=i+4 ) {
                    switch(i) {
                        case 0:
                        if (cell[0].innerHTML != '') {
                            bruno.checkFirst();
                        }
                        case 4:
                        if (cell[4].innerHTML != '') {
                            bruno.checkFirst();
                        }
                        case 8:
                        if (cell[8].innerHTML != '') {
                            bruno.checkFirst();
                        }                       
                    }
                        /*
                    } else 
                    if (i == 4 && cell[4].innerHTML != '') {
                        bruno.checkCenter();
                    } else 
                    if (i == 8 && cell[8].innerHTML != '') {
                        bruno.checkLast();
                    }*/
            }
        },
        reset: function () {
            var jogo = document.getElementById('jogo');
            var cell = jogo.getElementsByTagName('div');
            for ( j = 0 ; j < cell.length ; j++ ) {
                cell[j].innerHTML = "";
            }
        },
        checkFirst: function () {
            if (cell[0].innerHTML == cell[1].innerHTML && cell[1].innerHTML == cell[2].innerHTML) {
                alert("linha horizontal");
                return false;
            } else 
            if (cell[0].innerHTML == cell[3].innerHTML && cell[3].innerHTML == cell[6].innerHTML) {
                alert("linha vertical");
                return false;
            }
        },
        checkMiddle: function () {
            // check vertical and horizontal lines from the center
        },
        checkLast: function () {
            // check last horizontal and right edge vertical
        }

};
        window.onload = function () {
            bruno.init();
        };
        reset.onclick = function () {
            bruno.reset();
        };
        </script>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

我想出了一个更“紧凑”的代码版本。没有切换声明。看一看: http://jsfiddle.net/YANAW/1/

这是代码,适合那些喜欢在这里阅读的人。重要/更新的功能是 checkWin() checkCells()

var bruno = {
    init: function () {
        var jogo = document.getElementById('jogo');
        for ( i = 0 ; i < 9 ; i++ ) {
            var cell = document.createElement('div');
            cell.onclick = function () {
                // variavel publica dentro do obj?
                ultimo = (ultimo == "x") ? 0 : "x";
                this.innerHTML = ultimo;
                bruno.checkWin();   
            };
            jogo.appendChild(cell);
        }
    },
    checkWin: function () {
        var jogo = document.getElementById('jogo');
        var cells = jogo.getElementsByTagName('div');

        // Scan through every cell
        var numRows = 3;
        var numColumns = 3;
        for (var i = 0; i < cells.length; i++)
        {
            // Determine cell's position
            var isHorizontalFirstCell = ((i % numColumns) === 0);
            var isVerticalFirstCell = (i < numColumns);
            var isTopLeftCorner = (i == 0);
            var isTopRightCorner = (i == 2);

            // Check for horizontal matches
            if (isHorizontalFirstCell
                && bruno.checkCells(
                    cells, i, 
                    (i + 3), 1))
            {
                alert('Horizontal');
            }

            // Check for vertical matches
            if (isVerticalFirstCell
                && bruno.checkCells(
                    cells, i,
                    (i + 7), 3))
            {
                alert('Vertical');   
            }

            // Check for diagonal matches
            if (isTopLeftCorner
                && bruno.checkCells(
                    cells, i,
                    (i + 9), 4))
            {
                alert('Diagonal');
            }

            if (isTopRightCorner
                && bruno.checkCells(
                    cells, i,
                    (i + 5), 2))
            {
                alert('Diagonal');
            }            
        }
    },
    reset: function () {
        var jogo = document.getElementById('jogo');
        var cell = jogo.getElementsByTagName('div');
        for ( j = 0 ; j < cell.length ; j++ ) {
            cell[j].innerHTML = "";
        }
    },
    checkCells: function(cells, index, limit, step) {
        var sequenceChar = null;
        for (var i = index; i < limit; i += step)
        {
            // Return false immediately if one
            // of the cells in the sequence is empty
            if (!cells[i].innerHTML) 
                return false;

            // If this is the first cell we're checking,
            // store the character(s) it holds.
            if (sequenceChar === null)
                sequenceChar = cells[i].innerHTML;

            // Otherwise, confirm that this cell holds
            // the same character(s) as the previous cell(s).
            else if (cells[i].innerHTML !== sequenceChar)
                return false;
        }

        // If we reached this point, the entire sequence
        // of cells hold the same character(s).
        return true;
    }
};