多维数组 - 检查对角线的连续值 [英] Multi-dimensional Array - Check for diagonal consecutive values

查看:170
本文介绍了多维数组 - 检查对角线的连续值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

写一个数组的检查来确定是否有任何潜在的连续的在它内部,无论是 horizo​​ntal , vertical ,或者 diagonal 。下面的例子是一个对角线样例,但是我需要它同时处理 / \



小提琴: http:// jsfiddle .net / PXPn9 / 10 /



所以让我们来假装一下...

  var b = [
[0,0,X,0,0]
[0,0,0,X,0]
[0,0,0,0,X]
[0,0,0,0]
[0,0,0,0]
]

使用基本的2级深循环,遍历整个事物并使用一些三元运营商识别胜利

  function testWin(){
var win = 3,len = b.length,r = 0,c = 0,dr = 0,dl = 0; (var j = 0; j< len; j ++){

// COL WIN WINCHECK / /
(b [j] [i] ===X)? c ++:c = 0;

// ROW WIN CHECK //
(b [i] [j] ===X)? r ++:r = 0;
$ b $ // DIAG WIN CHECK //
//(b [i] [j] ===X&& b [i + 1] [j + 1] ===X)? dr ++:dr = 0;
//(b [j] [i] ===X&& b [i + 1] [j + 1] ===X)? dl ++:dl = 0;
//赢得所有4
的检查if(c === win || r === win){alert(YOU WIN!);返回true;}
}
r = 0;




$ 水平检查 code>和垂直检查看起来工作完美,直到我使注释的尝试创建一个对角线测试...我可以有人看看对角线测试,并帮助确定为什么使他们打破了一切,我做了什么错了?

我想特别协助创建一个 diagonal 检查。 (查看JSFiddle的全部源代码)

  // DIAG WIN CHECK // 
//(b [i] [j] ===X&& b [i + 1] [j + 1] ===X)? dr ++:dr = 0;
//(b [j] [i] ===X&& b [i + 1] [j + 1] ===X)? dl ++:dl = 0;



小提琴: http://jsfiddle.net/PXPn9/10/






新的评论
例如,我试过这个,但它是硬编码为3的对角线(我需要它稍后扩展使用 win 变量)。当我添加这个时,我的水平垂直检查的右下角失败。

$ b $(b [i] [j]& b [i + 1] [j + 1]& b [i + 2] [j + 2])===X){alert(YOU WON!Diag1);返回true; }(b [i] [j]&& b [i + 1] [j-1]& b [i + 2] [j-2])== =X){alert(YOU WON!Diag2);返回true; }

我知道这与dl的值有关,dr不能正确复位,它影响到其他的横向和纵向测试,但我有点失去了一个有效的方法来解决这个问题。

解决方案

你试过了:

 (b [i] [j] ===X&& i === j)? dl ++:0; 
(b [j] [i] ===X&&(i + j)===(len-1))? dr ++:0;



dl 或左对角线将有 i j 相等(so(0, 0)(1,1)和(2,2))

dr 或右对角线将有(0,2)(1,1)等于边长减1的总和 i j (2,0))

当对角线的长度与矩阵的长度相同时,这将起作用,在tic-tac-例如,脚趾游戏。对于部分对角线,您可以稍微修改一下代码:

  var diff = 0; 
var sum = len - 1;
(b [i] [j] ===X)? diff =(i-j):diff = 0;
(b [i] [j] ===X)? sum =(i + j):sum = len;
(b [i] [j] ===X&&(i-j)=== diff)? dl ++:0;
(b [j] [i] ===X&&(i + j)=== sum)? dr ++:0;


Writing a check for an array to determine if there are any potential consecutive values within it, be it horizontal, vertical, or either way diagonal. The example below is a sample diagonal, but I need it working both ways / and \.

Fiddle Away: http://jsfiddle.net/PXPn9/10/

So let's make a pretend scenario...

var b = [ 
    [ 0, 0, X, 0, 0 ]
    [ 0, 0, 0, X, 0 ]
    [ 0, 0, 0, 0, X ]
    [ 0, 0, 0, 0, 0 ]
    [ 0, 0, 0, 0, 0 ]
]

Using a basic 2 level deep loop, which iterates over the whole thing and uses some ternary operators to identify "wins"

function testWin() {
    var win=3, len=b.length, r=0, c=0, dr=0, dl=0;
    for(var i=0;i<len;i++){
        for(var j=0;j<len;j++){

        // COL WIN CHECK //
            (b[j][i]==="X") ? c++ : c=0;

        // ROW WIN CHECK //
            (b[i][j]==="X") ? r++ : r=0;

        // DIAG WIN CHECK //
            // (b[i][j]==="X" && b[i+1][j+1]==="X") ? dr++ : dr=0;
            // (b[j][i]==="X" && b[i+1][j+1]==="X") ? dl++ : dl=0;
        // WIN CHECK FOR ALL 4
            if(c===win || r===win){ alert("YOU WIN!"); return true;}
        }
        r=0;
    }
}

The horizontal check and the vertical check appear to work flawlessly, until I enable the commented attempts to create a diagonal test... Could I have somebody take a look at the diagonal tests and help identify why enabling them breaks everything, and what I have done wrong?

I would like assistance with this in particular to create a diagonal check. (view JSFiddle for whole source)

    // DIAG WIN CHECK //
        // (b[i][j]==="X" && b[i+1][j+1]==="X") ? dr++ : dr=0;
        // (b[j][i]==="X" && b[i+1][j+1]==="X") ? dl++ : dl=0;

Fiddle Away: http://jsfiddle.net/PXPn9/10/


NEW COMMENT I've tried this, for example, but it is hard coded for a diagonal of 3 (I need it to expand later to use the win variable). When I add this though, the bottom right corner of my horizontal and vertical checks fails.

    // if((b[i][j] && b[i+1][j+1] && b[i+2][j+2])==="X"){ alert("YOU WON! Diag1"); return true; }
    // if((b[i][j] && b[i+1][j-1] && b[i+2][j-2])==="X"){ alert("YOU WON! Diag2"); return true; }

I know it has something to do with the values of dl and dr aren't resetting properly, and its affecting the other horizontal and vertical tests, but I'm a tad lost of an effective way to solve it.

解决方案

Have you tried:

(b[i][j]==="X" && i===j) ? dl++ : 0;
(b[j][i]==="X" && (i+j)===(len-1)) ? dr++ : 0;

?

dl or the left diagonal would have i and j equal(so (0,0) (1,1) and (2,2))

dr or the right diagonal would have the sum of i and j equal to side-length minus 1(so (0,2) (1,1) (2,0))

This would work when the length of the diagonal is the same as the length of the matrix, full-body diagonal in a tic-tac-toe game, for example. For partial diagonals you can modify the code slightly to something like:

var diff = 0;
var sum = len - 1;  
(b[i][j]==="X") ? diff = (i-j) : diff = 0;  
(b[i][j]==="X") ? sum= (i+j) : sum = len;  
(b[i][j]==="X" && (i-j)===diff) ? dl++ : 0;
(b[j][i]==="X" && (i+j)===sum) ? dr++ : 0;

这篇关于多维数组 - 检查对角线的连续值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆