-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
99 lines (79 loc) · 3.23 KB
/
game.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
let target;
const humanGuessInput = document.getElementById('human-guess');
const roundNumberDisplay = document.getElementById('round-number');
const computerGuessDisplay = document.getElementById('computer-guess');
const humanScoreDisplay = document.getElementById('human-score');
const computerScoreDisplay = document.getElementById('computer-score');
const targetNumberDisplay = document.getElementById('target-number');
const computerWinsDisplay = document.getElementById('computer-wins');
const guessButton = document.getElementById('guess');
const nextRoundButton = document.getElementById('next-round')
guessButton.addEventListener('click', () => {
// Generate the target value
target = generateTarget();
// Retrieve the player's guess
const currentHumanGuess = humanGuessInput.value;
// Make a random 'computer guess'
const computerGuess = Math.floor(Math.random() * 10);
// Display the computer guess and the target
computerGuessDisplay.innerText = computerGuess;
targetNumberDisplay.innerText = target;
// Determine if the human or computer wins:
const humanIsWinner = compareGuesses(currentHumanGuess, computerGuess, target)
const winner = humanIsWinner ? 'human' : 'computer'
// Update the correct score:
updateScore(winner);
// Display the winner
if (humanIsWinner) {
guessButton.innerText = 'You Win!!!!!';
guessButton.classList.toggle('winning-text')
} else {
computerWinsDisplay.innerText = 'Computer Wins!!!';
}
// winnerDisplay.innerText = humanIsWinner ? 'You win!' : 'Computer wins!';
// Display the current scores:
humanScoreDisplay.innerText = humanScore;
computerScoreDisplay.innerText = computerScore;
// Set the correct disabled state for the buttons
guessButton.setAttribute('disabled', true)
nextRoundButton.removeAttribute('disabled');
});
nextRoundButton.addEventListener('click', () => {
// Increase the round number
advanceRound();
// Display the new round number
roundNumberDisplay.innerText = currentRoundNumber;
// Set the correct disabled state for the buttons
nextRoundButton.setAttribute('disabled', true);
guessButton.removeAttribute('disabled');
// Reset the guess input box and the target number display:
targetNumberDisplay.innerText = '?';
guessButton.innerText = 'Make a Guess';
humanGuessInput.value = '';
computerGuessDisplay.innerText = '?';
computerWinsDisplay.innerText = '';
guessButton.classList.remove('winning-text');
});
const addButton = document.getElementById('add');
const subtractButton = document.getElementById('subtract');
addButton.addEventListener('click', () => {
humanGuessInput.value = +humanGuessInput.value + 1;
handleValueChange(humanGuessInput.value);
});
subtractButton.addEventListener('click', () => {
humanGuessInput.value = +humanGuessInput.value - 1;
handleValueChange(humanGuessInput.value);
});
const handleValueChange = value => {
if (value > 0 && value <= 9) {
subtractButton.removeAttribute('disabled');
addButton.removeAttribute('disabled');
} else if (value > 9) {
addButton.setAttribute('disabled', true);
} else if (value <= 0) {
subtractButton.setAttribute('disabled', true);
}
}
humanGuessInput.addEventListener('input', function(e) {
handleValueChange(e.target.value);
});