Advertisement
Guest User

working

a guest
Oct 18th, 2015
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. div#memory_board{
  6.     background:#CCC;
  7.     border:#999 1px solid;
  8.     width:795px;
  9.     height:340px;
  10.     padding:10px;
  11.     margin:0px auto;
  12. }
  13. div#memory_board > div{
  14.     background: url(tile_bg.jpg) no-repeat;
  15.     border:#000 1px solid;
  16.     width:90px;
  17.     height:43px;
  18.     float:left;
  19.     margin:10px;
  20.     padding:10px;
  21.     font-size:36px;
  22.     cursor:pointer;
  23.     text-align:center;
  24. }
  25. </style>
  26. <script>
  27.     // Scripted By Adam Khoury in connection with the following video tutorial:
  28.     // http://www.youtube.com/watch?v=c_ohDPWmsM0
  29.     var memory_array = ['13%','0.13','25%','1/4','1/3','0.33','0.7','70%','.5','1/2','1/8','12.5%','2/5','40%','3/4','75%','3/5','0.60','20%','0.20','1/10','10%','30%','0.30'];
  30.     var memory_values = [];
  31.     var memory_tile_ids = [];
  32.     var tiles_flipped = 0;
  33.     Array.prototype.memory_tile_shuffle = function(){
  34.         var i = this.length, j, temp;
  35.         while(--i > 0){
  36.             j = Math.floor(Math.random() * (i+1));
  37.             temp = this[j];
  38.             this[j] = this[i];
  39.             this[i] = temp;
  40.         }
  41.     }
  42.     function newBoard(){
  43.         tiles_flipped = 0;
  44.         var output = '';
  45.         memory_array.memory_tile_shuffle();
  46.         for(var i = 0; i < memory_array.length; i++){
  47.             output += '<div id="tile_'+i+'" onclick="memoryFlipTile(this,\''+memory_array[i]+'\')"></div>';
  48.         }
  49.         document.getElementById('memory_board').innerHTML = output;
  50.     }
  51.     function endsWith(str, suffix) { return str.slice(-suffix.length) === suffix }
  52.     function memoryFlipTile(tile,val,val2){
  53.         if(tile.innerHTML == "" && memory_values.length < 2){
  54.             tile.style.background = '#FFF';
  55.             tile.innerHTML = val;
  56.             if(memory_values.length == 0){
  57.                 memory_values.push(val);
  58.                 memory_tile_ids.push(tile.id);
  59.             } else if(memory_values.length == 1){
  60.                 memory_values.push(val);
  61.                 memory_tile_ids.push(tile.id);
  62.                 if(endsWith(memory_values[0], '%')) {
  63.                     memory_values[0] = parseFloat(memory_values[0])/100.0;
  64.                 }
  65.                 if(endsWith(memory_values[1], '%')) {
  66.                     memory_values[1] = parseFloat(memory_values[1])/100.0;
  67.                 }
  68.                 if(eval(memory_values[0]) == eval(memory_values[1])){
  69.                     tiles_flipped += 2;
  70.                     // Clear both arrays
  71.                     memory_values = [];
  72.                     memory_tile_ids = [];
  73.                     // Check to see if the whole board is cleared
  74.                     if(tiles_flipped == memory_array.length){
  75.                         alert("Board cleared... generating new board");
  76.                         document.getElementById('memory_board').innerHTML = "";
  77.                         newBoard();
  78.                     }
  79.                 } else {
  80.                     function flip2Back(){
  81.                         // Flip the 2 tiles back over
  82.                         var tile_1 = document.getElementById(memory_tile_ids[0]);
  83.                         var tile_2 = document.getElementById(memory_tile_ids[1]);
  84.                         tile_1.style.background = 'url(tile_bg.jpg) no-repeat';
  85.                         tile_1.innerHTML = "";
  86.                         tile_2.style.background = 'url(tile_bg.jpg) no-repeat';
  87.                         tile_2.innerHTML = "";
  88.                         // Clear both arrays
  89.                         memory_values = [];
  90.                         memory_tile_ids = [];
  91.                     }
  92.                     setTimeout(flip2Back, 700);
  93.                 }
  94.             }
  95.         }
  96.     }
  97. </script>
  98. </head>
  99. <body>
  100. <div id="memory_board"></div>
  101. <script>newBoard();</script>
  102.     <p id="test"></p>
  103. </body>
  104. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement