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 includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

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

              
                <main>
  
  <div class="container">
    <h1>Tic Tac Toe</h1>
    <p>Choose your marker and opponent to begin</p>
    <!-- THIS SHOULD BE ANIMATED -->
    <div class="player-start">
      <div id="player-select-container">
         <h3>Choose marker</h3>
         <button class="js-player-select playerX" id="player-X" value="X">X</button> 
         
         <button class="js-player-select playerO" id="player-O" value="O">O</button>
      </div>
      <div id="opponent-select-container" class="js-hidden">
        <h3>Choose opponent</h3>
        <button class="opponent AI" value="AI">COMPUTER</button> 
        <button class="opponent HUMAN" value="HUMAN">HUMAN</button>
      </div>
      <button class="js-reset" id="reset">RESET</button> 
    </div>
    <!-- END animated div -->
    <div class="js-gameboard" id="gameboard"></div>

  </div>
</main>
<footer>
  <h2>Built by Jack</h2>
</footer>
<script src="js/index.js"></script><!-- </html> -->
              
            
!

CSS

              
                /* Page styles */
html {
  background: -webkit-linear-gradient(90deg, #dee1e1 10%, #f4f4f4 90%); /* Chrome 10+, Saf5.1+ */ 
  background: -moz-linear-gradient(90deg, #dee1e1 10%, #f4f4f4 90%); /* FF3.6+ */background: linear-gradient(90deg, #dee1e1 10%, #f4f4f4 90%); /* W3C */
  height: 100vh;
  position: relative;
}
h1, h2, h3, h4, h5, p {
  margin: 0;
}
body {
  font-family: Tahoma;
  font-size: 20px;
  margin: 0; /* bottom = footer height */
}
footer {
  bottom: 2px;  
  height: 40px;  
  margin-top: 40px;  
  text-align: center;  
  vertical-align: middle; 
  position: relative;
  width:100%;
}
.container {
  text-align: center;
}
/* END Page styles */

/* Buttons */
button {
  background: white;
  cursor: pointer;
  margin: .5em;
  padding: 1em;
}
.playerX {
  margin-right: 1em;
}
.playerO {
  margin-left: 1em;
}
.js-reset {
  margin: 1em auto;
}
button:hover, .tile:hover  {
  background: #D7D6D7;
  cursor: pointer;
  -webkit-transition: .3s ease;
          transition: .3s ease;
}
/* END Buttons */

/* Gameboard */
.js-gameboard {
  box-shadow: 1px 1px 1px 1px rgba(0, 0, 0, 0.45);
  height: 14.65em;
  margin: 0 auto;
  width: 15em;
}
.tile {
    width: 5em;
    height: 5em;
    border: 1px solid black;
    background-color: white;
    display: inline-block;
    margin: -1px;
    margin-bottom: -5px
}
.checked:after {
  content: attr(data-check);
  font-size: 3em;
  line-height: 1.5em;
  padding: .4em;
} 
.active-tile {
  pointer-events: auto;
}
.inactive-tile {
  pointer-events: none;
}

/* Player Start UI */
.player-start {
  margin: .5em auto;
  width: 75%;
  padding: .5em;
}
#opponent-select-container {
  display: none;
}
/* END Player Start UI */
              
            
!

JS

              
                /* TO DO
=> Learn the minimax algorithm
=> refactor
*/
// Set global player variables
var opponent,
  player_mark;
/* Set winning tiles */
var winners = [
  [0, 1, 2],
  [3, 4, 5],
  [6, 7, 8],
  [0, 3, 6],
  [1, 4, 7],
  [2, 5, 8],
  [0, 4, 8],
  [6, 4, 2]
];
var opponent_button_container = document.getElementById("opponent-select-container"),
      opponent_buttons          = document.querySelectorAll(".opponent"),
      player_button_container   = document.getElementById("player-select-container"),
      player_buttons            = document.querySelectorAll(".js-player-select"),
      gameboard                 = document.querySelector("#gameboard");

document.getElementById("reset").addEventListener("click", reset);
/* Initialise app */
initialize();
/* * * * * 
 * Logic *
 * * * * */
function initialize() {
    /* Create the gameboard and tiles */
    var tiles = Array(9).fill();
    var player_turn = 1,
      player_mark_O = [],
      player_mark_X = [];
    // Initialise button styles
    player_button_container.setAttribute("style", "display: inline-block, opacity: 1;");
    // opponent_button_container.style.display = "none";
    /* CREATE AI
    => maybe there could be a ranking based on whether the computer checkmark is within the winner array
       1. SCENARIO with computer X starting:
       - X picks tile 6 (i.e) gameboard[6], then the winning arrays include the following indexes: winners[2], winners[3], winners[5].
       - I put an O in tile 4 (i.e) gameboard[4].
       - Now the computer cannot use winners[5] but the following are still available: winners[2], winners[3].
       - Computer X now chooses winners[2] and randomly picks tile 7 or 8 within that array using Math.random()
       - I select tile 8, making winners[2] and winners[5] impossible and only winners[3] left.
       - Computer select tile 3 or tile 0 from winners[3] 
       - If I block him now, it's basically a tied game at best, Math.random() all available tiles to finish the game.
       
         |   |
       0 | 1 | 2   winners = [0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [6,4,2] 
      ___|___|___
         |   |  
       3 | 4 | 5
      ___|___|___
         |   | 
       6 | 7 | 8
         |   |    
    */
    /* 
     * Setup classes and append to div forEach tile
     */
  function markTiles(that, player, opponent, array, index) {
    that.dataset.check = player;
    that.classList.remove("active-tile");
    that.classList.add("inactive-tile");
    player_mark = opponent;
    array.push(index);
    // Check which tile was clicked to compae with winning tiles 
    console.log(`PLAYER X: ${array}`);
  }
  
  function markRandomTiles(that, AI, array) {
    player_turn++;
    that.dataset.check = AI;
    that.classList.remove("active-tile");
    that.classList.add("inactive-tile");
    console.log("a");
    console.log(array);
    console.log("b");
    console.log(array.length);
    array.push(1);
    console.log(array);
    console.log("c");
  }

    tiles.forEach((tile, i, arr) => {
      tile = document.createElement("div")
      tile.setAttribute("class", "tile checked active-tile");
      
      // Add each tile to the gameboard
      gameboard.appendChild(tile);
      // Add X's and O's
      tile.addEventListener("click", function() {
        // Increment player_turn to see what round we are up to    
        player_turn++;
        if (opponent === "HUMAN") {
          // Alternate between X & O
           (player_mark === "X") 
             ? markTiles(this, "X", "O", player_mark_X, i) 
             : markTiles(this, "O", "X", player_mark_O, i); 
        } // END human V human if statement
        /* Begin Human V Computer Battle */
        else if (opponent === "AI") {
            if (player_mark === "X")  {
              
              markTiles(this, "X", "O", player_mark_X, i);
              
              var activeTiles = document.querySelectorAll(".active-tile"),
                  randomTile = activeTiles[Math.floor(Math.random() * activeTiles.length)];
               console.log(activeTiles);
               markRandomTiles(randomTile, "O", player_mark_O);
               //player_mark_O.push(randomTile);
              console.log(player_mark_O);
              // player_mark_O.push(randomTile);
              // console.log(player_mark_O.push(randomTile));
              player_mark = "X";
            
            
            } else {
             
             markTiles(this, "O", "X", player_mark_X, i);
              
             
               markRandomTiles(randomTile, "X", player_mark_X);
              player_mark = "O";
           }
        } 
        
        /* 
         * Find a winning match
         */
        winners.forEach((winner) => {
          // Check winner array against player array
          function sort_winner(winner, player) {
            return winner.every((el) => {
              return player.indexOf(el) !== -1;
            });
          }
          
          // Alert the correct winner
          function alert_winner(player) {
            swal({
              title: `PLAYER ${player} WINS`,
              type: 'success',
            });
            reset();
          }
          
          // If a match is found alert and restart
          if (sort_winner(winner, player_mark_O)) {
            alert_winner("O");
          } else if (sort_winner(winner, player_mark_X)) {
            alert_winner("X");
          }
          
        });
        // TIE game & restart when all boxes are full
        player_turn === 10 && reset();
        
      }); // Close tile event listeners 
    }); // Close tiles forEach function
  } // Close initialize function


  /* 
   * Fade out buttons
   */
function fadeOut(element) {
  var opacity = 1,
    speed = 18;
  // decrease opacity over time
  function decrease() {
    opacity -= 0.05;
    if (opacity <= 0) {
      //reveal opponent button
      opponent_button_container.setAttribute("style", "display: block; opacity: 1;");
      // compvare fadOut for both buttons
      element.style.display = "none";
      return true;
    }
    element.style.opacity = opacity;
    setTimeout(decrease, speed);
  }
  decrease();
}

  /* * * * * * * * * * 
   * Player buttons  *
   * * * * * * * * * */
function player_and_opponent(el, selection, container) {
  return el.forEach((item) => {
    item.addEventListener("click", function() {
      // set global variables (yes, i know this is sketchy :\ )
      window[selection] = item.value;
      fadeOut(container);
    });
  });
}

// Call player buttons
player_and_opponent(player_buttons, "player_mark", player_button_container);
player_and_opponent(opponent_buttons, "opponent", opponent_button_container);
console.log("testing", player_mark);

/* Reset the game by removing & replacing tiles */
function reset() {
  // Reinitialize, however this creates 2 boards => see remove_tiles hack below.
  initialize();
  remove_tiles('tile');
  // Remove excess tiles to ensure 9 will always show
  function remove_tiles(className) {
    var elements = document.getElementsByClassName(className);
    while (elements.length > 9) {
      elements[0].parentNode.removeChild(elements[0]);
    }
  }
} // Close reset function

              
            
!
999px

Console