-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathscripts.js
128 lines (115 loc) · 4.88 KB
/
scripts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const weaponsButtons = document.querySelectorAll('.weapon-button');
const rounds = document.querySelector('.round');
const combatText = document.querySelector('.combat-text');
const buttonPlayAgain = document.querySelector('.play-again');
let playerLives = 5;
let computerLives = 5;
let round = 0;
function countRounds() {
round += 1;
rounds.innerText = `Round: ${round}`;
return round;
}
function computerPlay() {
const weapons = ['wand', 'bow', 'mace'];
const computerSelection = weapons[Math.floor(Math.random() * weapons.length)];
const computerIcon = document.querySelector('.computer-icon');
computerIcon.classList.remove('fa-skull', 'fa-wand-magic', 'fa-bow-arrow', 'fa-mace');
if (computerSelection === 'wand') {
computerIcon.classList.add('fa-wand-magic');
computerIcon.style.color = '#8070ac';
} else if (computerSelection === 'bow') {
computerIcon.classList.add('fa-bow-arrow');
computerIcon.style.color = '#62b49c';
} else if (computerSelection === 'mace') {
computerIcon.classList.add('fa-mace');
computerIcon.style.color = '#b96b78';
}
return computerSelection;
}
function countLives(playerSelection, computerSelection) {
const gameOutput = document.querySelector('.game-output');
const computerPlayDiv = document.querySelector('.computer-play-div');
switch (true) {
case (playerSelection === computerSelection):
combatText.innerText = `Hmm.. Two ${playerSelection}s means a draw, so no lives were lost. Let's try again.`;
gameOutput.style.border = '4px solid #8070ac';
computerPlayDiv.classList.remove('grey-border', 'green-border', 'red-border');
computerPlayDiv.classList.add('purple-border');
break;
case (playerSelection === 'wand' && computerSelection === 'mace'):
case (playerSelection === 'bow' && computerSelection === 'wand'):
case (playerSelection === 'mace' && computerSelection === 'bow'):
combatText.textContent = `Impressive attack! The enemy lost one life, because the great power of your ${playerSelection} crushed his ${computerSelection}!`;
gameOutput.style.border = '4px solid #62b49c';
computerPlayDiv.classList.remove('grey-border', 'red-border', 'purple-border');
computerPlayDiv.classList.add('green-border');
computerLives -= 1;
break;
default:
combatText.innerText = `Unfortunate defeat.. You lost one life, because your ${playerSelection} lacks of power against enemy's ${computerSelection}!`;
gameOutput.style.border = '4px solid #b96b78';
computerPlayDiv.classList.remove('grey-border', 'green-border', 'purple-border');
computerPlayDiv.classList.add('red-border');
playerLives -= 1;
break;
}
const lives = document.querySelector('.lives');
lives.innerText = `Your Lives: ${playerLives} ︱ Enemy's Lives: ${computerLives}`;
return [playerLives, computerLives];
}
function endGame(playerHealth, computerHealth) {
if (playerHealth === 0 || computerHealth === 0) {
weaponsButtons.forEach((button) => {
button.setAttribute('disabled', '');
button.classList.add('disabled-button', 'opacity');
});
const computerIcon = document.querySelector('.computer-icon');
computerIcon.style.opacity = '0.5';
const gameEndText = document.querySelector('.game-end-text');
if (playerLives > computerLives) {
combatText.innerText = 'Hehe, poor enemy has no lives left.. He barely holds himself in one piece.';
gameEndText.textContent = 'You Won This Battle!';
gameEndText.style.color = '#62b49c';
} else {
combatText.innerText = 'Ouch.. No lives left for you. Enjoy the mocking laughter of the enemy.';
gameEndText.textContent = 'You Lost This Battle!';
gameEndText.style.color = '#b96b78';
}
buttonPlayAgain.style.visibility = 'visible';
}
}
function resetGame() {
buttonPlayAgain.addEventListener('click', () => {
window.location.reload();
});
}
function playGame() {
let playerSelection;
weaponsButtons.forEach((weapon) => {
weapon.addEventListener('click', () => {
const weaponIcons = document.querySelectorAll('.weapon-icon');
if (weapon.classList.contains('wand-button')) {
weaponIcons[0].style.color = '#8070ac';
weaponIcons[1].style.color = '#5e5e5e';
weaponIcons[2].style.color = '#5e5e5e';
playerSelection = 'wand';
} else if (weapon.classList.contains('bow-button')) {
weaponIcons[1].style.color = '#62b49c';
weaponIcons[0].style.color = '#5e5e5e';
weaponIcons[2].style.color = '#5e5e5e';
playerSelection = 'bow';
} else {
weaponIcons[2].style.color = '#b96b78';
weaponIcons[0].style.color = '#5e5e5e';
weaponIcons[1].style.color = '#5e5e5e';
playerSelection = 'mace';
}
countRounds();
countLives(playerSelection, computerPlay());
endGame(playerLives, computerLives);
resetGame();
});
});
}
playGame();