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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <div class="container">
  <div class="row info-game">
    <h1 id="player1-info" class="col-xs-3 col-sm-3 col-md-3">Player 1</h1>
    <h1 id="player1-counter" class="col-xs-3 col-sm-3 col-md-3">0</h1>
    <h1 id="player2-counter" class="col-xs-3 col-sm-3 col-md-3">0</h1>
    <h1 id="player2-info" class="col-xs-3 col-sm-3 col-md-3">Player 2</h1>
  </div>
  <div class="row">
    <div id="uno" class="quadrato quadrato-bottom quadrato-right col-xs-4 col-sm-4 col-md-4 col-lg-4">1</div>
    <div id="due" class="quadrato quadrato-bottom quadrato-right col-xs-4 col-sm-4 col-md-4 col-lg-4">2</div>
    <div id="tre" class="quadrato quadrato-bottom col-xs-4 col-sm-4 col-md-4 col-lg-4">3</div>
  </div>
  <div class="row">
    <div id="quattro" class="quadrato quadrato-bottom quadrato-right col-xs-4 col-sm-4 col-md-4 col-lg-4">4</div>
    <div id="cinque" class="quadrato quadrato-bottom quadrato-right col-xs-4 col-sm-4 col-md-4 col-lg-4">5</div>
    <div id="sei" class="quadrato quadrato-bottom col-xs-4 col-sm-4 col-md-4 col-lg-4">6</div>
  </div>
  <div class="row">
    <div id="sette" class="quadrato col-xs-4 quadrato-right col-sm-4 col-md-4 col-lg-4">7</div>
    <div id="otto" class="quadrato col-xs-4 quadrato-right col-sm-4 col-md-4 col-lg-4">8</div>
    <div id="nove" class="quadrato col-xs-4 col-sm-4 col-md-4 col-lg-4">9</div>
  </div>
</div>

<div id="openModal" class="modal">
  <div class="modal-content">
    <div class="modal-title">
      <h3 class="">Start Game</h3>
    </div>
    <p id="presentzione">scegli il numero di giocatori</p>
    <div class="modal-buttons">
      <button id="one-player">one player</button>
      <button id="two-players">two players</button>
    </div>
  </div>
</div>

<div id="chooseModal" class="modal">
  <div class="modal-content">
    <div class="modal-title">
      
      <h3>Select team</h3>
    </div>
    <p id="scelta-team">scegli con chi giocare</p>
    <div class="modal-buttons">
      <button id="back"><i class="fa fa-long-arrow-left" aria-hidden="true"></i></button>
      <button id="scegli-croce">X</button>
      <button id="scegli-cerchio">O</button>
    </div>
  </div>
</div>

<div id="myModal" class="modal">
  <div class="modal-content">
    <div class="modal-title">
      <h3 class="">Fine Partita</h3>
    </div>
    <p id="winner"></p>
    <div class="modal-buttons">
      <button class="reset-all">reset</button>
      <button class="next">next</button>
    </div>
  </div>
</div>

<div id="popup-click"></div>
<div id="total-reset">
  <button id="total-reset-btn">reset all</button>
</div>


<div class="footer">Realizzato da <a id="firma" style="margin-left: 10px" href="https://codepen.io/Laguno/" target="_blank">  Alessandro Messori</a><br> <a href="https://it.linkedin.com/in/alessandromessori" target="_blank"><i class="fa fa-linkedin" aria-hidden="true"></i></a>  <a href="https://mobile.twitter.com/ChicoFmc" target="_blank"><i class="fa fa-twitter" aria-hidden="true"></i></a></div>

</div>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Lato:100,300');
* {
  font-family: 'Lato', sans-serif;
}

h1 {
  color: whitesmoke;
  padding-bottom: 8%;
  font-size: 30px;
}

body {
  /*   background-color: #E94E3D; */
  padding-bottom: 5%;
  background-image: linear-gradient(45deg, #06F8EC, #1AEED7 60%, #28E660);
  background-image: linear-gradient(45deg, #01E1E1, #14EFAB 50%, #2BF142);
}

#player2-info,
#player2-counter {
  text-align: right;
}

#player1-counter {
  text-align: left;
  padding-left: 50px;
}

#player2-counter {
  padding-right: 50px;
}

.prova {
  background: yellow;
}

.container {
  margin: 100px auto 0 auto;
  max-width: 700px;
}

.quadrato {
  color: transparent;
  height: 175px;
  max-height: 175px;
  text-align: center;
}

.quadrato-bottom {
  border-bottom: 3px solid white;
}

.quadrato-right {
  border-right: 3px solid white;
}

.cerchio:before {
  content: attr(data-char);
  width: 80px;
  height: 80px;
  border-top: 3px solid white;
  border-radius: 100%;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  animation: gira .3s normal forwards ease-in;
}

.croce:before {
  content: "x";
  color: whitesmoke;
  font-weight: 100;
  position: absolute;
  font-size: 100px;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  animation: ics .4s normal forwards ease-in;
}

.modal {
  color: whitesmoke;
  display: none;
  position: fixed;
  z-index: 1;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: rgb(255, 255, 255);
  background-color: rgba(255, 255, 255, 0.4);
  text-align: center;
}

button {
  background: none;
  color: whitesmoke;
  border: none;
  cursor: pointer;
  display: block;
  float: left;
  width: 50%;
  font-size: 20px;
  font-family: 'Lato', sans-serif;
}

button:hover,
i:hover {
  text-decoration: underline;
}

#total-reset-btn {
  float: right;
}

#total-reset {
  padding: 20px;
  width: 100%;
}

#popup-click {
  margin: -150px auto;
  width: 20%;
  color: whitesmoke;
  padding: 7px;
  border: 1px solid white;
  border-radius: 25px;
  display: none;
  background: rgba(0, 0, 0, 0.2);
  text-align: center;
  left: 40%;
  position:absolute;
}

.modal-title {
  background: none;
  padding: 10px;
  float: left;
  display: inline;
}

.modal-content {
  background: linear-gradient(to bottom, rgba(191, 210, 85, 1) 0%, rgba(142, 185, 42, 1) 31%, rgba(114, 170, 0, 1) 76%, rgba(114, 170, 0, 1) 76%, rgba(158, 203, 45, 1) 100%);
  background-color: #fefefe;
  margin: 15% auto;
  padding: 10px;
  border: 1px solid #888;
  width: 80%;
  max-width: 500px;
}


#back{
  position:absolute;
  top:10px;
  left: -70px;
}
@keyframes ics {
  0% {
    transform: scale(0) rotatey(0);
  }
  100% {
    transform: scale(1) rotatey(180deg);
  }
}

@keyframes gira {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
    border: 2px solid whitesmoke;
  }
}

.footer {
  
  margin-top: 4em;
  background-color: transparent;
  height: 60px;
  text-align: center;
  font-size: 15px;
  padding: 5px;
  color: greenyellow;
}

#firma {
  color: whitesmoke;
}

.fa-twitter,
.fa-linkedin {
  margin: 5px;
  font-size: 20px;
  color: greenyellow;
}
              
            
!

JS

              
                // COMPUTER AI still under development it is not unbeatable
var quadrato_cliccato;
var segno_computer, segno_umano;
var vs_human, vs_computer;
var player1_counter = 0,
  player2_counter = 0;
var player = 1,
  computer = 1;
var disponibili = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var possible_wins = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
  [1, 4, 7],
  [2, 5, 8],
  [3, 6, 9],
  [1, 5, 9],
  [3, 5, 7]
];
var player1_moves = [],
  player2_moves = [],
  computer_moves = [],
  mossa_calcolata = [],
  player_possible = [],
  comp_possible = [];

function computerMove() {
  var first_possible = [1, 3, 7, 9];
  if (computer == 2) {

    for (var y = 0; y <= disponibili.length; y++) {
      if (disponibili.length == 1 && disponibili[0] == 0) popUpPareggio();
    }

    if (checkAvailability(5)) {
      setTimeout(function() {
        $("#cinque").addClass(segno_computer);
      }, 1000);
      computer_moves.push(5);
      svuotaDisponibili(5);
    } else if (computer_moves.length <= 1 && player1_moves.length == 1) {
      var random_key = randomStart(0, first_possible.length - 1);
      var first_move = first_possible[random_key];
      for (var i = 0; i <= first_possible.length; i++) {
        if (!checkAvailability(first_move)) first_move = first_possible[i];
      }
      computer_moves.push(first_move);
      disegnaSegno(first_move);
      svuotaDisponibili(first_move);

    } else if (computer_moves.length >= 1 && player1_moves.length >= 2) {

      dance: for (var j = 1; j < disponibili.length; j++) {

        mossa_calcolata = [];
        comp_possible = [];
        player_possible = [];

        mossa_calcolata.push(disponibili[j]);
        comp_possible = computer_moves.concat(mossa_calcolata);
        player_possible = player1_moves.concat(mossa_calcolata);

        for (var x = 0; x < possible_wins.length; x++) {


          if (checkWin(comp_possible, possible_wins[x])) {
            
            console.log("primo caso");
            console.log(possible_wins[x]);
            
            disegnaSegno(disponibili[j]);
            svuotaDisponibili(disponibili[j]);
            computer_moves.push(disponibili[j]);

            setTimeout(function() {
              segnalaVittoria(possible_wins[x]);
            }, 1500);

            setTimeout(function() {
              popUpVittoria("computer");
            }, 2000);
            
            player2_counter++;
            $("#player2-counter").text(player2_counter);
            
            break dance;
            
          } else if (checkWin(player_possible, possible_wins[x])) {
            
            console.log("secondo caso");
            console.log(possible_wins[x]);
            disegnaSegno(disponibili[j]);
            computer_moves.push(disponibili[j]);
            svuotaDisponibili(disponibili[j]);
            
            break dance;
            
          } 
           else if (!checkWin(player_possible, possible_wins[x]) && !checkWin(comp_possible, possible_wins[x])) {
            
             console.log("terzo caso");
            var index = possible_wins[x].indexOf(computer_moves[0]);
             console.log("index  "+ index);
             if (possible_wins[x].includes(computer_moves[0]) && index == 1 && checkAvailability(possible_wins[x][0]) && checkAvailability(possible_wins[x][2])) {
              
               
               disegnaSegno(possible_wins[x][0]);
               computer_moves.push(possible_wins[x][0]);
               svuotaDisponibili(possible_wins[x][0]);
              
               break dance;
             }
           }
        }
      }
    }
    possible_wins.forEach(function(element) {
      if (checkWin(computer_moves, element) && computer_moves.length > 2) {
        popUpVittoria("computer");
        player2_counter++;
        $("#player2-counter").text(player2_counter);
      } else if (disponibili.length == 1 && disponibili[0] == 0) popUpPareggio();
    });
  }
  computer = 1;
  playerPulsate();
}

function pulsate(turn) {

  var pulser = $(turn);
  for (var i = 0; i <= 3; i++) {
    pulser.animate({
          opacity: 0.2
        },
        1000, 'linear')
      .animate({
          opacity: 1
        },
        1000, 'linear', pulsate);

  }

}

function playerPulsate() {
  if (player == 1 || computer == 1)
    pulsate("#player1-info");
  else if (player == 2 || computer == 2)
    pulsate("#player2-info");
}

function segnalaVittoria(arr) {
  arr.forEach(function(element) {

    $("div.quadrato").each(function(el) {
      if (element.toString() == $(this).text()) {
        var d = $(this);
        $(d).css("background", "blue");
        setTimeout(function() {
          $(d).css("background", "none");
        }, 1500);
      }
    });
  });
}

function checkWin(arr_player, arr_possible) {
  return arr_possible.every(function(value) {
    return (arr_player.indexOf(value) >= 0);
  });
}

function svuotaDisponibili(item) {
  var index = disponibili.indexOf(item);
  if (index > -1) {
    disponibili.splice(index, 1);
  }
}

function checkAvailability(e) {
  if (disponibili.includes(e)) return true;
  else return false;
}

function popUpVittoria(player) {
  $("#winner").text(player + " wins...");
  showModal("#myModal");
}

function popUpPareggio() {
  $("#winner").text("...draw...");
  showModal("#myModal");
}

function showModal(id) {
  $(id).css("display", "block");
}

function closeModal(id) {
  $(id).css("display", "none");
}

function simpleReset() {
  $("div.quadrato").each(function(element) {
    $(this).removeClass("croce cerchio");
  });
  player1_moves = [];
  player2_moves = [];
  computer_moves = [];
  player_possible = [];
  comp_possible = [];
  mossa_calcolata = [];
  disponibili = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
}

function resetAll() {
  simpleReset();
  player1_counter = 0;
  player2_counter = 0;
  $("#player1-counter, #player2-counter").text(player1_counter.toString());
}

function quadOccupatoPopup() {
  $("#popup-click").html("Quadrato gia occupato");
  $("#popup-click").slideDown(300).delay(800).slideUp(400);
}

function disegnaSegno(item) {
  $("div.quadrato").each(function(element) {

    if ($(this).text() == item.toString()) {
      var c = $(this);
      setTimeout(function() {
        $(c).addClass(segno_computer);
      }, 1000);
    }
  });
}

function randomStart(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function casellaLibera() {
  if (quadrato_cliccato.hasClass("croce") || quadrato_cliccato.hasClass("cerchio"))
    return false;
  else return true;
}
$(document).ready(function() {

  showModal("#openModal");
  //playerPulsate();

  $("#one-player").click(function() {
    vs_computer = true;
    vs_human = false;
    $("#player2-info").html("Computer");
    closeModal("#openModal");
    showModal("#chooseModal");
  });

  $("#two-players").click(function() {
    vs_computer = false;
    vs_human = true;
    computer = 0;
    closeModal("#openModal");
    showModal("#chooseModal");
  });
  $("#scegli-croce").click(function() {
    if (vs_computer) {
      computer = 1;
      segno_umano = "croce";
      segno_computer = "cerchio";
    }
    player = 1;
    closeModal("#chooseModal");
  });
  $("#scegli-cerchio").click(function() {
    if (vs_computer) {
      computer = 2;
      segno_computer = "croce";
      segno_umano = "cerchio";
      computerMove();
    } else {
      player = 2;
    }
    closeModal("#chooseModal");
  });
  $("#back").click(function() {
    closeModal("#chooseModal");
    showModal("#openModal");
  });

  $(".quadrato").click(function() {
    quadrato_cliccato = $(this);

    if (!casellaLibera())
      quadOccupatoPopup();

    else {
      if (player == 1 || computer == 1) {

        if (vs_computer) quadrato_cliccato.addClass(segno_umano);
        else if (vs_human) quadrato_cliccato.addClass("croce");

        player1_moves.push(parseInt(quadrato_cliccato.text()));

        possible_wins.forEach(function(element) {
          if (checkWin(player1_moves, element) && player1_moves.length > 2) {
            segnalaVittoria(element);
            popUpVittoria("player 1");
            player1_counter++;
            $("#player1-counter").text(player1_counter);
          }
        });

        for (var y = 0; y <= disponibili.length; y++) {
          if (disponibili.length == 1 && disponibili[0] == 0) popUpPareggio();
        }

        player = 2;
        playerPulsate();

        if (vs_computer) {
          svuotaDisponibili(parseInt(quadrato_cliccato.text()));
          computer = 2;
          playerPulsate();
          computerMove();
        }
        if (disponibili.length == 1 && disponibili[0] == 0) popUpPareggio();

      } else if (player == 2) {

        quadrato_cliccato.addClass("cerchio");
        player2_moves.push(parseInt(quadrato_cliccato.text()));

        possible_wins.forEach(function(element) {
          if (checkWin(player2_moves, element) && player2_moves.length > 2) {
            segnalaVittoria(element);
            popUpVittoria("player 2");
            player2_counter++;
            $("#player2-counter").text(player2_counter);
          }
        });
        if (disponibili.length == 1 && disponibili[0] == 0) popUpPareggio();
        player = 1;
        playerPulsate();
      }
    }
  });

  $(".reset-all").click(function() {
    resetAll();
    closeModal("#myModal");
  });

  $(".next").click(function() {
    simpleReset();
    closeModal("#myModal");
  });
  $('#total-reset-btn').click(function() {
    location.reload(true);
  });
});
              
            
!
999px

Console