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

              
                <!DOCTYPE HTML>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Tris</title>
        <meta name="keywords" content="tris, tic-tac-toe, gioco, javascript">
        <meta name="description" content="Gioco del tris, esercizio javascript">
        <meta name="viewport" content="width=device-width">
    </head>
    
    <body>
        <center>
            <div class="griglia">
                <div class="riga">
                    <div class="cella">

                    </div>
                    <div class="cella">

                    </div>
                    <div class="cella">

                    </div>
                </div>
                <div class="riga">
                    <div class="cella">

                    </div>
                    <div class="cella">

                    </div>
                    <div class="cella">

                    </div>
                </div>
                <div class="riga">
                    <div class="cella">

                    </div>
                    <div class="cella">

                    </div>
                    <div class="cella">
                </div>
            </div>
        </div>
        </center>
        
        <div id="gameOver">
            <center>
                Partita finita! 
            </center>
        </div>
        
    </body>
</html>
              
            
!

CSS

              
                * {
  margin: 0;
  padding: 0;
}

body {
    background-color: #FFFFCC;
}

.griglia {
    width: 288px;
    height: 288px;
    margin-top: 10vh;
}

.riga {
    display: block;
    height: 94px;
}

.cella {
    border: 1px solid black;
    display: inline-block;
    width: 93px;
    float: left;
    height: 100%;
    position: relative;
}

.croce {
  position: absolute;
  left: 14px;
  top: 14px;
  width: 64px;
  height: 64px;
}

.croce:before, .croce:after {
  position: absolute;
  content: ' ';
  height: 64px;
  width: 2px;
  background-color: #333;
}
.croce:before {
  transform: rotate(45deg);
}
.croce:after {
  transform: rotate(-45deg);
}

.cerchio {
  position: absolute;
  left: 13px;
  top: 12px;
  width: 64px;
  height: 64px;
  border: 2px solid red;
  border-radius: 100px;
}

#gameOver {
    margin-top: 1.5em;
    font-size: 1.5rem;
    opacity: 0;
    transition: 0.5s ease;
}
              
            
!

JS

              
                document.addEventListener('DOMContentLoaded', function() {
    
    /** Turno = 0 ==> Tocca al giocatore che usa O
        Turno = 1 ==> Tocca al giocatore che usa X
        
        mappaCelle[i][j] = -1 se non contiene niente, 0 se contiene un O, 1 se contiene X
    **/
    
    var mappaCelle = [];
    for( i=0; i < 3; i++ )
        mappaCelle[i] = new Array(3); 
        
    for( i=0; i < 3; i++ )
        for( j=0; j < 3; j++ )
            mappaCelle[ i ][ j ] = -1; 
            
    var celle = document.getElementsByClassName('cella');
    var turno = 0;
    var contoTurni = 0;
    var i;
    var j;
    
    
    function attivaCella(el) {
        
        contoTurni++;
        
        if( turno == 0 ) {
            var cerchio = document.createElement('a');
            cerchio.setAttribute( 'class', 'cerchio' );
            cerchio.setAttribute( 'href', '#' );
            el.target.appendChild( cerchio );
            mappaCelle[ el.target.id[1] ][ el.target.id[2] ] = 0;
        } else {
            var croce = document.createElement('a');
            croce.setAttribute( 'class', 'croce' );
            croce.setAttribute( 'href', '#' );
            el.target.appendChild( croce );
            mappaCelle[ el.target.id[1] ][ el.target.id[2] ] = 1;
        }
        
        if( turno == 0 ) 
            turno++;
        else   
            turno--;
        
        el.target.removeEventListener('click', attivaCella);
        
        if(vittoria()) {
            for( i = 0; i < celle.length; i++ ) {
                celle[ i ].removeEventListener('click',attivaCella);
                document.getElementById('gameOver').style.opacity = '1';
            }
        }
    }
    
    for( i = 0; i < 3; i++ )
        for( j = 0; j < 3; j++ ) {
            
            celle[3*i+j].addEventListener( 'click', attivaCella );
            celle[3*i+j].setAttribute('id', '_'+( (i == 0)? '0'+j:(i*10+j) ) );
            
            if( i == 0 )
                celle[3*i+j].style.borderTop = '0px';
            if( i == 2 )
                celle[3*i+j].style.borderBottom = '0px';
            if ( j == 0 )
                celle[3*i+j].style.borderLeft = '0px';
            if ( j == 2 )
                celle[3*i+j].style.borderRight = '0px';
        }
    
    function vittoria() {
        
        if(contoTurni < 5)
            return false;
        else 
            if( contoTurni > 8 )
                return true;
        
        var trisH = true;
        var trisV = true;
        var trisDiagA = true;
        var trisDiagB = true;
        
        for( i = 0; i < 3; i++ ) {
            
            trisH = true;
            trisV = true;
            
            for( j = 0; j < 3; j++ ) {

                if( mappaCelle[i][j] == -1 )
                    trisH = false;
                if( mappaCelle[j][i] == -1 )
                    trisV = false;
                
                if( j > 0 && mappaCelle[i][j] != mappaCelle[i][j-1] )
                    trisH = false;
                
                if( j > 0 && mappaCelle[j][i] != mappaCelle[j-1][i] )
                    trisV = false;
            }

            if( mappaCelle[i][i] == -1 ) 
                trisDiagA = false;
            if( i > 0 && mappaCelle[i][i] != mappaCelle[i-1][i-1] )
                trisDiagA = false;
            if( mappaCelle[i][2-i] == -1 )
                trisDiagB = false;
            if( i > 0 && mappaCelle[i][2-i] != mappaCelle[i-1][3-i] )
                trisDiagB = false;
            
            if( trisH || trisV )
                break;
        }
                
        return trisH || trisV || trisDiagA || trisDiagB;
    }
    
});

              
            
!
999px

Console