-
Notifications
You must be signed in to change notification settings - Fork 25
/
app.js
162 lines (147 loc) · 4.81 KB
/
app.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const options = ["rock", "paper", "scissor"];
let score = {
you: 0,
computer: 0
};
const WIN_GREEN = "#6ac475";
const LOSE_RED = "#c4736a";
const DRAW_BLUE = "#5865f2";
const playerHand = document.querySelector(".hands .player-hand");
const computerHand = document.querySelector(".hands .computer-hand");
function getRandomInt() {
return Math.floor(Math.random() * 3);
}
const btns = document.querySelectorAll(".options button");
btns.forEach((btn) => {
btn.addEventListener("click", () => {
document.querySelector(".hands .player-hand").src = "rock.png";
document.querySelector(".hands .computer-hand").src = "rock.png";
playerHand.classList.add('shakePlayer')
computerHand.classList.add('shakeComputer')
const playerA = btn.querySelector("label").innerText;
const playerB = options[getRandomInt()];
setTimeout(()=>compare(playerA, playerB),1000)
});
});
function compare(player, computer) {
const won = "YOU WON";
const lose = "YOU LOST";
const resultEl = document.querySelector(".hands .result");
const youScore = document.querySelector(".score .you");
const computerScore = document.querySelector(".score .computer");
if (player == computer) {
console.log(`${player} is equal to ${computer}`);
update(player, computer);
resultEl.style.color = DRAW_BLUE;
resultEl.innerText = "DRAW";
} else if (player == "rock" && computer == "scissor") {
console.log(`${player} defeats ${computer}`);
update(player, computer);
score.you++;
resultEl.style.color = WIN_GREEN;
resultEl.innerText = won;
youScore.innerText = score.you;
} else if (player == "rock" && computer == "paper") {
console.log(`${player} loses to ${computer}`);
update(player, computer);
score.computer++;
resultEl.style.color = LOSE_RED;
resultEl.innerText = lose;
computerScore.innerText = score.computer;
} else if (player == "paper" && computer == "scissor") {
console.log(`${player} loses to ${computer}`);
update(player, computer);
score.computer++;
resultEl.style.color = LOSE_RED;
resultEl.innerText = lose;
computerScore.innerText = score.computer;
} else if (player == "paper" && computer == "rock") {
console.log(`${player} defeats ${computer}`);
update(player, computer);
score.you++;
resultEl.style.color = WIN_GREEN;
resultEl.innerText = won;
youScore.innerText = score.you;
} else if (player == "scissor" && computer == "rock") {
console.log(`${player} loses to ${computer}`);
update(player, computer);
score.computer++;
resultEl.style.color = LOSE_RED;
resultEl.innerText = lose;
computerScore.innerText = score.computer;
} else if (player == "scissor" && computer == "paper") {
console.log(`${player} defeats ${computer}`);
update(player, computer);
score.you++;
resultEl.style.color = WIN_GREEN;
resultEl.innerText = won;
youScore.innerText = score.you;
}
// playerHand.classList.remove('shake')
// computerHand.classList.remove('shake')
}
function update(player, computer) {
const rock = "rock.png";
const paper = "paper.png";
const scissor = "scissor.png";
// playerHand
if (player == "rock") {
playerHand.src = rock;
} else if (player == "paper") {
playerHand.src = paper;
} else if (player == "scissor") {
playerHand.src = scissor;
}
// computerHand
if (computer == "rock") {
computerHand.src = rock;
} else if (computer == "paper") {
computerHand.src = paper;
} else if (computer == "scissor") {
computerHand.src = scissor;
}
playerHand.classList.remove('shakePlayer')
computerHand.classList.remove('shakeComputer')
}
//Reset Game Button ---------------------------------
const resetBtn = document.querySelector(".reset");
resetBtn.addEventListener("click", () => {
score.you = 0;
score.computer = 0;
document.querySelector(".score .you").innerText = score.you;
document.querySelector(".score .computer").innerText = score.computer;
document.querySelector(".hands .result").innerText = "";
document.querySelector(".hands .player-hand").src = "rock.png";
document.querySelector(".hands .computer-hand").src = "rock.png";
});
// // Mapping
// // 0 -> rock, 1->paper, 2->scissor
// const options = ["rock", "paper", "scissor"];
// // number
// /**
// *
// * @param {number} player1Choice
// * @param {number} player2Choice
// * return string player1, player2, draw
// */
// function whoWon(player1Choice, player2Choice) {
// /**
// * 0, 0 => draw
// * 0, 1 => rock
// * 0, 2 => scissor
// * 1, 0 => rock
// * 1, 1 => draw
// * 1, 2
// * 2, 0
// * 2, 1
// * 2, 2 => draw
// */
// }
// function generateComputerChoice() {
// let a = options[getRandomInt(3)];
// console.log(a);
// }
// generateComputerChoice();
// function getRandomInt(max) {
// return Math.floor(Math.random() * max);
// }