Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5.   <meta charset="utf-8">
  6.   <title>Memory</title>
  7.   <style>
  8.   *{
  9.     margin:0 auto;
  10.   }
  11.   #area{
  12.     width: 500px;
  13.   }
  14.   img{
  15.     width:120px;
  16.   }
  17.   h1{
  18.     text-align: center;
  19.     margin-bottom: 10px;
  20.   }
  21.   </style>
  22. </head>
  23. <body>
  24.   <h1>Memory</h1>
  25.   <div id="area">
  26.     <img src="img/0.jpg"/>
  27.     <img src="img/0.jpg"/>
  28.     <img src="img/0.jpg"/>
  29.     <img src="img/0.jpg"/>
  30.     <img src="img/0.jpg"/>
  31.     <img src="img/0.jpg"/>
  32.     <img src="img/0.jpg"/>
  33.     <img src="img/0.jpg"/>
  34.     <img src="img/0.jpg"/>
  35.     <img src="img/0.jpg"/>
  36.     <img src="img/0.jpg"/>
  37.     <img src="img/0.jpg"/>
  38.     <img src="img/0.jpg"/>
  39.     <img src="img/0.jpg"/>
  40.     <img src="img/0.jpg"/>
  41.     <img src="img/0.jpg"/>
  42.   </div>
  43.   <script>
  44.   //img:nth-child(4n+1){clear:both;}
  45.   var initImages = document.getElementsByTagName("img");
  46.   var cardsImagesSource = ["img/1.jpg","img/1.jpg","img/2.jpg","img/2.jpg","img/3.jpg","img/3.jpg","img/4.jpg","img/4.jpg",
  47.   "img/5.jpg","img/5.jpg","img/6.jpg","img/6.jpg","img/7.jpg","img/7.jpg","img/8.jpg","img/8.jpg"];
  48.   var shownCount = 0;
  49.   var choices = [];
  50.  
  51.   function showCard(x) {
  52.     initImages[x].setAttribute("src",cardsImagesSource[x]);
  53.   }
  54.  
  55.   function hideCard(x) {
  56.     initImages[x].setAttribute("src","img/0.jpg");
  57.   }
  58.  
  59.   function setAgainImages() {
  60.     for(var i = 0; i < initImages.length; i++) {
  61.       hideCard(i)
  62.     }
  63.   }  //startGame ones
  64.  
  65.   function shuffleArray(array) {
  66.     if (0 !== array.length) {
  67.       for (var i = array.length - 1; i >= 1; i--) {
  68.         var randomIndex = Math.floor(Math.random() * (i + 1)) //0-ostatniego
  69.         //console.log(randomIndex)
  70.         var temp = array[randomIndex];
  71.         array[randomIndex] = array[i];
  72.         array[i] = temp;
  73.         //console.log(array)
  74.       }
  75.       console.log(array)
  76.     }
  77.   }
  78.  
  79.   function startGame() {
  80.     setAgainImages();
  81.     shuffleArray(cardsImagesSource);
  82.   }
  83.  
  84.   function isToShow(x) {
  85.     if(x < 2) {
  86.       return true;
  87.     } else {
  88.       return false;
  89.     }
  90.   }
  91.  
  92.   function setupEventListener(j) {
  93.     initImages[j].addEventListener("click", function(){
  94.     var index = j;
  95.       if (isToShow(shownCount) == true) {
  96.         showCard(j)
  97.         choices.push(cardsImagesSource[j])
  98.         shownCount += 1;
  99.       }
  100.       if (isToShow(shownCount) == false) {
  101.         setTimeout(function(){
  102.         checkChoice();
  103.         choices = [];
  104.         shownCount = 0;
  105.         }, 2000);
  106.       }
  107.       console.log(choices)
  108.     }, false);
  109.   }  //pojawianie się kart
  110.  
  111.   function addEventListenerToShowCard() {
  112.     for (var i = 0 ; i < initImages.length; i++) {
  113.       setupEventListener(i);
  114.     }
  115.   }
  116.  
  117.   function checkChoice() {
  118.       if (choices[0] !== choices[1]) {
  119.         hideCard(); //that's the problemos
  120.         hideCard();
  121.         console.log(choices)
  122.       }
  123.   }
  124.  
  125.   if (1) {
  126.     startGame();
  127.     addEventListenerToShowCard();
  128.  
  129.   }
  130.   </script>
  131.   </body>
  132.   </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement