-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStats.js
52 lines (43 loc) · 1.38 KB
/
Stats.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
class Stats {
constructor(statsWidth, statsHeight) {
this.width = statsWidth
this.height = statsHeight
}
sum(items) {
this.rockCount = 0
this.paperCount = 0
this.scissorsCount = 0
for (let item of items) {
switch (item.type) {
case 'rock':
this.rockCount++
break
case 'paper':
this.paperCount++
break
case 'scissors':
this.scissorsCount++
break
default:
throw new Error("unknown item type")
}
}
}
show() {
const sum = this.rockCount + this.paperCount + this.scissorsCount
rectMode(CORNER);
noStroke()
// rock
fill(136, 140, 141);
const rockMap = map(this.rockCount, 0, sum, 0, this.width)
rect(0, height - this.height, rockMap);
// paper
fill(245, 245, 245)
const paperMap = map(this.paperCount, 0, sum, 0, this.width)
rect(rockMap, height - this.height, paperMap);
// scissors
fill(191, 23, 56)
const scissorsMap = map(this.scissorsCount, 0, sum, 0, this.width)
rect(rockMap + paperMap, height - this.height, scissorsMap);
}
}