-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
124 lines (108 loc) · 3.13 KB
/
app.js
File metadata and controls
124 lines (108 loc) · 3.13 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
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
const choices = document.querySelectorAll('.choice');
const score = document.getElementById('score')
const result = document.getElementById('result')
const restart = document.getElementById('restart')
const modal = document.querySelector('.modal');
const scoreboard = {
player: 0,
computer: 0
}
// Play game
function play(e) {
restart.style.display = 'inline-block';
const playerChoice = (e.target.id);
const computerChoice = getComputerChoice();
const winner = getWinner(playerChoice, computerChoice);
showWinner(winner, computerChoice);
}
// Get computer choice
function getComputerChoice() {
const rand = Math.random();
if(rand < 0.34) {
return 'rock';
}else if(rand <= 0.63) {
return 'paper';
}else {
return 'scissors'
}
}
// Get game winner
function getWinner(player, computer) {
if(player === computer) {
return 'draw';
} else if (player === 'rock') {
if(computer === 'paper') {
return 'computer';
}else {
return 'player';
}
} else if (player === 'paper') {
if(computer === 'scissors') {
return 'computer';
} else {
return 'player';
}
} else if (player === 'scissors') {
if (computer === 'rock') {
return 'computer';
} else {
return 'player';
}
}
}
// Show te winner
function showWinner(winner,computerChoice) {
if(winner === 'player') {
// Increment player score
scoreboard.player++;
// Show modal result
result.innerHTML = `
<h1 class="text-win">You Win</h1>
<i class="fas fa-hand-${computerChoice} fa-10x"></i>
<p>Computer Chose <strong>${computerChoice.charAt(0).toUpperCase() +
computerChoice.slice(1)}</strong></p>
`;
}else if(winner === 'computer') {
// Increment computer score
scoreboard.computer++;
// Show modal result
result.innerHTML = `
<h1 class="text-lose">You lose</h1>
<i class="fas fa-hand-${computerChoice} fa-10x"></i>
<p>Computer Chose <strong>${computerChoice.charAt(0).toUpperCase() +
computerChoice.slice(1)}</strong></p>
`;
} else {
result.innerHTML = `
<h1>It's A Draw</h1>
<i class="fas fa-hand-${computerChoice} fa-10x"></i>
<p>Computer Chose <strong>${computerChoice.charAt(0).toUpperCase() +
computerChoice.slice(1)}</strong></p>
`;
}
// Show score
score.innerHTML =
`<p>Player: ${scoreboard.player}</p>
<p>Computer: ${scoreboard.computer}</p>
`;
modal.style.display = 'block';
}
// Restart game
function restartGame(e) {
scoreboard.player = 0;
scoreboard.computer = 0;
score.innerHTML = `
<p>Player: 0</p>
<p>Computer: 0</p>
`;
}
// Clear modal
function clearModal(e) {
if(e.target == modal) {
modal.style.display = 'none';
}
}
// Event Listeners
choices.forEach(choice => choice.addEventListener('click', play));
window.addEventListener('click', clearModal);
restart.addEventListener('click', restartGame);