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

              
                <div id="headboard">
    <h1><span>I'm invincible.</span></h1>
    <div id="textOutput"></div>
    <div><span id="gameCounter" title="Games played"></span><span id="status" Title="Status  &#10; Black: Ready to play &#10; Green: Game in progress"><span></span></span></div>  
</div>
<div id="help"><div></div></div>
<div id="machineCounter" title="Games I have won"></div>
<div id="playerCounter" title="Games you have won"></div>
<div id="container"></div>
              
            
!

CSS

              
                :root{
--size : min(85vw ,85vh);
--oneThird : calc(var(--size) / 3)
}
*{
cursor:default;
}
html,body{
height: 100%;
margin: 0;
}
body{
font-size: 16px;
font-family: sans-serif;
background-image: linear-gradient(75deg,white,  lightgray 5%,white 15%, lightgray 55% , lightgray 90%,white);
background-size: 100%;
background-attachment: fixed;
min-height: 300px;
display:flex;
flex-direction: column;
min-width:350px; 
  min-height: calc(var(--size) + 50px) ;
}
#container{
width: var(--size);
height: var(--size);
margin:auto;
}
#container>div{
width: var(--oneThird);
height: var(--oneThird);
display:inline-block;
vertical-align: top;
}
#container>div>div{
box-sizing: border-box;
width: 90%;
height: 90%;
margin:5%;
border-radius: 5%;
cursor:pointer;
border:1px solid transparent; 
background-color: rgb(255,255,255,.6);
background-color: rgb(0,0,0,.2);
transition: .1s;
}
#container>div>div:before{
overflow: hidden;
display: block;
line-height: 100%;;
width:100%;
height: 100%;
text-align: center;
border-radius: 50%;
font-size: calc( var(--oneThird) /1.1);
}
#container>div[marked=player]>div:before{
content:"✚";
color:green;
transform:rotate(45deg);
}
#container>div[marked=machine]>div:before{
content:"✚";
}
#container>div[empty=no]>div{
border-color:gray white white gray;
background-color:transparent;
cursor: default;
}
#headboard{
display:flex;
justify-content: space-between;
padding-top:2px;
height: 30px;
overflow: hidden;
}
h1{
margin:0;
font-size: 30px;
line-height: 24px;
font-weight: normal;
transition:.4s;
}
h1:before{
content:" (ง ͠° ͟ʖ ͡°)ง ";
font-size: 24px;
}
.nIn>span{
text-decoration:line-through double black;
}
.nIn:before{
content:  "(ง ˘ ʖ ˘ )ง !";
}
#textOutput{
position: absolute;
width: 100%;
top:20px;
font-weight: bold;
text-align: center;
font-size: 50px;
}
@keyframes output {
0% {transform:scale(0);}
5% {transform:scale(1.2);}
95% {transform:scale(1.2);}   
100% {transform:scale(1);}
}
#machineCounter,#playerCounter{
position: absolute;
background-color: aqua;
line-height: 60px;
width: 60px;
bottom: 0;
margin:5px;
text-align: center;
font-size: 32px;
background-color: black;
color:white;
border-radius: 50%;
transition:transform .16s ease-in-out, filter .31s;
}
#playerCounter{
right: 0;
background-color: green;
}
#status{
display:inline-block;
vertical-align: top;
width: 20px;
height: 20px;
background-color: black;
border-radius: 50%;
margin:2px 10px;
transition:.2s;
animation-name: rotates;
animation-duration: 5s;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
#status[on]{
background-color: green;
animation-duration: 1s;
animation-direction: reverse;
}
#status>span{
display: block;
width: 5px;
height: 5px;
border-radius: 50%;
margin:auto;
background-color: white;
margin-top:1px;
opacity:.6;
}
@keyframes rotates {
0% {transform:rotate(360deg);}
100% {transform:rotate(0deg);}
}
#gameCounter{
display:inline-block;
vertical-align: top;
font-size: 24px;
line-height: 24px;
margin-right:5px;
background-color: rgba(0,0,0,.2);
padding:0 2px 0 10px ;
border-radius: 3px;
color:white;
}
#gameCounter:empty{
padding:0;
}
#help{
position:absolute;
bottom:5px;
width: 100%;
text-align:center;
font-size: 24px;
}
              
            
!

JS

              
                function initialize(){
    gameInProgress = true
    document.getElementById("status").setAttribute("on","")
    document.querySelectorAll("#container>DIV").forEach(function(box,i){
        var row = i<3 ? 0 : i>5 ? 2 : 1
        var col = i % 3
        box.setAttribute("marked","no")
        box.setAttribute("empty","yes")
        box.setAttribute("line"+row,"")
        box.setAttribute("line"+(col+3),"")
        box.firstChild.setAttribute("style","")// style.backgroundColor = ""
        if (row==col)box.setAttribute("line6","")
        if (row+col ==2 )box.setAttribute("line7","")
    })
    if (turn == "machine") {
        setTimeout(machineShift,500)
        boxClicked = false
        displayHelp("Check a box") 
    }else{
        setTimeout(function(){boxClicked= false;displayHelp("Make your move")},240)
    }
    turn = turn == "machine" ? "player" : "machine"
    displayText("· · ·","gray")
}

function displayText(text,color){
var output = document.getElementById("textOutput")
output.style.transition= "0s"
output.style.transform ="scale(0)"
output.style.color=color
output.style.filter="blur(100px)"
setTimeout(()=>{
    output.style.transition= ".3s ease-out"
    output.innerHTML=text;
    output.style.transform ="scale(1,.8) translateY(-2px)"//translateY(30px)"
    output.style.filter="blur(0px)"// drop-shadow(0 0 10px "+color+")"
    output.style.letterSpacing="8px"
},10)

}
function addEffect(id,value){
    var obj = document.getElementById(id)
    obj.style.transform ="scale(0)"
        obj.style.filter="blur(20px)"
        obj.style.transition = "0s"
        setTimeout(function(){
            obj.style.transition="transform .16s ease-in-out , filter .8s"
            obj.innerHTML=value
            obj.style.transform =""
            obj.style.filter=""
        },120)
}
function endGame(winner){   
    var text, color   
    if (winner == "machine"){
        text="I win"
        color ="black"
        counter[0]++
        addEffect("machineCounter",counter[0])
    }
    if (winner == "player"){
        text="You win"
        color ="green"
        counter[1]++        
        addEffect("playerCounter",counter[1])
    }
    if (winner == "tie"){
        text="We tied"
         color ="gray"        
    }
    counter[2]++
    addEffect("gameCounter",counter[2])
    displayText(text,color)
    document.querySelectorAll("#container>DIV[empty=yes]").forEach(function(box){
        box.setAttribute("empty","no")
        box.firstChild.style.backgroundColor = "rgba(0,0,0,.2)"
        box.firstChild.style.borderColor = "transparent"
    })
    document.getElementById("status").removeAttribute("on")
    setTimeout(function(){
        displayHelp("Click to play again")
        gameInProgress = false;
    },500)  
}
function checkBoard(option){
    for (var i=0 ; i<8 ;i++){
        var player = 0
        var machine = 0
        var boxEmpty = ""
        document.querySelectorAll("[line"+i+"]").forEach(function(box){
            if (box.getAttribute("marked") == "player") player++
            if (box.getAttribute("marked") == "machine") machine++
            if (box.getAttribute("marked") == "no") boxEmpty = box
        })
        switch (option){
            case "winning play":
                if (machine==2 && player==0 ){
                    boxEmpty.setAttribute("marked","machine")
                    boxEmpty.setAttribute("empty","no")
                    document.querySelectorAll("[line"+i+"]>div").forEach(function(box){
                        box.style.backgroundColor="white"
                    })
                    return true
                }
            break;
            case "cover winning play":
                if (machine==0 && player==2){
                    boxEmpty.setAttribute("marked","machine")
                    boxEmpty.setAttribute("empty","no")
                    return true
                }

            break;
            case "the player wins":
                if (player==3){
                    document.querySelectorAll("[line"+i+"]>div").forEach(function(box){
                        box.style.backgroundColor="white"
                    })
                    setTimeout(function(){document.querySelector("h1").className ="nIn"},120)
                    return true
                }
        }

    }
    return false
}
function machineShift(){
    if (checkBoard("winning play")){
        endGame("machine")
        return
    }
    if (!checkBoard("cover winning play")){
        var emptyBoxes = document.querySelectorAll("#container>DIV[empty=yes]")
        var maxIndex = emptyBoxes.length
        var selectedBoxIndex = Math.floor(Math.random() * maxIndex)
        emptyBoxes[selectedBoxIndex].setAttribute("marked","machine")
        emptyBoxes[selectedBoxIndex].setAttribute("empty","no")            
    }
    if (document.querySelectorAll("[empty=no]").length==9) endGame("tie")
    if (document.querySelectorAll("[empty=no]").length==8){
       setTimeout(function(){
           boxClick(document.querySelector("#container>DIV[empty=yes]"))
       },120)               
    }
}
function boxClick(box){
    if (box.getAttribute("empty") != "yes") return
    box.setAttribute("marked","player")
    box.setAttribute("empty","no")
    if (checkBoard("the player wins")){
        endGame("player")
        return
    }
    if (document.querySelectorAll("[empty=no]").length==9){
        endGame("tie")
        return
    }
    setTimeout(machineShift,120)
}  
function displayHelp(text){
    var help =  document.getElementById("help")
    help.innerHTML=text
    help.style.transition="0s"
    help.style.transform="scale(0)"
    help.style.filter="blur(4px)"
    help.style.color="green"
    setTimeout(function(){
        help.style.transition="transform .3s, filter .3s ease-out .4s, color .8s ease .6s"
        help.style.transform=""
        help.style.filter=""
        help.style.color=""
    },10)
}
   
   
document.body.addEventListener("click",function(){ 
    if (!boxClicked) return
    displayHelp("")
    if (gameInProgress) return
    initialize()
})
    
var container=document.getElementById("container")    
var counter =[0,0,0]
var boxClicked = false
var turn = "machine"
var gameInProgress = false;
for (i=0;i<9;i++){
    var div =document.createElement("div")        
    div.content = document.createElement("div")
    div.appendChild(div.content)
    container.appendChild(div)
    div.onclick= function(){
        boxClick(this)
        boxClicked = true
    }
}
initialize()
              
            
!
999px

Console