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

              
                <!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="style.css">
    <link href="https://fonts.googleapis.com/css?family=Raleway&display=swap" rel="stylesheet">
    <script src="index.js"></script>    
    <title>Tic-Tac-Toe</title>
</head>
<body>
    <h1>Tic-Tac-Toe</h1>
    <div class="modal-choice">
            <div class="modal-start">
                    <h3 class="header-3">Player Start</h3>
                    <h3 class="header-3">Pick your weapon</h3>
                    <button class="choice">X</button>
                    <button class="choice">O</button>
            </div>
        </div>
    <section class="scoreboard">
        <h3 class="score">Player</h3>
        <h3 class="score">Player</h3>
    </section>
    <div class="board">
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
        <div class="cell"></div>
    </div>
    <button class="btn-game">Start New Game</button>
    <div class="modal">
        <div class="modal-content">
            <span class="close-button">
                <div class="center">X</div></span>
            <h3 class="header-3 end-game">Game Over!</h3>
            <button class="btn-restart">Play Again</button>
        </div>
    </div>
    
</body>
</html>
              
            
!

CSS

              
                * {
    border: 0;
    margin: 0;
    padding: 0;
}

html {
    font-size: 10vmin;
    font-family: 'Raleway', sans-serif;
}

h1 {
    font-size: 1em;
    margin: 2vmin;
}

body {
    height: 100vh;
    display: flex;
    flex-flow: column;
    justify-content: center;
    align-items: center;
}

.scoreboard {
    display: flex;
    margin: 2vmin;
    padding: 0 6vmin 0 6vmin;
    width: 45vmin;
    justify-content: space-between;
}

.score {
    font-size: 3.4vmin;
}

.board {
    height: 60vmin;
    width: 60vmin;
    border: 2px solid black;
    display: grid;
    grid-template-rows: 1fr 1fr 1fr;
    grid-template-columns: 1fr 1fr 1fr;
}

.cell {
    border: 2px solid black;
    display: flex;
    justify-content: center;
    align-items: center;
    user-select: none;
}

.btn-game,
.btn-restart,
.choice {
    background: none;
    color: inherit;
    border: none;
    padding: 1vmin;
    font: inherit;
    cursor: pointer;
    outline: inherit;
    margin: 3vmin;
    font-size: 4vmin;
    border: .5vmin solid black;
    border-radius: 0.5vmin;
}

.btn-game:hover {
    transform: scale(1.1);
    box-shadow: .5vmin .5vmin 1vmin black;
}

.btn-game:active {
    transform: scale(0.9);
}

.choice:hover {
    transform: scale(1.1);
    box-shadow: .5vmin .5vmin 1.2vmin black;
}

.modal,
.modal-choice {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    opacity: 0;
    visibility: hidden;
    transform: scale(1.1);
    transition: visibility 0s linear 0.25s, opacity 0.25s 0s, transform 0.25s;
}

.modal-content,
.modal-start {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: white;
    padding: 1vmin 1.5vmin;
    width: 50vmin;
    height: 40vmin;
    border-radius: 0.2rem;
    text-align: center;
}

.close-button {
    float: right;
    width: .5rem;
    line-height: .5rem;
    text-align: center;
    cursor: pointer;
    border-radius: 0.1rem;
    border: 0.2vmin solid lightgray;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 4.5vmin;
}

.close-button:hover {
    border: 0.2vmin solid darkgray;
}

.show-modal {
    opacity: 1;
    visibility: visible;
    transform: scale(1.0);
    transition: visibility 0s linear 0s, opacity 0.25s 0s, transform 0.25s;
}

.header-3 {
    margin-left: 7vmin;
    font-size: 5vmin;
}

.won {
    font-size: 4vmin;
    margin: 4vmin;
}

.modal-start>.header-3 {
    margin: 0;
}

.choice {
    font-size: 8vmin;
    width: 15vmin;
}
              
            
!

JS

              
                const winColor = "#32CD32"

function Cell(element) {
    this.element = element;
    this.type = "";
    this.value = 0;
    this.winner = function () {
        this.element.style.backgroundColor = winColor;
    };
    this.select = function (symbol) {
        if (!(this.type === "")) {
            return false;
        }
        if ((symbol === "X") || (symbol === "O")) {
            this.type = symbol;
            this.element.innerHTML = symbol;
            if (symbol === "X") {
                this.value = 1;
            } else if (symbol === "O") {
                this.value = -1;
            }
            return true;
        }
        return false;
    }
    this.reset = function () {
        this.type = "";
        this.value = 0;
        this.element.innerHTML = "";
        this.element.style.backgroundColor = "transparent";
    }
}

function Player(symbol, scoreboardElement) {
    this.symbol = symbol;
    this.score = 0;
    this.scoreboardElement = scoreboardElement;
    this.scoreboardElement.textContent = `Player ${this.symbol} : ${this.score}`
    this.getSymbol = function () {
        return this.symbol;
    }
    this.incrementScore = function (num) {
        this.score += num;
        this.scoreboardElement.textContent = `Player ${this.symbol} : ${this.score}`;
    }
}

function Board() {
    this.players = [];
    this.currentPlayer;
    this.turnsTaken = 0;
    this.endGameDetected = false;

    this.modal = document.querySelector(".modal");
    this.toggleModal = function () {
        this.modal.classList.toggle("show-modal");
    }
    this.startGame = function () {
        modalStart = document.querySelector(".modal-choice");
        modalStart.classList.toggle("show-modal");
    }
    this.choice = function () {
        let b = this;
        let choices = document.querySelectorAll(".choice");
        let scoreboardElements = document.querySelectorAll(".score");
        for (let i = 0; i < choices.length; i++) {
            choices[i].addEventListener("click", () => {
                let playerOne = new Player(choices[i].innerHTML, scoreboardElements[0]);
                let playerTwo = new Player((choices[i].innerHTML === "X") ? "O" : "X", scoreboardElements[1]);
                b.players.push(playerOne, playerTwo);
                b.startGame();
                b.setUp();
            });
        }
    }
    this.setUp = function () {
        let cells = document.querySelectorAll(".cell");

        this.currentPlayer = this.players[0];
        let curArr = 0;
        for (let i = 0; i < cells.length; i++) {
            if (this.cells[curArr].length === 3) {
                curArr++;
            }
            //get the board
            let b = this;
            //get instance of current player
            let c = new Cell(cells[i]);
            this.cells[curArr].push(c);

            let closeButton = document.querySelector(".close-button");
            closeButton.addEventListener("click", function () {
                b.toggleModal();
            });

            c.element.addEventListener("click", () => {
                if (!b.endGameDetected) {
                    if (c.select(b.currentPlayer.symbol)) {
                        b.turnsTaken++;
                        let endGameResults = b.isGameOver();
    
                        if (endGameResults.winningCells.length === 3) {
                            b.endGameDetected = true;
                            for (let i = 0; i < endGameResults.winningCells.length; i++) {
                                endGameResults.winningCells[i].winner();
                            }
                            b.endgameScreen(b.currentPlayer.symbol, endGameResults.winPosition);
                            b.currentPlayer.incrementScore(1);
                            b.toggleModal();
                        }
                        b.changePlayer();
                    }
                }
            });
        }
    }
    this.isGameOver = function () {
        let counterDia, counterDiaL, offsetInvertDia, outcome;
        counterDia = 0;
        counterDiaL = 0;
        offsetInvertDia = 2;
        outcome = {
            winPosition: "",
            winningCells: []
        };

        for (let i = 0; i <= 2; i++) {
            let counterAcross = 0;
            let counterDown = 0;

            for (let j = 0; j <= 2; j++) {
                counterAcross += this.cells[i][j].value;
                counterDown += this.cells[j][i].value;

                //checks across
                if ((counterAcross === 3) || (counterAcross === -3)) {
                    for (let x = 0; x <= 2; x++) {
                        outcome.winningCells.push(this.cells[i][x]);
                    }
                    outcome.winPosition = "across";
                    return outcome;
                }

                if ((counterDown === 3) || (counterDown === -3)) {
                    for (let x = 0; x <= 2; x++) {
                        outcome.winningCells.push(this.cells[x][i]);
                    }
                    outcome.winPosition = "down";
                    return outcome;
                }
            }
            counterDia += this.cells[i][i].value;
            counterDiaL += this.cells[i][offsetInvertDia].value;

            //checking win state diagonally
            if ((counterDia === 3) || (counterDia === -3)) {
                for (let x = 0; x <= 2; x++) {
                    outcome.winningCells.push(this.cells[x][x]);
                }
            }
            let = offset = 2;
            if ((counterDiaL === 3) || (counterDiaL === -3)) {
                for (let x = 0; x <= 2; x++) {
                    outcome.winningCells.push(this.cells[x][offset]);
                    offset--;
                }
            }
            offsetInvertDia--;
        }
        if ((counterDia === 3) || (counterDiaL === 3) || (counterDia === 3) || (counterDiaL === -3)) {
            outcome.winPosition = "diagonally";
            return outcome;
        }
        return outcome;
    }
    this.changePlayer = function () {
        if (this.currentPlayer === this.players[0]) {
            this.currentPlayer = this.players[1];
            return;
        }
        this.currentPlayer = this.players[0];
    }
    this.endgameScreen = function (symbol, position) {
        let headerEndGame = document.querySelector(".end-game");
        headerEndGame.insertAdjacentHTML("afterend", `<p class="won">${symbol} won ${position}!</p>`);
    }
    this.cells = [
        [],
        [],
        []
    ]
    this.restart = function () {
        for (let i = 0; i < this.cells.length; i++) {
            for (let j = 0; j < this.cells.length; j++) {
                this.cells[i][j].reset();
            }
        }
        this.endGameDetected = false;
        this.turnsTaken = 0;
        let gameStateDescription = document.querySelector(".won");
        if (gameStateDescription) {
            gameStateDescription.remove();
        }
    }
}

window.addEventListener("load", function () {
    init();
});

function init() {
    let board = new Board();
    board.startGame();
    board.choice();
    let resetButton = document.querySelector(".btn-game");
    let restartButton = document.querySelector(".btn-restart")
    restartButton.addEventListener("click", function () {
        board.restart();
        board.toggleModal();
    })
    resetButton.addEventListener("click", function () {
        board.restart();
    });
};
              
            
!
999px

Console