-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
128 lines (118 loc) · 4.18 KB
/
app.js
File metadata and controls
128 lines (118 loc) · 4.18 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
124
125
126
127
128
// NEED TO ADD LOGIC WHERE ANIMATION RESETS (REMOVE THE WIGGLE CLASS) AFTER EACH ANIMATION..
//
const game = () => {
const options = document.querySelectorAll(".options button");
// Possible computer options
const comupterOptions = ["Rock", "Paper", "Scissors"];
let pScore = 0;
let cScore = 0;
//player hands
const pHand = document.querySelector(".player-hand");
const cHand = document.querySelector(".computer-hand");
// function to start bring in the game
const startGame = () => {
const startButton = document.querySelector(".start-game");
const introScreen = document.querySelector(".intro");
const scoreBoard = document.querySelector(".score");
const arena = document.querySelector(".arena");
const pCtrls = document.querySelector(".options");
const pMess = document.querySelector(".message1");
startButton.addEventListener("click", () => {
introScreen.classList.add("fadeOut");
arena.classList.add("fadeIn");
scoreBoard.classList.add("fadeIn");
pMess.classList.add("fadeIn");
pCtrls.classList.add("fadeIn");
});
};
//call start game function just after it is defined
startGame();
// Go through each of the player option buttons and add an eventlister of click that ...
// runs a function on click
options.forEach(options => {
options.addEventListener("click", function() {
const playerHand = document.querySelector(".player-hand");
const computerHand = document.querySelector(".computer-hand");
// Create a random number between 0-1 mutliply by 3 (number of options in array)...
// ... then round to nearest interger.
const compIndex = Math.floor(Math.random() * 3);
// random interger between 0-2 used to select a string from the computerOptions array ...
removeWiggle();
const computerChoice = comupterOptions[compIndex];
console.log("comp" + computerChoice);
console.log(this.textContent);
//update the hand images
playerHand.src = `./images/H` + `${this.textContent}.png`;
computerHand.src = `./images/${computerChoice}.png`;
// Run checker function passing in this.textContent (the contents of the button element) ...
// and the computer's random choice of rock paper or scissors from array
removeWiggle();
checker(this.textContent, computerChoice);
console.log(pScore);
console.log(cScore);
// score updated at the end of each game
updateScore();
});
});
const updateScore = () => {
const playerScore = document.querySelector(".player-score p");
const computerScore = document.querySelector(".computer-score p");
// changing the html element with the score
playerScore.innerHTML = pScore;
computerScore.innerHTML = cScore;
};
const removeWiggle = () => {
cHand.classList.remove("winnerShake");
pHand.classList.remove("winnerShake");
};
const checker = (playerChoice, computerChoice) => {
if (playerChoice === computerChoice) {
console.log("Draw");
removeWiggle();
return;
}
// When player clicks rock
if (playerChoice === "Rock") {
if (computerChoice === "Scissors") {
console.log("You Win");
pScore++;
pHand.classList.add("winnerShake");
return;
} else if (computerChoice === "Paper") {
console.log("You Lose");
cScore++;
cHand.classList.add("winnerShake");
return;
}
}
// When player clicks scissors
if (playerChoice === "Scissors") {
if (computerChoice === "Paper") {
console.log("You Win");
pScore++;
pHand.classList.add("winnerShake");
return;
} else if (computerChoice === "Rock") {
console.log("You Lose");
cScore++;
cHand.classList.add("winnerShake");
return;
}
}
// When player clicks paper
if (playerChoice === "Paper") {
if (computerChoice === "Rock") {
console.log("You Win");
pScore++;
pHand.classList.add("winnerShake");
return;
} else if (computerChoice === "Scissors") {
console.log("You Lose");
cScore++;
cHand.classList.add("winnerShake");
return;
}
}
};
};
game();