Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
107 lines (89 loc) · 3.43 KB
/
Copy pathscript.js
File metadata and controls
107 lines (89 loc) · 3.43 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
// Step 1: Check if script is linked properly
console.log("Hello, World!");
// Step 2: Write the logic to get the computer choice
function getComputerChoice() {
const random = Math.floor(Math.random() * 3);
switch (random) {
case 0: return 'rock';
case 1: return 'paper';
case 2: return 'scissors';
}
}
// Step 4: Declare the player score variables
let humanScore = 0;
let computerScore = 0;
// Step 5: Write the logic to play a single round (UI-driven)
const WINNING_SCORE = 5;
function decideRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection) return 'tie';
if (
(playerSelection === 'rock' && computerSelection === 'scissors') ||
(playerSelection === 'paper' && computerSelection === 'rock') ||
(playerSelection === 'scissors' && computerSelection === 'paper')
) {
return 'player';
}
return 'computer';
}
function playRound(playerSelection) {
if (!playerSelection) return;
const computerSelection = getComputerChoice();
const outcome = decideRound(playerSelection, computerSelection);
if (outcome === 'tie') {
showResult(`You chose ${playerSelection}, computer chose ${computerSelection}. It's a tie!`);
} else if (outcome === 'player') {
humanScore++;
showResult(`You chose ${playerSelection}, computer chose ${computerSelection}. You win this round!`);
} else {
computerScore++;
showResult(`You chose ${playerSelection}, computer chose ${computerSelection}. You lose this round.`);
}
updateScoreboard();
if (humanScore >= WINNING_SCORE || computerScore >= WINNING_SCORE) {
const finalMessage = humanScore > computerScore
? `Congratulations — you reached ${WINNING_SCORE} points and won the game!`
: `Game over — the computer reached ${WINNING_SCORE} points and won. PS: this is all random.`;
showResult(finalMessage);
setButtonsEnabled(false);
}
}
// UI helper functions
function updateScoreboard() {
const el = document.getElementById('scoreboard');
if (el) el.textContent = `You: ${humanScore} - Computer: ${computerScore}`;
}
function showResult(message) {
const el = document.getElementById('result');
if (el) el.textContent = message;
console.log(message);
}
function setButtonsEnabled(enabled) {
['rock', 'paper', 'scissors'].forEach(id => {
const btn = document.getElementById(id);
if (btn) {
btn.disabled = !enabled;
if (!enabled) btn.classList.add('disabled'); else btn.classList.remove('disabled');
}
});
}
function resetGame() {
humanScore = 0;
computerScore = 0;
updateScoreboard();
showResult('Game reset. Make your move!');
setButtonsEnabled(true);
}
// Event listeners for buttons
document.addEventListener('DOMContentLoaded', () => {
const rock = document.getElementById('rock');
const paper = document.getElementById('paper');
const scissors = document.getElementById('scissors');
const reset = document.getElementById('reset');
if (rock) rock.addEventListener('click', () => playRound('rock'));
if (paper) paper.addEventListener('click', () => playRound('paper'));
if (scissors) scissors.addEventListener('click', () => playRound('scissors'));
if (reset) reset.addEventListener('click', resetGame);
// initial UI
updateScoreboard();
showResult('Time to act! First to 5 wins.');
});