blob: 73c58885bebe249c00af62dcc2194f330d9cda75 (
plain)
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
|
import RPS_Game from "./rps-game.js";
export default class RPS_Instance extends RPS_Game {
#buttons = {};
#scoring = { player1: '', player2: '' };
#resultBox = '';
#resultMsg = '';
constructor(player1, player2, buttons, scoring, result) {
super(player1, player2);
this.#buttons = buttons;
this.#scoring = scoring;
this.#resultBox = result;
this.#resultMsg = result.innerHTML;
}
updateScoring() {
for (const [name, element] of Object.entries(this.#scoring)) {
if ( 'player1' === name ) {
element.innerHTML = this.player1Score;
}
if ( 'player2' === name ) {
element.innerHTML = this.player2Score;
}
}
}
updateResult(result = '') {
let txt;
if ( result === 'player1' ) {
txt = this.player1Choice + ' beats ' + this.player2Choice + ".<br>You win!"
this.#resultBox.style.color = 'green';
} else if ( result === 'player2' ) {
txt = this.player1Choice + ' loses to ' + this.player2Choice + ".<br>You lose..."
this.#resultBox.style.color = 'red';
} else if ( result === 'even' ) {
txt = this.player1Choice + ' equals to ' + this.player2Choice + ".<br>No winner.";
this.#resultBox.style.color = 'black';
} else {
txt = this.#resultMsg;
this.#resultBox.style.color = '';
}
this.#resultBox.innerHTML = txt;
}
listen() {
for (const [name, element] of Object.entries(this.#buttons)) {
element.addEventListener('click', () => {
if ( 'reset' === name ) {
this.reset();
this.updateResult();
this.updateScoring();
} else {
this.player1Choice = name;
const result = this.calculateScore();
this.setScore(result);
this.updateResult(result);
this.updateScoring();
}
})
}
}
init() {
this.reset();
this.listen();
}
}
|