-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·58 lines (53 loc) · 1.83 KB
/
Copy pathapp.js
File metadata and controls
executable file
·58 lines (53 loc) · 1.83 KB
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
const startTheGameButton = window.document.getElementById("start-the-game-button");
const ROCK = 'ROCK';
const PAPER = 'PAPER';
const SCISSORS = 'SCISSORS';
const DEFAULT_CHOICE = ROCK;
const DRAW = 'DRAW';
const USER_WINS = 'Human being is still the winner';
const AI_WINS = 'Artificial Intelligence won the human race';
let running = false
// The idea is to expand the classical Rock, Paper and Scissors game to cover Star Trek symbolism
// You could replace Scissors, Rock and Paper with Spock greeting, holodeck, Klingon or like etc..
// and also you can compose more terms/actions/rules than it was in the original game
getActionsFromThePlayer = function() {
const selection = prompt(`${ROCK}, ${PAPER} or ${SCISSORS}`, '').toUpperCase();
if (selection !== ROCK && selection !== PAPER && selection !== SCISSORS) {
alert(`You did something wrong... the ${DEFAULT_CHOICE} is automatically chosen for you`);
return DEFAULT_CHOICE;
}
return selection;
};
getAIChoice = function () {
const randomizedValue = Math.random();
if (randomizedValue < 0.34) {
return ROCK;
} else if (randomizedValue < 0.67) {
return PAPER;
} else {
return SCISSORS;
}
}
const findTheWinner = function(aChoice, uChoice) {
if (aChoice === uChoice) {
return DRAW;
} else if (
aChoice === ROCK && uChoice === PARER ||
aChoice === PAPER && uChoice === SCISSORS ||
aChoice === SCISSORS && uChoice === ROCK) {
return USER_WINS;
} else {
return AI_WINS;
}
}
startTheGameButton.addEventListener('click', function() {
if (running) {
return;
}
running = true
console.log('The game is about to begin');
const whatThePlayerHasSelected = getActionsFromThePlayer();
const whatTheAIHasSelected = getAIChoice();
const winner = findTheWinner(whatTheAIHasSelected, whatThePlayerHasSelected );
console.log(winner);
});