forked from kunjgit/GameZone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
110 lines (93 loc) · 3.77 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
108
109
110
// Function to generate a random choice for the computer
function getRandomComputerResult() {
const options = ["Rock", "Paper", "Scissors", "Lizard", "Spock"];
const randomIndex = Math.floor(Math.random() * options.length);
return options[randomIndex];
}
// Function to determine if the player has won the round
function hasPlayerWonTheRound(player, computer) {
return (
(player === "Scissors" && computer === "Paper") ||
(player === "Paper" && computer === "Rock") ||
(player === "Rock" && computer === "Lizard") ||
(player === "Lizard" && computer === "Spock") ||
(player === "Spock" && computer === "Scissors") ||
(player === "Scissors" && computer === "Lizard") ||
(player === "Lizard" && computer === "Paper") ||
(player === "Paper" && computer === "Spock") ||
(player === "Spock" && computer === "Rock") ||
(player === "Rock" && computer === "Scissors")
);
}
// Initialize scores
let playerScore = 0;
let computerScore = 0;
// Function to get the results of a round
function getRoundResults(userOption) {
const computerResult = getRandomComputerResult();
if (hasPlayerWonTheRound(userOption, computerResult)) {
playerScore++;
return `Player wins! ${userOption} beats ${computerResult}`;
} else if (computerResult === userOption) {
return `It's a tie! Both chose ${userOption}`;
} else {
computerScore++;
return `Computer wins! ${computerResult} beats ${userOption}`;
}
}
// Get DOM elements
const playerScoreSpanElement = document.getElementById("player-score");
const computerScoreSpanElement = document.getElementById("computer-score");
const roundResultsMsg = document.getElementById("results-msg");
const winnerMsgElement = document.getElementById("winner-msg");
const optionsContainer = document.querySelector(".options-container");
const resetGameBtn = document.getElementById("reset-game-btn");
// Function to display results and check for game end
function showResults(userOption) {
roundResultsMsg.innerText = getRoundResults(userOption);
computerScoreSpanElement.innerText = computerScore;
playerScoreSpanElement.innerText = playerScore;
// Check if game has ended (first to 5 points)
if (playerScore === 5 || computerScore === 5) {
winnerMsgElement.innerText = `${
playerScore === 5 ? "Player" : "Computer"
} has won the game!`;
resetGameBtn.style.display = "block";
optionsContainer.style.display = "none";
}
};
// Function to reset the game
function resetGame() {
playerScore = 0;
computerScore = 0;
playerScoreSpanElement.innerText = playerScore;
computerScoreSpanElement.innerText = computerScore;
resetGameBtn.style.display = "none";
optionsContainer.style.display = "block";
winnerMsgElement.innerText = "";
roundResultsMsg.innerText = "";
};
// Add event listener to reset button
resetGameBtn.addEventListener("click", resetGame);
// Get button elements
const rockBtn = document.getElementById("rock-btn");
const paperBtn = document.getElementById("paper-btn");
const scissorsBtn = document.getElementById("scissors-btn");
const lizardBtn = document.getElementById("lizard-btn");
const spockBtn = document.getElementById("spock-btn");
// Add event listeners to game option buttons
rockBtn.addEventListener("click", function () {
showResults("Rock");
});
paperBtn.addEventListener("click", function () {
showResults("Paper");
});
scissorsBtn.addEventListener("click", function () {
showResults("Scissors");
});
lizardBtn.addEventListener("click", function () {
showResults("Lizard");
});
spockBtn.addEventListener("click", function () {
showResults("Spock");
});