Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 KB | None | 0 0
  1. (function () {
  2. let gameScene = document.querySelector("#game-scene");
  3.  
  4. let cardList = [{
  5. src: "img/card1.png",
  6. },
  7. {
  8. src: "img/card2.jpg",
  9. },
  10. ];
  11.  
  12. let patternList = [{
  13. label: "2/3",
  14. row: 2,
  15. column: 3,
  16. },
  17. {
  18. label: "3/4",
  19. row: 3,
  20. column: 4
  21. }
  22. ];
  23.  
  24. let imgList = [{
  25. src: "img/img1.png"
  26. },
  27. {
  28. src: "img/img2.png"
  29. },
  30. {
  31. src: "img/img3.png"
  32. },
  33. {
  34. src: "img/img4.png"
  35. },
  36. {
  37. src: "img/img5.png"
  38. },
  39. {
  40. src: "img/img6.png"
  41. },
  42. ];
  43.  
  44. // Configuration
  45. let config = {
  46. cardChoiceIndex: 0,
  47. patternChoiceIndex: 0
  48. };
  49.  
  50. // Runtime variable
  51. let attempts = 0,
  52. step = 0,
  53. cardFound = [],
  54. timerActive = false;
  55. start = null;
  56.  
  57. initMenu(cardList, patternList);
  58.  
  59. /**
  60. * Initialize menu
  61. * @param {Array<{ src: string }>} cardList The list of card back
  62. * @param {Array<{ label: string, row: number, column: number }>} patternList The list of possible patterns
  63. */
  64. function initMenu(cardList, patternList) {
  65. let cardChoiceContainer = document.querySelector("#card-choice");
  66.  
  67. let choices = new Array(cardList.length);
  68.  
  69. cardList.forEach(function (value, index) {
  70. let choice = document.createElement("img");
  71. choice.classList.add("card");
  72. if (index == config.cardChoiceIndex)
  73. choice.classList.add("active");
  74. choice.src = value.src;
  75.  
  76. choice.addEventListener("click", function (_) {
  77. if (index != config.cardChoiceIndex) {
  78. choices[config.cardChoiceIndex].classList.remove("active");
  79. choices[index].classList.add("active");
  80.  
  81. config.cardChoiceIndex = index;
  82. }
  83. });
  84.  
  85. choices[index] = choice;
  86. cardChoiceContainer.appendChild(choice);
  87. });
  88.  
  89. let patternChoiceContainer = document.querySelector("#pattern-choice");
  90.  
  91. let patterns = new Array(patternList.length);
  92.  
  93. patternList.forEach(function (pattern, index) {
  94. let patternElement = document.createElement("div");
  95. patternElement.classList.add("pattern");
  96. if (index == config.patternChoiceIndex)
  97. patternElement.classList.add("active");
  98. patternElement.innerHTML = pattern.label;
  99.  
  100. patternElement.addEventListener("click", function (_) {
  101. if (index != config.patternChoiceIndex) {
  102. patterns[config.patternChoiceIndex].classList.remove("active");
  103. patterns[index].classList.add("active");
  104.  
  105. config.patternChoiceIndex = index;
  106. }
  107. });
  108.  
  109. patterns[index] = patternElement;
  110. patternChoiceContainer.appendChild(patternElement);
  111. });
  112.  
  113. document.querySelector("#play").addEventListener("click", function (_) {
  114. document.querySelector("#menu").classList.add("hidden");
  115.  
  116. let card = cardList[config.cardChoiceIndex].src,
  117. pattern = patternList[config.patternChoiceIndex];
  118.  
  119. initGameBoard(gameScene, imgList, {
  120. card,
  121. row: pattern.row,
  122. column: pattern.column
  123. });
  124. });
  125. }
  126.  
  127. /**
  128. * Initialize game board container. Populate the container with interrogation mark image placeholder
  129. * @param {HTMLDivElement} gameScene The game scene div container
  130. * @param {Array<{ src: string }>} imgList List of images
  131. * @param {{ card: string, row: number, column: number }} config Configuration
  132. */
  133. function initGameBoard(gameScene, imgList, config) {
  134. gameScene.classList.remove("hidden");
  135.  
  136. let gameBoard = gameScene.querySelector("#game-board");
  137.  
  138. let state = new Array(config.row * config.column),
  139. list = imgList.slice(),
  140. blocks = new Array(config.row * config.column);
  141.  
  142. for (let i = 0; i < config.row; i++) {
  143. let rowElement = document.createElement("div");
  144. rowElement.classList.add("row");
  145.  
  146. for (let j = 0; j < config.column; j++) {
  147. let block = document.createElement("div");
  148. block.classList.add("block");
  149.  
  150. let cardBack = document.createElement("img");
  151. cardBack.classList.add("back");
  152. cardBack.src = config.card;
  153.  
  154. blocks[i * config.column + j] = block;
  155.  
  156. block.appendChild(cardBack);
  157.  
  158. rowElement.appendChild(block);
  159. }
  160.  
  161. gameBoard.appendChild(rowElement);
  162. }
  163.  
  164. let imageIndex = Math.floor(Math.random() * list.length);
  165.  
  166. let indexes = new Array(config.row * config.column);
  167. for (let i = 0; i < indexes.length; i++) indexes[i] = i;
  168.  
  169. while (indexes.length) {
  170. let i = Math.floor(Math.random() * indexes.length),
  171. index = indexes[i];
  172. indexes.splice(i, 1);
  173.  
  174. let b = blocks[index];
  175.  
  176. let img = b.querySelector(".image");
  177.  
  178. if (!img) {
  179. img = document.createElement("img");
  180. img.classList.add("image");
  181. }
  182.  
  183. img.src = list[imageIndex].src;
  184. state[index] = list[imageIndex].src;
  185.  
  186. if (list[imageIndex].usedOnce) {
  187. list.splice(imageIndex, 1);
  188. imageIndex = Math.floor(Math.random() * list.length);
  189. } else
  190. list[imageIndex].usedOnce = true;
  191.  
  192. b.appendChild(img);
  193. }
  194.  
  195. // Event handler for each card block
  196. blocks.forEach(block => {
  197. block.addEventListener("click", () => {
  198. if (!attempts) {
  199. start = Date.now();
  200. timerActive = true;
  201. requestAnimationFrame(refreshTimer);
  202. }
  203.  
  204. if (block.classList.contains("active")) {
  205. step--;
  206. setTimeout(() => {
  207. block.querySelector(".image").classList.remove("active");
  208. setTimeout(() => block.classList.remove("active"), 200);
  209. }, 600);
  210. } else {
  211. attempts++;
  212. step++;
  213. document.querySelector("#attempts").innerHTML = `${attempts}`;
  214.  
  215. block.classList.add("active");
  216.  
  217. setTimeout(() => {
  218. block.querySelector(".image").classList.add("active");
  219. if (step == 2) {
  220. let arr = blocks.filter(b => b.classList.contains("active"));
  221.  
  222. setTimeout(() => {
  223. if (arr[0].querySelector(".image").src == arr[1].querySelector(".image").src) {
  224. let secElapsed = new Date(Date.now() - start);
  225. cardFound.push({
  226. src: arr[0].querySelector(".image").src
  227. });
  228. addCardFound({
  229. src: arr[0].querySelector(".image").src
  230. });
  231. arr.forEach(el => {
  232. el.querySelector(".image").classList.remove("active");
  233. setTimeout(() => {
  234. el.classList.remove("active");
  235. el.classList.add("found");
  236. }, 400);
  237. });
  238. if (cardFound.length == (config.row * config.column) / 2) {
  239. // Game finished!!!
  240. timerActive = false;
  241. }
  242. } else {
  243. arr.forEach(el => {
  244. el.querySelector(".image").classList.remove("active");
  245. setTimeout(() => el.classList.remove("active"), 400);
  246. });
  247. }
  248. step = 0;
  249. }, 600);
  250. }
  251. }, 200);
  252. }
  253. });
  254. });
  255. }
  256.  
  257. function addCardFound({
  258. src
  259. }) {
  260. let cardFound = document.querySelector("#card-found>#list"),
  261. img = document.createElement("img");
  262.  
  263. img.src = src;
  264. cardFound.appendChild(img);
  265. }
  266.  
  267. function refreshTimer() {
  268. let diff = new Date(Date.now() - start),
  269. min = diff.getMinutes(),
  270. sec = diff.getSeconds(),
  271. ms = Math.floor(diff.getMilliseconds() / 10);
  272.  
  273. document.querySelector("#timer").innerHTML = `${min < 10 ? '0' : ''}${min}:${sec < 10 ? '0' : ''}${sec}:${ms < 10 ? '0' : ''}${ms}`;
  274.  
  275. if (timerActive)
  276. requestAnimationFrame(refreshTimer);
  277. }
  278. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement