JavaScript Tic Tac Toe - 查找数组中所有可能的数字组合

时间:2013-03-28 00:43:15

标签: javascript html css web tic-tac-toe

我目前正在使用网页(HTML,CSS,Javascript)编写基本的Tic Tac Toe游戏(多人游戏,没有人工智能)。游戏逻辑显然都在Javascript中。我有个问题。以下是我的代码。

window.onload = function () {
    console.log("Page has loaded");
}
var ctx;
var turn = 0;
var winningCombo = [
    [1, 4, 7],
    [2, 5, 8],
    [3, 6, 9],
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
    [1, 5, 9],
    [3, 5, 7]
];
var playedComboX = [];
var playedComboO = [];
var filledSquares = [0];
var filled = false;
console.log(winningCombo.length);
var checkWinnerX = function () {
    for(var i = 0; i < winningCombo.length; i++) {
        console.log('Its in the X check winner loop');
        if((winningCombo[i][0] == playedComboX[0]) && (winningCombo[i][1] == playedComboX[1]) && (winningCombo[i][2] == playedComboX[2])) {
            alert("Congrats, you have won!");
            return true;
        }
        return false;
    }
}
var checkWinnerO = function () {
    for(var i = 0; i < winningCombo.length; i++) {
        console.log('Its in the 0 check winner loop');
        if(winningCombo[i][0] == playedComboO[0] && winningCombo[i][1] == playedComboO[1] && winningCombo[i][2] == playedComboO[2]) {
            console.log('It has passed the if statement for X');
            alert("Congrats, you have won!");
            return true;
        }
        return false;
    }
}
var checkWinner = function () {}
var draw = function (squareNumber) {
    console.log('draw has been called');
    var squareID = "square" + squareNumber;
    var squareClicked = document.getElementById(squareID);
    ctx = squareClicked.getContext('2d');
    for(var i = 0; i < filledSquares.length; i++) {
        if(filledSquares[i] == squareNumber) {
            filled = true;
        } else {
            filled = false;
        }
    }
    if(filled == true) {
        alert("Invalid Move! Square is already occupied");
    } else {
        filledSquares.push(squareNumber);
        ctx.beginPath();
        if(turn % 2 == 0) {
            //Drawing a 'X'
            ctx.moveTo(20, 20);
            ctx.lineTo(135, 135);
            ctx.moveTo(135, 20);
            ctx.lineTo(20, 135);
            ctx.lineWidth = 6;
            ctx.stroke();
            turn++;
            playedComboX.push(squareNumber);
            checkWinnerX();
        } else {
            //Drawing a Circle
            ctx.arc(75, 75, 65, 2 * Math.PI, false);
            ctx.lineWidth = 6;
            ctx.stroke();
            turn++;
            playedComboO.push(squareNumber);
            checkWinnerO();
        }
    }
}

目前,checkWinner函数仅检查与winningCombo数组中数组元素的完全匹配(例如:如果组合为3,1,2而不是1,2,3则它不会注册)。无论如何,我可以检查每个元素中3个数字的所有可能组合吗?希望我的解释有意义,谢谢。

PS:请原谅我,如果任何代码写得不像我这样,这是我第一次尝试编写游戏。但要像你希望的那样批评!

更新2:

var checkWinnerX = function () {
    for(var i = 0; i < winningCombo.length; i++) {
        console.log('Its in the X check winner loop');
        if((winningCombo[i][0] == sortedArrayX[0]) && (winningCombo[i][1] == sortedArrayX[1]) && (winningCombo[i][2] == sortedArrayX[2])) {
            alert("Congrats, you have won!");
            return true;
        }
        return false;
    }
}

2 个答案:

答案 0 :(得分:0)

在与winsCombo比较之前对播放的数组进行排序。

playedComboO.sort(function (a, b) {
    return a - b;
});

我认为playedComboO.sort()(和playedComboX.sort())也可以使用,但默认的排序功能是字符串而不是数字。

答案 1 :(得分:0)

我认为更好的方法是:

for(var i = 0; i < winningCombo.length; i++) {
    if ( playedComboO.indexOf(winningCombo[i][0]) >= 0 ) {
        if ( playedComboO.indexOf(winningCombo[i][1]) >= 0 ) {
            if ( playedComboO.indexOf(winningCombo[i][2]) >= 0 ) {
                 //blah blah blah you win whatever
            alert("Congrats, you have won!");
        return true;
            }
       }
    }
}