-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
60 lines (50 loc) · 1.46 KB
/
main.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
let playerScore = 0;
let botScore = 0;
let previusResults = [0 , 0];
function play(choice) {
let myChoice = choice
let botChoice = Math.floor(Math.random() * 3);
let result = getWinner(myChoice, botChoice);
removeSelect(previusResults[0], previusResults[1]);
addSelect(myChoice, botChoice);
showResult(result);
previusResults = [myChoice, botChoice];
}
// 0 = Empate
// 1 = Ganhou
// 2 = Perdeu
// 0 = Pedra
// 1 = Papel
// 2 = Tesoura
function getWinner(myChoice, botChoice) {
if (myChoice == botChoice) {
return 0;
}
return ((myChoice - botChoice) + 3) % 3
}
function showResult(result) {
switch(result) {
case 0:
return alert("Você empatou!")
case 1:
return addScorePlayer();
case 2:
return addScoreBot();
}
}
function addScoreBot() {
botScore++;
document.getElementById("bot_score").innerHTML = botScore;
}
function addScorePlayer() {
playerScore++;
document.getElementById("player_score").innerHTML = playerScore;
}
function addSelect(myChoice, botChoice) {
document.getElementById("player_choice_" + myChoice).classList.add('select')
document.getElementById("bot_choice_" + botChoice).classList.add('select')
}
function removeSelect(myChoice, botChoice) {
document.getElementById("player_choice_" + myChoice).classList.remove('select')
document.getElementById("bot_choice_" + botChoice).classList.remove('select')
}