diff --git a/main.js b/main.js index f242959..274e362 100644 --- a/main.js +++ b/main.js @@ -14,8 +14,8 @@ var playerLife = 5; var hackerLife = 5; // Message to be displayed when the game is over -var hackerWinnerMessage = "Write the message here"; -var playerWinnerMessage = "Write the message here"; +var hackerWinnerMessage = "Womp Womp Womp, the hacker has won!"; +var playerWinnerMessage = "Hurrah! You defeated the Hacker!"; // ---------------Game code starts here ---------------// @@ -23,6 +23,7 @@ var playerWinnerMessage = "Write the message here"; var playerStartLife = parseInt(playerLife); var hackerStartLife = parseInt(hackerLife); +var cardSelected = false; // we will declare the functions for you and you will complete those updateScores(); @@ -33,7 +34,11 @@ document.querySelector(".game-board").classList.add("before-game"); var allCardElements = document.querySelectorAll(".card"); // Adds click handler to all player card elements so that your cards are actionable - +for(let i=0; i cardClicked(allCardElements[i])); + } +} // An example of a function that controls what would happen when a card is clicked @@ -52,7 +57,7 @@ function cardClicked(cardEl) { },500) setTimeout(function(){ - revealPlayerPower(); + revealPlayerPower(cardEl); },800) setTimeout(function(){ @@ -61,36 +66,102 @@ function cardClicked(cardEl) { } // Now write a function that shows the power level on the player card -function revealPlayerPower(){ - +function revealPlayerPower(cardEl){ + cardEl.classList.add("reveal-power"); } // Write a function that shows the power level on the hacker card function revealHackerPower(){ - + document.querySelector('.hacker-card').classList.add("reveal-power"); } // 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(){ + let playedCard = document.querySelector(".played-card"); + let hackerCard = document.querySelector(".hacker-card"); + + let playerPoint = parseInt(playedCard.querySelector(".power").innerHTML); + let hackerPoint = parseInt(hackerCard.querySelector(".power").innerHTML); + + let hackerIcon = document.querySelector(".hacker-stats .thumbnail"); + let playerIcon = document.querySelector(".player-stats .thumbnail"); + + if(playerPoint>hackerPoint) { + playedCard.classList.add("better-card"); + hackerCard.classList.add("worse-card"); + hackerIcon.classList.add("ouch"); + hackerLife -= (playerPoint-hackerPoint); + } else if(hackerPoint>playerPoint) { + playedCard.classList.add("worse-card"); + hackerCard.classList.add("better-card"); + playerIcon.classList.add("ouch"); + playerLife -= (hackerPoint-playerPoint); + } else { + playedCard.classList.add("tie-card"); + hackerCard.classList.add("tie-card"); + } + + updateScores(); + + if(playerLife<=0) { + gameOver(0); // hacker wins + } else if(hackerLife<=0) { + gameOver(1); // player wins + } + document.querySelector(".next-turn").removeAttribute("disabled"); } //Use conditional statements and complete the function that shows the winner message function gameOver(winner) { - + const winMessage = document.querySelector(".winner-message"); + if(winner == 0) { + winMessage.textContent = hackerWinnerMessage; + } else if (winner == 1) { + winMessage.textContent = playerWinnerMessage; + } else { + winMessage.textContent = "Error"; // for my reference + } + + document.querySelector(".game-board").classList.remove("during-game"); + document.querySelector(".game-board").classList.add("game-over"); + document.querySelector(".winner-section").style.display = "flex"; //changing it from none } // Write a function that starts the game function startGame() { - + document.querySelector(".game-board").classList.remove("before-game"); + document.querySelector(".game-board").classList.add("during-game"); + playTurn(); } // Now write a function that starts the game over from scratch function restartGame(){ + // revert all changed made during the game + playerLife = parseInt(playerStartLife); + hackerLife = parseInt(hackerStartLife); + + let playedCard = document.querySelector(".played-card"); + playedCard.classList.remove("played-card"); + + document.querySelector(".game-board").classList.remove("card-selected"); + cardSelected = false; + + document.querySelector(".game-board").classList.remove("game-over"); + document.querySelector(".game-board").classList.add("before-game"); + // left more + document.querySelector(".winner-section").style.display = "none"; + document.querySelector(".next-turn").removeAttribute("disabled"); + + for(let i=0; i Math.random() - 0.5); +} // Write a function that Plays one turn of the game function playTurn() { + document.querySelector(".game-board").classList.remove("card-selected"); + cardSelected = false; + document.querySelector(".next-turn").setAttribute("disabled", true); + + let hackerIcon = document.querySelector(".hacker-stats .thumbnail"); + let playerIcon = document.querySelector(".player-stats .thumbnail"); + hackerIcon.classList.remove("ouch"); + playerIcon.classList.remove("ouch"); + + for(let i=0; i