Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 152 additions & 42 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,126 @@

// 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;

// Message to be displayed when the game is over
var hackerWinnerMessage = "Write the message here";
var playerWinnerMessage = "Write the message here";
var hackerWinnerMessage = "Oops! You got hacked";
var playerWinnerMessage = "You are safe";

// ---------------Game code starts here ---------------//

// declare a few handy variables like we've done :p

var playerStartLife = parseInt(playerLife);
var hackerStartLife = parseInt(hackerLife);
var startbutton=document.querySelector(".start-game");
var hackercard=document.querySelector(".card.hacker-card.hacker-color")
var playercard=document.querySelectorAll(".card.player-card.player-color");
var cardSelected;
var hackercardpower;
var playercardpower;
var selectedplayercard;
var i=0;


// we will declare the functions for you and you will complete those
updateScores();
// Finally write the function that reveals the cards. Use
function revealCards(){
if(i==3)
i=0;
document.querySelectorAll(".card")[0].style.opacity=10;
document.querySelectorAll(".card")[1].style.opacity=10;
document.querySelectorAll(".card")[2].style.opacity=10;
document.querySelectorAll(".card")[3].style.opacity=10;
hackercard.querySelector(".text").textContent=scenarios[i].hackerCard.description;
playercard[0].querySelector(".text").textContent=scenarios[i].playerCards[0].description;
playercard[1].querySelector(".text").textContent=scenarios[i].playerCards[1].description;
playercard[2].querySelector(".text").textContent=scenarios[i].playerCards[2].description;
i++;
cardSelected=false;
}

// 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");

// Adds click handler to all player card elements so that your cards are actionable
// We've also used a cool life bar that displays the life left. Write a function that updates the displayed life bar and life totals
// use innerHTML and a lot of other cool things to do this.
function updateScores(){

// Here these update life totals for each player
document.querySelector(".player-stats .life-total").innerHTML = playerLife;

// We've added the code to update the player lifebar
var playerPercent = playerLife / playerStartLife * 100;
if (playerPercent < 0) {
playerPercent = 0;
}
document.querySelector(".player-stats .life-left").style.height = playerPercent + "%";

// Now you write the code to update the hacker lifebar
document.querySelector(".hacker-stats .life-total").innerHTML = hackerLife;
var hackerPercent = hackerLife / hackerStartLife * 100;
if (hackerPercent < 0) {
hackerPercent = 0;
}
document.querySelector(".hacker-stats .life-left").style.height = hackerPercent + "%";
if(playerPercent>0 && hackerPercent>0)
document.querySelector("button.next-turn").style.display="block";
else
gameOver();
}
document.querySelector("button.next-turn").style.display="none";

// Now write a function that shows the power level on the player card
function revealPlayerPower(){
if(selectedplayercard==playercard[0])
{playercard[0].querySelector(".power").textContent=scenarios[i-1].playerCards[0].power;
playercardpower=scenarios[i-1].playerCards[0].power;}
else if(selectedplayercard==playercard[1])
{playercard[1].querySelector(".power").textContent=scenarios[i-1].playerCards[1].power;
playercardpower=scenarios[i-1].playerCards[1].power;}
else
{playercard[2].querySelector(".power").textContent=scenarios[i-1].playerCards[2].power;
playercardpower=scenarios[i-1].playerCards[2].power;}
}

// Write a function that shows the power level on the hacker card
function revealHackerPower(){

hackercard.querySelector(".power").textContent=scenarios[i-1].hackerCard.power;
hackercardpower=scenarios[i-1].hackerCard.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()
{
if(hackercardpower>playercardpower)
{
playerLife=playerLife-hackercardpower+playercardpower;
hackercard.classList.add("better-card");
selectedplayercard.classList.add("worse-card");
document.querySelector(".player-area .thumbnail").classList.add("ouch");
}

else if(hackercardpower<playercardpower)
{
hackerLife=hackerLife+hackercardpower-playercardpower;
selectedplayercard.classList.add("better-card");
hackercard.classList.add("worse-card");
document.querySelector(".hacker-area .thumbnail").classList.add("ouch");
}
else
{selectedplayercard.classList.add("tie-card");
hackercard.classList.add("tie-card");}
updateScores();
}


// An example of a function that controls what would happen when a card is clicked
Expand All @@ -42,10 +137,12 @@ function cardClicked(cardEl) {
if(cardSelected) { return; }
cardSelected = true;

selectedplayercard=cardEl;

cardEl.classList.add("played-card");

document.querySelector(".game-board").classList.add("card-selected");

// Yes JS is cool and this would allow you to wait 500ms before revealing the hacker power
setTimeout(function(){
revealHackerPower();
Expand All @@ -60,30 +157,40 @@ function cardClicked(cardEl) {
}, 1400);
}

// Now write a function that shows the power level on the player card
function revealPlayerPower(){

}

// Write a function that shows the power level on the hacker card
function revealHackerPower(){

}
// 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(){

}
// Adds click handler to all player card elements so that your cards are actionable
playercard[0].addEventListener('click',function(e){
if(e.target.parentNode==playercard[0])
{cardClicked(e.target.parentNode);}
else
{cardClicked(e.target)}});

playercard[1].addEventListener('click',function(e){
if(e.target.parentNode==playercard[1])
cardClicked(e.target.parentNode);
else
cardClicked(e.target)});

playercard[2].addEventListener('click',function(e){
if(e.target.parentNode==playercard[2])
cardClicked(e.target.parentNode);
else
cardClicked(e.target)});

//Use conditional statements and complete the function that shows the winner message
function gameOver(winner) {

function gameOver() {
if(playerLife<=0)
document.querySelector(".winner-section .winner-message").textContent=hackerWinnerMessage;
else
document.querySelector(".winner-section .winner-message").textContent=playerWinnerMessage;
document.querySelector(".winner-section").style.display="block";
}


// Write a function that starts the game
function startGame() {
startbutton.parentNode.removeChild(startbutton);
revealCards();


}

Expand All @@ -93,32 +200,35 @@ function restartGame(){

}

// We've also used a cool life bar that displays the life left. Write a function that updates the displayed life bar and life totals
// use innerHTML and a lot of other cool things to do this.
function updateScores(){

// Here these update life totals for each player
document.querySelector(".player-stats .life-total").innerHTML = playerLife;

// We've added the code to update the player lifebar
var playerPercent = playerLife / playerStartLife * 100;
if (playerPercent < 0) {
playerPercent = 0;
}
document.querySelector(".player-stats .life-left").style.height = playerPercent + "%";

// Now you write the code to update the hacker lifebar

}



// Write a function that Plays one turn of the game
function playTurn() {

document.querySelector("button.next-turn").style.display="none";
document.querySelectorAll(".card")[0].style.opacity=0;
document.querySelectorAll(".card")[1].style.opacity=0;
document.querySelectorAll(".card")[2].style.opacity=0;
document.querySelectorAll(".card")[3].style.opacity=0;
if(hackercardpower>playercardpower)
{ selectedplayercard.classList.remove("worse-card");
hackercard.classList.remove("better-card");
document.querySelector(".player-area .thumbnail").classList.remove("ouch");
}
else if(hackercardpower<playercardpower)
{
selectedplayercard.classList.remove("better-card");
hackercard.classList.remove("worse-card");
document.querySelector(".hacker-area .thumbnail").classList.remove("ouch");}
else
{selectedplayercard.classList.remove("tie-card");
hackercard.classList.remove("tie-card");}
hackercard.querySelector(".power").textContent=" ";
selectedplayercard.querySelector(".power").textContent=" ";
selectedplayercard.classList.remove("played-card");
document.querySelector(".game-board").classList.remove("card-selected");
revealCards();
}

// Finally write the function that reveals the cards. Use
function revealCards(){

}
21 changes: 21 additions & 0 deletions scenario.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,25 @@ var scenarios = [
}
]
},
{
hackerCard : {
description : "I made a fake movie downloading platform for tracking your activity",
power : 2,
},
playerCards : [
{
description : "I am not interested in watching movies",
power : 3,
},
{
description : "I am only interested in watching movies and dont care about such stuffs",
power : 0,
},
{
description : "I never download movies from such third party platforms",
power : 4,
}
]
},

];
8 changes: 6 additions & 2 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ button.next-turn {
bottom: 20px;
transition: all .15s ease-out;
background: black;
display: none;
display:none;
}

.during-game .next-turn {
Expand Down Expand Up @@ -174,6 +174,7 @@ button:active {
.winner-section {
display: none;
position: fixed;
text-align: center;
padding: 15px 0;
left: 0;
right: 0;
Expand Down Expand Up @@ -397,7 +398,7 @@ button[disabled=true] {
height: 30px;
text-align: center;
border-radius: 50%;
color: rgba(0,0,0,0);
color: black;
background-size: contain;
background-repeat: no-repeat;
background-position: center;
Expand Down Expand Up @@ -428,4 +429,7 @@ button[disabled=true] {
100% {
transform: scaleX(1);
}
}
.hacker-area .life-total{
height:30px;
}