css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="container">
		<h1>Memory Game</h1>
		<h2>Test your memory by finding pairs. Try to find all the matches in the fewest attempts!</h2>
		<ul class="tiles"></ul>
		<h2>Inspired by <a href="https://codepen.io/coderontheroad/pen/EvwAb">this codepen</a></h2>
	</div>
            
          
!
            
              @import "compass/css3";

$blue: dodgerblue;
$orange: orange;
html, body {
  height: 100%;
  width: 100%;
  background: darken($blue, 10%);
}
.container {
  width: 600px;
  margin: 0 auto;
  h1, h2 {
    text-align: center; 
    margin-left: 50px;
    color: white;
  }
  a {
    color: inherit; 
  }
}
.tiles {
  margin: 15px auto;
  width: 100%;
  height: 100%;
}
li {
  width: 100px;
  height: 100px;
  display: inline-block;
  position: relative;
  -webkit-perspective: 800;
  margin: 10px;
  padding: 10px;
}
.tile {
  width: 100%;
  height: 100%;
  position: absolute;
  -webkit-transform-style: preserve-3d;
  -webkit-transition: -webkit-transform 0.6s;
  .tile-front, .tile-back {
    position: absolute;
    height: 100%;
    width: 100%;
    -webkit-backface-visibility: hidden;
  }
  .tile-front {
    background: $blue;
    -webkit-transition: -webkit-transform 0.3s;
    &:hover {
      background: lighten($blue, 10%);
      -webkit-transform: scale(1.1);
    }
  }
  .tile-back {
    background-color: orange;
    -webkit-transform: rotateY(180deg);
    &:hover {
      background: lighten($orange, 10%); 
    }
    img {
      width: 100%;
      height: 100%;
    }
    
  }
}
.tile.flipped {
     -webkit-transform: rotateY(180deg);
}
.disabled {
  pointer-events: none;
  opacity: 0.5;
}
            
          
!
            
              $(document).ready(function(){

    function memory(){
        //tile Constructor function
        function TileItem () {
            this.tile_type;
            this.addToScene = function(id, img) {
                var tileItem = '<li id="'+id+' data-type="'+this.tile_type+'"><div class="tile"><div class="tile-front"></div><div class="tile-back">'+img+'</div</div</li>';
                $('.tiles').append(tileItem);
            };
        }

        //vars
        var tiles = [];
        var tile = new TileItem();
        var num_tiles = 16;
        var openings = 0;
        var attempts = 0;
        var can_pick = true;
        var picked_tiles = [];
        var pictures = [
                                    '<img src="https://i.imgur.com/oTxUcRw.jpg"</img>',
                                    '<img src="https://i.imgur.com/IDHEiz2.jpg"></img>',
                                    '<img src="https://i.imgur.com/ONsXQ9a.jpg"></img>',
                                    '<img src="https://i.imgur.com/sPbp3pp.jpg"></img>',
                                    '<img src="https://i.imgur.com/cZHeXQy.jpg"></img>',
                                    '<img src="https://i.imgur.com/SUFRnQT.jpg"></img>',
                                    '<img src="https://i.imgur.com/3IAM1pP.jpg"></img>',
                                    '<img src="https://i.imgur.com/jIIfUw7.jpg"></img>',
                                    '<img src="https://i.imgur.com/oTxUcRw.jpg"</img>',
                                    '<img src="https://i.imgur.com/IDHEiz2.jpg"></img>',
                                    '<img src="https://i.imgur.com/ONsXQ9a.jpg"></img>',
                                    '<img src="https://i.imgur.com/sPbp3pp.jpg"></img>',
                                    '<img src="https://i.imgur.com/cZHeXQy.jpg"></img>',
                                    '<img src="https://i.imgur.com/SUFRnQT.jpg"></img>',
                                    '<img src="https://i.imgur.com/3IAM1pP.jpg"></img>',
                                    '<img src="https://i.imgur.com/jIIfUw7.jpg"></img>',
                                    ];
        //retrieves an img from pictures array
        function givePic(i) {
            return pictures[i];
        }
        
        //loop that creates tiles                                     
        for(var i=0; i<num_tiles; i++) {
            tiles.push(Math.floor(i/2));
        }

        //loop that randomizes the tiles within array
        var randomize, temp;
        for(var k=num_tiles-1; k>0; k--) {
            randomize = Math.floor(Math.random()*k);
            temp = tiles[k];
            tiles[k] = tiles[randomize];
            tiles[randomize] = temp;
        }

    //loop to place tiles with a random id
        for(var p=0; p<num_tiles; p++) {
            tile = new TileItem();
            var id = Math.floor(Math.random()*300);
            var img = givePic(p);
            tile.tile_type = tiles[p];
            tile.addToScene(id, img);
        }

        //tile click 
        function clicked() {

            if(can_pick) {
                var picked = $(this);
                picked.find('.tile').addClass('flipped');

                //add tiles selected to picked_tiles array
                if(picked_tiles.indexOf(picked) === -1) {
                    picked_tiles.push(picked);
                }

                //checks if 2 tiles have been clicked
                if(picked_tiles.length === 2) {
                    console.log('2 have been picked');
                    //don't allow more tiles to be picked yet
                    can_pick = false;
                    //keep track of attempts
                    attempts++;

                    //checks if 2 tiles match
                    if(picked_tiles[0].find('img').attr('src')===picked_tiles[1].find('img').attr('src')) {
                        console.log(picked_tiles[0]);
                        setTimeout(function(){
                            console.log('match');
                            picked_tiles[0].addClass('disabled');
                            picked_tiles[1].addClass('disabled');
                            picked_tiles = [];
                            can_pick = true;
                            //keeps track of pairs 'opened', if all pairs open, resetGame() is called
                            openings++;
                            if(openings === (num_tiles/2)) {
                                resetGame();
                            }
                        }, 1000);
                        //if 2 selections didn't match
                    } else {
                        setTimeout(function() {
                            console.log('they didnt match');
                            //reset our array that collects the selected tiles
                            picked_tiles[0].children().removeClass('flipped');
                            picked_tiles[1].children().removeClass('flipped');
                            picked_tiles = [];
                            can_pick = true;
                        }, 1000);
                    }
                }
            }
        }
        
        //shuffle DOM list 
        $('ul li').shuffle();
        //add event listeners to tiles
        var elements = $('li');
        for(var q=0; q<elements.length; q++){
            elements[q].addEventListener('click',clicked);
        }

        function resetGame(){
            alert("Congratulations! You took "+attempts+" attempts to complete.");
            $('.tiles').children().remove();
            memory();
        }
    }

    memory();

});
              
              //http://james.padolsey.com/javascript/shuffling-the-dom/
              //shuffle plugin
              
              (function($){
 
    $.fn.shuffle = function() {
 
        var allElems = this.get(),
            getRandom = function(max) {
                return Math.floor(Math.random() * max);
            },
            shuffled = $.map(allElems, function(){
                var random = getRandom(allElems.length),
                    randEl = $(allElems[random]).clone(true)[0];
                allElems.splice(random, 1);
                return randEl;
           });
 
        this.each(function(i){
            $(this).replaceWith($(shuffled[i]));
        });
 
        return $(shuffled);
 
    };
 
})(jQuery);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console