This is part 2 of the game I am building. After some good feedback on my first post I decided it was time to post my updated code.
The differences in this part are: Updated code after feedback, new dodge mechanic and better formatted code.
What I am looking for are: performance feedback, code style feedback, things I can do in a better way and JavaScript best practices.
For more information about the game I should advice you to visit my first question.
var cards = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10];
var shuffledCards = shuffle(cards);
var playerDeck = shuffledCards.slice(0, 20);
var computerDeck = shuffledCards.slice(20);
var playerlife = 25;
var computerLife = 25;
var playerFeedback = document.getElementById('playerFeedback');
var playerName = '';
var swordSound = new Audio('sounds/sword.mp3');
var blockSound = new Audio('sounds/block.mp3');
var dodgeSound = new Audio('sounds/dodged.mp3');
var dodged = false;
/**
* @param array
* @returns {*}
*/
function shuffle(array) {
"use strict";
var m = array.length;
var t;
var i;
// While there remain elements to shuffle…
while (m) {
// Pick a remaining element…
i = Math.floor(Math.random() * m--);
// And swap it with the current element.
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
}
/**
* @returns {boolean}
*/
function getPlayerName() {
"use strict";
playerName = document.getElementById('userInput').value;
if (playerName.length === 0) {
return false;
}
document.getElementById('playerName').innerHTML = playerName;
document.getElementById('overlay').style.display = 'none';
}
function checkIsGameOver() {
"use strict";
var gameResult = document.getElementById('gameResult');
var gameResultContent = gameResult.innerHTML;
var replay = document.getElementById('replay');
var click = document.getElementById('click');
var dodgeL = document.getElementById('dodgeL');
var dodgeR = document.getElementById('dodgeR');
function gameIsOver() {
playerFeedback.innerHTML = 'Game Over, do you want to play again?';
gameResult.innerHTML = gameResultContent;
gameResult.style.display = 'block';
replay.style.display = 'inline';
click.style.display = 'none';
dodgeL.style.display = 'none';
dodgeR.style.display = 'none';
}
if (playerlife <= 0) {
playerlife = 'Lost';
computerLife = 'Won';
gameResultContent = '<p>Hunter won the game!</p>';
gameIsOver();
} else if (computerLife <= 0) {
computerLife = 'Lost';
playerlife = 'Won';
gameResultContent = '<p>' + playerName + ' won the game!</p>';
gameIsOver();
}
}
function whoWon() {
"use strict";
var playerTopCard = playerDeck[0];
var computerTopCard = computerDeck[0];
document.getElementById('computerDamageImage').setAttribute('src', 'images/empty.png');
document.getElementById('playerDamageImage').setAttribute('src', 'images/empty.png');
if (playerTopCard > computerTopCard) {
computerLife = computerLife - (playerTopCard - computerTopCard);
swordSound.play();
document.getElementById('computerDamageImage').setAttribute('src', 'images/damage.gif');
playerFeedback.innerHTML = 'Hunter lost: ' + (playerTopCard - computerTopCard) + ' HP';
} else if (playerTopCard < computerTopCard) {
playerlife = playerlife - (computerTopCard - playerTopCard);
swordSound.play();
document.getElementById('playerDamageImage').setAttribute('src', 'images/damage.gif');
playerFeedback.innerHTML = playerName + ' lost: ' + (computerTopCard - playerTopCard) + ' HP';
} else {
blockSound.play();
playerFeedback.innerHTML = 'It was a tie!';
}
checkIsGameOver();
playerDeck.shift();
computerDeck.shift();
}
function didPlayerDodged() {
"use strict";
var playerTopCard = playerDeck[0];
var computerTopCard = computerDeck[0];
document.getElementById('computerDamageImage').setAttribute('src', 'images/empty.png');
document.getElementById('playerDamageImage').setAttribute('src', 'images/empty.png');
if (dodged === true) {
computerLife = computerLife - playerTopCard;
dodgeSound.play();
document.getElementById('computerDamageImage').setAttribute('src', 'images/damage.gif');
playerFeedback.innerHTML = 'You just dodged the attack! Hunter lost: ' + playerTopCard + ' HP';
} else {
playerlife = playerlife - computerTopCard;
swordSound.play();
document.getElementById('playerDamageImage').setAttribute('src', 'images/damage.gif');
playerFeedback.innerHTML = 'You could not dodged the attack! ' + playerName + ' lost: ' + computerTopCard + ' HP';
}
checkIsGameOver();
playerDeck.shift();
computerDeck.shift();
}
/**
* @param type
*/
function dealCards(type) {
"use strict";
var playerTopCard = playerDeck[0];
var computerTopCard = computerDeck[0];
var cardImage = "<img src='images/cardBack.png'/>";
if (type === 'attack') {
whoWon();
} else if (type === 'dodgeTrue') {
dodged = true;
didPlayerDodged();
} else {
dodged = false;
didPlayerDodged();
}
document.getElementById("card1").innerHTML = "<div class='cardNumber'>" + playerTopCard + '</div>' + cardImage;
document.getElementById("card2").innerHTML = "<div class='cardNumber'>" + computerTopCard + '</div>' + cardImage;
document.getElementById("scoreComputer").innerHTML = computerLife;
document.getElementById("scorePlayer").innerHTML = playerlife;
}
function playAgain() {
"use strict";
window.location.reload();
}
/**
* @param direction
* @returns {boolean}
*/
function dodgeDirection(direction) {
"use strict";
var goodDirection = Math.floor(Math.random() * 2) + 1;
return direction === goodDirection;
}
(function startGame() {
"use strict";
document.getElementById("replay").addEventListener("click", playAgain);
document.getElementById("userSubmit").addEventListener("click", getPlayerName);
document.getElementById("click").addEventListener("click", function () {
dealCards('attack');
});
document.getElementById("dodgeL").addEventListener("click", function () {
if (dodgeDirection(1)) {
dealCards('dodgeTrue');
} else {
dealCards('dodgeFalse');
}
});
document.getElementById("dodgeR").addEventListener("click", function () {
if (dodgeDirection(2)) {
dealCards('dodgeTrue');
} else {
dealCards('dodgeFalse');
}
});
}());