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.

13
  • 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 do it. Commented Jan 9, 2014 at 3:17
  • Hi Nicholas, You already asked for this days ago, and I gave you a working answer Commented Jan 9, 2014 at 3:18
  • Yes you did Edgar, and I am expanding on it and trying to build my knowledge by utilizing ternary operators. The problem with your answer was that it forced the horizontal and vertical and diagonal check across the entire board, and now I am expanding to make it a limited version, and also to check for internal diagonals that are not the length of the board. Commented Jan 9, 2014 at 3:22
  • I will certainly do that, as your post is what made me expand into this solution in the first place :-) Commented Jan 9, 2014 at 3:25
  • Thanks. Let me see what I can do here :) Commented Jan 9, 2014 at 3:26

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

6 Comments

The problem with that is it could be, for example, [0,3],[1,4],[2,5]
@NicholasHazel Yeah, I have added a possible fix for that. See my last edit
Oh, I see where you're going... Hmm, direct port didn't work, but I see the logical pathway. Let me play tomorrow. Thanks for the answer, I think you have my solution bottled up and I just need to snap the cork. Will mark as accepted tomorrow if I get it going!
I see, I believe this will totally work for me, but I have to identify what is breaking the horizontal/vertical/diagonal check in one go. It appears to be giving me false positives.
@NicholasHazel I may have 'cork'ed the problem. Same logic, slightly different implementation. See jsfiddle.net/tewathia/Y7VUQ/5
|
0

Final answer (due to @tewathia's answer and some further research)

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++){
            (b[j][i]==="X") ? c++ : c=0;
            (b[i][j]==="X") ? r++ : r=0;
            if(b[i][j]==="X" && i<len-win+1){ dr=0; dl=0;
                for(var z=0;z<win;z++){ 
                    (b[i+z][j+z]==="X") ? dr++ : dr=0;
                    (b[i+z][j-z]==="X") ? dl++ : dl=0;
                }
            }
            if(c===win || r===win || dr===win || dl===win){ alert("YOU WIN!"); return true;}
        } r=0;
    }
}

Fiddle Here: http://jsfiddle.net/atk3V/1/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.