Multidimensional Array - Check Diagonal Sequential Values

Writing a check for an array to determine if it has potential sequential values , be it horizontal , vertical or in any case diagonal . Below is an example of a diagonal, but I need it to work in both directions / and \ .

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

So let's make a fake script ...

 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 that iterates through the whole subject and uses several ternary operators to determine the β€œwinnings”

 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; } } 

horizontal check and vertical check seem flawless until I turn on commented attempts to create a diagonal test ... Can I ask someone to look at the diagonal tests and help determine why they allow me to break everything and what I did wrong?

I would like to help with this, in particular, create a diagonal check. (view JSFiddle for the 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 tried this, for example, but it is hardcoded for diagonal 3 (I need it to expand later to use the win variable). When I add this, the lower right corner of my horizontal and vertical tags 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 that this has something to do with dl and dr values ​​that are not being restored correctly, and also affect other horizontal and vertical tests, but I have lost an effective way to solve it.

0
source share
2 answers

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 the side length minus 1 (so (0.2) (1.1) (2.0))

This will work when the length of the diagonal is the same as the length of the matrix, the diagonal of the whole body in a tick-to-nose game. For partial diagonals, you can change the code a bit:

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

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][jz]==="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/

0
source

Source: https://habr.com/ru/post/1011576/


All Articles