Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                  <div class="container">
    <div class="col-xs-6 col-xs-offset-2 main" id="main">
      <div class="row header">
        <h1 class="text-center">Tic Tac Toe</h1>
      </div>
      <div class="row gameBox" id="gameBox">
        <div class="row">
          <div class="col-xs-8 col-xs-offset-2">
            <h3 class="text-center" id="msgBox"></h3>
          </div>
        </div>
        <div class="row">
          <div class="col-xs-8 col-xs-offset-2 game" id="game">
          </div>
        </div>
        <div class="row footer" id="footer">
          Chris Dodds | CC Share-Alike 2016
        </div>
      </div>
              
            
!

CSS

              
                body {
  background-color: #086788;
  color: #fff;
  font-family: 'Francois One', sans-serif;
}

h1 {
  font-size: 5em;
}

.main {
  margin-top: 10%;
  background-color: #086788;
  padding-bottom: 20px;
  min-width:760px;
}

.header {
  margin: 5px 0px 10px 0px;
}

.gameBox {
  min-height: 350px;
  margin-bottom: 20px;
  padding:  0px;
}

.game {
  margin-top: 40px;
  min-height:200px;
}

.btn {
  font-size:3em;
  min-width:120px;
}

#playingField {
  height:400px;
  border-radius:5px;
}


.userSelected {
  background-color: #07a0c3;
  color: #000;
}

.compSelected {
  background-color: #f0c808;
  color: #000;
}

.selected {
  pointer-events: none;
}

.cell {
   height: 150px;
   text-align: center;
   font-size:4.6em;
   border-radius:5px;
   padding-top: 30px;
   margin: 15px 0px;
}

.footer {
  margin-top: 20px;
  text-align:center;
}
              
            
!

JS

              
                $(document).ready(function () {
    // vars
    var userChoice = '';
    var computerChoice = '';
    var userClicks = [];
    var computerClicks = [];
    var turn = true;
    var choice = '';
    var sClass = '';
    var availableCells = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    var startChoice = "<div class='col-md-6 text-center'><button class='btn btn-success btn-lg' id='startButton'><i class='glyphicon glyphicon-play'></i> Yes</button></div><div class='col-md-6 text-center'><button class='btn btn-lrg btn-danger btn-lg' id='refuseButton'><i class='glyphicon glyphicon-ban-circle'></i> No</button></div>"
    var xOrOchoice = "<div class='col-md-6 text-center'><button class='btn btn-success btn-lg' id='xChoice'>X</button></div><div class='col-md-6 text-center'><button class='btn btn-lrg btn-danger btn-lg' id='oChoice'>O</button></div>"
    var playingField = "<div id='playingField'><div class='row'><div class='col-xs-4'><div class='cell' id='1'>--</div></div><div class='col-xs-4'><div class='cell' id='2'>--</div></div><div class='col-xs-4'><div class='cell' id='3'>--</div></div></div><div class='row'><div class='col-xs-4'><div class='cell' id='4'>--</div></div><div class='col-xs-4'><div class='cell' id='5'>--</div></div><div class='col-xs-4'><div class='cell' id='6'>--</div></div></div><div class='row'><div class='col-xs-4'><div class='cell' id='7'>--</div></div><div class='col-xs-4'><div class='cell' id='8'>--</div></div><div class='col-xs-4'><div class='cell' id='9'>--</div></div></div></div>";
    var drawText = "<div class='text-center'><h3>Game Over. It's a draw.</h3><hr><button class='btn btn-danger' id='tryAgain'>Try Again?</button></div>";
    var winText = "<div class='text-center'><h3>You win!</h3><hr><button class='btn btn-success' id='tryAgain'>Play again?</button></div>";
    var loseText = "<div class='text-center'><h3>You lose.</h3><hr><button class='btn btn-danger' id='tryAgain'>Play again?</button></div>";
    var combos = [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9],
        [1, 4, 7],
        [2, 5, 8],
        [3, 6, 9],
        [1, 5, 9],
        [7, 5, 3]
    ];

    initialState();


    /////////////////////// FUNCTIONS ///////////////////////

    //load initial state
    function initialState() {

        $("#playingField").hide();
        $("#msgBox").text("Ready to play?");
        $("#game").html(startChoice)
        $("#startButton").click(startGame);
        $("#refuseButton").click(doNotStart);
        resetScores();

    }

    //start game
    function startGame() {
        userScore = 0;
        computerScore = 0;
        xOrO();
    }

    // don't start game
    function doNotStart() {
        alert("Y U NO WANT PLAY?");
    }

    // x or o?
    function xOrO() {
        $("#msgBox").text("Want to be X or O?");
        $("#game").html(xOrOchoice);
        $("button#xChoice").click(function () {
            userChoice = 'X';
            drawBoard();

        });
        $("button#oChoice").click(function () {
            userChoice = 'O';
            drawBoard();
        });
        if (userChoice = 'X') {
            computerChoice = 'O';
        } else {
            computerChoice = 'X';
        }
    }


    // who starts?
    function whoStarts() {
        var x = (Math.floor(Math.random() * 2) == 0);
        if (x) {
            $("#msgBox").text("It's your turn.");
            turn = 1;

        } else {
            $("#msgBox").text("It's the robot's turn.");
            turn = 0;

        }

    }

    // draw game board
    function drawBoard() {
        $("#game").html(playingField);
        whoStarts();
        registerClick();

    }

    // register click
    function registerClick() {
        defineClick();
        $("div.cell").click(function () {
            $(this).text(choice);
            $(this).addClass(sClass);
            if (!$(this).hasClass('selected')) {
                $(this).addClass('selected');
            }
            removeFromOptions(this);
            addSelectedToPlayer(this);
            
            flipTurn();
            defineClick();
            drawCheck();
            winCheck();
            loseCheck()
            robotTurn();
        })
    }

    // robot turn
    function robotTurn(){
        while (turn == 0){
            var pick = availableCells[Math.floor(Math.random() * (availableCells.length))];
            $("#" + pick).addClass('compSelected');
            $("#" + pick).addClass('selected');
            $("#" + pick).text(choice);
            removeFromOptions(pick);
            flipTurn();
            defineClick();
            turn = 1;
        }
    }

    // define which click type
    function defineClick() {
        if (turn == 1) {
            choice = userChoice;
            sClass = 'userSelected'
        } else {
            choice = computerChoice;
            sClass = 'compSelected'
        }
    }

    // removed selected cell from available options
    function removeFromOptions(val) {
        var cellId = Number($(val).attr('id'));
        var option = availableCells.indexOf(cellId);
        if (option != -1) {
            availableCells.splice(option, 1);
        }
    }

    // add selected cells to an array to check for win
    function addSelectedToPlayer(val) {
        if (turn == 1) {
            userClicks.push(Number($(val).attr('id')))
        } else {
            computerClicks.push($(val).attr('id'))
        }
    }

    // flip turn
    function flipTurn() {
        if (turn === 0) {
            turn = 1;
            $("#msgBox").text("It's your turn.");
        } else {
            (turn = 0);
            $("#msgBox").text("It's the robot's turn.");
        }
    }


    // check for a draw
    function drawCheck() {
        setTimeout(function () {
            if (availableCells.length == 0) {
                $("#msgBox").empty();
                $("#game").html(drawText).fadeIn("slow");
                $("#tryAgain").click(initialState);
            }
        }, 3000)
        clearTimeout();
    }

    // check for a win
    function winCheck() {
        userClicks.sort();
        for (var i = 0; i < combos.length; i++) {
            var win = combos[i].every(function (val) {
                return userClicks.indexOf(val) >= 0;
            });
            if (win) {
                winReset();
            }
        }
    }

    // check for loss
    function loseCheck() {
        computerClicks.sort();
        for (var i = 0; i < combos.length; i++) {
            var lose = combos[i].every(function (val) {
                return computerClicks.indexOf(val) >= 0;
            });
            if (lose) {
                loseReset();
            }
        }
    }

    // reset on win
    function winReset() {
        $("#msgBox").empty();
        $("#game").html(winText).fadeIn("slow");
        $("#tryAgain").click(initialState);
    }

    // reset on loss
    function loseReset() {
        $("#msgBox").empty();
        $("#game").html(loseText).fadeIn("slow");
        $("#tryAgain").click(initialState);
    }

    // var reset scores
    function resetScores() {
        availableCells = [1, 2, 3, 4, 5, 6, 7, 8, 9];
        userClicks = [];
        computerClicks = [];
    }



})
              
            
!
999px

Console