-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rock-Paper-Scissor.js
130 lines (127 loc) · 4.75 KB
/
Rock-Paper-Scissor.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
const rockbuttonelement = document.querySelector('.js-rock');
const paperbuttonelement = document.querySelector('.js-paper');
const scissorbuttonelement = document.querySelector('.js-scissor');
const displayScore = document.querySelector('.js-display');
const displayscore2 = document.querySelector('.js-moves');
const resetScoreButton = document.querySelector('.js-reset-button');
const autoPlayButton = document.querySelector('.js-auto-play-button');
const confermResetMessage = document.querySelector('.js-confermreset');
let computerMove = '';
let playerMove = 'none';
let computerMoveDisplay = 'none';
let autoplay = false;
let move ;
const computer = () => {
num = Math.random();
if (num >=0 && num <=1/3) {
computerMove = 'rock';
} else if (num >=1/3 && num <=2/3) {
computerMove = 'paper';
} else if (num >=2/3 && num <=1) {
computerMove = 'scissor';
}
return computerMove ;
}
const score = JSON.parse(localStorage.getItem('score')) || {
wins: 0,
loses: 0,
ties: 0
}
const playerMoveRock = () => {
playerMove = '<img src="icones/Screenshot 2024-04-18 at 12-26-22 Rock Paper Scissors Emojis ✊✌️✋👊✌️✋ Copy & Paste.png">';
computerDisplay();
if (computer() === 'rock') {
score.ties++;
} else if (computer() === 'paper') {
score.loses++;
} else if (computer() === 'scissor') {
score.wins++;
}
localStorage.setItem('score', JSON.stringify(score));
displayMove();
display();
}
const playerMovePaper = () => {
playerMove = '<img src="icones/Screenshot 2024-04-18 at 12-26-58 Rock Paper Scissors Emojis ✊✌️✋👊✌️✋ Copy & Paste.png">';
computerDisplay();
if (computer() === 'rock') {
score.wins++;
} else if (computer() === 'paper') {
score.ties++;
} else if (computer() === 'scissor') {
score.loses++;
}
localStorage.setItem('score', JSON.stringify(score));
displayMove();
display();
}
const playerMoveScissor = () => {
playerMove = '<img src="icones/Screenshot 2024-04-18 at 12-27-04 Rock Paper Scissors Emojis ✊✌️✋👊✌️✋ Copy & Paste.png">';
computerDisplay();
if (computer() === 'rock') {
score.loses++;
} else if (computer() === 'paper') {
score.wins++;
} else if (computer() === 'scissor') {
score.ties++;
}
localStorage.setItem('score', JSON.stringify(score));
displayMove();
display();
}
const computerDisplay = () => {
if (computer() === 'rock'){
computerMoveDisplay = '<img src="icones/Screenshot 2024-04-18 at 12-26-22 Rock Paper Scissors Emojis ✊✌️✋👊✌️✋ Copy & Paste.png">'
}else if (computer() === 'paper'){
computerMoveDisplay = '<img src="icones/Screenshot 2024-04-18 at 12-26-58 Rock Paper Scissors Emojis ✊✌️✋👊✌️✋ Copy & Paste.png">'
}else if (computer() === 'scissor'){
computerMoveDisplay = '<img src="icones/Screenshot 2024-04-18 at 12-27-04 Rock Paper Scissors Emojis ✊✌️✋👊✌️✋ Copy & Paste.png">'
}
}
const display = () => {
displayScore.innerHTML = `Wins: ${score.wins}, Loses: ${score.loses}, Ties: ${score.ties}`;
}
const displayMove = () => {
displayscore2.innerHTML = `player: ${playerMove}, Computer: ${computerMoveDisplay}`;
}
displayMove();
display();
rockbuttonelement.addEventListener('click', playerMoveRock)
paperbuttonelement.addEventListener('click', playerMovePaper)
scissorbuttonelement.addEventListener('click', playerMoveScissor)
resetScoreButton.addEventListener('click', () => {
confermResetMessage.innerHTML = `Are you sure you want to reset the score?
<button class='js-yesbutton'>Yes</button> <button class='js-nobutton'>No</button>`
const yesButton = document.querySelector('.js-yesbutton');
const noButton = document.querySelector('.js-nobutton');
yesButton.addEventListener('click', () => {
localStorage.removeItem('score');
location.reload();
})
noButton.addEventListener('click', () => {
confermResetMessage.innerHTML = '';
})
})
autoPlayButton.addEventListener('click', () => {
if (autoPlayButton.innerText === 'Auto play'){
autoPlayButton.innerText = 'Stop';
} else if (autoPlayButton.innerText === 'Stop'){
autoPlayButton.innerText = 'Auto play';
}
if (autoplay === true){
clearInterval(interval)
autoplay = false;
} else if (autoplay === false){
interval = setInterval(() => {
move = computer()
if (move === 'rock'){
playerMoveRock();
} else if (move === 'paper'){
playerMovePaper();
} else if (move === 'scissor'){
playerMoveScissor();
}
}, 500);
autoplay = true;
}
})