-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
106 lines (97 loc) · 3.48 KB
/
script.js
File metadata and controls
106 lines (97 loc) · 3.48 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
// Game memory
let currentRound = 0;
let currentUserScore = 0;
let currentComputerScore = 0;
// DOM Elements
const choices = document.querySelectorAll('article');
const round = document.getElementById('round');
const userScore = document.getElementById('user-score');
const computerScore = document.getElementById('computer-score');
const gameStatus = document.getElementById('game-status');
choices.forEach((choice) => {
choice.addEventListener('click', game, {
capture: true,
});
});
// Game Logic
function game(event) {
event.stopPropagation();
const result = playRound(this.classList.value.toLowerCase(), getComputerChoice());
currentRound += 1;
if (currentRound <= 5) {
//Update the stored scores
currentUserScore += result.userScore;
currentComputerScore += result.computerScore;
//Update the DOM to reflect the scores
round.innerText = currentRound;
userScore.innerText = currentUserScore;
computerScore.innerText = currentComputerScore;
gameStatus.innerText = result.message;
} else {
if (currentUserScore > currentComputerScore) {
gameStatus.innerText = 'User wins! Make another selection to play another round!'
} else if (currentUserScore < currentComputerScore) {
gameStatus.innerText = 'Computer wins :( Make another selection to play another round!'
} else {
gameStatus.innerText = '5 Round Draw! What an unlikely event!'
};
//Reset the scores
currentRound = 0;
currentUserScore = 0;
currentComputerScore = 0;
//Update the DOM to reflect the scores
round.innerText = currentRound;
userScore.innerText = currentUserScore;
computerScore.innerText = currentComputerScore;
}
}
// Logic to determine winner of round
function playRound(playerSelection, computerSelection) {
if (
playerSelection === 'rock' && computerSelection === 'scissors'
||
playerSelection === 'paper' && computerSelection === 'rock'
||
playerSelection === 'scissors' && computerSelection === 'rock'
) {
console.log(`You win! ${playerSelection} beats ${computerSelection}`);
return {
'message': `You win! ${playerSelection} beats ${computerSelection}`,
'userScore': 1,
'computerScore': 0};
} else if (
playerSelection === 'rock' && computerSelection === 'paper'
||
playerSelection === 'paper' && computerSelection === 'scissors'
||
playerSelection === 'scissors' && computerSelection === 'rock'
) {
console.log(`You lose! ${playerSelection} defeated by ${computerSelection}`);
return {
'message': `You lose! ${playerSelection} defeated by ${computerSelection}`,
'userScore': 0,
'computerScore': 1};
} else if (
playerSelection === 'rock' && computerSelection === 'rock'
||
playerSelection === 'paper' && computerSelection === 'paper'
||
playerSelection === 'scissors' && computerSelection === 'scissors'
) {
console.log(`Draw! ${playerSelection} is equal to ${computerSelection}`);
return {
'message': `Draw! ${playerSelection} is equal to ${computerSelection}`,
'userScore': 0,
'computerScore': 0};
}
}
function getComputerChoice() {
let choices = ['rock', 'paper', 'scissors'];
let index = getRandomArbitrary(0, choices.length);
return choices[index];
}
function getRandomArbitrary(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min);
}