Advertisement
Guest User

i need more information

a guest
May 26th, 2019
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.    // let player1History = 0;
  3.    // let player2History = 0;
  4.  
  5.  
  6.    // let player1Images = document.querySelectorAll("div#player1Div img");
  7.    // for (let i = 0; i < player1Images.length; i++) {
  8.    //    player1Images[i].addEventListener("click", function () {
  9.    //       player1Images[i].src = "images/whiteCard.jpg";
  10.  
  11.    //       let player1Strength = document.getElementsByTagName("span")[0];
  12.    //       player1Strength.textContent = Number(player1Images[i].name);
  13.  
  14.    //       player1History = (Number(player1Images[i].name));
  15.    //    });
  16.    // }
  17.  
  18.    // let player2Images = document.querySelectorAll("div#player2Div img");
  19.    // for (let i = 0; i < player2Images.length; i++) {
  20.    //    player2Images[i].addEventListener("click", function () {
  21.    //       player2Images[i].src = "images/whiteCard.jpg";
  22.    //       let player2Strength = document.getElementsByTagName("span")[2];
  23.    //       player2Strength.textContent = Number(player2Images[i].name);
  24.  
  25.    //       player2History = (Number(player2Images[i].name));
  26.  
  27.    //       if (player1History < player2History) {
  28.    //          player2Images[i].style.border = "2px solid green";
  29.    //       } else if (player1History[i] > player2History[i]) {
  30.    //          player2Images[i].style.border = "2px solid red";
  31.    //       }
  32.    //    });
  33.    // }
  34.  
  35.  
  36.    let p1CardIndex = 0;
  37.    let p2CardIndex = 0;
  38.    let p1CardValue = 0;
  39.    let p2CardValue = 0;
  40.    const cardHistory = [];
  41.  
  42.    let images = document.querySelectorAll("section.cards img");
  43.    for (let i = 0; i < images.length; i++) {
  44.       images[i].addEventListener("click", function (e) {
  45.          const currentImg = e.currentTarget
  46.          currentImg.src = "images/whiteCard.jpg";
  47.          const imgName = currentImg.name
  48.  
  49.          const parentId = e.target.parentElement.id
  50.          if (parentId === "player1Div") {
  51.             let player1Result = document.getElementsByTagName("span")[0];
  52.             player1Result.textContent = imgName;
  53.             p1CardValue = imgName
  54.             p1CardIndex = i
  55.          } else if (parentId === "player2Div") {
  56.             let player2Result = document.getElementsByTagName("span")[2];
  57.             player2Result.textContent = imgName;
  58.             p2CardValue = imgName
  59.             p2CardIndex = i
  60.          }
  61.  
  62.          if (p1CardIndex > 0 && p2CardIndex > 0) {
  63.             if (p1CardValue < p2CardValue) {
  64.                images[p1CardIndex].style.border = "2px solid red";
  65.                images[p2CardIndex].style.border = "2px solid green";
  66.             } else if (p1CardValue > p2CardValue) {
  67.                images[p1CardIndex].style.border = "2px solid green";
  68.                images[p2CardIndex].style.border = "2px solid red";
  69.             }
  70.             cardHistory.push(`[${p1CardValue} vs ${p2CardValue}]`)
  71.             p1CardIndex = null;
  72.             p2CardIndex = null;
  73.             p1CardValue = null;
  74.             p2CardValue = null;
  75.             // player1History = (Number(currentImg.name));
  76.             let history = document.getElementById("history");
  77.             history.textContent = cardHistory.join(' ');
  78.             console.log(cardHistory)
  79.          }
  80.  
  81.       });
  82.    }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement