|
| 1 | +const game = () => { |
| 2 | + const randomInt = (minimum, maximum) => { |
| 3 | + return Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; |
| 4 | + }; |
| 5 | + const startGame = () => { |
| 6 | + level = 1; |
| 7 | + attempts = 0; |
| 8 | + score = 0; |
| 9 | + levelScore = 0; |
| 10 | + maxAttempts = |
| 11 | + modeButton.value === "attempts" |
| 12 | + ? attemptsLimit |
| 13 | + : Math.max(Math.ceil(level * difficultyMultiplier)); |
| 14 | + guess = 0; |
| 15 | + maxGuess = 2 ** level; |
| 16 | + secret = randomInt(1, maxGuess); |
| 17 | + feedbackText.textContent = ""; |
| 18 | + maxText.textContent = maxGuess; |
| 19 | + scoreText.textContent = score; |
| 20 | + attemptsText.textContent = maxAttempts - attempts; |
| 21 | + levelText.textContent = level; |
| 22 | + guessText.max = maxGuess; |
| 23 | + guessText.value = guess; |
| 24 | + startButton.disabled = true; |
| 25 | + guessButton.disabled = false; |
| 26 | + }; |
| 27 | + const checkGuess = () => { |
| 28 | + guess = parseInt(guessText.value, 10); |
| 29 | + if (guess !== secret) { |
| 30 | + if (guess > secret) { |
| 31 | + feedbackText.textContent = "Too high!"; |
| 32 | + } else { |
| 33 | + feedbackText.textContent = "Too low!"; |
| 34 | + } |
| 35 | + levelScore += |
| 36 | + maxGuess * |
| 37 | + Math.pow( |
| 38 | + Math.min( |
| 39 | + Math.max(guess, 1) / Math.max(secret, 1), |
| 40 | + Math.max(secret, 1) / Math.max(guess, 1), |
| 41 | + ), |
| 42 | + 2, |
| 43 | + ) * |
| 44 | + (1 - |
| 45 | + Math.pow( |
| 46 | + Math.abs(secret - guess) / |
| 47 | + Math.max(secret, maxGuess - secret + 1), |
| 48 | + 1.5, |
| 49 | + )) * |
| 50 | + (guess < secret |
| 51 | + ? Math.pow(guess / secret, 1.5) |
| 52 | + : Math.pow( |
| 53 | + (maxGuess - guess + 1) / (maxGuess - secret + 1), |
| 54 | + 1.5, |
| 55 | + )); |
| 56 | + attempts++; |
| 57 | + attemptsText.textContent = maxAttempts - attempts; |
| 58 | + if (attempts >= maxAttempts) { |
| 59 | + feedbackText.textContent = |
| 60 | + "Game over! Your score is " + score + "."; |
| 61 | + level = 1; |
| 62 | + attempts = 0; |
| 63 | + score = 0; |
| 64 | + levelScore = 0; |
| 65 | + maxAttempts = |
| 66 | + modeButton.value === "attempts" |
| 67 | + ? attemptsLimit |
| 68 | + : Math.max(Math.ceil(level * difficultyMultiplier)); |
| 69 | + guess = 0; |
| 70 | + maxGuess = 2 ** level; |
| 71 | + secret = randomInt(1, maxGuess); |
| 72 | + maxText.textContent = maxGuess; |
| 73 | + scoreText.textContent = score; |
| 74 | + attemptsText.textContent = maxAttempts - attempts; |
| 75 | + levelText.textContent = level; |
| 76 | + guessText.max = maxGuess; |
| 77 | + startButton.disabled = false; |
| 78 | + guessButton.disabled = true; |
| 79 | + } |
| 80 | + } else { |
| 81 | + feedbackText.textContent = |
| 82 | + "You guessed correctly! +" + |
| 83 | + Math.round( |
| 84 | + (((levelScore + maxGuess) * |
| 85 | + Math.pow(maxAttempts - attempts + 1, 1.5)) / |
| 86 | + Math.pow(attempts + 1, 1.2)) * |
| 87 | + maxAttempts, |
| 88 | + ) + |
| 89 | + " points!"; |
| 90 | + levelScore += maxGuess; |
| 91 | + score += Math.round( |
| 92 | + ((levelScore * Math.pow(maxAttempts - attempts + 1, 1.5)) / |
| 93 | + Math.pow(attempts + 1, 1.2)) * |
| 94 | + maxAttempts, |
| 95 | + ); |
| 96 | + level++; |
| 97 | + levelScore = 0; |
| 98 | + attempts = 0; |
| 99 | + maxAttempts = |
| 100 | + modeButton.value === "attempts" |
| 101 | + ? attemptsLimit |
| 102 | + : Math.max(Math.ceil(level * difficultyMultiplier)); |
| 103 | + maxGuess = 2 ** level; |
| 104 | + secret = randomInt(1, maxGuess); |
| 105 | + maxText.textContent = maxGuess; |
| 106 | + scoreText.textContent = score; |
| 107 | + attemptsText.textContent = maxAttempts - attempts; |
| 108 | + levelText.textContent = level; |
| 109 | + guessText.max = maxGuess; |
| 110 | + } |
| 111 | + }; |
| 112 | + let level = 1; |
| 113 | + let difficultyMultiplier = 1; |
| 114 | + let attempts = 0; |
| 115 | + let attemptsLimit = 10; |
| 116 | + let score = 0; |
| 117 | + let levelScore = 0; |
| 118 | + const modeButton = document.getElementById("mode"); |
| 119 | + let maxAttempts = |
| 120 | + modeButton.value === "attempts" |
| 121 | + ? attemptsLimit |
| 122 | + : Math.max(Math.ceil(level * difficultyMultiplier), 1); |
| 123 | + let guess = 0; |
| 124 | + let maxGuess = 2 ** level; |
| 125 | + let secret = randomInt(1, maxGuess); |
| 126 | + const maxText = document.getElementById("max"); |
| 127 | + const scoreText = document.getElementById("score"); |
| 128 | + const attemptsText = document.getElementById("attempts"); |
| 129 | + const levelText = document.getElementById("level"); |
| 130 | + const guessText = document.getElementById("guess"); |
| 131 | + const startButton = document.getElementById("start-button"); |
| 132 | + const guessButton = document.getElementById("guess-button"); |
| 133 | + const difficultyButton = document.getElementById("difficulty"); |
| 134 | + switch (difficultyButton.value) { |
| 135 | + case "easy": |
| 136 | + difficultyMultiplier = 1; |
| 137 | + attemptsLimit = 10; |
| 138 | + break; |
| 139 | + case "medium": |
| 140 | + difficultyMultiplier = 0.75; |
| 141 | + attemptsLimit = 8; |
| 142 | + break; |
| 143 | + case "hard": |
| 144 | + difficultyMultiplier = 0.5; |
| 145 | + attemptsLimit = 5; |
| 146 | + break; |
| 147 | + case "expert": |
| 148 | + difficultyMultiplier = 0.25; |
| 149 | + attemptsLimit = 3; |
| 150 | + break; |
| 151 | + } |
| 152 | + const feedbackText = document.getElementById("feedback"); |
| 153 | + startButton.addEventListener("click", startGame); |
| 154 | + guessButton.addEventListener("click", checkGuess); |
| 155 | +}; |
| 156 | +game(); |
0 commit comments