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

              
                <body>
  <div class="full text-center">
    <div id="tittle">TIC-TAC-TOE</div>
    <div id="main">
      <input class="space" id="pos0" pos="0" readonly>
      <input class="space" id="pos1" pos="1" readonly>
      <input class="space" id="pos2" pos="2" readonly>
      <input class="space" id="pos3" pos="3" readonly>
      <input class="space" id="pos4" pos="4" readonly>
      <input class="space" id="pos5" pos="5" readonly>
      <input class="space" id="pos6" pos="6" readonly>
      <input class="space" id="pos7" pos="7" readonly>
      <input class="space" id="pos8" pos="8" readonly>
    </div>
    <div id="score">
      <input id="result" readonly><button class="boton" id="again">🔃</button>
    </div>
    <div id="choose">
      <span>which player do you want to be</span></br>
      <button class="boton welcome" id="playx">X</button><span>or</span><button class="boton  welcome" id="playo">O</button>
    </div>
  
  <div class="row text-center">
  <span class="mama">You: <input class="marca" id="personScore" value="0" readonly></span>
  <span class="mama">Computer: <input class="marca" id="aiScore" value="0"  readonly></span>
 </div>
  </div>



</body>
              
            
!

CSS

              
                
.space {
  border: solid 2px;
  margin-bottom: 4px;
  text-align: center;
  animation-duration: 0.3;
  width: 90px;
  height: 90px;
  font-size: 5em;
  font-weight: bold;
  background-color: #EF5350;
  border-radius: 8px;
  color: #F5F5F5;
}


#main {
  position: static;
  width: 288px;
  height: 288px;
  padding: 5px;
  border-radius: 8px;
  background-color: #F5F5F5;
}

body {
  padding-top: 60px;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #EF5350;
}

#tittle {
  font-family: Century Gothic;
  font-size: 3.5em;
  color: #F5F5F5;
  margin-bottom: 30px;
  font-weight: bold;
}

#result {
  font-weight: bold;
  text-align: center;
  color: #F5F5F5;
  border: none;
  background-color: transparent;
   font-family: Century Gothic;
  font-size: 1.5em;
  width: 150px;
}

.boton {
  text-align: center;
  border: none;
  color:  #EF5350;
  width: 30px;
  height: 30px;
  font-weight: bold;
  border-radius: 8px;
  margin: 8px;
  background-color: #F5F5F5;
}

span {
  font-size: 1.5em;
  color: #F5F5F5;
}

.welcome {
  font-size: 2em;
  border-radius: 15px;
  width: 50px;
  height: 50px;
}

.row {
  font-weight: bold;
  color: #F5F5F5;
}
.marca{
  margin-top:10px;
   width: 26px;
   font-family: Century Gothic;
  margin-right:20px;
   background-color: transparent;
  border:none;
}

              
            
!

JS

              
                var positionsav = [0, 0, 0, 0, 0, 0, 0, 0, 0],
  minimax = [0, 0, 0, 0, 0, 0, 0, 0],
  pause = false,
  winai = 0,
  win = false,
  winperson = 0,
  move = 0;

$(document).ready(function() {
  $("#main").hide();
  $("#score").hide();
  $(".row").hide();
});

//gives user the icon they want
$("#playx").click(function() {
  user = "X";
  ai = "O";
  $("#main").show();
  $("#choose").hide();
  $(".row").show();
});

$("#playo").click(function() {
  user = "O";
  ai = "X";
  $("#main").show();
  $("#choose").hide();
  $(".row").show();
});

//when a space is clicked
$(".space").click(function() {

  //pause ===true means the game is over, and its waiting for the again button, so it wont keep filling the board
  if (pause === false) {
    pos = $(this).attr("pos");
    pos = parseInt(pos);
    pos = available(pos, user);
    //if space is not taken, then ai will do his turn
    if (enemy === true) {
      //while the posicion is empty or the board is not full    

      while (positionsav[pos] !== 0 && minimax.indexOf(0) !== -1) {
        pos = Math.floor(Math.random() * 9);
      }
      available(pos, ai);
    }
  }
});

function terminator(pos, player) {
  if (enemy === true) {
    if (player === ai) {
      var point = -5;
    } else {
      point = 5;
    }
    switch (pos) {
      case 0:
        minimax[0] += point;
        minimax[3] += point;
        minimax[6] += point;
        break;
      case 1:
        minimax[0] += point;
        minimax[4] += point;
        break;
      case 2:
        minimax[0] += point;
        minimax[5] += point;
        minimax[7] += point;
        break;
      case 3:
        minimax[1] += point;
        minimax[3] += point;
        break;
      case 4:
        minimax[1] += point;
        minimax[4] += point;
        minimax[6] += point;
        minimax[7] += point;
        break;
      case 5:
        minimax[1] += point;
        minimax[5] += point;
        break;
      case 6:
        minimax[2] += point;
        minimax[3] += point;
        minimax[7] += point;
        break;
      case 7:
        minimax[2] += point;
        minimax[4] += point;
        break;
      case 8:
        minimax[2] += point;
        minimax[5] += point;
        minimax[6] += point;
        break;
    }

    move = -1;
    enter = -1;
    //here where i give the idea to the ai, i return a sugestion

    switch (move !== "a") {
      case minimax.indexOf(-10) !== -1:
        move = minimax.indexOf(-10);
        break;
      case minimax.indexOf(10) !== -1:
        move = minimax.indexOf(10);
        break;
      case minimax.indexOf(-5) !== -1:
        move = minimax.indexOf(-5);
    }

    if (move >= 0) {
      switch (move) {
        case 0:
          for (i = 0; i < 3; i++) {
            move = positionsav[i];
            if (move === 0) {
              enter = i;
            }
          }
          break;
        case 1:
          for (i = 3; i < 6; i++) {
            move = positionsav[i];
            if (move === 0) {
              enter = i;
            }
          }
          break;
        case 2:
          for (i = 6; i < 9; i++) {
            move = positionsav[i];
            if (move === 0) {
              enter = i;
            }
          }
          break;
        case 3:
          for (i = 0; i <= 6; i = i + 3) {
            move = positionsav[i];
            if (move === 0) {
              enter = i;
            }
          }
          break;
        case 4:
          for (i = 1; i <= 7; i = i + 3) {
            move = positionsav[i];
            if (move === 0) {
              enter = i;
            }
          }
          break;
        case 5:
          for (i = 2; i <= 8; i = i + 3) {
            move = positionsav[i];
            if (move === 0) {
              enter = i;
            }
          }
          break;
        case 6:
          for (i = 0; i <= 8; i = i + 4) {
            move = positionsav[i];
            if (move === 0) {
              enter = i;
            }
          }
          break;
        case 7:
          for (i = 2; i <= 6; i = i + 2) {
            move = positionsav[i];
            if (move === 0) {
              enter = i;
            }
          }
          break;
      }
    }
    return enter;
  }
}

function available(pos, player) {
  if (positionsav[pos] === 0) {
    enemy = true;
    positionsav[pos] = player;
  } else {
    enemy = false;
  }

  //marks the screen with x or o
  if (positionsav[pos] === player) {
    var recomend = terminator(pos, player);
    document.getElementById("pos" + pos).value = player;
  }
  //call function that check if that was a winning move
  winner();
  return recomend;
}

//waits for click to start again
$("#again").click(function() {
  $("#score").hide();
  positionsav = [0, 0, 0, 0, 0, 0, 0, 0, 0];
  minimax = [0, 0, 0, 0, 0, 0, 0, 0];
  $("#pos0,#pos1,#pos2,#pos3,#pos4,#pos5,#pos6,#pos7,#pos8").removeClass("animated flash");
  pause = false;
  $(".row").show();
  win = false;
  for (var i = 0; i < 9; i++) {
    document.getElementById("pos" + i).value = "";
  }
});

function winner() {

  //check if a line won 
  if (minimax.indexOf(-15) !== -1 || minimax.indexOf(15) !== -1) {
    win = true;
  }

  if (win === true) {
    if (positionsav[pos] === ai) {
      document.getElementById("result").value = "Ai Won";
      winai++;
      document.getElementById("aiScore").value = winai;
    } else {
      document.getElementById("result").value = "You Won";
      winperson++;
      document.getElementById("personScore").value = winperson;
    }
    enemy = false;
    $("#score").show();
    pause = true;
    $(".row").hide();
    if (minimax.indexOf(-15) !== -1) {
      line = minimax.indexOf(-15);
    } else {
      line = minimax.indexOf(15);
    }

    switch (line) {
      case 0:
        $("#pos0,#pos1,#pos2").addClass("animated flash");
        break;
      case 1:
        $("#pos3,#pos4,#pos5").addClass("animated flash");
        break;
      case 2:
        $("#pos6,#pos7,#pos8").addClass("animated flash");
        break;
      case 3:
        $("#pos0,#pos3,#pos6").addClass("animated flash");
        break;
      case 4:
        $("#pos1,#pos4,#pos7").addClass("animated flash");
        break;
      case 5:
        $("#pos2,#pos5,#pos8").addClass("animated flash");
        break;
      case 6:
        $("#pos0,#pos4,#pos8").addClass("animated flash");
        break;
      case 7:
        $("#pos2,#pos6,#pos4").addClass("animated flash");
        break;
    }

  } else {
    //check for empty spaces in the board, if they are not then its draw
    if (positionsav.indexOf(0) === -1) {
      document.getElementById("result").value = "Draw";
      $("#score").show();
      $(".row").hide();
      enemy = false;
      pause = true;
    }
  }
}
              
            
!
999px

Console