-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
100 lines (87 loc) · 3.06 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
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
windisplay = document.getElementById('wins');
losedisplay = document.getElementById('loses');
buttons = document.querySelectorAll('.gamebutton');
jombotron = document.querySelector('.title');
modal = document.querySelector('.modal');
retry = document.getElementById('restartBtn');
display = document.querySelector('#display');
endgamemsg = document.getElementById('endgameMsg');
function computerSelection() {
options = ["ROCK", "PAPER", "SCISSORS"];
index = Math.floor((Math.random()*3));
return options[index]
}
function simulateRound(playerSelection, computerSelection) {
if (computerSelection === playerSelection) {
return `It's a tie! Both have chosen ${computerSelection}.`
}
if (computerSelection === "ROCK") {
if (playerSelection === "SCISSORS") {
return `You lose! Computer played ${computerSelection}, it beats ${playerSelection}.`
} else {
return `You win! ${playerSelection} beats ${computerSelection}.`
}
}
if (computerSelection === "SCISSORS") {
if (playerSelection === "PAPER") {
return `You lose! Computer played ${computerSelection}, it beats ${playerSelection}.`
} else {
return `You win! ${playerSelection} beats ${computerSelection}.`
}
}
if (computerSelection === "PAPER") {
if (playerSelection === "ROCK") {
return `You lose! Computer played ${computerSelection}, it beats ${playerSelection}.`
} else {
return `You win! ${playerSelection} beats ${computerSelection}.`
}
}
}
function evaluate(win, lose) {
console.log(jombotron);
if (win > lose) {
jombotron.textContent = "You won, Hooray!";
endgamemsg.textContent = "Well done!"
} else if (win < lose) {
jombotron.textContent = "You lost :(";
endgamemsg.textContent = "Better luck next time!"
}
}
function handleEnding() {
modal.classList.add("active");
retry.addEventListener('click', () => closeEndGameModal());
}
function closeEndGameModal() {
modal.classList.remove('active');
restartGame();
}
function restartGame() {
windisplay.textContent = `Player : 0`;
losedisplay.textContent = `Computer : 0`;
display.textContent = "";
jombotron.textContent = "Let the fight begin!"
win = 0, lose = 0;
}
var win = 0, lose = 0;
function game() {
buttons.forEach((button) => {
button.addEventListener('click', () => {
userDecision = button.id;
computerDecision = computerSelection();
result = simulateRound(userDecision, computerDecision);
display.textContent = `${result}`;
if (result.includes("win")) {
win++;
windisplay.textContent = `Player : ${win}`;
} else if (result.includes("lose")) {
lose++;
losedisplay.textContent= `Computer : ${lose}`;
}
if (win >=5 || lose >= 5) {
evaluate(win, lose);
handleEnding();
}
})
});
}
game();