-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
130 lines (113 loc) · 4.46 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
let roundCounter = 0;
let userPoints = 0;
let computerPoints = 0;
let playAgain;
// Getting DOM elements
const rockBtn = document.querySelector("#rock");
const paperBtn = document.querySelector("#paper");
const scissorsBtn = document.querySelector("#scissors");
const container = document.querySelector("#buttons-container");
const round = document.querySelector("#round");
const score = document.querySelector("#score");
const hand = document.querySelector("#hand");
//User options
rockBtn.addEventListener("click", () => {
const playerSelection = "rock";
const computerSelection = getComputerChoice();
playRound(playerSelection, computerSelection);
});
paperBtn.addEventListener("click", () => {
const playerSelection = "paper";
const computerSelection = getComputerChoice();
playRound(playerSelection, computerSelection);
});
scissorsBtn.addEventListener("click", () => {
const playerSelection = "scissors";
const computerSelection = getComputerChoice();
playRound(playerSelection, computerSelection);
});
// Create a the hand computer will choose
function getComputerChoice() {
let randomNumber = Math.floor(Math.random() * 3);
const options = ["rock", "paper", "scissors"];
return (finalchoice = options[randomNumber]);
}
// Play a round using computer selection against user's
function playRound(playerSelection, computerSelection) {
playerSelection = playerSelection.toLowerCase();
roundCounter++;
round.textContent = `Round number ${roundCounter}`;
//Check hand
if (playerSelection == computerSelection) {
hand.textContent = `You've both chosen ${computerSelection} no one wins round, play again`;
} else if (playerSelection == "rock" && computerSelection == "paper") {
computerPoints++;
hand.textContent = `Computer played ${computerSelection}, paper beats rock, you lose this round.`;
} else if (playerSelection == "rock" && computerSelection == "scissors") {
userPoints++;
hand.textContent = `Computer played ${computerSelection}, rock beats scissors, you win this round.`;
} else if (playerSelection == "paper" && computerSelection == "rock") {
userPoints++;
hand.textContent = `Computer played ${computerSelection}, paper beats rock, you win this round.`;
} else if (playerSelection == "paper" && computerSelection == "scissors") {
computerPoints++;
hand.textContent = `Computer played ${computerSelection}, scissors beats paper, you lose this round.`;
} else if (playerSelection == "scissors" && computerSelection == "paper") {
userPoints++;
hand.textContent = `Computer played ${computerSelection}, scissors beats paper, you win this round.`;
} else if (playerSelection == "scissors" && computerSelection == "rock") {
computerPoints++;
hand.textContent = `Computer played ${computerSelection}, rock beats scissors, you lose this round.`;
} else {
hand.textContent = "You didn't choose a valid option, try again";
}
score.textContent = `You have ${userPoints} points.
Computer has ${computerPoints} points`;
//Check scores
if (computerPoints == 3) {
hand.textContent = `You lose, computer has ${computerPoints} points`;
setGameOver();
} else if (userPoints == 3) {
hand.textContent = `You have ${userPoints} points, you win`;
setGameOver();
}
}
//Game over function
function setGameOver() {
rockBtn.disabled = true;
paperBtn.disabled = true;
scissorsBtn.disabled = true;
playAgain = document.createElement("button");
playAgain.textContent = "Play again";
playAgain.classList.add("button");
playAgain.classList.add("is-large");
playAgain.classList.add("is-warning");
container.appendChild(playAgain);
playAgain.addEventListener("click", resetGame);
}
// Reset game function
function resetGame() {
roundCounter = 0;
userPoints = 0;
computerPoints = 0;
const resetMessages = document.querySelectorAll(".messages p");
for (const message of resetMessages) {
message.textContent = "";
}
playAgain.parentNode.removeChild(playAgain);
rockBtn.disabled = false;
paperBtn.disabled = false;
scissorsBtn.disabled = false;
}
// Creating a five round match
// function game() {
// for (let i = 1; i <= 5; i++) {
// playerSelection = prompt("What you choose?");
// const computerSelection = getComputerChoice();
// console.log(playRound(playerSelection, computerSelection));
// }
// }
// testing our app
// const playerSelection = "scissors";
// const computerSelection = getComputerChoice();
// console.log(playRound(playerSelection, computerSelection));