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

javascript游戏(3行)行检查逻辑,javascript,Javascript,从昨天开始,我就一直在努力解决这个问题,我担心我已经有了隧道视力 游戏: 第一个排成3队(xxx或000)的玩家获胜。 捕获物: 只有第一条水平线在工作我可以使用大量的IFS使一切正常运行,但一遍又一遍地重复相同的代码通常是一个很好的指标,表明我做错了什么 问题: bruno.checkWin();将检查是否有一条线,给我看这个游戏chalenge的家伙告诉我,可以使用for循环检查线,我应该使用它而不是IFS。没有IFS我无法解决这个问题不幸的是 <!doctype html>

从昨天开始,我就一直在努力解决这个问题,我担心我已经有了隧道视力

游戏:

第一个排成3队(xxx或000)的玩家获胜。

捕获物:

只有第一条水平线在工作我可以使用大量的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>

#乔戈{
边框:#000 1px实心;
宽度:150px;
位置:绝对位置;
左:50%;
最高:50%;
左边距:-75px;
利润上限:-75px;
}
#乔戈分区{
显示:内联块;
垂直对齐:顶部;
宽度:28px;
高度:28px;
填充:10px;
字体大小:20px;
边框:#000 1px实心;
边界塌陷:塌陷;
文本对齐:居中;
}
#重置{
字体系列:Verdana;
宽度:153px;
高度:30px;
背景色:黑色;
颜色:白色;
文本对齐:居中;
光标:指针;
左:50%;
最高:50%;
位置:绝对位置;
左边距:-76px;
边缘顶部:100px;
}
重置
var ultimo=“0”;
var reset=document.getElementById('reset');
var jogo=document.getElementById('jogo');
var cell=jogo.getElementsByTagName('div');
var bruno={
init:函数(){
var jogo=document.getElementById('jogo');
对于(i=0;i<9;i++){
var cell=document.createElement('div');
cell.onclick=函数(){
//公共交通的变化?
ultimo=(ultimo=“x”)?0:“x”;
this.innerHTML=ultimo;
bruno.checkWin();
};
jogo.appendChild(单元);
}
},
checkWin:函数(){
var jogo=document.getElementById('jogo');
var cell=jogo.getElementsByTagName('div');
//as诊断是nao验证CAR por循环
对于(i=0;i
我想出了一个更“紧凑”的代码版本。没有开关语句。看看:

这是代码,供那些喜欢在这里阅读的人使用。重要/更新的函数有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;
    }
};
var bruno={
init:函数(){
var jogo=document.getElementById('jogo');
对于(i=0;i<9;i++){
var cell=document.createElement('div');
cell.onclick=函数(){
//公共交通的变化?
ultimo=(ultimo=“x”)?0:“x”;
this.innerHTML=ultimo;
bruno.checkWin();
};
jogo.appendChild(单元);
}
},
checkWin:函数(){
var jogo=document.getElementById('jogo');
var cells=jogo.getElementsByTagName('div');
//扫描每个单元格
var numRows=3;
var numColumns=3;
对于(变量i=0;i