-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
129 lines (114 loc) · 3.23 KB
/
script.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
//Score object
let Score = JSON.parse(localStorage.getItem('Score')) || {wins: 0,loses: 0,ties:0};
//Set Score paragraph to zero scores
updateScoreElement();
//Pick computer move function
function pickComputerMove(){
let computerMove;
const randomNo = Math.random();
if(randomNo<1/3){
computerMove = 'Stone';
}
else if(randomNo>=2/3){
computerMove = 'Scissors';
}
else{
computerMove = 'Paper';
}
return computerMove;
}
//Update score function
function updateScoreElement(){
document.querySelector('.js-score')
.innerHTML = `Wins:${Score.wins} Loses:${Score.loses} Ties:${Score.ties}`;
}
let isAutoPlaying = false;
let intervalId;
// auto play function
function autoPlay(){
if(!isAutoPlaying){
intervalId = setInterval(() => {
const playerMove = pickComputerMove();
playGame(playerMove);
},1000);
isAutoPlaying = true;
}else{
clearInterval(intervalId);
isAutoPlaying = false;
}script.js
}
//eventlistener stone
document.querySelector('.js-stone-button')
.addEventListener('click', () => {
playGame('Stone');
});
// eventlistener paper
document.querySelector('.js-paper-button')
.addEventListener('click', () => {
playGame('Paper');
});
// eventlistener scissors
document.querySelector('.js-scissors-button')
.addEventListener('click', () => {
playGame('Scissors');
});
//keydown a, b, c
document.body.addEventListener('keydown', (event) => {
if(event.key === 'a'){
playGame('Stone');
} else if(event.key === 'b'){
playGame('Paper');
}else if(event.key === 'c'){
playGame('Scissors')
}
})
//Play game function
function playGame(playerMove){
const computerMove = pickComputerMove();
if(playerMove === 'Stone'){
if(computerMove === 'Stone'){
result = 'It\'s a Tie!';
}
else if(computerMove==='Paper'){
result = 'Computer won :<';
}
else{
result = 'You won :>';
}
}
else if(playerMove === 'Paper'){
if(computerMove === 'Stone'){
result = 'You won :>';
}
else if(computerMove==='Paper'){
result = 'It\'s a Tie!';
}
else{
result = 'Computer won :<';
}
}
else{
if(computerMove === 'Stone'){
result = 'Computer won :<';
}
else if(computerMove==='Paper'){
result = 'You won :>';
}
else{
result = 'It\'s a Tie!';
}
}
if(result === 'You won :>') {
Score.wins++;
}
else if(result === 'Computer won :<') {
Score.loses++;
}
else{
Score.ties++;
}
localStorage.setItem('Score',JSON.stringify(Score))
updateScoreElement();
document.querySelector('.js-result').innerHTML = result;
document.querySelector('.js-moves').innerHTML = `(You) <img src="images/${playerMove}.png" alt="${playerMove}" class="move-icon"> (Computer) <img src="images/${computerMove}.png" alt="${computerMove}" class="move-icon">`;
}