簡體   English   中英

JavaScript Tic Tac Toe-查找數組中數字的所有可能組合

[英]JavaScript Tic Tac Toe - Finding All Possible Combinations of Numbers within an Array

我目前正在使用Web(HTML,CSS,Javascript)編寫基本的Tic Tac Toe游戲(多人游戲,沒有AI)。 游戲邏輯顯然全部在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;
    }
}

在與winningCombo比較之前,對播放的數組進行排序。

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

我認為playedComboO.sort() (和playedComboX.sort() )也可以工作,但是默認的排序功能是按字符串而不是數字。

我認為一種更好的方法是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM