123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              
<html>

    <head>
        <title>FCC Tic-Tac-Toe Game</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" type="text/css" href="FccTicTacToe.css">
    </head>

    <body>
        <div class="title">Tic-Tac-Toe</div>
        
        <div class="menuclass">
            <div class="menuwrap">
                <div class="columnwrap">
                    <p class="line">Player 1</p>
                    <div class="line"><input type="radio" id="player1x" name="player1" value="X" checked="checked">X</div>
                    <div class="line"><input type="radio" id="player1o" name="player1" value="O">O</div>
                </div>
                <div class="columnwrap">
                    <p class="line">Player 2</p>
                    <div class="line"><input type="radio" id="computer" name="player2" value="computer" checked="checked">computer</div>
                    <div class="line"><input type="radio" id="human" name="player2" value="human">human</div>
                </div>
            </div>
            <p class="turn">X goes first</p>
            <button class="menu" id="start">START</button>
            <button class="menu" id="reset">RESET</button>
        </div>
        <div class="boardwrap">
            <div class="row">
                <div class="btn" id="btn4" value=4>&nbsp</div><!--
                --><div class="btn" id="btn3" value=3>&nbsp</div><!--
                --><div class="btn" id="btn8" value=8>&nbsp</div>
            </div>
            <div class="row">
                <div class="btn" id="btn9" value=9>&nbsp</div><!--
                --><div class="btn" id="btn5" value=5>&nbsp</div><!--
                --><div class="btn" id="btn1" value=1>&nbsp</div>
            </div>
            <div class="row">
                <div class="btn" id="btn2" value=2>&nbsp</div><!--
                --><div class="btn" id="btn7" value=7>&nbsp</div><!--
                --><div class="btn" id="btn6" value=6>&nbsp</div>
            </div>
        </div>
        <script src="jquery-3.1.0.js"></script>
        <script src="FccTicTacToe.js"></script>
    </body> 

</html>
            
          
!
            
              body {
    background-color: black;
    margin: 20px; 
    text-align: center; 
    font-family: Chalkduster, fantasy; 
    color: #e6e6e6; 
    cursor: default; }

.title {
    font-size: 50px; }

.menuclass {
    max-width: 100%;
    margin: 0px auto 20px auto; }

.columnwrap {
    margin: 0px 40px 5px 40px;
    max-width: 50%;
    text-align: left;
    display: inline-block; }

.line {
    display: block; }

input {
    cursor: pointer; }

.menu {
    display: inline;
    cursor: pointer;
    font-family: Chalkduster, fantasy; 
    font-size: 15px;
    color: #ccc;
    background-color: #555; }

.boardwrap {
    margin: 30px 0px 50px 0px; }

.btn {
    cursor: default;
    font-size: 80px;
    border-style: solid;
    width: 100px;
    height: 100px; 
    background-color: rgba(0,0,0,0);
    display: inline-block; }

@media all and (max-width: 410px) {
    .btn {
        font-size: 40px;
        width: 50px;
        height: 50px; }
    
    .columnwrap {
        font-size: 15px;
        margin: 0px 10px 10px 10px; }
}

#btn4 { border-width: 0px 1px 1px 0px; }
#btn3 { border-width: 0px 1px 1px 1px; }
#btn8 { border-width: 0px 0px 1px 1px; }
#btn9 { border-width: 1px 1px 1px 0px; }
#btn5 { border-width: 1px; }
#btn1 { border-width: 1px 0px 1px 1px; }
#btn2 { border-width: 1px 1px 0px 0px; }
#btn7 { border-width: 1px 1px 0px 1px; }
#btn6 { border-width: 1px 0px 0px 1px; }
            
          
!
            
              $(document).ready(function () {
    'use strict';
    
    var turn, turnstaken = 0, spot = 1, opponent, player1, player2, gameinprogress = false, player1picks = [], player2picks = [], usedspots = [];
    
    function reset() { //resets everything for a new game
        $("#player1x, #computer").prop("checked", true);
        $(".btn").html("&nbsp");
        $(".btn").css("cursor", "default");
        $("input, #start").css("cursor", "pointer");
        $("input, #start").prop("disabled", false);
        gameinprogress = false;
        turnstaken = 0;
        player1picks = [];
        player2picks = [];
        usedspots = [];
        spot = 1;
        $(".turn").html("X goes first");
    } //end reset();
    
    function checkforwinner(array) { //this cycles through the array to see if any of the three numbers add up to 15, which would give a winner
        var i, j, k;
        for (i = 0; i < array.length; i++) {
            for (j = i + 1; j < array.length; j++) {
                for (k = j + 1; k < array.length; k++) {
                    if (array[i] + array[j] + array[k] === 15) {
                        return true;
                    }
                }
            }
        }
        return false;
    } //end checkforwinner()

    function checkifused(x) { //checks if current spot clicked has already been used
        var i;
        for (i = 0; i < usedspots.length; i++) {
            if (x === usedspots[i]) {
                return true;
            }
        }
        return false;
    } //end checkifused
    
    function findspot(array) { //this finds what move to do, looks for first available in best moves, only used in computerturn()
        var i, j, move;
        for (i = 0; i < array.length; i++) {
            for (j = 0; j < usedspots.length; j++) {
                if (!checkifused(array[i])) {
                    move = array[i];
                    return move;
                }
            }
        }
    } //end findspot()
    
    function endofturn(array) {
        turnstaken += 1;
        if (checkforwinner(array)) { //computer wins this turn
            alert(turn + " wins!");
            reset();
        } else if (usedspots.length === 9) { //all spots useds - its a tie
            alert("It's a Tie");
            reset();
        } else { //no winner - no tie
            if (turn === "Player 2") { //@ end of player 2's turn
                $(".turn").html("Player 1's turn"); //changes display of whose turn it is next
                turn = "Player 1"; //changes whose turn it is, so next click will go to correct section of js
            } else { //@ end of player 1's turn
                $(".turn").html("Player 2's turn"); //changes display of whose turn it is next
                turn = "Player 2"; //changes whose turn it is, so next click will go to correct section of js   
                if (opponent === "computer") {
                    computerturn();
                }
            }
        }
    } //end endofturn()
    
    function computerturn() {
        var i, j, k, l, move, bestmoves = [];

        for (i = 0; i < player2picks.length; i++) { //this pushes any winning moves to an array
            for (j = i + 1; j < player2picks.length; j++) {
                if (15 - player2picks[i] - player2picks[j] > 0 && 15 - player2picks[i] - player2picks[j] < 10) {
                    bestmoves.push(15 - player2picks[i] - player2picks[j]);
                }
            }
        } //end find winning moves
        
        for (i = 0; i < player1picks.length; i++) { //this pushes any blocking moves to an array
            for (j = i + 1; j < player1picks.length; j++) {
                if (15 - player1picks[i] - player1picks[j] > 0 && 15 - player1picks[i] - player1picks[j] < 10) {
                    bestmoves.push(15 - player1picks[i] - player1picks[j]);
                }
            }
        } //end find blocking moves

        if (turnstaken === 1) {
            bestmoves.push(5);
            do {
                move = 1 + Math.floor(Math.random() * 9);
            } while (move % 2 !== 0); //this gets random even# between 1&9 for cpu to pick random corner
            bestmoves.push(move);
        }
        
        if (turnstaken === 3) {
            if (player1picks[1] % 2 === 0 && (player1picks[0] === 5 || player1picks[1] === 5)) { //meaning player 1's 2nd pick is a corner
                do { //this gets random corner
                    move = 1 + Math.floor(Math.random() * 9);
                } while (move % 2 !== 0 || (checkifused(move)));
                bestmoves.push(move);
            } else {
                do { //this gets random edge
                    move = 1 + Math.floor(Math.random() * 9);
                } while (move % 2 === 0 && move !== 5);
                bestmoves.push(move);
            }
        }

        do { //this pushes a random unused spot to the array to ensure a move is made
            move = 1 + Math.floor(Math.random() * 9);
        } while (checkifused(move));
        bestmoves.push(move);

        spot = findspot(bestmoves);
        player2picks.push(spot);
        usedspots.push(spot);
        $("#btn" + spot).html(player2); //this fills in the board with X or O
        endofturn(player2picks);
    } //end computerturn
    
    $("#start").click(function () { //sets variables when start is clicked
        if (!gameinprogress) {
            gameinprogress = true;
            player1 = $('input:radio[name=player1]:checked').val();
            opponent = $('input:radio[name=player2]:checked').val();
            if (player1 === "X") {
                player2 = "O";
            } else { //player1 === "O"
                player2 = "X";
            } //end if/else
            $(".btn").css("cursor", "pointer");
            $("input, #start").css("cursor", "default");
            $("input, #start").attr('disabled', 'disabled');
            if (player2 === "X" && opponent === "computer") { //this is the first turn of the game (X goes first)
                do {
                    spot = 1 + Math.floor(Math.random() * 9);
                } while (spot % 2 !== 0); //this gets random even# between 1&9 for cpu's first turn, ...so it picks a corner
                turnstaken += 1;
                player2picks.push(spot);
                usedspots.push(spot);
                $("#btn" + spot).html("X");
                $(".turn").html("Player 1's turn");
                turn = "Player 1";
            } else if (player2 === "X" && opponent === "human") {
                $(".turn").html("Player 2's turn");
                turn = "Player 2";
            } else { //player1 === "X"
                $(".turn").html("Player 1's turn");
                turn = "Player 1";
            } //end first turn of game
        } //end if(!gameinprogress)
    }); //end $("#start").click
    
    $("#reset").click(function () { //calls reset when clicked
        reset();
    }); //end $("#reset").click
    
    $(".btn").click(function () {
        if (gameinprogress) { //can only click spots if gameinprogress === true
            spot = parseInt($(this).attr("value")); //gives value of what box was clicked
            
            if (!checkifused(spot)) { //this happens if the spot clicked is not already used
                if (turn === "Player 1") { //this means its player1's turn and they just picked a spot
                    player1picks.push(spot);
                    usedspots.push(spot);
                    $("#btn" + spot).html(player1); //this fills in the board with X or O
                    endofturn(player1picks);
                } else if (turn === "Player 2") { //this means its player2's turn and they just picked a spot
                    player2picks.push(spot);
                    usedspots.push(spot);
                    $("#btn" + spot).html(player2); //this fills in the board with X or O
                    endofturn(player2picks);
                } //end of turn
            } //end if(!checkifused(current))
        } //end if(gameinprogress)
    }); //end $(".btn").click
}); //end $(document).ready
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console