-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
130 lines (102 loc) · 3.17 KB
/
app.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var playerScore;
var computerScore;
var roundNumber;
var isGamePlaying;
const playerScoreH2 = document.getElementById("player-score");
const computerScoreH2 = document.getElementById("computer-score");
const playerChoiceImg = document.getElementById("player-choice");
const computerChoiceImg = document.getElementById("computer-choice");
const optionRockImg = document.getElementById("rock");
const optionPaperImg = document.getElementById("paper");
const optionScissorsImg = document.getElementById("scissors");
const roundNumberSpan = document.getElementById("round-number");
const roundResultH3 = document.getElementById("round-result");
const newGameButton = document.getElementById("new-game-btn");
newGameButton.addEventListener("click", function() {
startNewGame();
});
optionRockImg.addEventListener("click", function() {
if (isGamePlaying) {
selectGesture("rock");
}
});
optionPaperImg.addEventListener("click", function() {
if (isGamePlaying) {
selectGesture("paper");
}
});
optionScissorsImg.addEventListener("click", function() {
if (isGamePlaying) {
selectGesture("scissors");
}
});
function startNewGame() {
playerScore = 0;
computerScore = 0;
roundNumber = 0;
isGamePlaying = true;
playerScoreH2.innerHTML = playerScore;
computerScoreH2.innerHTML = computerScore;
roundNumberSpan.innerHTML = roundNumber;
roundResultH3.innerHTML = "";
playerChoiceImg.src = "";
computerChoiceImg.src = "";
optionRockImg.classList.add("active");
optionPaperImg.classList.add("active");
optionScissorsImg.classList.add("active");
}
function computerChoice() {
var options = ["rock", "paper", "scissors"];
var randomNumber = Math.floor(Math.random() * 3);
return options[randomNumber];
}
function selectGesture(playerChoiceValue) {
var computerChoiceValue = computerChoice();
playerChoiceImg.src = "img/" + playerChoiceValue + ".svg";
computerChoiceImg.src = "img/" + computerChoiceValue + ".svg";
switch (playerChoiceValue + " " + computerChoiceValue) {
case "rock scissors":
case "paper rock":
case "scissors paper":
playerWins(playerChoiceValue, computerChoiceValue);
break;
case "scissors rock":
case "rock paper":
case "paper scissors":
playerLoses(playerChoiceValue, computerChoiceValue);
break;
case "rock rock":
case "paper paper":
case "scissors scissors":
draw();
break;
}
}
function playerWins(player, computer) {
playerScore++;
playerScoreH2.innerHTML = playerScore;
roundResultH3.innerHTML = player + " beats " + computer + "!";
updateRound();
}
function playerLoses(player, computer) {
computerScore++;
computerScoreH2.innerHTML = computerScore;
roundResultH3.innerHTML = computer + " beats " + player + "!";
updateRound();
}
function draw() {
roundResultH3.innerHTML = "It's a draw!";
updateRound();
}
function updateRound() {
roundNumber++;
roundNumberSpan.innerHTML = roundNumber;
if (roundNumber >= 10) {
roundResultH3.innerHTML = "Game over!";
isGamePlaying = false;
optionRockImg.classList.remove("active");
optionPaperImg.classList.remove("active");
optionScissorsImg.classList.remove("active");
}
}
startNewGame();