-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
107 lines (97 loc) · 3.57 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
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
const rockButton = document.getElementById("rock");
rockButton.addEventListener("click", () => {
playRound(rockButton.innerText);
});
const paperButton = document.getElementById("paper");
paperButton.addEventListener("click", () => {
playRound(paperButton.innerText);
});
const scissorsButton = document.getElementById("scissors");
scissorsButton.addEventListener("click", () => {
playRound(scissorsButton.innerText);
});
const buttons = document.querySelectorAll("button");
const clickSound = document.getElementById("click");
buttons.forEach((button) => {
button.addEventListener("click", () => {
clickSound.play();
});
});
const newGame = document.getElementById("new-game");
let finalMessage = document.querySelector(".result-display > h2");
let playResult = document.querySelector(".round-result > p");
const userScore = document.querySelector("div > .h-s-num");
const compScore = document.querySelector("div > .c-s-num");
let wins;
let losses;
let playing;
function start() {
wins = 0;
losses = 0;
playing = true;
userScore.innerHTML = 0;
compScore.innerHTML = 0;
document.querySelector("body").classList.remove("player--wins");
document.querySelector("body").classList.remove("player--loses");
finalMessage.textContent = "Can You Win?";
playResult.textContent = "";
}
start();
newGame.addEventListener("click", start);
function getComputerChoice() {
if (playing) {
let randomChoice = Math.floor(Math.random() * 3) + 1;
if (randomChoice == 1) {
return "Rock";
} else if (randomChoice == 2) {
return "Paper";
} else {
return "Scissors";
}
}
}
function playRound(playerSelection, computerSelection) {
if (playing) {
computerSelection = getComputerChoice();
if (playerSelection == computerSelection) {
finalMessage.textContent = "No winner,It's a Draw.";
} else if (playerSelection == "Rock" && computerSelection == "Paper") {
finalMessage.textContent = "You lose,Paper covers Rock.";
} else if (playerSelection == "Rock" && computerSelection == "Scissors") {
finalMessage.textContent = "You win,Rock breaks Scissors!";
} else if (playerSelection == "Paper" && computerSelection == "Rock") {
finalMessage.textContent = "You win,Paper covers Rock!";
} else if (playerSelection == "Paper" && computerSelection == "Scissors") {
finalMessage.textContent = "You lose,Scissors cuts Paper.";
} else if (playerSelection == "Scissors" && computerSelection == "Paper") {
finalMessage.textContent = "You win, Scissors cuts Paper!";
} else if (playerSelection == "Scissors" && computerSelection == "Rock") {
finalMessage.textContent = "You lose,Rock breaks Scissors.";
}
game();
}
}
function game() {
if (playing) {
document.querySelector("div > .h-s-num").innerHTML = wins;
document.querySelector("div > .c-s-num").innerHTML = losses;
const winRegex = /You win,*/;
const loseRegex = /You lose,*/;
if (winRegex.test(finalMessage.textContent)) {
document.querySelector("div > .h-s-num").innerHTML = wins += 1;
} else if (loseRegex.test(finalMessage.textContent)) {
document.querySelector("div > .c-s-num").innerHTML = losses += 1;
}
if (document.querySelector("div > .h-s-num").innerHTML == 5) {
playing = false;
document.querySelector("body").classList.add("player--wins");
finalMessage.textContent = "Congratulations,You Won!";
playResult.textContent = "You were first to 5.";
} else if (document.querySelector("div > .c-s-num").innerHTML == 5) {
playing = false;
document.querySelector("body").classList.add("player--loses");
finalMessage.textContent = "You Lost.";
playResult.textContent = "Computer was first to 5.";
}
}
}