-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
84 lines (78 loc) · 3.73 KB
/
script.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
var playerCounter = 0;
var cpuCounter = 0;
var computerSelection;
document.querySelector('.rock').addEventListener('click', playGame);
document.querySelector('.paper').addEventListener('click', playGame);
document.querySelector('.scissors').addEventListener('click', playGame);
document.querySelector('.reset').addEventListener('click', resetGame);
document.getElementById('score').innerHTML = `Player Score: ${playerCounter} | CPU Score: ${cpuCounter}`;
// This function make sure the score is below 5. It assigns a random number from 1 to 3 to computerSelection
// and grab the name of the selected button from object 'e' and assign it to playerSelection variable.
function playGame (e) {
if (playerCounter < 5 && cpuCounter <5 ) {
computerSelection = Math.floor(Math.random() * 3);
var playerSelection = e.target.classList[2].toLowerCase();
// console.log(e)
computerPlay();
// compare(playerSelection, computerSelection)
document.getElementById('round-winner').innerHTML = compare(playerSelection, computerSelection);
document.getElementById('score').innerHTML = `Player Score: ${playerCounter} | CPU Score: ${cpuCounter}`;
} else {
// (playerCounter > cpuCounter) ? console.log("Player Wins!") : console.log("CPU Wins!");
document.getElementById('score').innerHTML = `Player Score: ${playerCounter} | CPU Score: ${cpuCounter}`;
return;
}
}
function resetGame () {
playerCounter = 0;
cpuCounter = 0;
document.getElementById('score').innerHTML = `Player Score: ${playerCounter} | CPU Score: ${cpuCounter}`;
document.getElementById('round-winner').innerHTML = 'Play!'
}
// This function converts the random number in computerSelection
// for the CPU to a selection of "Rock", "Paper" or "Scissors".
function computerPlay () {
switch (computerSelection) {
case 0:
computerSelection = "Paper"
break;
case 1:
computerSelection = "Rock"
break;
case 2:
computerSelection = "Scissors"
break;
}
return computerSelection;
}
// This function compare the selections of Player and CPU and update the counters and anounce
// round winner.
function compare (playerSelection, computerSelection) {
if ( playerSelection == "paper" && computerSelection == "Rock") {
playerCounter++
console.log(`Player Wins! ${'\n'} Player Score: ${playerCounter} - CPU Score: ${cpuCounter}`)
return 'Player Wins!'
} else if ( playerSelection == "paper" && computerSelection == "Scissors") {
cpuCounter++
console.log(`CPU Wins! ${'\n'} Player Score: ${playerCounter} - CPU Score: ${cpuCounter}`)
return 'CPU Wins!'
} else if ( playerSelection == "rock" && computerSelection == "Paper") {
cpuCounter++
console.log(`CPU Wins! ${'\n'} Player Score: ${playerCounter} - CPU Score: ${cpuCounter}`)
return 'CPU Wins!'
} else if ( playerSelection == "rock" && computerSelection == "Scissors") {
playerCounter++
console.log(`Player Wins! ${'\n'} Player Score: ${playerCounter} - CPU Score: ${cpuCounter}`)
return 'Player Wins!'
} else if ( playerSelection == "scissors" && computerSelection == "Paper") {
playerCounter++
console.log(`Player Wins! ${'\n'} Player Score: ${playerCounter} - CPU Score: ${cpuCounter}`)
return 'Player Wins!'
} else if ( playerSelection == "scissors" && computerSelection == "Rock") {
cpuCounter++
console.log(`CPU Wins! ${'\n'} Player Score: ${playerCounter} - CPU Score: ${cpuCounter}`)
return 'CPU Wins!'
} else {
return "Tie!"
}
}