Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <style type="text/css">
  5.  
  6.  
  7.         #memory_board{
  8.             background: #CCC;
  9.             border: #999 1px solid;
  10.             width: 400px;
  11.             height: 289px;
  12.             padding: 24px;
  13.             margin: 0px auto;
  14.             background: url(tausta.jpg) no-repeat;
  15.             background-size: 100%;
  16.             z-index: 1;
  17.             /*filter: blur(2px);
  18.             -webkit-filter: blur(2px);*/
  19.            
  20.         }
  21.         .card {
  22.             background: url(background.jpg) no-repeat top center;
  23.             background-size:111px 111px;
  24.             border: #000 1px solid;
  25.             width: 71px;
  26.             height:71px;
  27.             float:left;
  28.             margin:10px;
  29.             padding:20px;
  30.             font-size:64px;
  31.             cursor:pointer;
  32.             text-align:center;
  33.             z-index: 2;
  34.  
  35.            
  36.  
  37.         }
  38.  
  39.  
  40.     </style>
  41.     <script type="text/javascript">
  42.     var memory_array = ['A','A','B','B','C','C'];
  43.     var memory_values = []; // For storing the memory values
  44.     var memory_tile_ids = []; // For storing the memory tile ids
  45.     var tiles_flipped = 0; // Keep track of the flipped tiles
  46.     var matches = 0;
  47.  
  48.     //Suffle method
  49.     Array.prototype.memory_tile_shuffle = function(){
  50.         var i = this.length, j, temp;
  51.         while(--i > 0){
  52.             j = Math.floor(Math.random() * (i+1));
  53.             temp = this[j];
  54.             this[j] = this[i];
  55.             this[i] = temp;
  56.         }
  57.     }
  58.     // Create a new board
  59.     function newBoard(){
  60.         tiles_flipped = 0; // Init all the tiles
  61.         var output = '';
  62.         memory_array.memory_tile_shuffle(); // Shuffle the tiles
  63.  
  64.         // Loop over all of the cards and add the tiles to output
  65.         // Each div get id that matches the tile number
  66.         // Add click-event to every div -> every tile/card will have own memory_array character A, B, C etc...
  67.         for(var i = 0; i < memory_array.length; i++){
  68.             output += '<div class="card" id="tile_'+i+'" onclick="memoryFlipTile(this,\''+memory_array[i]+'\')"></div>';
  69.         }
  70.         // Place the output to #memory_board
  71.         document.getElementById('memory_board').innerHTML = output;
  72.     }
  73.     function memoryFlipTile(tile,val){
  74.         // Check if tile is empty and memory_values-array smaller than 2 <-- True, if game is starting
  75.         if(tile.innerHTML == "" && memory_values.length < 2){
  76.             tile.style.background = '#FFF'; // background of a tile = white
  77.             if (tiles_flipped == memory_array.length -2 ){
  78.                 tile.style.background = 'url(troll.jpg) no-repeat top center / 111px 111px';
  79.             }
  80.             else {
  81.                 tile.innerHTML = val; // When tile/card is clicked, it gets value taht is memory_array[i]
  82.             }
  83.  
  84.             // if memory_values is empty
  85.             if (memory_values.length == 0){
  86.                 memory_values.push(val); // push val to array
  87.                 memory_tile_ids.push(tile.id); // push id to array
  88.  
  89.             // if memory_values already has a value
  90.             } else if(memory_values.length == 1){
  91.                 memory_values.push(val); // push val to array
  92.                 memory_tile_ids.push(tile.id); // push id to array
  93.  
  94.                 // if both cards match
  95.                 if(memory_values[0] == memory_values[1]) {
  96.                     tiles_flipped += 2; // set tiles_flipped to 2
  97.  
  98.                     // Hide both cards
  99.                     var tile_1 = document.getElementById(memory_tile_ids[0]);
  100.                     var tile_2 = document.getElementById(memory_tile_ids[1]);
  101.                     tile_1.style.visibility = 'hidden';
  102.                     tile_2.style.visibility = 'hidden';
  103.  
  104.                     //  Clear both arrays
  105.                     memory_values = [];
  106.                     memory_tile_ids = [];
  107.                     matches = matches + 1;
  108.                     document.getElementById('matchNumber').innerHTML = matches;
  109.                     document.getElementById('memory_board').style.background = 'blur 10px';
  110.  
  111.  
  112.                     // Check to see if the whole board is cleared
  113.                     if (tiles_flipped == memory_array.length){ // if all the tiles are flipped over (all matches have been made)
  114.                         //document.getElementById('memory_board').style.background = 'url(background.jpg) no-repeat';
  115.                         //window.open("http://www.google.fi");
  116.                         alert("Voitit pelin");
  117.                         document.getElementById('memory_board').innerHTML = "";
  118.                         matches = 0;
  119.                         document.body.requestFullScreen();
  120.                         newBoard();
  121.                     }
  122.  
  123.                 // if no match
  124.                 } else {
  125.  
  126.                     // Flip the 2 tiles back over
  127.                     function flip2Back() {
  128.                        
  129.                         var tile_1 = document.getElementById(memory_tile_ids[0]);
  130.                         var tile_2 = document.getElementById(memory_tile_ids[1]);
  131.                         tile_1.style.background = 'url(background.jpg) no-repeat top center / 111px 111px';
  132.                         tile_1.innerHTML = "";
  133.                         tile_2.style.background = 'url(background.jpg) no-repeat top center / 111px 111px';
  134.                         tile_2.innerHTML = "";
  135.  
  136.                         // Clear both arrays after flip
  137.                         memory_values = [];
  138.                         memory_tile_ids = [];
  139.                     }
  140.  
  141.                     // Flip cards back, time 700ms
  142.                     setTimeout(flip2Back, 700);
  143.  
  144.                 }
  145.             }
  146.         }
  147.     }
  148.     </script>
  149. </head>
  150. <body>
  151.     <header>
  152.         <div id="memory_board"></div>
  153.             <script>newBoard();</script>
  154.         <div id="matchNumber"></div>
  155.     </header>
  156.     </body>
  157.     </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement