为什么阵列打印不正确?它不再是一个阵列?

时间:2013-05-01 05:39:05

标签: javascript html5 css3 artificial-intelligence

所以我正在制作一个用户对抗AI的井字游戏。程序员假设他是AI 有几个数组:

myWins包含AI可以获胜的组合。
yourWins包含人类可以获胜的组合 freeWins包含对两者都开放的组合。

每次人类移动时,程序都会调用findWinningCombinations()并传递__wins数组并考虑正方形。这旨在用于AI和人类移动 然后有removeOwnedSquares()也需要考虑__wins和按钮。

重点是:
如果myWins包含长度为1的组合,AI可以在下一步获胜。如果yourWins包含长度为1的组合,则人类可以获胜。

所有内容都基于String的数组。但是,阵列在中途占据阵列。

这是输出:

Human Picked::: 5 Human's Array is:: 46,37,28,19
I Could Not Take 5. My Array Now Is:: 39 
Human Picked::: 3 Human's Array is:: 7,28,19,12   

应该是["7","28","19","12"]

完整代码

<!DOCTYPE html>
<html>
    <title> 1Player </title>
    <style>
        #stage{
            width: 400px;
            height: 400px;
            padding: 0px;
            position: relative;
        }
        .square{
            user-select : none;
            -webkit-user-select: none;
            -moz-user-select: none;

            width: 64px;
            height: 64px;
            background-color: gray;
            position: absolute;
        }

    </style>
    <body>
        <div id="stage">
        </div>
    </body>
    <script>
        const ROWS = 3;
        const COLS = 3;
        const GAP = 10;
        const SIZE = 64;
        var stage = document.querySelector("#stage");

        var lotOfButtons = [];
        var isUsed = [false,false,false,false,
                     false,false,false,false,false];


        var myPositions = "";
        var yourPositions = "";
        var youPicked = "";
        var iPicked = "";
        var isX = true;
        var isFirstMove = true;


        var myWins = [];
        var yourWins = [];
        var freeWins = ["123","159","147",
                        "258",
                        "369","357",
                        "456",
                        "789"];

        prepareBoard();
        stage.addEventListener("click",clickHandler,false);


        function prepareBoard(){ //Prepare the board on which the users will play.
            for(var row = 0;row<ROWS;row++){ // Prepare the rows.
            for(var col = 0;col < COLS;col++){ //Prepare the columns.
                var square = document.createElement("button"); //Prepare the button which represents a square.
                square.setAttribute("class","square"); //Make it a square 'officially'.
                stage.appendChild(square); //Add the square to the play area..
                lotOfButtons.push(square); //Keep a record of squares.
                square.style.left = col * (SIZE+GAP) + "px"; //Properly set the horizontal spacing.
                square.style.top = row * (SIZE+GAP) + "px"; //Properly set the vertical spacing.
            }
            }
        }

        function clickHandler(event){
            var targetIndex = lotOfButtons.indexOf(event.target);
            if(targetIndex === -1 || !isX || isUsed[targetIndex]){
                return;
            }
            isX = false;
            isUsed[targetIndex] = true;

            event.target.style.backgroundImage = "url(../img/X.PNG)";

            yourMove(targetIndex+1);
            myMove();
        }

        function yourMove(buttonIndex){

            yourWins = findWinningCombinations(yourWins,buttonIndex);
            yourWins = removeOwnedSquares(yourWins,buttonIndex);
            console.log("Human Picked::: " + buttonIndex + " Human's Array is:: " + yourWins);
            if(myWins.length != 0){
                myWins = removeCommonCombinations(myWins,buttonIndex);
            }
        }

        function findWinningCombinations(combinationArray,targetIndex){
            for(var i = (freeWins.length-1);i >= 0;i--){
                if(freeWins[i].indexOf(targetIndex) !== -1){
                    combinationArray.push(freeWins[i]);
                    freeWins.splice(i,1);
                }
            }
            return combinationArray;
        }

        function removeOwnedSquares(combinationArray,targetIndex){
            for(var i = (combinationArray.length-1); i >= 0;i--){
                if(combinationArray[i].indexOf(targetIndex) != -1){
                    combinationArray[i] = combinationArray[i].replace(targetIndex,"");
                }
            }
            return combinationArray;
        }

        function myMove(){
            if(isUsed[4] === false){ //TAKE 5
                isFirstMove = false;
                isX = true;
                isUsed[4] = true;
                var fifthSquare = lotOfButtons[4];
                fifthSquare.style.backgroundImage = "url(../img/O.PNG)";

                myWins = findWinningCombinations(myWins,5);
                myWins = removeOwnedSquares(myWins,5);
                yourWins = removeCommonCombinations(yourWins,5);

                console.log("I Took 5. My Array Now Is:: " + myWins);

            }else if(isUsed[4] === true && isFirstMove){//FIVE NOT AVAILABLE ?? TAKE A RANDOM SQUARE.
                while(true){
                    var randomNumber = Math.round(Math.random()*8);
                    if(isUsed[randomNumber] === false){
                        isX = true;
                        isFirstMove = false;
                        isUsed[randomNumber] = true;
                        lotOfButtons[randomNumber].style.backgroundImage = "url(../img/O.PNG)";
                        myWins = findWinningCombinations(myWins,randomNumber+1);
                        myWins = removeOwnedSquares(myWins,randomNumber+1);
                        yourWins = removeCommonCombinations(yourWins,randomNumber+1);
                        break;
                    }
                }
                console.log("I Could Not Take 5. My Array Now Is:: " + myWins);
            }else{//PLAY USING STRATEGY.
                //TODO add logic here
            }
        }

        function removeCommonCombinations(combinationArray,someIndex){
            if(combinationArray.length === 0){
                return;
            }

            for(var i = (combinationArray.length-1);i >= 0;i--){
                if(combinationArray[i].indexOf(someIndex) != -1){
                    combinationArray.splice(i,1);
                }
            }
            return combinationArray;
        }
    </script>
</html>  

为什么会这样?

0 个答案:

没有答案