-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
83 lines (83 loc) · 2.57 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
var startGameBtn = document.getElementById('start-game-btn');
var ROCK = 'ROCK';
var PAPER = 'PAPER';
var SCISSORS = 'SCISSORS';
var LIZARD = 'LIZARD';
var SPOCK = 'SPOCK';
var DEFAULT_CHOICE = ROCK;
var RESULT_DRAW = 'DRAW';
var RESULT_USER_WINS = 'USER_WINS';
var RESULT_BOT_WINS = 'BOT_WINS';
var gameIsRunning = false;
var getUserChoice = function () {
var selection = prompt("".concat(ROCK, ", ").concat(PAPER, ", ").concat(SCISSORS, ", ").concat(LIZARD, " or ").concat(SPOCK, " ?"), '').toUpperCase();
if (selection !== ROCK &&
selection !== PAPER &&
selection !== SCISSORS &&
selection !== LIZARD &&
selection !== SPOCK) {
alert("Invalid choice! We chose ".concat(DEFAULT_CHOICE, " for you!"));
return DEFAULT_CHOICE;
}
return selection;
};
var getBotChoice = function () {
var randomValue = Math.random();
if (randomValue < 0.2) {
return ROCK;
}
else if (randomValue < 0.4) {
return PAPER;
}
else if (randomValue < 0.6) {
return SCISSORS;
}
else if (randomValue < 0.8) {
return LIZARD;
}
else {
return SPOCK;
}
};
var getWinner = function (bChoice, uChoice) {
if (uChoice === void 0) { uChoice = DEFAULT_CHOICE; }
if (bChoice === uChoice) {
return RESULT_DRAW;
}
else if ((bChoice === ROCK && uChoice === PAPER) ||
(bChoice === PAPER && uChoice === SCISSORS) ||
(bChoice === LIZARD && uChoice === ROCK) ||
(bChoice === SPOCK && uChoice === LIZARD) ||
(bChoice === SCISSORS && uChoice === SPOCK) ||
(bChoice === LIZARD && uChoice === SCISSORS) ||
(bChoice === PAPER && uChoice === LIZARD) ||
(bChoice === SPOCK && uChoice === PAPER) ||
(bChoice === ROCK && uChoice === SPOCK) ||
(bChoice === SCISSORS && uChoice === ROCK)) {
return RESULT_USER_WINS;
}
else {
return RESULT_BOT_WINS;
}
};
startGameBtn.addEventListener('click', function () {
if (gameIsRunning) {
return;
}
gameIsRunning = true;
var userSelection = getUserChoice();
var botSelection = getBotChoice();
var winner = getWinner(botSelection, userSelection);
var message = "You picked ".concat(userSelection || DEFAULT_CHOICE, ", bot picked ").concat(botSelection, ", so you ");
if (winner === RESULT_DRAW) {
message = message + 'had a draw.';
}
else if (winner === RESULT_USER_WINS) {
message = message + 'won.';
}
else {
message = message + 'lost.';
}
alert(message);
gameIsRunning = false;
});