-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
136 lines (122 loc) · 4.03 KB
/
script.js
File metadata and controls
136 lines (122 loc) · 4.03 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
129
130
131
132
133
134
135
136
$(document).ready(function () {
let playerScore = 0;
let computerScore = 0;
let round = 1;
let matchGoal = 0; // Match goal will be 3, 5, or 10
let playerName = "";
const choices = ["rock", "paper", "scissors"];
const getComputerChoice = () => choices[Math.floor(Math.random() * 3)];
const getResult = (player, computer) => {
if (player === computer) return "draw";
if (
(player === "rock" && computer === "scissors") ||
(player === "scissors" && computer === "paper") ||
(player === "paper" && computer === "rock")
) {
return "win";
}
return "lose";
};
const updateScore = (result) => {
if (result === "win") playerScore++;
if (result === "lose") computerScore++;
$("#player-score").text(playerScore);
$("#computer-score").text(computerScore);
};
const updateRound = () => {
$("#round-counter").text(round);
};
const showBanner = (result) => {
const banner = $("#game-banner");
if (result === "win") {
banner.text(`${playerName}'s Point!`);
banner.removeClass().addClass("game-banner win-banner").fadeIn();
$("#win-sound")[0].play();
} else if (result === "lose") {
banner.text(`Computer's Point!`);
banner.removeClass().addClass("game-banner lose-banner").fadeIn();
$("#lose-sound")[0].play();
} else {
banner.text(`It's a Draw!`);
banner.removeClass().addClass("game-banner draw-banner").fadeIn();
}
setTimeout(() => {
banner.fadeOut();
}, 1000);
};
const showFinal = (result) => {
const Final = $("#Final");
if (result === "win") {
Final.text(`You Won!! Great Match 🎉🎉`);
Final.removeClass().addClass("Final win-banner").fadeIn();
$("#winning-sound")[0].play();
} else if (result === "lose") {
Final.text(`You Lost! Try again`);
Final.removeClass().addClass("Final lose-banner").fadeIn();
} else {
Final.text(`It's a Draw!`);
Final.removeClass().addClass("Final draw-banner").fadeIn();
}
setTimeout(() => {
Final.fadeOut();
}, 4000);
};
const resetRound = () => {
if (playerScore >= matchGoal || computerScore >= matchGoal) {
setTimeout(() => {
if (playerScore > computerScore) {
showFinal("win");
} else if (playerScore < computerScore) {
showFinal("lose");
} else {
showFinal("draw");
}
playerScore = 0;
computerScore = 0;
round = 1;
updateScore();
updateRound();
$("body").css("background", "linear-gradient(to right, #3a6186, #89253e)");
}, 1000);
} else {
round++;
updateRound();
}
};
// Start Game - Name Entry
$("#start-game").click(function () {
playerName = $("#player-name").val().trim();
if (playerName === "") {
alert("Please enter your name to start the game.");
return;
}
$(".name-input").hide();
$(".match-type-input").show();
});
// Start Match - Match Type Selection
$("#start-match").click(function () {
matchGoal = parseInt($("#match-type").val());
$("#match-goal").text(matchGoal);
$(".match-type-input").hide();
$(".game-section").show();
$("#player-name-display").text(playerName);
});
// Player's Choice and Game Logic
$(".choice").click(function () {
const playerChoice = $(this).data("choice");
const computerChoice = getComputerChoice();
const result = getResult(playerChoice, computerChoice);
let resultMessage;
if (result === "win") {
resultMessage = `Great ${playerName}!! ${playerChoice} beats ${computerChoice}.`;
} else if (result === "lose") {
resultMessage = `Ohh!! ${computerChoice} beats ${playerChoice}.`;
} else {
resultMessage = `It's a Draw! You both chose ${playerChoice}.`;
}
$("#result-text").text(resultMessage);
updateScore(result);
showBanner(result);
resetRound();
});
});