Skip to content

Commit 7d6f068

Browse files
committed
Add game mode based on fixed number of attempts
1 parent 1b6e639 commit 7d6f068

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

app.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ const game = () => {
99
attempts = 0; //set attempts to 0
1010
score = 0; //set score to 0
1111
levelScore = 0; //set level score to 0
12-
maxAttempts = Math.max(Math.ceil(level * difficultyMultiplier)); //set max attempts based on level and difficulty
12+
maxAttempts =
13+
modeButton.value === "attempts"
14+
? attemptsLimit
15+
: Math.max(Math.ceil(level * difficultyMultiplier)); //set max attempts based on level and difficulty
1316
guess = 0; //set initial guess value to 0
1417
maxGuess = 2 ** level; //set max guess value to 2 raised to level
1518
secret = randomInt(1, maxGuess); //set secret value to random integer between 1 and max guess value
@@ -47,14 +50,18 @@ const game = () => {
4750
? guess / secret
4851
: (maxGuess - guess + 1) / (maxGuess - secret + 1)); //increase level score
4952
attempts++;
53+
attemptsText.textContent = maxAttempts - attempts;
5054
if (attempts >= maxAttempts) {
5155
//if all attempts used reset game to initial state
5256
feedbackText.textContent = "Game over! Your score is " + score + ".";
5357
level = 1;
5458
attempts = 0;
5559
score = 0;
5660
levelScore = 0; //set level score to 0
57-
maxAttempts = Math.max(Math.ceil(level * difficultyMultiplier)); //set max attempts based on level and difficulty
61+
maxAttempts =
62+
modeButton.value === "attempts"
63+
? attemptsLimit
64+
: Math.max(Math.ceil(level * difficultyMultiplier)); //set max attempts based on level and difficulty
5865
guess = 0;
5966
maxGuess = 2 ** level;
6067
secret = randomInt(1, maxGuess);
@@ -84,7 +91,10 @@ const game = () => {
8491
level++; //increase level by 1
8592
levelScore = 0; //set level score to 0
8693
attempts = 0; //set attempts to 0
87-
maxAttempts = Math.max(Math.ceil(level * difficultyMultiplier)); //set max attempts based on level and difficulty
94+
maxAttempts =
95+
modeButton.value === "attempts"
96+
? attemptsLimit
97+
: Math.max(Math.ceil(level * difficultyMultiplier)); //set max attempts based on level and difficulty
8898
maxGuess = 2 ** level; //increase max guess number by double
8999
secret = randomInt(1, maxGuess); //get random secret guess number
90100
maxText.textContent = maxGuess;
@@ -97,9 +107,14 @@ const game = () => {
97107
let level = 1; //set initial level to 1
98108
let difficultyMultiplier = 1; //set difficulty multiplier to 1
99109
let attempts = 0; //set attempts to 0
110+
let attemptsLimit = 10; //set attempts limit to 10
100111
let score = 0; //set score to 0
101112
let levelScore = 0; //set level score to 0
102-
let maxAttempts = Math.max(Math.ceil(level * difficultyMultiplier), 1); //set max attempts based on level and difficulty
113+
const modeButton = document.getElementById("mode"); //mode selection
114+
let maxAttempts =
115+
modeButton.value === "attempts"
116+
? attemptsLimit
117+
: Math.max(Math.ceil(level * difficultyMultiplier), 1); //set max attempts based on level and difficulty
103118
let guess = 0; //set initial guess value to 0
104119
let maxGuess = 2 ** level; //set max guess value to 2 raised to level
105120
let secret = randomInt(1, maxGuess); //set secret value to random integer between 1 and max guess value
@@ -116,15 +131,19 @@ const game = () => {
116131
) {
117132
case "easy":
118133
difficultyMultiplier = 1;
134+
attemptsLimit = 10;
119135
break;
120136
case "medium":
121137
difficultyMultiplier = 0.75;
138+
attemptsLimit = 8;
122139
break;
123140
case "hard":
124141
difficultyMultiplier = 0.5;
142+
attemptsLimit = 5;
125143
break;
126144
case "expert":
127145
difficultyMultiplier = 0.25;
146+
attemptsLimit = 3;
128147
break;
129148
}
130149
const feedbackText = document.getElementById("feedback"); //feedback text

index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
</head>
77
<body>
88
<h1>Number Guessing Game with Score</h1>
9+
<label for="mode">Choose a game mode:</label>
10+
<select id="mode" name="mode">
11+
<option value="multiplier" selected>
12+
Attempts with multiplier based on level
13+
</option>
14+
<option value="attempts">Fixed number of attempts</option>
15+
</select>
916
<label for="difficulty">Choose a difficulty level:</label>
1017
<select id="difficulty" name="difficulty">
1118
<option value="easy" selected>Easy</option>

0 commit comments

Comments
 (0)