I tried to create a 2D array and place four "X" in diagonal and created a double loop for detecting and counting the number of Xs ,it works but it doesnt do the job properly what it really does is that it counts the number of Xs in every row, my question how can i make it count just the Xs on diagonal ?
var creatematrix = function (nbRang, nbColumn) {
var result = Array(nbRang);
for (var i=0; i<nbRang; i++) {
result[i] = Array(nbColumn);
}
return result;
};
var m = creatematrix(6, 7);
for example if i did this
m[4][1] = "X";
m[3][2] = "X";
m[2][3] = "X";
m[1][4] = "X";
m[5][3] = "O";
m[3][3] = "O";
m[4][3] = "O";
m[2][4] = "O";
or this the result is the same
m[1][1] = "X";
m[2][1] = "X";
m[3][1] = "X";
m[4][1] = "X";
m[5][3] = "O";
m[3][3] = "O";
m[4][3] = "O";
m[2][4] = "O";
Here is what I have:
var sumX = 0;
for(var k = 5; k >= 0 ; k--){
if(sumX == 4){
alert("player 1 won");
}
for(var i = 0 ; i<= 6; i++){
if(m[k][i]== "X" ){
sumX += 1;
break;
}
if(m[k][i]== "O" ){
sumX = 0;
}
}
}