-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
103 lines (92 loc) · 4.9 KB
/
script.js
File metadata and controls
103 lines (92 loc) · 4.9 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
document.addEventListener('DOMContentLoaded', (event) => { // Wait until the DOM is fully loaded before running the script
let computerStartPt = 0; // start point for the matrix
let playerStartPt = 0; // start point for player
const choices = ["apata", "iwe", "scissors"]; // Define the choices for the game
const buttons = document.querySelectorAll('button'); // Select all buttons
const resultsDiv = document.getElementById('results'); // Select the results div
// Add event listeners to each button
buttons.forEach(button => {
button.addEventListener('click', () => { // Add a click event listener to each button
if (button.id === 'reset') { // Check if the clicked button is the reset button
resetGame(); // Call the resetGame function if it is the reset button
} else {
const playerSelection = button.id; // Get the player's choice from the button's id
const computerSelection = computerPlayer(); // Get the computer's random choice
const overallRound = playRound(playerSelection, computerSelection); // Determine the result of the round
switch (overallRound) { // Update scores based on the round's result
case "You Win! Iwe beats Apata":
case "You Win! Apata beats Scissors":
case "You Win! Scissors beats Iwe":
playerStartPt++; // Increment player's score
break;
case "You Lose! Apata beats Scissors":
case "You Lose! Scissors beats Iwe":
case "You Lose! Iwe beats Apata":
computerStartPt++; // Increment computer's score
break;
}
// Update the results and score in the DOM
updateResults(playerSelection, computerSelection, overallRound, playerStartPt, computerStartPt);
// Check if either player has won
if (playerStartPt === 5 || computerStartPt === 5) {
declareWinner(playerStartPt, computerStartPt); // Declare the winner if a score reaches 5
}
}
});
});
// Function to generate a random computer choice
const computerPlayer = () => {
const randomChoice = choices[Math.floor(Math.random() * choices.length)]; // Select a random choice
return randomChoice; // Return the computer's choice
};
// Function to update the results in the DOM
const updateResults = (playerSelection, computerSelection, overallRound, playerStartPt, computerStartPt) => {
resultsDiv.innerHTML = ` <!-- Update the resultsDiv with the current game status -->
<p>You chose: ${playerSelection.charAt(0).toUpperCase() + playerSelection.slice(1)}</p> <!-- Display player's choice -->
<p>Computer chose: ${computerSelection}</p> <!-- Display computer's choice -->
<p>${overallRound}</p> <!-- Display round result -->
<p>Player Score: ${playerStartPt}</p> <!-- Display player's score -->
<p>Computer Score: ${computerStartPt}</p> <!-- Display computer's score -->
`;
};
// Function to declare the winner and reset the game
const declareWinner = (playerStartPt, computerStartPt) => {
if (playerStartPt === 5) { // Check if the player won
resultsDiv.innerHTML += `<p>Congratulations! You beat the Matrix ${playerStartPt} to ${computerStartPt}</p>`;
} else { // Check if the computer won
resultsDiv.innerHTML += `<p>Try again. The Matrix beat you ${computerStartPt} to ${playerStartPt}</p>`;
}
// Reset scores
computerStartPt = 0; // Reset computer's score
playerStartPt = 0; // Reset player's score
};
// Function to reset the game
const resetGame = () => {
computerStartPt = 0; // Reset computer's score
playerStartPt = 0; // Reset player's score
resultsDiv.innerHTML = ''; // Clear the resultsDiv
};
// Function to determine the outcome of a round
function playRound(playerSelection, computerSelection) {
// Determine the winner or if the game is tied based on player and computer choices
if (playerSelection === "apata" && computerSelection === "iwe") {
return "You Lose! Iwe beats Apata";
} else if (playerSelection === "scissors" && computerSelection === "apata") {
return "You Lose! Apata beats Scissors";
} else if (playerSelection === "iwe" && computerSelection === "scissors") {
return "You Lose! Scissors beats Iwe";
} else if (playerSelection === "iwe" && computerSelection === "apata") {
return "You Win! Iwe beats Apata";
} else if (playerSelection === "iwe" && computerSelection === "iwe") {
return "Game is Tied";
} else if (playerSelection === "apata" && computerSelection === "scissors") {
return "You Win! Apata beats Scissors";
} else if (playerSelection === "apata" && computerSelection === "apata") {
return "Game is Tied";
} else if (playerSelection === "scissors" && computerSelection === "iwe") {
return "You Win! Scissors beats Iwe";
} else {
return "Game is Tied";
}
}
});