Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. const tile = document.querySelector('.game-board');
  2. const btn = document.querySelector('.game-start');
  3. let modeVal = document.getElementsByTagName('option');
  4.  
  5. const max = 8;
  6. const min = 1;
  7.  
  8. var a = undefined;
  9. var m = undefined;
  10. var x = undefined;
  11.  
  12. let tilesChecked = [];
  13.  
  14. let arr = [];
  15.  
  16. let mat = Math.floor(Math.random() * (max - min) + min);
  17.  
  18. function shuffle(arr) {
  19. var currentIndex = arr.length,
  20. temporaryValue,
  21. randomIndex;
  22.  
  23. while (0 !== currentIndex) {
  24. randomIndex = Math.floor(Math.random() * currentIndex);
  25. currentIndex -= 1;
  26.  
  27. temporaryValue = arr[currentIndex];
  28. arr[currentIndex] = arr[randomIndex];
  29. arr[randomIndex] = temporaryValue;
  30. }
  31.  
  32. return arr;
  33. }
  34.  
  35. document.addEventListener(
  36. 'input',
  37. function(e) {
  38. if (e.target.value === 'easy') {
  39. x = 1;
  40. } else if (e.target.value === 'medium') {
  41. x = 2;
  42. } else if (e.target.value === 'hard') {
  43. x = 3;
  44. } else {
  45. x = undefined;
  46. }
  47. },
  48. false
  49. );
  50.  
  51. //modeVal = modeVal[x].label;
  52.  
  53. const click = function() {
  54. if (modeVal[x].label === 'Easy') {
  55. y = 4;
  56. m = 'e';
  57. } else if (modeVal[x].label === 'Medium') {
  58. y = 8;
  59. m = 'm';
  60. } else if (modeVal[x].label === 'Hard') {
  61. y = 16;
  62. m = 'h';
  63. } else {
  64. alert('error');
  65. }
  66. for (let i = 0; i < y / 2; i++) {
  67. let a = tiles.splice(
  68. Math.floor(Math.random() * (tiles.length - 1) + 1),
  69. 1
  70. )[0];
  71. arr.push(a);
  72. arr.push(a);
  73. shuffle(arr);
  74. }
  75. for (let i = 0; i < y; i++) {
  76. var tl = document.createElement('div');
  77. tl.className = `${m}tile`;
  78. tl.innerHTML = `<img src=${arr[i]} class="display-none" id='a'/> <img src=tile.png class="display" id='a' >`;
  79. a = i;
  80. tl.id = i;
  81. tile.appendChild(tl);
  82.  
  83. tl.addEventListener('click', clicked);
  84. }
  85. };
  86.  
  87. const clicked = function(e) {
  88. e.target.className = 'display-none';
  89. e.target.previousElementSibling.className = 'display';
  90. tilesChecked.push(e.target.previousElementSibling.src);
  91. console.log(tilesChecked[0]);
  92. console.log(tilesChecked[1]);
  93. if (tilesChecked.length === 2) {
  94. if (tilesChecked[0] === tilesChecked[1]) {
  95. console.log('dupa');
  96. deleteTiles();
  97. //e.target.previousElementSibling = null;
  98. //tilesChecked[0].remove();
  99. //tilesChecked[1].remove();
  100. //console.log(tilesChecked);
  101. //tilesChecked = [];
  102. }
  103. }
  104. };
  105.  
  106. let deleteTiles = function() {
  107. tilesChecked[0].remove();
  108. tilesChecked[1].remove();
  109. console.log(tilesChecked);
  110. tilesChecked = [];
  111. };
  112.  
  113. btn.addEventListener('click', click);
  114.  
  115. let tiles = [
  116. 'tile_1.png',
  117. 'tile_2.png',
  118. 'tile_3.png',
  119. 'tile_4.png',
  120. 'tile_5.png',
  121. 'tile_6.png',
  122. 'tile_7.png',
  123. 'tile_8.png',
  124. 'tile_9.png',
  125. 'tile_10.png'
  126. ];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement