Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <!DOCTYPE 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">
  <link rel="stylesheet" href="main.css">
  <title>Tic Tac Toe Game</title>
</head>
<body>
  <div class="header">
    <h1>Tic Tac Toe Game</h1>
    <p>Made by Tigran Kirakosov</p>
  </div>
  <div class="wrapper">
    <div class="board">
      <div class="squares">
        <div class="square" id="0"></div>
        <div class="square" id="1"></div>
        <div class="square" id="2"></div>
        <div class="square" id="3"></div>
        <div class="square" id="4"></div>
        <div class="square" id="5"></div>
        <div class="square" id="6"></div>
        <div class="square" id="7"></div>
        <div class="square" id="8"></div>
      </div>
    </div>
    <div class="currentTurn">X goes first</div>
    <a class="startGame" onClick="startGame()">Start new game</a>
  </div>

  <script src="main.js"></script>
  <script src="https://kit.fontawesome.com/a863ebaea0.js"></script>
</body>
</html>
              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Roboto);

*{margin: 0; padding: 0}

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  font-family: 'Roboto', sans-serif;
  background-color: black;
  color: white;
}
.header {
  margin: 0 auto;
  margin-top: 20px;
  text-align: center;
}
.header h1 {
  text-shadow: 0px 0px 3px rgb(255, 255, 255);
}
.header p {
  font-size: 15px;
}
.wrapper {
  position: relative;
  top: 5rem;
  margin: 0 auto;
  width: 335px;
  height: 335px;
}
.board {
  position: relative;
  width: 335px;
  height: 335px;
}
.squares {
  position: absolute;
  left: 1px;
  display: grid;
  border: 2px solid white;
  border-radius: 5px;
  grid-template-columns: 110px 110px 110px;
  grid-template-rows: 110px 110px 110px;
  grid-gap: 0;
  vertical-align: middle;
}
.square {
  display: flex;
  justify-content: center;
  align-items: center;
  border: 2px solid white;
  font-size: 30px;
  transition: 1s;
  text-shadow: 0px 0px 3px rgb(255, 255, 255);
}
.currentTurn {
  position: absolute;
  right: 0;
  margin-top: 5px;
  transition: ease-in-out;
}
.startGame {
  position: absolute;
  left: 0;
  margin-top: 5px;
  cursor: pointer;
  color: white;
  transition: all 0.3s ease;
}
.startGame:hover {
  text-shadow: 0px 0px 10px rgb(255, 255, 255);
}
              
            
!

JS

              
                // Creating players objects
let xPlayer = {
  name: '<i class="fas fa-times"></i>',
  color: '#DA2C43',
  turns: []
};
let oPlayer = {
  name: '<i class="far fa-circle"></i>',
  color: '#009DC4',
  turns: []
};

// Winning combinations
const Comb = {
  row: {a: [0, 1, 2], b: [3, 4, 5], c: [6, 7, 8]},
  col: {a: [0, 3, 6], b: [1, 4, 7], c: [2, 5, 8]},
  dia: {a: [0, 4, 8], b: [2, 4, 6]}
}

// Board fields
let originalBoard;
const squares = document.querySelectorAll('.square');
const currentTurn = document.querySelector('.currentTurn');

// A turn counter
let turnCounter;

// We need it here, above anything else to avoid bugs
startGame();

// Preparing and clearing board by reloading it
function startGame() {

  // Reset counters
  xPlayer.turns.splice(0);
  oPlayer.turns.splice(0);
  turnCounter = 1;
  currentTurn.innerHTML = `${xPlayer.name}\'s are going first`

  // Allowing to splice board on peaces in order to get access to a single square later
  originalBoard = Array.from(Array(9).keys());

  // Looping through each square and applying methods
  for (let i = 0; i < squares.length; i++) {
    squares[i].innerHTML = '';
    squares[i].style.backgroundColor = '';
    squares[i].addEventListener('click', turnClick, false);
  }
}

// Defining which player's turn and making a move
function turnClick(e) {
  if (typeof originalBoard[e.target.id] == 'number' && (turnCounter % 2) != 0) {
    turn(e.target.id, xPlayer.name);

    // Counting players current turns
    xPlayer.turns.push(parseInt(e.target.id));

  } else if (typeof originalBoard[e.target.id] == 'number' && (turnCounter % 2) == 0) {
    turn(e.target.id, oPlayer.name);

    // Counting players current turns
    oPlayer.turns.push(parseInt(e.target.id));

  }

  // Which player moves next
  let playersTurn;
  if ((turnCounter % 2) != 0){
    playersTurn = xPlayer.name;
  } else {
    playersTurn = oPlayer.name;
  }
  currentTurn.innerHTML = `It's ${playersTurn}'s turn`;

  // Checking game state
  gameTie();
  gameWon(xPlayer.turns, xPlayer);
  gameWon(oPlayer.turns, oPlayer);
}

// Upon making turn, visualize who took the square and disable that square for additional clicks
function turn(squareId, player) {
  document.getElementById(squareId).innerHTML = player;

  // A simple statement made to remove event listener for clicked square
  // which prevents various bugs
  if (player != '') {
    turnCounter++;
    squares[squareId].removeEventListener('click', turnClick, false);
  }
}

// Check for a winner
function gameWon(combo, player) {
  
  // Using array methods filter&incluedes to find common turn pattern which match winning combination.
  // Turn pattern always equals 3
  if        (combo.filter(turn => Comb.row.a.includes(turn)).length == 3) {
    winnerMsg(player);
    squares[0].style.backgroundColor = player.color;
    squares[1].style.backgroundColor = player.color;
    squares[2].style.backgroundColor = player.color;
  } else if (combo.filter(turn => Comb.row.b.includes(turn)).length == 3) {
    winnerMsg(player);
    squares[3].style.backgroundColor = player.color;
    squares[4].style.backgroundColor = player.color;
    squares[5].style.backgroundColor = player.color;
  } else if (combo.filter(turn => Comb.row.c.includes(turn)).length == 3) {
    winnerMsg(player);
    squares[6].style.backgroundColor = player.color;
    squares[7].style.backgroundColor = player.color;
    squares[8].style.backgroundColor = player.color;
  } else if (combo.filter(turn => Comb.col.a.includes(turn)).length == 3) {
    winnerMsg(player);
    squares[0].style.backgroundColor = player.color;
    squares[3].style.backgroundColor = player.color;
    squares[6].style.backgroundColor = player.color;
  } else if (combo.filter(turn => Comb.col.b.includes(turn)).length == 3) {
    winnerMsg(player);
    squares[1].style.backgroundColor = player.color;
    squares[4].style.backgroundColor = player.color;
    squares[7].style.backgroundColor = player.color;
  } else if (combo.filter(turn => Comb.col.c.includes(turn)).length == 3) {
    winnerMsg(player);
    squares[2].style.backgroundColor = player.color;
    squares[5].style.backgroundColor = player.color;
    squares[8].style.backgroundColor = player.color;
  } else if (combo.filter(turn => Comb.dia.a.includes(turn)).length == 3) {
    winnerMsg(player);
    squares[0].style.backgroundColor = player.color;
    squares[4].style.backgroundColor = player.color;
    squares[8].style.backgroundColor = player.color;
  } else if (combo.filter(turn => Comb.dia.b.includes(turn)).length == 3) {
    winnerMsg(player);
    squares[2].style.backgroundColor = player.color;
    squares[4].style.backgroundColor = player.color;
    squares[6].style.backgroundColor = player.color;
  }
}

// The winner message
function winnerMsg(player) {
  currentTurn.innerHTML = `${player.name}\'s won!'`;
  for (let i = 0; i < 9; i++) {

    // Removing event listeners because the game is over
    squares[i].removeEventListener('click', turnClick, false);

    // Clearing board colors
    squares[i].style.backgroundColor = '';
  }
}


// Tie check
function gameTie() {
  if (turnCounter >= 10) {
    for (let i = 0; i < 9; i++) {
      squares[i].style.backgroundColor = '#14A989';
    }
    currentTurn.innerText = 'It\'s a tie!';
  }
}
              
            
!
999px

Console