Advertisement
Guest User

cada oveja

a guest
Oct 22nd, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. div#memory_board{
  6. background:#CCC;
  7. border:#999 1px solid;
  8. width:900px;
  9. height:900px;
  10. padding:24px;
  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:71px;
  17. height:71px;
  18. float:left;
  19. margin:10px;
  20. padding:20px;
  21. font-size:64px;
  22. cursor:pointer;
  23. text-align:center;
  24. }
  25. </style>
  26. <script>
  27.  
  28.  
  29. var memory_array = ['A','A','B','B','C','C','D','D','E','E','F','F','G','G','H','H','I','I','J','J','K','K','L','L'];
  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 memoryFlipTile(tile,val){
  52. if(tile.innerHTML === "" && memory_values.length < 2){
  53. tile.style.background = '#FFF';
  54. tile.innerHTML = val;
  55. if(memory_values.length === 0){
  56. memory_values.push(val);
  57. memory_tile_ids.push(tile.id);
  58. } else if(memory_values.length === 1){
  59. memory_values.push(val);
  60. memory_tile_ids.push(tile.id);
  61. if(memory_values[0] === memory_values[1]){
  62. tiles_flipped += 2;
  63. // Clear both arrays
  64. memory_values = [];
  65. memory_tile_ids = [];
  66. // Check to see if the whole board is cleared
  67. if(tiles_flipped === memory_array.length){
  68. alert("you win");
  69. document.getElementById('memory_board').innerHTML = "";
  70.  
  71. }
  72. } else {
  73. function flip2Back(){
  74. // Flip the 2 tiles back over
  75. var tile_1 = document.getElementById(memory_tile_ids[0]);
  76. var tile_2 = document.getElementById(memory_tile_ids[1]);
  77. tile_1.style.background = 'url(tile_bg.jpg) no-repeat';
  78. tile_1.innerHTML = "";
  79. tile_2.style.background = 'url(tile_bg.jpg) no-repeat';
  80. tile_2.innerHTML = "";
  81. // Clear both arrays
  82. memory_values = [];
  83. memory_tile_ids = [];
  84. }
  85. setTimeout(flip2Back, 700);
  86. }
  87. }
  88. }
  89. }
  90. </script>
  91.  
  92. </head>
  93. <body>
  94. <button type="button" class="btn btn-default" onclick="newBoard();">Reset</button>
  95. <div id="memory_board"></div>
  96. <script>newBoard();</script>
  97.  
  98. </body>
  99. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement