From 082f95cc8af2e131a3c25094da320715acda0adc Mon Sep 17 00:00:00 2001 From: AnantJain05 Date: Mon, 14 Feb 2022 13:34:59 +0530 Subject: [PATCH] Solution for the hacker card assignment of Javascript --- index.html | 2 +- main.js | 144 ++++++++++++++++++++++++++++++++++++++++++---------- scenario.js | 64 ++++++++++++++++++++++- style.css | 4 +- 4 files changed, 183 insertions(+), 31 deletions(-) diff --git a/index.html b/index.html index bb03a42..cc72f91 100644 --- a/index.html +++ b/index.html @@ -78,4 +78,4 @@

- \ No newline at end of file + diff --git a/main.js b/main.js index f242959..7ab6263 100644 --- a/main.js +++ b/main.js @@ -1,6 +1,6 @@ -// HELLO FELLOW CODERS! WE WELCOME YOU TO THE WORLD OF JAVASCRIPT +// HELLO FELLOW CODERS! WE WELCOME YOU TO THE WORLD OF JAVASCRIPT -//----- We've curated this assignment for someone staring out in exploring the beauty of JavaScript and would urge you to go through the resources shared with you before you start with this. ----- // +//----- We've curated this assignment for someone staring out in exploring the beauty of JavaScript and would urge you to go through the resources shared with you before you start with this. ----- // //Go through the CSS file as well to get a hold of all the attributes we've added. You're free to add some of your own and maybe delete some of ours xD @@ -8,7 +8,6 @@ // Your main work would be here, in main.js and would advice you to also pay a visit to the scenario.js - // Life of the player and the hacker. var playerLife = 5; var hackerLife = 5; @@ -23,68 +22,114 @@ var playerWinnerMessage = "Write the message here"; var playerStartLife = parseInt(playerLife); var hackerStartLife = parseInt(hackerLife); +var cardSelected = false; +var playedCardIndex; +var turn = 0; +var rounds = 0; -// we will declare the functions for you and you will complete those +// we will declare the functions for you and you will complete those updateScores(); // you learnt DOM manipulation right? here's an example of the same. Go ahead and use manipulate the DOM! document.querySelector(".game-board").classList.add("before-game"); var allCardElements = document.querySelectorAll(".card"); - +var hackerCard = allCardElements[0] ; // Adds click handler to all player card elements so that your cards are actionable - +for (var i=1; i<=3; i++){ + allCardElements[i].addEventListener("click", cardClicked.bind(null, allCardElements[i], i)); +} // An example of a function that controls what would happen when a card is clicked - -function cardClicked(cardEl) { +function cardClicked(cardEl, index) { if(cardSelected) { return; } cardSelected = true; cardEl.classList.add("played-card"); + playedCardIndex = index ; document.querySelector(".game-board").classList.add("card-selected"); + hackerPower = scenarios[turn]["hackerCard"]["power"]; + playerPower = scenarios[turn]["playerCards"][index-1]["power"]; + turn++ ; + if (turn===5){ + turn = 0; + rounds++; + } // Yes JS is cool and this would allow you to wait 500ms before revealing the hacker power setTimeout(function(){ - revealHackerPower(); + revealHackerPower(hackerPower); },500) setTimeout(function(){ - revealPlayerPower(); + revealPlayerPower(playerPower); },800) - + setTimeout(function(){ - compareCards(); + compareCards(hackerPower, playerPower, hackerCard, cardEl); }, 1400); } // Now write a function that shows the power level on the player card -function revealPlayerPower(){ - +function revealPlayerPower(playerPower){ + document.querySelector(".card.player-card.played-card .power").innerHTML = playerPower ; + document.querySelector(".card.player-card.played-card").classList.add("reveal-power") ; } // Write a function that shows the power level on the hacker card function revealHackerPower(){ - + document.querySelector(".card.hacker-card .power").innerHTML = hackerPower ; + document.querySelector(".card.hacker-card").classList.add("reveal-power") ; } -// Write a function to compare the cards. Here is where all your skills would come in handy! +// Write a function to compare the cards. Here is where all your skills would come in handy! // P.S: We've added the 'disabled' attribute in the CSS file for the button and you should use it in case you want a certain element to just go away or 'vanish' at a certain time. For eg: You'd definitely want the 'Next' button to go away after a player chooses a card right? -function compareCards(){ - +function compareCards(hackerPower, playerPower, hackerCard, playerCard){ + if (playerPower>hackerPower){ + hackerCard.classList.add("worse-card"); + playerCard.classList.add("better-card"); + hackerLife -= playerPower - hackerPower ; + } + else if(playerPower