-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
150 lines (126 loc) · 6.08 KB
/
index.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const computerChoice = document.querySelector('#computerChoice');
const playerChoice = document.querySelector('#playerChoice');
let playerScoreValue = 0;
let computerScoreValue = 0;
const playerScoreContainer = document.querySelector('#playerScore');
const computerScoreContainer = document.querySelector('#computerScore');
playerScoreContainer.textContent = `Player Score : ${playerScoreValue}`;
computerScoreContainer.textContent = `Computer Score : ${computerScoreValue}`;
function getComputerChoice() {
const CHOICES = ['rock', 'paper', 'scissors'];
return CHOICES[Math.floor(Math.random() * CHOICES.length)];
}
const computerChoiceImg = document.querySelector('#computerChoiceImg');
const computerChoiceImgElement = document.createElement('img');
function displayComputerChoice() {
computerChoice.textContent = getComputerChoice();
computerChoiceImgElement.width = 100;
computerChoiceImgElement.height = 100;
while (computerChoiceImg.firstChild) {
computerChoiceImg.removeChild(computerChoiceImg.firstChild);
}
if (computerChoice.textContent === 'scissors') {
computerChoiceImgElement.src = `../assets/${computerChoice.textContent}.png`;
computerChoiceImgElement.alt = `${computerChoice.textContent}`;
computerChoiceImg.appendChild(computerChoiceImgElement);
} else if (computerChoice.textContent === 'rock') {
computerChoiceImgElement.src = `../assets/${computerChoice.textContent}.png`;
computerChoiceImgElement.alt = `${computerChoice.textContent}`;
computerChoiceImg.appendChild(computerChoiceImgElement);
} else {
computerChoiceImgElement.src = `../assets/${computerChoice.textContent}.png`;
computerChoiceImgElement.alt = `${computerChoice.textContent}`;
computerChoiceImg.appendChild(computerChoiceImgElement);
}
}
const gameResult = document.querySelector('#result');
function playGame(comp, player) {
if (comp == 'paper' && player == 'paper') {
gameResult.textContent = `Paper vs. Paper? It's a draw! Go again!`;
} else if (comp == 'paper' && player == 'rock') {
computerScoreValue++;
gameResult.textContent = `You lose! Paper beats Rock.`;
} else if (comp == 'paper' && player == 'scissors') {
playerScoreValue++;
gameResult.textContent = `You win! Scissors beats Paper.`;
} else if (comp == 'scissors' && player == 'scissors') {
gameResult.textContent = `It's a draw! Scissors vs. Scissors, really? C'mon, once more!`;
} else if (comp == 'scissors' && player == 'paper') {
computerScoreValue++;
gameResult.textContent = `You lose! Scissors beats Paper.`;
} else if (comp == 'scissors' && player == 'rock') {
playerScoreValue++;
gameResult.textContent = `You win! Rock beats Scissors.`;
} else if (comp == 'rock' && player == 'rock') {
gameResult.textContent = `Ugh, Rock vs. Rock, it's a draw! Another round!`;
} else if (comp == 'rock' && player == 'paper') {
playerScoreValue++;
gameResult.textContent = `You win! Paper beats Rock.`;
} else if (comp == 'rock' && player == 'scissors') {
computerScoreValue++;
gameResult.textContent = `You lose! Rock beats Scissors.`;
} else {
gameResult.textContent = `Something's wrong, try again later.`;
}
if (gameResult.textContent.includes('win')) {
gameResult.style.color = '#06D6A0';
} else if (gameResult.textContent.includes('lose')) {
gameResult.style.color = '#EF476F';
} else if (gameResult.textContent.includes('draw')) {
gameResult.style.color = '#FFD166';
} else gameResult.style.color = '#1a1a1a';
playerScoreContainer.textContent = `Player Score : ${playerScoreValue}`;
computerScoreContainer.textContent = `Computer Score : ${computerScoreValue}`;
}
const optionRock = document.querySelector('#rock');
const optionPaper = document.querySelector('#paper');
const optionScissors = document.querySelector('#scissors');
const playerChoiceImg = document.querySelector('#playerChoiceImg');
const playerChoiceElement = document.createElement('img');
playerChoiceElement.width = 100;
playerChoiceElement.height = 100;
const modalActive = document.querySelector('#modal');
const restartBtn = document.querySelector('#restartBtn');
function restartGame() {
modalActive.classList.remove('active');
gameResult.textContent = 'Rock, Paper, or Scissors? What is it gonna be?';
gameResult.style.color = '#1a1a1a';
computerChoice.textContent = '?';
playerChoice.textContent = '?';
computerChoiceImg.removeChild(computerChoiceImg.firstChild);
playerChoiceImg.removeChild(playerChoiceImg.firstChild);
computerScoreValue = 0;
computerScoreContainer.textContent = `Computer Score : ${computerScoreValue}`;
playerScoreValue = 0;
playerScoreContainer.textContent = `Player Score : ${playerScoreValue}`;
}
const modalTitle = document.querySelector('#modalTitle');
function handleClickEvent(playerSelection) {
if (playerScoreValue === 5) {
modalActive.classList.add('active');
modalTitle.textContent = 'Congratulations, you win! Play again?';
restartBtn.addEventListener('click', () => restartGame());
} else if (computerScoreValue === 5) {
modalActive.classList.add('active');
modalTitle.textContent = 'Too bad, you lose! Play Again?';
restartBtn.addEventListener('click', () => restartGame());
}
displayComputerChoice();
playerChoice.textContent = playerSelection;
playerChoiceElement.src = `../assets/${playerSelection}.png`;
playerChoiceElement.alt = playerSelection;
playerChoiceImg.appendChild(playerChoiceElement);
playGame(computerChoice.textContent, playerSelection);
if (playerScoreValue === 5) {
modalActive.classList.add('active');
modalTitle.textContent = 'Congratulations, you win! Play again?';
restartBtn.addEventListener('click', () => restartGame());
} else if (computerScoreValue === 5) {
modalActive.classList.add('active');
modalTitle.textContent = 'Too bad, you lose! Play Again?';
restartBtn.addEventListener('click', () => restartGame());
}
}
optionRock.addEventListener('click', () => handleClickEvent('rock'));
optionPaper.addEventListener('click', () => handleClickEvent('paper'));
optionScissors.addEventListener('click', () => handleClickEvent('scissors'));