-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
97 lines (87 loc) · 2.57 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
class Game {
constructor(player) {
this.player = player
this.choices = ["Rock", "Paper", "Scissors"]
this.choice = Math.floor(Math.random() * this.choices.length)
}
winner() {
var win;
var display
if (this.player === this.choices[this.choice]) {
win = "draw";
} else if (this.player == 'Rock') {
if (this.choices[this.choice] === 'Paper') {
win = "computer"
} else {
win = 'you'
}
} else if (this.player == 'Scissors') {
if (this.choices[this.choice] === 'Rock') {
win = 'computer'
} else {
win = 'you'
}
} else if (this.player == 'Paper') {
if (this.choices[this.choice] === 'Scissors') {
win = 'computer'
} else {
win = 'you'
}
}
switch (win) {
case 'draw':
display = "It's a draw!"
break;
case 'computer':
display = "Computer wins!"
break;
case 'you':
display = "You win!"
break;
default:
break;
}
document.getElementById('winner').innerHTML = display;
}
picture() {
//player1
switch (this.player) {
case 'Rock':
var srcPic1 = "/pictures/rock.png"
break;
case 'Paper':
var srcPic1 = "/pictures/paper.png"
break;
case 'Scissors':
var srcPic1 = "/pictures/scissors.png"
break;
default:
break;
}
//computer
switch (this.choices[this.choice]) {
case 'Rock':
var srcPic2 = "/pictures/rock2.png"
break;
case 'Paper':
var srcPic2 = "/pictures/paper2.png"
break;
case 'Scissors':
var srcPic2 = "/pictures/scissors2.png"
break;
default:
break;
}
document.getElementById("player").src = srcPic1;
document.getElementById("computer").src = srcPic2;
this.winner()
}
}
const choiceButton = document.querySelectorAll('[data-choice]')
choiceButton.forEach(button => {
button.addEventListener('click', () => {
const game = new Game(button.innerText)
//console.log(button.innerText)
game.picture()
})
})