Javascript Tic Tac Toe游戏:我如何防止;它';“是平局”;当所有磁砖都填满时,当获胜时发出警报?

Javascript Tic Tac Toe游戏:我如何防止;它';“是平局”;当所有磁砖都填满时,当获胜时发出警报?,javascript,html,Javascript,Html,这是我使用HTML5画布编写的Tic-Tac-Toe游戏代码: //Global Variables var filled; var content; var winningCombos; var turn = 0; var tilesFilled = 0; window.onload=function(){ filled = new Array(); //An array for checkin

这是我使用HTML5画布编写的Tic-Tac-Toe游戏代码:

    //Global Variables
    var filled;
    var content;
    var winningCombos;
    var turn = 0;
    var tilesFilled = 0;


    window.onload=function(){

        filled = new Array();           //An array for checking if the tile contains content (Xs or Os) to prevent it from being reused
        content = new Array();          //An array for checking what the tile content is (Xs or Os) to determine a winner
        winningCombos = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]];  //A dual array of the 8 different winning combos (3 across, 3 down, 2 diagonal)
        for(var l = 0; l <= 8; l++){        //A for loop, which starts out as unfilled and blank, to assign values to the tiles incrementally
        filled[l] = false;
        content[l]='';
        }
    }       

    //The Canvas and Tiles Function
    function canvasClicked(canvasNumber){               //This will get the number from canvasClicked in the html body
        var theTile = "canvas"+canvasNumber;            //And then append “canvas” to the number. This equals the canvas id.
        var c = document.getElementById(theTile);
        var ctx = c.getContext("2d");               //This is adding 2d context to theTile (canvas id)

        // Filling in the Tiles
        if(filled[canvasNumber-1] ==false){         //If the tile is empty

            if(turn%2==0){                  //Determine whose turn it is by the modulo operator

                ctx.beginPath();            //Drawing the X     
                ctx.moveTo(20,20);
                ctx.lineTo(80,80);
                ctx.moveTo(80,20);
                ctx.lineTo(20,80);
                ctx.lineWidth = 2;
                ctx.strokeStyle="palevioletred";
                ctx.stroke();
                ctx.closePath();
                content[canvasNumber-1] = 'X';
            }

            else{                       //If it isn’t Xs turn, then it’s Os turn

                ctx.beginPath();            //Drawing the O     
                ctx.arc(50,50,40,0,2*Math.PI);
                ctx.lineWidth = 2;
                ctx.strokeStyle="skyblue";
                ctx.stroke();
                ctx.closePath();
                content[canvasNumber-1] = 'O';  
            }

            turn++;                     //Alternate Xs and Os turn by using the increment operator, starting with 0 in the global variable turn
            filled[canvasNumber-1] = true;
            tilesFilled++;                  //Checks whether all tiles are filled yet, again with the increment operator, starting at 0 in the variable tilesFilled 
            checkForWinners(content[canvasNumber-1]);   //Check for winners

            if(tilesFilled==9) {                //If there is no winner after all tiles are filled start a new game
                alert("It's a Tie!");
                location.reload(true);
            }

        }

    }

    function checkForWinners(symbol){

        for(var i = 0; i < winningCombos.length; i++){  //This for loop will go through the 8 winningCombos

        //This will go through all the winningCombos to see whether the 0,1,2 within the index have the same content (Xs or Os)
        if(content[winningCombos[i][0]]==symbol&&content[winningCombos[i][1]]==symbol&&content[winningCombos[i][2]]==symbol){
            alert(symbol+ " WON THE GAME!");
            location.reload(true);
        }
        }

    }

    </script>

</head>

<body>
    <h1>Tic Tac Toe</h1>
        <canvas id = "canvas1"  width="100" height="100" onclick="canvasClicked(1)"></canvas>
        <canvas id = "canvas2"  width="100" height="100" onclick="canvasClicked(2)"></canvas>
        <canvas id = "canvas3"  width="100" height="100" onclick="canvasClicked(3)"></canvas><br/>
        <canvas id = "canvas4"  width="100" height="100" onclick="canvasClicked(4)"></canvas>
        <canvas id = "canvas5"  width="100" height="100" onclick="canvasClicked(5)"></canvas>
        <canvas id = "canvas6"  width="100" height="100" onclick="canvasClicked(6)"></canvas><br/>
        <canvas id = "canvas7"  width="100" height="100" onclick="canvasClicked(7)"></canvas>
        <canvas id = "canvas8"  width="100" height="100" onclick="canvasClicked(8)"></canvas>
        <canvas id = "canvas9"  width="100" height="100" onclick="canvasClicked(9)"></canvas>
</body>
//全局变量
var填充;
var含量;
var winningCombos;
var-turn=0;
var tilesFilled=0;
window.onload=function(){
filled=new Array();//用于检查磁贴是否包含内容(Xs或Os)以防止重复使用的数组
content=new Array();//用于检查磁贴内容(Xs或Os)以确定赢家的数组
winningCombos=[[0,1,2]、[3,4,5]、[6,7,8]、[0,3,6]、[1,4,7]、[2,5,8]、[0,4,8]、[2,4,6];//8个不同获胜组合的双重数组(3个交叉、3个向下、2个对角)

对于(var l=0;l我看不出在哪里打印它是平局。但是我会更改此方法调用:

    //Global Variables
    var filled;
    var content;
    var winningCombos;
    var turn = 0;
    var tilesFilled = 0;


    window.onload=function(){

        filled = new Array();           //An array for checking if the tile contains content (Xs or Os) to prevent it from being reused
        content = new Array();          //An array for checking what the tile content is (Xs or Os) to determine a winner
        winningCombos = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]];  //A dual array of the 8 different winning combos (3 across, 3 down, 2 diagonal)
        for(var l = 0; l <= 8; l++){        //A for loop, which starts out as unfilled and blank, to assign values to the tiles incrementally
        filled[l] = false;
        content[l]='';
        }
    }       

    //The Canvas and Tiles Function
    function canvasClicked(canvasNumber){               //This will get the number from canvasClicked in the html body
        var theTile = "canvas"+canvasNumber;            //And then append “canvas” to the number. This equals the canvas id.
        var c = document.getElementById(theTile);
        var ctx = c.getContext("2d");               //This is adding 2d context to theTile (canvas id)

        // Filling in the Tiles
        if(filled[canvasNumber-1] ==false){         //If the tile is empty

            if(turn%2==0){                  //Determine whose turn it is by the modulo operator

                ctx.beginPath();            //Drawing the X     
                ctx.moveTo(20,20);
                ctx.lineTo(80,80);
                ctx.moveTo(80,20);
                ctx.lineTo(20,80);
                ctx.lineWidth = 2;
                ctx.strokeStyle="palevioletred";
                ctx.stroke();
                ctx.closePath();
                content[canvasNumber-1] = 'X';
            }

            else{                       //If it isn’t Xs turn, then it’s Os turn

                ctx.beginPath();            //Drawing the O     
                ctx.arc(50,50,40,0,2*Math.PI);
                ctx.lineWidth = 2;
                ctx.strokeStyle="skyblue";
                ctx.stroke();
                ctx.closePath();
                content[canvasNumber-1] = 'O';  
            }

            turn++;                     //Alternate Xs and Os turn by using the increment operator, starting with 0 in the global variable turn
            filled[canvasNumber-1] = true;
            tilesFilled++;                  //Checks whether all tiles are filled yet, again with the increment operator, starting at 0 in the variable tilesFilled 
            checkForWinners(content[canvasNumber-1]);   //Check for winners

            if(tilesFilled==9) {                //If there is no winner after all tiles are filled start a new game
                alert("It's a Tie!");
                location.reload(true);
            }

        }

    }

    function checkForWinners(symbol){

        for(var i = 0; i < winningCombos.length; i++){  //This for loop will go through the 8 winningCombos

        //This will go through all the winningCombos to see whether the 0,1,2 within the index have the same content (Xs or Os)
        if(content[winningCombos[i][0]]==symbol&&content[winningCombos[i][1]]==symbol&&content[winningCombos[i][2]]==symbol){
            alert(symbol+ " WON THE GAME!");
            location.reload(true);
        }
        }

    }

    </script>

</head>

<body>
    <h1>Tic Tac Toe</h1>
        <canvas id = "canvas1"  width="100" height="100" onclick="canvasClicked(1)"></canvas>
        <canvas id = "canvas2"  width="100" height="100" onclick="canvasClicked(2)"></canvas>
        <canvas id = "canvas3"  width="100" height="100" onclick="canvasClicked(3)"></canvas><br/>
        <canvas id = "canvas4"  width="100" height="100" onclick="canvasClicked(4)"></canvas>
        <canvas id = "canvas5"  width="100" height="100" onclick="canvasClicked(5)"></canvas>
        <canvas id = "canvas6"  width="100" height="100" onclick="canvasClicked(6)"></canvas><br/>
        <canvas id = "canvas7"  width="100" height="100" onclick="canvasClicked(7)"></canvas>
        <canvas id = "canvas8"  width="100" height="100" onclick="canvasClicked(8)"></canvas>
        <canvas id = "canvas9"  width="100" height="100" onclick="canvasClicked(9)"></canvas>
</body>
checkForWinners(content[canvasNumber-1]);
返回布尔值,如果调用该方法中的警报,则为真

    //Global Variables
    var filled;
    var content;
    var winningCombos;
    var turn = 0;
    var tilesFilled = 0;


    window.onload=function(){

        filled = new Array();           //An array for checking if the tile contains content (Xs or Os) to prevent it from being reused
        content = new Array();          //An array for checking what the tile content is (Xs or Os) to determine a winner
        winningCombos = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]];  //A dual array of the 8 different winning combos (3 across, 3 down, 2 diagonal)
        for(var l = 0; l <= 8; l++){        //A for loop, which starts out as unfilled and blank, to assign values to the tiles incrementally
        filled[l] = false;
        content[l]='';
        }
    }       

    //The Canvas and Tiles Function
    function canvasClicked(canvasNumber){               //This will get the number from canvasClicked in the html body
        var theTile = "canvas"+canvasNumber;            //And then append “canvas” to the number. This equals the canvas id.
        var c = document.getElementById(theTile);
        var ctx = c.getContext("2d");               //This is adding 2d context to theTile (canvas id)

        // Filling in the Tiles
        if(filled[canvasNumber-1] ==false){         //If the tile is empty

            if(turn%2==0){                  //Determine whose turn it is by the modulo operator

                ctx.beginPath();            //Drawing the X     
                ctx.moveTo(20,20);
                ctx.lineTo(80,80);
                ctx.moveTo(80,20);
                ctx.lineTo(20,80);
                ctx.lineWidth = 2;
                ctx.strokeStyle="palevioletred";
                ctx.stroke();
                ctx.closePath();
                content[canvasNumber-1] = 'X';
            }

            else{                       //If it isn’t Xs turn, then it’s Os turn

                ctx.beginPath();            //Drawing the O     
                ctx.arc(50,50,40,0,2*Math.PI);
                ctx.lineWidth = 2;
                ctx.strokeStyle="skyblue";
                ctx.stroke();
                ctx.closePath();
                content[canvasNumber-1] = 'O';  
            }

            turn++;                     //Alternate Xs and Os turn by using the increment operator, starting with 0 in the global variable turn
            filled[canvasNumber-1] = true;
            tilesFilled++;                  //Checks whether all tiles are filled yet, again with the increment operator, starting at 0 in the variable tilesFilled 
            checkForWinners(content[canvasNumber-1]);   //Check for winners

            if(tilesFilled==9) {                //If there is no winner after all tiles are filled start a new game
                alert("It's a Tie!");
                location.reload(true);
            }

        }

    }

    function checkForWinners(symbol){

        for(var i = 0; i < winningCombos.length; i++){  //This for loop will go through the 8 winningCombos

        //This will go through all the winningCombos to see whether the 0,1,2 within the index have the same content (Xs or Os)
        if(content[winningCombos[i][0]]==symbol&&content[winningCombos[i][1]]==symbol&&content[winningCombos[i][2]]==symbol){
            alert(symbol+ " WON THE GAME!");
            location.reload(true);
        }
        }

    }

    </script>

</head>

<body>
    <h1>Tic Tac Toe</h1>
        <canvas id = "canvas1"  width="100" height="100" onclick="canvasClicked(1)"></canvas>
        <canvas id = "canvas2"  width="100" height="100" onclick="canvasClicked(2)"></canvas>
        <canvas id = "canvas3"  width="100" height="100" onclick="canvasClicked(3)"></canvas><br/>
        <canvas id = "canvas4"  width="100" height="100" onclick="canvasClicked(4)"></canvas>
        <canvas id = "canvas5"  width="100" height="100" onclick="canvasClicked(5)"></canvas>
        <canvas id = "canvas6"  width="100" height="100" onclick="canvasClicked(6)"></canvas><br/>
        <canvas id = "canvas7"  width="100" height="100" onclick="canvasClicked(7)"></canvas>
        <canvas id = "canvas8"  width="100" height="100" onclick="canvasClicked(8)"></canvas>
        <canvas id = "canvas9"  width="100" height="100" onclick="canvasClicked(9)"></canvas>
</body>
然后更改此选项:

    //Global Variables
    var filled;
    var content;
    var winningCombos;
    var turn = 0;
    var tilesFilled = 0;


    window.onload=function(){

        filled = new Array();           //An array for checking if the tile contains content (Xs or Os) to prevent it from being reused
        content = new Array();          //An array for checking what the tile content is (Xs or Os) to determine a winner
        winningCombos = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]];  //A dual array of the 8 different winning combos (3 across, 3 down, 2 diagonal)
        for(var l = 0; l <= 8; l++){        //A for loop, which starts out as unfilled and blank, to assign values to the tiles incrementally
        filled[l] = false;
        content[l]='';
        }
    }       

    //The Canvas and Tiles Function
    function canvasClicked(canvasNumber){               //This will get the number from canvasClicked in the html body
        var theTile = "canvas"+canvasNumber;            //And then append “canvas” to the number. This equals the canvas id.
        var c = document.getElementById(theTile);
        var ctx = c.getContext("2d");               //This is adding 2d context to theTile (canvas id)

        // Filling in the Tiles
        if(filled[canvasNumber-1] ==false){         //If the tile is empty

            if(turn%2==0){                  //Determine whose turn it is by the modulo operator

                ctx.beginPath();            //Drawing the X     
                ctx.moveTo(20,20);
                ctx.lineTo(80,80);
                ctx.moveTo(80,20);
                ctx.lineTo(20,80);
                ctx.lineWidth = 2;
                ctx.strokeStyle="palevioletred";
                ctx.stroke();
                ctx.closePath();
                content[canvasNumber-1] = 'X';
            }

            else{                       //If it isn’t Xs turn, then it’s Os turn

                ctx.beginPath();            //Drawing the O     
                ctx.arc(50,50,40,0,2*Math.PI);
                ctx.lineWidth = 2;
                ctx.strokeStyle="skyblue";
                ctx.stroke();
                ctx.closePath();
                content[canvasNumber-1] = 'O';  
            }

            turn++;                     //Alternate Xs and Os turn by using the increment operator, starting with 0 in the global variable turn
            filled[canvasNumber-1] = true;
            tilesFilled++;                  //Checks whether all tiles are filled yet, again with the increment operator, starting at 0 in the variable tilesFilled 
            checkForWinners(content[canvasNumber-1]);   //Check for winners

            if(tilesFilled==9) {                //If there is no winner after all tiles are filled start a new game
                alert("It's a Tie!");
                location.reload(true);
            }

        }

    }

    function checkForWinners(symbol){

        for(var i = 0; i < winningCombos.length; i++){  //This for loop will go through the 8 winningCombos

        //This will go through all the winningCombos to see whether the 0,1,2 within the index have the same content (Xs or Os)
        if(content[winningCombos[i][0]]==symbol&&content[winningCombos[i][1]]==symbol&&content[winningCombos[i][2]]==symbol){
            alert(symbol+ " WON THE GAME!");
            location.reload(true);
        }
        }

    }

    </script>

</head>

<body>
    <h1>Tic Tac Toe</h1>
        <canvas id = "canvas1"  width="100" height="100" onclick="canvasClicked(1)"></canvas>
        <canvas id = "canvas2"  width="100" height="100" onclick="canvasClicked(2)"></canvas>
        <canvas id = "canvas3"  width="100" height="100" onclick="canvasClicked(3)"></canvas><br/>
        <canvas id = "canvas4"  width="100" height="100" onclick="canvasClicked(4)"></canvas>
        <canvas id = "canvas5"  width="100" height="100" onclick="canvasClicked(5)"></canvas>
        <canvas id = "canvas6"  width="100" height="100" onclick="canvasClicked(6)"></canvas><br/>
        <canvas id = "canvas7"  width="100" height="100" onclick="canvasClicked(7)"></canvas>
        <canvas id = "canvas8"  width="100" height="100" onclick="canvasClicked(8)"></canvas>
        <canvas id = "canvas9"  width="100" height="100" onclick="canvasClicked(9)"></canvas>
</body>
if(tilesFilled==9) { 

    //Global Variables
    var filled;
    var content;
    var winningCombos;
    var turn = 0;
    var tilesFilled = 0;


    window.onload=function(){

        filled = new Array();           //An array for checking if the tile contains content (Xs or Os) to prevent it from being reused
        content = new Array();          //An array for checking what the tile content is (Xs or Os) to determine a winner
        winningCombos = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]];  //A dual array of the 8 different winning combos (3 across, 3 down, 2 diagonal)
        for(var l = 0; l <= 8; l++){        //A for loop, which starts out as unfilled and blank, to assign values to the tiles incrementally
        filled[l] = false;
        content[l]='';
        }
    }       

    //The Canvas and Tiles Function
    function canvasClicked(canvasNumber){               //This will get the number from canvasClicked in the html body
        var theTile = "canvas"+canvasNumber;            //And then append “canvas” to the number. This equals the canvas id.
        var c = document.getElementById(theTile);
        var ctx = c.getContext("2d");               //This is adding 2d context to theTile (canvas id)

        // Filling in the Tiles
        if(filled[canvasNumber-1] ==false){         //If the tile is empty

            if(turn%2==0){                  //Determine whose turn it is by the modulo operator

                ctx.beginPath();            //Drawing the X     
                ctx.moveTo(20,20);
                ctx.lineTo(80,80);
                ctx.moveTo(80,20);
                ctx.lineTo(20,80);
                ctx.lineWidth = 2;
                ctx.strokeStyle="palevioletred";
                ctx.stroke();
                ctx.closePath();
                content[canvasNumber-1] = 'X';
            }

            else{                       //If it isn’t Xs turn, then it’s Os turn

                ctx.beginPath();            //Drawing the O     
                ctx.arc(50,50,40,0,2*Math.PI);
                ctx.lineWidth = 2;
                ctx.strokeStyle="skyblue";
                ctx.stroke();
                ctx.closePath();
                content[canvasNumber-1] = 'O';  
            }

            turn++;                     //Alternate Xs and Os turn by using the increment operator, starting with 0 in the global variable turn
            filled[canvasNumber-1] = true;
            tilesFilled++;                  //Checks whether all tiles are filled yet, again with the increment operator, starting at 0 in the variable tilesFilled 
            checkForWinners(content[canvasNumber-1]);   //Check for winners

            if(tilesFilled==9) {                //If there is no winner after all tiles are filled start a new game
                alert("It's a Tie!");
                location.reload(true);
            }

        }

    }

    function checkForWinners(symbol){

        for(var i = 0; i < winningCombos.length; i++){  //This for loop will go through the 8 winningCombos

        //This will go through all the winningCombos to see whether the 0,1,2 within the index have the same content (Xs or Os)
        if(content[winningCombos[i][0]]==symbol&&content[winningCombos[i][1]]==symbol&&content[winningCombos[i][2]]==symbol){
            alert(symbol+ " WON THE GAME!");
            location.reload(true);
        }
        }

    }

    </script>

</head>

<body>
    <h1>Tic Tac Toe</h1>
        <canvas id = "canvas1"  width="100" height="100" onclick="canvasClicked(1)"></canvas>
        <canvas id = "canvas2"  width="100" height="100" onclick="canvasClicked(2)"></canvas>
        <canvas id = "canvas3"  width="100" height="100" onclick="canvasClicked(3)"></canvas><br/>
        <canvas id = "canvas4"  width="100" height="100" onclick="canvasClicked(4)"></canvas>
        <canvas id = "canvas5"  width="100" height="100" onclick="canvasClicked(5)"></canvas>
        <canvas id = "canvas6"  width="100" height="100" onclick="canvasClicked(6)"></canvas><br/>
        <canvas id = "canvas7"  width="100" height="100" onclick="canvasClicked(7)"></canvas>
        <canvas id = "canvas8"  width="100" height="100" onclick="canvasClicked(8)"></canvas>
        <canvas id = "canvas9"  width="100" height="100" onclick="canvasClicked(9)"></canvas>
</body>
if(noWinnersAlertBoolean && tilesFilled==9) { 
//全局变量
var填充;
var含量;
var winningCombos;
var-turn=0;
var tilesFilled=0;
window.onload=function(){
filled=new Array();//用于检查磁贴是否包含内容(Xs或Os)以防止重复使用的数组
content=new Array();//用于检查磁贴内容(Xs或Os)以确定赢家的数组
winningCombos=[[0,1,2]、[3,4,5]、[6,7,8]、[0,3,6]、[1,4,7]、[2,5,8]、[0,4,8]、[2,4,6];//8个不同获胜组合的双重数组(3个交叉、3个向下、2个对角)
对于(var l=0;l