-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrps-script.js
130 lines (112 loc) · 3.6 KB
/
rps-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
130
let score = JSON.parse(localStorage.getItem('score')) || {
wins: 0,
losses: 0,
ties: 0
};
updatescore();
// if(!score){
// score={
// wins:'0',
// losses:0,
// ties:0
// };
// }
// let computervar ='';
function stop(){
const name = document.querySelector('.js-ap');
if(name.innerHTML === 'Auto Play'){
name.innerHTML = 'Stop';
}else{
name.innerHTML = 'Auto Play';
}
}
let interval ;
let playing= false;
function autoplay(){
if(!playing){
interval =setInterval( function(){
const choice =compcall();
playerchoice(choice);
}, 1000);
playing= true;
}else{
clearInterval(interval);
playing= false;
}
}
document.body.addEventListener('keydown', (event)=>{
if(event.key =='r'){
playerchoice('rock');
}else if(event.key =='p'){
playerchoice('paper');
}else if(event.key =='s'){
playerchoice('scissor');
}
});
function compcall() {
let computervar = '';
const randomvar = Math.random();
if (randomvar >= 0 && randomvar < 1 / 3) {
computervar = 'rock';
} else if (randomvar >= 1 / 3 && randomvar < 2 / 3) {
computervar = 'paper';
} else if (randomvar >= 2 / 3 && randomvar <= 1) {
computervar = 'scissor';
}
return computervar;
}
//updatescore();
// document.querySelector('.js-write').innerHTML= ` Wins=${score.wins}. losses= ${score.losses}. Ties=${score.ties}`;
function updatescore(){
document.querySelector('.js-score-write')
.innerHTML= ` Wins=${score.wins}. losses= ${score.losses}. Ties=${score.ties}`;
}
function playerchoice(choice) {
const computervar = compcall();
let result = '';
if (choice === 'scissor') {
if (computervar === 'rock') {
result = 'You lose';
} else if (computervar === 'paper') {
result = 'You Win';
} else if (computervar === 'scissor') {
result = 'Tie';
}
} else if (choice === 'paper') {
if (computervar === 'rock') {
result = 'You Win';
} else if (computervar === 'paper') {
result = 'Tie';
} else if (computervar === 'scissor') {
result = 'You lose';
}
} else if (choice === 'rock') {
if (computervar === 'rock') {
result = 'Tie';
} else if (computervar === 'paper') {
result = 'You lose';
} else if (computervar === 'scissor') {
result = 'You Win';
}
}
//console.log(score)
if (result === 'You Win') {
score.wins++;
} else if (result === 'You lose') {
score.losses++;
} else if (result === 'Tie') {
score.ties++;
}
//alert(`You choose ${choice}. Computer chose ${computervar}. ${result}. Wins=${score.wins}. losses= ${score.losses}. Ties=${score.ties}`);
localStorage.setItem('score', JSON.stringify(score));
updatescore();
document.querySelector('.js-write ').innerHTML= `${result}. ` ;
// document.querySelector('.js-you-write').innerHTML= `You choose ${choice}. ` ;
//document.querySelector('.js-comp-write').innerHTML= `Computer chose ${computervar}.` ;
document.querySelector('.js-move-write ').innerHTML=
` You
<img class="move-icon" src="rock-paper-scissor-photos/${choice}-emoji.png">
<img class="move-icon" src="rock-paper-scissor-photos/${computervar}-emoji.png">
Computer` ;
//updatescore();
}