-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
96 lines (77 loc) · 3.05 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
class Result {
static whoWin(yourChoice, PCchoice) {
if (yourChoice === "rock" && PCchoice === "scissors" || yourChoice === "scissors" && PCchoice === "paper" || yourChoice === "paper" && PCchoice === "rock") return "win";
else if (yourChoice === "scissors" && PCchoice === "rock" || yourChoice === "paper" && PCchoice === "scissors" || yourChoice === "rock" && PCchoice === "paper") return "lose";
else return "draw";
}
}
class Choice {
constructor(yourChoice) {
this.yourChoice = yourChoice;
this.PCchoice = this.drawPcChoice();
}
getYourChoice = () => this.yourChoice;
getPcChoice = () => this.PCchoice;
drawPcChoice() {
const options = ["rock", "paper", "scissors"];
return options[Math.floor(Math.random() * options.length)];
}
}
class Stats {
constructor(wins, draws, loses) {
this.status = {
wins: wins,
draws: draws,
loses: loses,
}
}
getStats = () => this.status;
refreshStats(result) {
switch (result) {
case "win":
this.status.wins++;
break;
case "draw":
this.status.draws++;
break;
case "lose":
this.status.loses++;
break;
}
}
}
class Game {
constructor() {
this.optionsImg = document.querySelectorAll('.img');
this.optionsBtns = document.querySelectorAll('button');
this.optionsBtns.forEach(option => option.addEventListener('click', this.startGame.bind(this)))
this.youWins = document.querySelector('.results > .you-win');
this.draw = document.querySelector('.results > .draw');
this.PcWins = document.querySelector('.results > .pc-win');
this.stats = new Stats(0, 0, 0);
this.render.call(this, this.stats.getStats());
}
startGame(e) {
this.optionsImg.forEach(choice => choice.className = 'img');
if (this.draw.classList.contains('draw-animation')) {
this.draw.classList.toggle('draw-animation');
}
this.choice = new Choice(e.target.dataset.option);
const yourChoice = this.choice.getYourChoice(),
PcChoice = this.choice.getPcChoice();
if (yourChoice === PcChoice) {
[...this.optionsImg].find(choice => choice.dataset.option === PcChoice && choice.dataset.option === PcChoice).classList.add('draw-color');
this.draw.classList.toggle('draw-animation');
} else {
[...this.optionsImg].find(choice => choice.dataset.option === yourChoice).classList.add('player-choice');
[...this.optionsImg].find(choice => choice.dataset.option === PcChoice).classList.add('pc-choice');
}
this.stats.refreshStats(Result.whoWin(yourChoice, PcChoice));
this.render.call(this, this.stats.getStats());
}
render(stats) {
this.youWins.textContent = `You: ${stats.wins}`;
this.PcWins.textContent = `PC: ${stats.loses}`;
}
}
const newGame = new Game();