-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (76 loc) · 3.31 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
document.addEventListener('DOMContentLoaded', () => {
const choices = ['rock', 'paper', 'scissors'];
const choiceButtons = document.querySelectorAll('.choice-btn');
const playerScoreSpan = document.getElementById('player-score');
const computerScoreSpan = document.getElementById('computer-score');
const rulesBtn = document.getElementById('rules-btn');
const closeBtn = document.getElementById('close-rules');
const rulesPopup = document.getElementById('rules-popup');
const resetBtn = document.getElementById('reset-btn');
let playerScore = localStorage.getItem('playerScore') ? parseInt(localStorage.getItem('playerScore')) : 0;
let computerScore = localStorage.getItem('computerScore') ? parseInt(localStorage.getItem('computerScore')) : 0;
playerScoreSpan.textContent = playerScore;
computerScoreSpan.textContent = computerScore;
choiceButtons.forEach((button, index) => {
button.addEventListener('click', () => {
const playerChoice = choices[index];
const computerChoice = choices[Math.floor(Math.random() * choices.length)];
const winner = getWinner(playerChoice, computerChoice);
if (winner === 'player') {
playerScore++;
localStorage.setItem('playerScore', playerScore);
playerScoreSpan.textContent = playerScore;
redirectToResultPage(playerChoice, computerChoice, 'win');
} else if (winner === 'computer') {
computerScore++;
localStorage.setItem('computerScore', computerScore);
computerScoreSpan.textContent = computerScore;
redirectToResultPage(playerChoice, computerChoice, 'lose');
} else {
redirectToResultPage(playerChoice, computerChoice, 'tie');
}
});
});
rulesBtn.addEventListener('click', () => {
rulesPopup.style.display = 'block';
});
closeBtn.addEventListener('click', () => {
rulesPopup.style.display = 'none';
});
resetBtn.addEventListener('click', () => {
localStorage.removeItem('playerScore');
localStorage.removeItem('computerScore');
playerScore = 0;
computerScore = 0;
playerScoreSpan.textContent = playerScore;
computerScoreSpan.textContent = computerScore;
});
function getWinner(playerChoice, computerChoice) {
if (playerChoice === computerChoice) {
return 'draw';
} else if (
(playerChoice === 'rock' && computerChoice === 'scissors') ||
(playerChoice === 'paper' && computerChoice === 'rock') ||
(playerChoice === 'scissors' && computerChoice === 'paper')
) {
return 'player';
} else {
return 'computer';
}
}
function redirectToResultPage(playerChoice, computerChoice, result) {
localStorage.setItem('playerChoice', playerChoice);
localStorage.setItem('computerChoice', computerChoice);
localStorage.setItem('playerScore', playerScore);
localStorage.setItem('computerScore', computerScore);
localStorage.setItem('result', result);
// Redirect to the appropriate result page
if (result === 'win') {
window.location.href = 'win-page.html';
} else if (result === 'lose') {
window.location.href = 'lose-page.html';
} else {
window.location.href = 'tie-page.html';
}
}
});