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

              
                <html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>tic tac toe</title>
  <link rel="stylesheet" href="styles.css">
  <link href="https://fonts.googleapis.com/css?family=Roboto+Mono:500" rel="stylesheet">

</head>
<body>
  <h1>tic tac toe</h1>
  <div class="game-container">
    <div id="one" class="grid"></div>
    <div id="two" class="grid"></div>
    <div id="three" class="grid"></div>
    <div id="four" class="grid"></div>
    <div id="five" class="grid"></div>
    <div id="six" class="grid"></div>
    <div id="seven" class="grid"></div>
    <div id="eight" class="grid"></div>
    <div id="nine" class="grid"></div>
  </div>
  <div class="console">

  </div>


  <script src="app.js"></script>
</body>
</html>

              
            
!

CSS

              
                *{
  box-sizing: border-box;
  margin:0;
  padding:0;
}
body{
  font-family: 'Roboto Mono', monospace;
  text-align: center;
  color:hsl(120, 87%, 50%);
  background-color: #000000;
  background-color: #000000;
  background-image: url("https://www.transparenttextures.com/patterns/medic-packaging-foil.png");
  /* This is mostly intended for prototyping; please download the pattern and re-host for production environments. Thank you! */
}
h1{
  margin: 20px;
}

.game-container{
  margin:0 auto;
  height:280px;
  width:280px;
  display:flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  align-content:center;
  border:groove white 4px;
  border-radius: 15px;
  background-color: rgba(0, 0, 0, 0.00);	position: relative;	box-shadow: 0px 0px 49px -7px rgba(42, 247, 32, 0.90);
}


.grid{
  width:90px;
  height:90px;
  background-color:rgba(0,0,0,0.3);
  border:4px solid hsl(120, 87%, 50%);
  display:flex;
  align-items: center;
  justify-content: center;
  font-size: 50px;
  user-select: none;
}

.console{
  background-color: black;
  width:100%;
  height:30%;
  position:absolute;
  bottom:0;
  margin:0 auto;
  display:flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  transition: transform 2s;
}
span{
  padding:20px;
}

.icon{
  font-size: 50px;
  cursor: pointer;
  user-select: none;
  transition: all 0.5s;
}

#one{
  border-top:none;
  border-left: none;
}
#two{
  border-top: none;
}
#four{
  border-left:none;
}
#seven{
  border-left:none;
  border-bottom: none;
}
#eight{
  border-bottom: none;
}
#nine{
  border-bottom: none;
  border-right:none;
}
#six{
  border-right: none;
}
#three{
  border-right: none;
  border-top: none;
}

              
            
!

JS

              
                const consoleWindow = document.querySelector('.console');
consoleWindow.innerHTML =

`
  <span>Choose your weapon...</span>
  <span><span class="icon eggplant">🍆</span> OR <span class="icon corn">🌽</span></span>

  `;

const eggplant = document.querySelector('.eggplant');
const corn = document.querySelector('.corn');

corn.addEventListener('click', selectIcon);
eggplant.addEventListener('click', selectIcon);

let playerTurn;

function coinFlip() {
    return Math.floor(Math.random() * 2)+1;
}

function selectIcon(e){

  if(this.textContent ==='🍆'){
    player1 = '🍆';
    player2 = '🌽';
    this.style.position = 'fixed';
    this.style.top = '30%';
    this.style.width = '75%';
    this.style.left = '50%';
    this.style.marginLeft = '-37.5%';
    this.style.transform = 'scale(4) rotate(1080deg)';
    consoleWindow.style.top ='-1000px';
    this.addEventListener('transitionend', function(){
      this.style.transition='all 0.3s';
      this.style.transitionDelay = '0.3s'
      this.style.opacity = '0';
      this.addEventListener('transitionend', function(){
        this.style.visibility='hidden';
      })
    });
  } else{
    player1 = '🌽';
    player2 = '🍆';
    this.style.position = 'fixed';
    this.style.top = '30%';
    this.style.width = '75%';
    this.style.left = '50%';
    this.style.marginLeft = '-37.5%';
    this.style.transform = 'scale(4) rotate(1080deg)';
    consoleWindow.style.top ='-1000px';
    this.addEventListener('transitionend', function(){
      this.style.transition='all 0.7s';
      this.style.transitionDelay = '0.7s'
      this.style.opacity = '0';
      this.addEventListener('transitionend', function(){
        this.style.visibility='hidden';
      })
    });
  }
  playerTurn = coinFlip();
  console.log(playerTurn);

  if(playerTurn === 1){
    console.log('player 1 is up');
  } else{
    console.log('player 2 is up');
    computerResponse();
  }
  console.log(player1);

}

// Select Grids and Place icon
const grids = Array.from(document.querySelectorAll('.grid'));

grids.forEach(function(grid){
  grid.addEventListener('click', punchGrid)
})

function punchGrid(){
  if(playerTurn===undefined){
    console.log('Select an emoji before playing!');
  } else if(playerTurn===1){
    this.textContent=player1;
    this.classList.add('p1');
    computerResponse();
  }
  else{
    console.log('Player 2 is First!');
    computerResponse();
  }
}

function computerResponse(){
  let availGrids = grids.filter(grid=>{
    if(!grid.classList.contains('p1')){
      return grid;
    }
  });
  availGrids = availGrids.filter(grid=>{
    if(!grid.classList.contains('p2')){
      return grid;
    }
  })
  const randomIndex = Math.floor(getRandom(0,availGrids.length));
  console.table(availGrids);
  // if there are no grids then the game MUST be over
  // do stuff here
  // get the opponents positions
    const opponentGrids = grids.filter(grid=>{
      if (grid.classList.contains('p1')){
        return grid;
      }
    })

    switch(true){
      case (opponentGrids.includes(document.getElementById('seven')))
        && (opponentGrids.includes(document.getElementById('four'))) :
        if (availGrids.includes(document.getElementById('one'))){
          document.getElementById('one').textContent=player2;
          document.getElementById('one').classList.add('p2');
          break;
        }
      case (opponentGrids.includes(document.getElementById('seven')))
        && (opponentGrids.includes(document.getElementById('five'))) :
        if (availGrids.includes(document.getElementById('three'))){
          document.getElementById('three').textContent=player2;
          document.getElementById('three').classList.add('p2');
          break;
        }
      case (opponentGrids.includes(document.getElementById('seven')))
        && (opponentGrids.includes(document.getElementById('eight'))) :
        if (availGrids.includes(document.getElementById('nine'))){
          document.getElementById('nine').textContent=player2;
          document.getElementById('nine').classList.add('p2');
          break;
        }
      case (opponentGrids.includes(document.getElementById('seven')))
        && (opponentGrids.includes(document.getElementById('nine'))) :
        if (availGrids.includes(document.getElementById('eight'))){
          document.getElementById('eight').textContent=player2;
          document.getElementById('eight').classList.add('p2');
          break;
        }
      case (opponentGrids.includes(document.getElementById('seven')))
        && (opponentGrids.includes(document.getElementById('one'))) :
        if (availGrids.includes(document.getElementById('four'))){
          document.getElementById('four').textContent=player2;
          document.getElementById('four').classList.add('p2');
          break;
        }
      case (opponentGrids.includes(document.getElementById('seven')))
        && (opponentGrids.includes(document.getElementById('three'))) :
        if (availGrids.includes(document.getElementById('five'))){
          document.getElementById('five').textContent=player2;
          document.getElementById('five').classList.add('p2');
          break;
        }
      case (opponentGrids.includes(document.getElementById('one')))
        && (opponentGrids.includes(document.getElementById('five'))) :
        if (availGrids.includes(document.getElementById('nine'))){
          document.getElementById('nine').textContent=player2;
          document.getElementById('nine').classList.add('p2');
          break;
        }
      case (opponentGrids.includes(document.getElementById('one')))
        && (opponentGrids.includes(document.getElementById('nine'))) :
        if (availGrids.includes(document.getElementById('five'))){
          document.getElementById('five').textContent=player2;
          document.getElementById('five').classList.add('p2');
          break;
        }
      case (opponentGrids.includes(document.getElementById('one')))
        && (opponentGrids.includes(document.getElementById('two'))) :
        if (availGrids.includes(document.getElementById('three'))){
          document.getElementById('three').textContent=player2;
          document.getElementById('three').classList.add('p2');
          break;
        }
      case (opponentGrids.includes(document.getElementById('five')))
        && (opponentGrids.includes(document.getElementById('six'))) :
        if (availGrids.includes(document.getElementById('four'))){
          document.getElementById('four').textContent=player2;
          document.getElementById('four').classList.add('p2');
          break;
        }
      case (opponentGrids.includes(document.getElementById('five')))
        && (opponentGrids.includes(document.getElementById('four'))) :
        if (availGrids.includes(document.getElementById('six'))){
          document.getElementById('six').textContent=player2;
          document.getElementById('six').classList.add('p2');
          break;
        }
      case (opponentGrids.includes(document.getElementById('nine')))
        && (opponentGrids.includes(document.getElementById('six'))) :
        if (availGrids.includes(document.getElementById('three'))){
          document.getElementById('three').textContent=player2;
          document.getElementById('three').classList.add('p2');
          break;
        }
      case (opponentGrids.includes(document.getElementById('nine')))
        && (opponentGrids.includes(document.getElementById('three'))) :
        if (availGrids.includes(document.getElementById('six'))){
          document.getElementById('six').textContent=player2;
          document.getElementById('six').classList.add('p2');
          break;
        }
      case (opponentGrids.includes(document.getElementById('three')))
        && (opponentGrids.includes(document.getElementById('five'))) :
        if (availGrids.includes(document.getElementById('seven'))){
          document.getElementById('seven').textContent=player2;
          document.getElementById('seven').classList.add('p2');
          break;
        }
      case (opponentGrids.includes(document.getElementById('five')))
        && (opponentGrids.includes(document.getElementById('eight'))) :
        if (availGrids.includes(document.getElementById('two'))){
          document.getElementById('two').textContent=player2;
          document.getElementById('two').classList.add('p2');
          break;
        }
      case (opponentGrids.includes(document.getElementById('three')))
        && (opponentGrids.includes(document.getElementById('six'))) :
        if (availGrids.includes(document.getElementById('nine'))){
          document.getElementById('nine').textContent=player2;
          document.getElementById('nine').classList.add('p2');
          break;
        }
      case (opponentGrids.includes(document.getElementById('nine')))
        && (opponentGrids.includes(document.getElementById('five'))) :
        if (availGrids.includes(document.getElementById('one'))){
          document.getElementById('one').textContent=player2;
          document.getElementById('one').classList.add('p2');
          break;
        }
      case (opponentGrids.includes(document.getElementById('eight')))
        && (opponentGrids.includes(document.getElementById('nine'))) :
        if (availGrids.includes(document.getElementById('seven'))){
          document.getElementById('seven').textContent=player2;
          document.getElementById('seven').classList.add('p2');
          break;
        }
      case (opponentGrids.includes(document.getElementById('four')))
        && (opponentGrids.includes(document.getElementById('six'))) :
        if (availGrids.includes(document.getElementById('five'))){
          document.getElementById('five').textContent=player2;
          document.getElementById('five').classList.add('p2');
          break;
        }
      case (opponentGrids.includes(document.getElementById('one')))
        && (opponentGrids.includes(document.getElementById('three'))) :
        if (availGrids.includes(document.getElementById('two'))){
          document.getElementById('two').textContent=player2;
          document.getElementById('two').classList.add('p2');
          break;
        }
      case (opponentGrids.includes(document.getElementById('five')))
        && (opponentGrids.includes(document.getElementById('two'))) :
        if (availGrids.includes(document.getElementById('eight'))){
          document.getElementById('eight').textContent=player2;
          document.getElementById('eight').classList.add('p2');
          break;
        }
      case (opponentGrids.includes(document.getElementById('one')))
        && (opponentGrids.includes(document.getElementById('four'))) :
        if (availGrids.includes(document.getElementById('seven'))){
          document.getElementById('seven').textContent=player2;
          document.getElementById('seven').classList.add('p2');
          break;
        }
      case (opponentGrids.includes(document.getElementById('two')))
        && (opponentGrids.includes(document.getElementById('three'))) :
        if (availGrids.includes(document.getElementById('one'))){
          document.getElementById('one').textContent=player2;
          document.getElementById('one').classList.add('p2');
          break;
        }
      case (opponentGrids.includes(document.getElementById('eight')))
        && (opponentGrids.includes(document.getElementById('two'))) :
        if (availGrids.includes(document.getElementById('five'))){
          document.getElementById('five').textContent=player2;
          document.getElementById('five').classList.add('p2');
          break;
        }

      // the first two moves don't matter and can be random so we'll default to random since none of our cases will be met
      default:
        console.log(availGrids);
        console.log('is this working?');
        availGrids[randomIndex].textContent=player2;
        availGrids[randomIndex].classList.add('p2');
        break;
    }

  console.log(opponentGrids);
    playerTurn=1;
    console.log(playerTurn);
}

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

              
            
!
999px

Console