Comparing arrays to determine win conditions
I'm trying to create a simple tic-tac-toe game but I've run into a problem. I created a 2D array called winConditions to be compared with xMoves and oMoves to determine when someone has won, but nothing seems to be happening when I call my determineWin function. I'm really bad with arrays and scope so I'm assuming the problem lies with an incorrect scope issue but this is a bit over my head. Could somebody help determine the problem with this code? http://jsfiddle.net/Skww6/1/ (This is the function that I probably have my mistake in)
var determineWin = function (pMoves) {
var winAlert;
for (var i = 0; i < winConditions.length; i++) {
if (winConditions[i].length > pMoves.length) {
continue;
}
for (var j = 0; j < winConditions[i].length; j++) {
winAlert = false;
for (var k = 0; k < pMoves.length; k++) {
if (pMoves[k] === winConditions[i][j]) {
winAlert = true;
break;
}
}
if (!winAlert) break;
}
if (winAlert) {
alert(currentPlayer + "wins!");
break;
}
}
};
I'm trying to create a simple tic-tac-toe game but I've run into a problem. I created a 2D array called winConditions to be compared with xMoves and oMoves to determine when someone has won, but nothing seems to be happening when I call my determineWin function. I'm really bad with arrays and scope so I'm assuming the problem lies with an incorrect scope issue but this is a bit over my head. Could somebody help determine the problem with this code? http://jsfiddle.net/Skww6/1/ (This is the function that I probably have my mistake in)
var determineWin = function (pMoves) {
var winAlert;
for (var i = 0; i < winConditions.length; i++) {
if (winConditions[i].length > pMoves.length) {
continue;
}
for (var j = 0; j < winConditions[i].length; j++) {
winAlert = false;
for (var k = 0; k < pMoves.length; k++) {
if (pMoves[k] === winConditions[i][j]) {
winAlert = true;
break;
}
}
if (!winAlert) break;
}
if (winAlert) {
alert(currentPlayer + "wins!");
break;
}
}
};
No comments:
Post a Comment