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 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

              
                
<head>
    <title>Tic-Tac-Toe</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div data-screen>
        <div class="board" data-board>
        </div>
    </div>
    <script src="/script.js" async defer></script>
</body>
              
            
!

CSS

              
                *, *::after, *::before {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --cell-size: 100px;
    --mark-size: calc(var(--cell-size) * .8);
}

.board {
    display: grid;
    height: 100vh;
    width: 100%;
    justify-content: center;
    align-content: center;
    justify-items: center;
    align-items: center;
    grid-template-columns: repeat(3, auto);

}

.cell {
    display: flex;
    width: var(--cell-size);
    height: var(--cell-size);
    border: 1px solid black;
    justify-content: center;
    align-items: center;
    font-size: var(--mark-size);
    cursor: pointer;
    
}

/* X shapes */
.cell.x::before, .cell.x::after {
    content: "";
    position: absolute;
    width: var(--mark-size);
    height: calc(var(--mark-size) * .2);
    background-color: black;
    border-radius: 50%;
}

.cell.x::before {
    transform: rotate(45deg);
}
.cell.x::after {
    transform: rotate(-45deg);
}

/* O Shape */
.cell.o::before, .cell.o::after {
    content: "";
    position: absolute;
    border-radius: 50%;
}

.cell.o::before {
    width: calc(var(--mark-size) * .8);
    height: calc(var(--mark-size) * .8);
    background-color: black;
}

.cell.o::after {
    width: calc(var(--mark-size) * .45);
    height: calc(var(--mark-size) * .45);
    background-color: white;
}

/* Not allowed on already used cells */
.cell.x, .cell.o{
    cursor: not-allowed;
}

/* Hover effects */
.board .cell:not(.x):not(.o):hover {
    border: .2rem solid lightgreen;
}

/* Pause screen */
.pause-screen {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.9);
    justify-content: center;
    align-items: center;
    color: white;
    font-size: 5rem;
    flex-direction: column;
    z-index: 99;
}

.pause-screen button {
    font-size: 3rem;
    color: black;
    background-color: white;
    border: .2rem solid black;
    padding: 0rem 1rem;
    cursor: pointer;
}

.pause-screen button:hover {
    color: white;
    background-color: black;
    border-color: white;
}
.pause-screen.show {
    display:flex;
}

option:empty{
    display: none;
}
              
            
!

JS

              
                const mainDiv = document.querySelector('[data-screen]')

class TicTacToe {
    constructor(div) {
        this.board = div.querySelector('[data-board]')  
    }

    //most of the initial variables here
    varUpdater = () => {
        this.pauseButton = this.board.querySelector('[data-pause-button]')
        this.pauseButton.addEventListener('click', this.setUp)
        this.pauseScreen = this.board.querySelector('[data-pause]')
        this.pauseText = this.board.querySelector('[data-pause-text]')

        this.playerOne = "x"
        this.playerTwo = "o"
        this.currentPlayer = this.playerOne

        this.winCondition = 3

        this.rows = 5
        this.columns = 5
    }

    setUp = () => {
        this.pauseMenuSetUp()
        this.varUpdater()
        this.cellSetUp()
        this.start()
    }

    pauseMenuSetUp = () => {
        this.board.innerHTML = `
        <div class="pause-screen show" data-pause>
            <div data-pause-text></div>
            <button data-pause-button>Start</button>
        </div>
        `
    }

    //Sets up cell and css style based on given parameters
    cellSetUp = () => {
        this.board.style.cssText = `grid-template-columns: repeat(${this.columns}, auto);`
        this.colSeperatorDiv = document.createElement('div')
        this.cellElement = document.createElement('div')
        this.cellElement.setAttribute('class', 'cell')
        for (let i = 0; i < this.rows; i++){
            this.cellElement.setAttribute('data-cell', i)
            this.colSeperatorDiv.appendChild(this.cellElement.cloneNode(true))
        } 
        for (let i = 0; i < this.columns;i++){
            this.colSeperatorDiv.setAttribute('data-seperator', i)
            this.board.appendChild(this.colSeperatorDiv.cloneNode(true))
        }
    }

    //Clears cell attributes and adds event listener before removing pause screen
    start = () => {
        this.board.querySelectorAll('[data-cell]').forEach(cell => {
            cell.classList.remove('x', 'o')
            cell.removeEventListener('click', this.clickHandle)
            cell.addEventListener('click', this.clickHandle, { once: true })
        })
        this.pauseScreen.classList.remove('show')
    }

    //Adds symbol based on cell target
    clickHandle = (cell) => {
        this.cell = cell.target
        this.cell.classList.add(this.currentPlayer)
        this.checkBoard()
    }

    //Decides if it should select next player or conclude game
    checkBoard = () => {
        this.columnIndex = parseInt(this.cell.getAttribute('data-cell'))
        this.rowIndex = parseInt(this.cell.parentElement.getAttribute('data-seperator'))
        if (this.isConcluded() == true) {
            this.pauseText.innerHTML = `${this.currentPlayer} wins!`
            this.pauseScreen.classList.add('show')
        } else if (this.isConcluded() === 'tie') {
            this.pauseText.innerHTML = `Tie!`
            this.pauseScreen.classList.add('show')
        } else {
            this.playerSelect()
        }
    }

    //Selects next player
    playerSelect = () => {
        this.currentPlayer !== this.playerTwo ? this.currentPlayer = this.playerTwo
            : this.currentPlayer = this.playerOne;
    }

    //Checks for game conclusion. Does NOT check for horizontal and diagonal conditions atm
    isConcluded = () => {
        return this.verticalScore(0, this.columnIndex) >= this.winCondition ? true
        : Array.from(this.board.querySelectorAll('[data-cell]')).every(c => c.classList.contains(this.playerOne) || c.classList.contains(this.playerTwo)) ? 'tie'
        : false
    }

    //Goes down the column using recusion to add up score
    verticalScore = (score, col) => {
        return this.cell.parentElement.querySelector(`[data-cell="${col}"]`) == null ? score += this.reverseDirectionScore(0, this.columnIndex - 1)
        : this.cell.parentElement.querySelector(`[data-cell="${col}"]`).classList.contains(this.currentPlayer) ? score += this.verticalScore(1, col + 1)
        : score += this.reverseDirectionScore(0, this.columnIndex - 1);
    }
    //Goes up column using recusion to return a score
    reverseDirectionScore = (rScore, rCol) => {
        return this.cell.parentElement.querySelector(`[data-cell="${rCol}"]`) == null ? rScore
        : this.cell.parentElement.querySelector(`[data-cell="${rCol}"]`).classList.contains(this.currentPlayer) ? rScore += this.reverseDirectionScore(1, rCol - 1)
        : rScore
    }
}

const newBoard = new TicTacToe(mainDiv)
newBoard.setUp()

              
            
!
999px

Console