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
167 changes: 140 additions & 27 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ var hackerLife = 5;
var hackerWinnerMessage = "Write the message here";
var playerWinnerMessage = "Write the message here";

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

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

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
updateScores();
Expand All @@ -32,70 +36,124 @@ 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
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; }
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();
},500)

setTimeout(function(){
revealPlayerPower();
},800)
setTimeout(function(){
compareCards();
setTimeout(function () {
revealHackerPower(hackerPower);
}, 500)

setTimeout(function () {
revealPlayerPower(playerPower);
}, 800)

setTimeout(function () {
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(){

function revealHackerPower(hackerPower) {
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!
// 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 < hackerPower) {
hackerCard.classList.add("better-card");
playerCard.classList.add("worse-card");
playerLife -= hackerPower - playerPower;
}
updateScores();
document.querySelector(".next-turn").style.display = "flex";


}

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

function gameOver() {
var winner = document.querySelector(".winner-section");
var winnerMessage = document.querySelector(".winner-message");
if (playerLife === 0) {
winnerMessage.innerHTML = "You got Hacked!";
winner.style.display = "flex";
document.querySelector(".winner-section .restart").disabled = false;
document.querySelector(".next-turn").style.display = "none";
return true;
}
else if (hackerLife === 0) {
winnerMessage.innerHTML = "You defeated the hacker!";
winner.style.display = "flex";
document.querySelector(".winner-section .restart").disabled = false;
document.querySelector(".next-turn").style.display = "none";
return true;
}
else {
return false;
}
}


// Write a function that starts the game
function startGame() {
document.querySelector(".hacker-card").classList.add("prepared");
document.getElementById("app").classList.remove("before-game");
playTurn();

}


// Now write a function that starts the game over from scratch
function restartGame(){
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(){
function updateScores() {

// Here these update life totals for each player
document.querySelector(".player-stats .life-total").innerHTML = playerLife;
Expand All @@ -104,21 +162,76 @@ function updateScores(){
var playerPercent = playerLife / playerStartLife * 100;
if (playerPercent < 0) {
playerPercent = 0;
playerLife = 0;
}
document.querySelector(".player-stats .life-left").style.height = playerPercent + "%";

document.querySelector(".player-stats .life-total").innerHTML = playerLife;

document.querySelector(".player-stats .life-left").style.height = playerPercent + "%";

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

var hackerPercent = hackerLife / hackerStartLife * 100;
if (hackerPercent < 0) {
hackerPercent = 0;
hackerLife = 0;
}
document.querySelector(".hacker-stats .life-total").innerHTML = hackerLife;
document.querySelector(".hacker-stats .life-left").style.height = hackerPercent + "%";



}



// Write a function that Plays one turn of the game
function playTurn() {
if (turn === 0 && rounds === 0) {
revealCards();
}
else {
var check = gameOver();
if (!check) {
document.querySelector(".next-turn").style.display = "none";
allCardElements[playedCardIndex].classList.remove("played-card");
allCardElements[playedCardIndex].classList.remove("worse-card", "better-card", "reveal-power");
allCardElements[0].classList.remove("worse-card", "better-card", "reveal-power");
for (var i = 0; i < 4; i++) {
document.querySelectorAll(".card")[i].classList.remove("showCard");
}
setTimeout(revealCards, 500);
cardSelected = false;
document.querySelector(".game-board").classList.remove("card-selected");
}
}


}

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

}
function revealCards() {



document.querySelector(".hacker-card .text").innerHTML = scenarios[turn]["hackerCard"]["description"];
document.querySelector(".hacker-card .power").innerHTML = scenarios[turn]["hackerCard"]["power"];
document.querySelector(".hacker-card").classList.add("showCard");


setTimeout(function () {
document.querySelectorAll(".player-card .text")[0].innerHTML = scenarios[turn]["playerCards"][0]["description"];
document.querySelector(".player-card .power").innerHTML = scenarios[turn]["playerCards"][0]["power"];
document.querySelectorAll(".player-card")[0].classList.add("showCard");
}, 200);
setTimeout(function () {
document.querySelectorAll(".player-card .text")[1].innerHTML = scenarios[turn]["playerCards"][1]["description"];
document.querySelector(".player-card .power").innerHTML = scenarios[turn]["playerCards"][1]["power"];
document.querySelectorAll(".player-card")[1].classList.add("showCard");
}, 400);
setTimeout(function () {
document.querySelectorAll(".player-card .text")[2].innerHTML = scenarios[turn]["playerCards"][2]["description"];
document.querySelector(".player-card .power").innerHTML = scenarios[turn]["playerCards"][2]["power"];
document.querySelectorAll(".player-card")[2].classList.add("showCard");
}, 600);
}
83 changes: 62 additions & 21 deletions scenario.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,85 @@

var scenarios = [
{ // add the text you'd want should appear on the hacker's card
hackerCard : {
description : "I set up a fake Wi-Fi station to steal people’s email and track them online.",
power : 4,
hackerCard: {
description: "I set up a fake Wi-Fi station to steal people’s email and track them online.",
power: 4,
},
// add 3 card descriptions you'd want should appear on the player's card. Keeping in mind that at least ONE of them should be an apt counter!
playerCards : [
playerCards: [
{
description : "I never use public wifi networks.",
power : 5,
description: "I never use public wifi networks.",
power: 5,
},
{
description : "I browse the web, but I never do any personal business on a public wifi network.",
power : 3,
description: "I browse the web, but I never do any personal business on a public wifi network.",
power: 3,
},
{
description : "I connect to any wifi network I can use in public.",
power : 1,
description: "I connect to any wifi network I can use in public.",
power: 1,
}
]
},
{
hackerCard : {
description : "I sent a fake email from your bank asking for your account details.",
power : 3,
hackerCard: {
description: "I sent a fake mail regarding prize money won.",
power: 3,
},
playerCards : [
playerCards: [
{
description : "I checked the email address - the message didn’t come from my bank.",
power : 5,
description: "I checked the email address - the message didn’t come from any trustworthy account.",
power: 5,
},
{
description : "I never give out personal information in response to an email.",
power : 4,
description: "I never give information in response to any random email.",
power: 4,
},
{
description : "I sent the details you asked for so you could check on my account.",
power : 1,
description: "I sent all my important details you asked for.",
power: 1,
}
]
},
];
{
hackerCard: {
description: "I watched you type your password and hacked your account",
power: 2,
},
playerCards: [
{
description: "I deleted the account and started a new one",
power: 1,
},
{
description: "I changed my password on all of the accounts because they are all the same.",
power: 4,
},
{
description: "I use different password for all of my different accounts.",
power: 5,
}
]
},
{
hackerCard: {
description: "I hacked your system and all data has been deleted now.",
power: 2,
},
playerCards: [
{
description: "I have my data backed up in google drive.",
power: 3,
},
{
description: "I have not backed up my data.",
power: 1,
},
{
description: "I have kept back up in many ways .",
power: 5,
}
]
},
];
];