css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

            
              <!-- Built off of FCC YoutTube Exmaple -->

<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Tic Tac Toe Game</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!--
  <link rel='stylesheet' href='bootstrap_v4.css'>
  <link rel="stylesheet" href="TTC_style.css">
-->
</head>

<body>

  <!--
User Story: I can play a game of Tic Tac Toe with the computer.
User Story: My game will reset as soon as it's over so I can play again.
User Story: I can choose whether I want to play as X or O.
-->

  <!-- 
Colors: 
#00B4F1, rgb(0, 180, 241)
#FF9933, rgb(255, 153, 51)
-->
  <div class="title">
    <sup class="orange">Tic <sub class="blue">Tac </sub>Toe</sup>
  </div>

  <table id="gameBoard">
    <tr>
      <td class="cell" id="0"></td>
      <td class="cell" id="1"></td>
      <td class="cell" id="2"></td>
    </tr>
    <tr>
      <td class="cell" id="3"></td>
      <td class="cell" id="4"></td>
      <td class="cell" id="5"></td>
    </tr>
    <tr>
      <td class="cell" id="6"></td>
      <td class="cell" id="7"></td>
      <td class="cell" id="8"></td>
    </tr>
  </table>
  <div class="endgame">
    <div class="text"></div>
    <button class="replayButton" onClick="startGame()">Replay</button>
  </div>
  
    <div class="toggleDiv">
<p class="blue O">O</p>
      <label class="switch" id="toggle">
  <input type="checkbox" id="XorO">
  <span class="slider round">    
    </span>
    </label><p class="orange X"> X
    <p/>    
  </div>
 
  
  <div class="sigDiv">  <i class="fas fa-chevron-left"></i><i class="fas fa-chevron-right"></i>Built with  <i class="far fa-heart"></i>  by <a class = "blue" href="https://codepen.io/CurtisjCamp/#" target = "_blank">CurtisJCamp</a><i class="fas fa-chevron-left"></i>/<i class="fas fa-chevron-right"></i></div>
  


  <!--
<script src='jQuery_v3.3.js'></script>
<script src='bootstrap_v4.js'></script>
<script src="TTC_script.js"></script>
-->

</body>

</html>
            
          
!
            
              body {
  font-family: "Lato", sans-serif;
  background-color: #fffff0;
  
}

.orange {
  color: #ff9933;
}
.blue {
  color: #00b4f1;
}
.title {
  display: block;
  margin-left: auto;
  margin-right: auto;
  text-align: center;
  font-size: 6em;
}
.toggleText{
  display:inline-block;
  font-size: 4em;
  padding-right: 25px;
}
.O {
  display:inline-block;
  font-size: 4em;
  margin-right: 3px;
}
.X {
  display:inline-block;
  font-size: 4em;
  margin-right: 12px;  
}

#gameBoard {
  margin-bottom: 50px;
  margin-left: auto;
  margin-right: auto;
  border-collapse: collapse;
}

td {
  color: grey;
  border: 5px solid grey;
  height: 100px;
  width: 100px;
  text-align: center;
  vertical-align: middle;
  font-family: "Lato", sans-serif;
  font-size: 60px;
  cursor: pointer;
}

table tr:first-child td {
  border-top: 0;
}

table tr:last-child td {
  border-bottom: 0;
}

table tr td:first-child {
  border-left: 0;
}

table tr td:last-child {
  border-right: 0;
}

.endgame {
  display: none;
  text-decoration: none;
  width: 200px;
  top: 195px;
  background-color: rgba(217, 217, 217, 0.8);
  position: absolute;
  left: 50%;
  margin-left: -100px;
  padding-top: 50px;
  padding-bottom: 50px;
  text-align: center;
  border-radius: 5px;
  color: #4d4d4d;
  font-size: 1.7em;
}
.replayButton{
  font-size: 1.2em;
  border: none;
  background-color: #00B4F1;
  zoom: 1;
	filter: alpha(opacity=70);
	opacity: 0.7;
  color: white;
  box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
}
.replayButton:focus {
  text-decoration: none;
  border: none;
}

 /* The switch - the box around the slider */
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;  
}

/* Hide default HTML checkbox */
.switch input {
  display:none;
}

/* The slider */
.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #00B4F1;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slider {
  background-color: #FF9933;
}

input:focus + .slider {
  box-shadow: 0 0 1px #FF9933;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
} 
.toggleDiv{
  white-space : nowrap; 
  overflow : hidden;
  display: block;
  margin: auto;
  width: 300px;
  height: 105px;
  text-align: center;
  border: 5px solid grey;
  padding-left: 9px;
  margin-bottom: 50px;
}
.sigDiv {
  display: block;
  margin: auto;
  margin-bottom: 10px;
  width: 70%;
  border-radius: 2px;
  background-color: grey;
  text-align: center;
  padding: 30px;
  color: #e6e6e6;
}
a:link {
    color: #FF9933;
    text-decoration: none;
}

a:visited {
    text-decoration: none;
}

a:hover {
    color: #FF9933;
    text-decoration: none;
}

a:active {
    text-decoration: none;
}

            
          
!
            
              //Basic Setup   Determine winner   Basic AI and winner note   minimax algorithm

var origBoard;
var huPlayer = "O";
var aiPlayer = "X";
var tog = true;
var Orangeblue = "#00B4F1"
const winCombos = [
  [0, 1, 2],
  [3, 4, 5],
  [6, 7, 8],
  [0, 3, 6],
  [1, 4, 7],
  [2, 5, 8],
  [0, 4, 8],
  [6, 4, 2]
];

$("#XorO").click(function() {
  if (tog === false) {
    tog = true;
    huPlayer = "O";
    aiPlayer = "X";
    Orangeblue = "#00B4F1";
  } else if (tog === true) {
    tog = false;
    huPlayer = "X";
    aiPlayer = "O";
    Orangeblue = "#FF9933";
  }
});



const cells = document.querySelectorAll(".cell");
startGame();

function startGame() {
  document.querySelector(".endgame").style.display = "none";
  origBoard = Array.from(Array(9).keys());
  for (var i = 0; i < cells.length; i++) {
    cells[i].innerText = "";
    cells[i].style.removeProperty("background-color");
    cells[i].addEventListener("click", turnClick, false);
  }
}

function turnClick(square) {
  $("#toggle").hide(700);
  if (typeof origBoard[square.target.id] == "number") {
    turn(square.target.id, huPlayer);
    if (!checkWin(origBoard, huPlayer) && !checkTie())
      turn(bestSpot(), aiPlayer);
  }
}

function turn(squareId, player) {
  origBoard[squareId] = player;
  document.getElementById(squareId).innerText = player;
  let gameWon = checkWin(origBoard, player);
  if (gameWon) gameOver(gameWon);
}

function checkWin(board, player) {
  let plays = board.reduce((a, e, i) => (e === player ? a.concat(i) : a), []);
  let gameWon = null;
  for (let [index, win] of winCombos.entries()) {
    if (win.every(elem => plays.indexOf(elem) > -1)) {
      gameWon = { index: index, player: player };
      break;
    }
  }
  return gameWon;
}

function gameOver(gameWon) {
  for (let index of winCombos[gameWon.index]) {
    document.getElementById(index).style.backgroundColor =
      gameWon.player == huPlayer ? Orangeblue : "grey";
  }
  for (var i = 0; i < cells.length; i++) {
    cells[i].removeEventListener("click", turnClick, false);
  }
  declareWinner(gameWon.player == huPlayer ? "You Win!" : "You Lose!");
}

function declareWinner(who) {
  document.querySelector(".endgame").style.display = "block";
  document.querySelector(".endgame .text").innerText = who;
  $("#toggle").show(600);
}

function emptySquares() {
  return origBoard.filter(s => typeof s == "number");
}


 
function bestSpot() {
  return minimax(origBoard, aiPlayer).index;
}



function checkTie() {
  if (emptySquares().length === 0) {
    for (var i = 0; i < cells.length; i++) {
      cells[i].style.backgroundColor = Orangeblue;
      cells[i].removeEventListener("click", turnClick, false);
    }
    declareWinner("Tie Game!");
    return true;
  }
  return false;
}

function minimax(newBoard, player) {
  var availSpots = emptySquares();

  if (checkWin(newBoard, huPlayer)) {
    return { score: -10 };
  } else if (checkWin(newBoard, aiPlayer)) {
    return { score: 10 };
  } else if (availSpots.length === 0) {
    return { score: 0 };
  }
  var moves = [];
  for (var i = 0; i < availSpots.length; i++) {
    var move = {};
    move.index = newBoard[availSpots[i]];
    newBoard[availSpots[i]] = player;

    if (player == aiPlayer) {
      var result = minimax(newBoard, huPlayer);
      move.score = result.score;
    } else {
      var result = minimax(newBoard, aiPlayer);
      move.score = result.score;
    }

    newBoard[availSpots[i]] = move.index;

    moves.push(move);
  }

  var bestMove;
  if (player === aiPlayer) {
    var bestScore = -10000;
    for (var i = 0; i < moves.length; i++) {
      if (moves[i].score > bestScore) {
        bestScore = moves[i].score;
        bestMove = i;
      }
    }
  } else {
    var bestScore = 10000;
    for (var i = 0; i < moves.length; i++) {
      if (moves[i].score < bestScore) {
        bestScore = moves[i].score;
        bestMove = i;
      }
    }
  }

  return moves[bestMove];
} 

            
          
!
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.
Loading ..................

Console